Class: Mysql2psql::PostgresWriter

Inherits:
Writer
  • Object
show all
Defined in:
lib/mysql2psql/postgres_writer.rb

Direct Known Subclasses

PostgresDbWriter, PostgresFileWriter

Instance Method Summary collapse

Instance Method Details

#column_description(column) ⇒ Object



8
9
10
# File 'lib/mysql2psql/postgres_writer.rb', line 8

def column_description(column)
  "#{PGconn.quote_ident(column[:name])} #{column_type_info(column)}"
end

#column_type(column) ⇒ Object



12
13
14
# File 'lib/mysql2psql/postgres_writer.rb', line 12

def column_type(column)
  column_type_info(column).split(" ").first
end

#column_type_info(column) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mysql2psql/postgres_writer.rb', line 16

def column_type_info(column)
  if column[:auto_increment]
    return "integer DEFAULT nextval('#{column[:table_name]}_#{column[:name]}_seq'::regclass) NOT NULL"
  end

  default = column[:default] ? " DEFAULT #{column[:default] == nil ? 'NULL' : "'"+PGconn.escape(column[:default])+"'"}" : nil
  null = column[:null] ? "" : " NOT NULL"
  type = 
  case column[:type]

  # String types
  when "char"
    default = default + "::char" if default
    "character(#{column[:length]})"
  when "varchar"
    default = default + "::character varying" if default
  #      puts "VARCHAR: #{column.inspect}"
    "character varying(#{column[:length]})"
  
  # Integer and numeric types
  when "integer"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
    "integer"
  when "bigint"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
    "bigint"
  when "tinyint"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
    "smallint"

  when "boolean"
    default = " DEFAULT #{column[:default].to_i == 1 ? 'true' : 'false'}" if default
    "boolean"
  when "float"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_f}" if default
    "real"
  when "float unsigned"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_f}" if default
    "real"
  when "decimal"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
    "numeric(#{column[:length] || 10}, #{column[:decimals] || 0})"

  when "double precision"
    default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
    "double precision"

  # Mysql datetime fields
  when "datetime"
    default = nil
    "timestamp without time zone"
  when "date"
    default = nil
    "date"
  when "timestamp"
    default = " DEFAULT CURRENT_TIMESTAMP" if column[:default] == "CURRENT_TIMESTAMP"
    default = " DEFAULT '1970-01-01 00:00'" if column[:default] == "0000-00-00 00:00"
    default = " DEFAULT '1970-01-01 00:00:00'" if column[:default] == "0000-00-00 00:00:00"
    "timestamp without time zone"
  when "time"
    default = " DEFAULT NOW()" if default
    "time without time zone"

  when "tinyblob"
    "bytea"
  when "mediumblob"
    "bytea"
  when "longblob"
    "bytea"
  when "blob"
    "bytea"
  when "varbinary"
    "bytea"
  when "tinytext"
    "text"
  when "mediumtext"
    "text"
  when "longtext"
    "text"
  when "text"
    "text"
  when /^enum/
    default = default + "::character varying" if default
    enum = column[:type].gsub(/enum|\(|\)/, '')
    max_enum_size = enum.split(',').map{ |check| check.size() -2}.sort[-1]
    "character varying(#{max_enum_size}) check( #{column[:name]} in (#{enum}))"
  else
    puts "Unknown #{column.inspect}"
    column[:type].inspect
    return ""
  end
  "#{type}#{default}#{null}"
end

#process_row(table, row) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mysql2psql/postgres_writer.rb', line 110

def process_row(table, row)
	table.columns.each_with_index do |column, index|

      if column[:type] == "time"
        row[index] = "%02d:%02d:%02d" % [row[index].hour, row[index].minute, row[index].second]
      end
    
      if row[index].is_a?(Mysql::Time)
        row[index] = row[index].to_s.gsub('0000-00-00 00:00', '1970-01-01 00:00')
        row[index] = row[index].to_s.gsub('0000-00-00 00:00:00', '1970-01-01 00:00:00')
      end
    
      if column_type(column) == "boolean"
        row[index] = row[index] == 1 ? 't' : row[index] == 0 ? 'f' : row[index]
      end
    
      if row[index].is_a?(String)
        if column_type(column) == "bytea"
          row[index] = PGconn.escape_bytea(row[index])
        else
          row[index] = row[index].gsub(/\\/, '\\\\\\').gsub(/\n/,'\n').gsub(/\t/,'\t').gsub(/\r/,'\r').gsub(/\0/, '')
        end
      end
    
      row[index] = '\N' if !row[index]
    end
end

#truncate(table) ⇒ Object



138
139
# File 'lib/mysql2psql/postgres_writer.rb', line 138

def truncate(table)
end