Class: Xmysql2psql::PostgresWriter
- Defined in:
- lib/xmysql2psql/postgres_writer.rb
Direct Known Subclasses
Instance Method Summary collapse
- #column_description(column) ⇒ Object
- #column_type(column) ⇒ Object
- #column_type_info(column) ⇒ Object
- #db_writer? ⇒ Boolean
- #escape_bytea(value) ⇒ Object
- #process_row(table, row) ⇒ Object
- #truncate(table) ⇒ Object
Instance Method Details
#column_description(column) ⇒ Object
20 21 22 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 20 def column_description(column) "#{PGconn.quote_ident(column[:name])} #{column_type_info(column)}" end |
#column_type(column) ⇒ Object
24 25 26 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 24 def column_type(column) column_type_info(column).split(" ").first end |
#column_type_info(column) ⇒ Object
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 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 28 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 "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 |
#db_writer? ⇒ Boolean
8 9 10 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 8 def db_writer? raise StandardError.new("not implemented") end |
#escape_bytea(value) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 12 def escape_bytea(value) if db_writer? self.conn.escape_bytea(value) else PGConn.escape_bytea(value) end end |
#process_row(table, row) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 121 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] = 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
149 150 |
# File 'lib/xmysql2psql/postgres_writer.rb', line 149 def truncate(table) end |