Class: Mysql2postgres::MysqlReader::Table
- Inherits:
-
Object
- Object
- Mysql2postgres::MysqlReader::Table
- Defined in:
- lib/mysql2postgres/mysql_reader.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #columns ⇒ Object
- #convert_type(type) ⇒ Object
- #count_for_pager ⇒ Object
- #count_rows ⇒ Object
- #foreign_keys ⇒ Object
- #id? ⇒ Boolean
- #indexes ⇒ Object
-
#initialize(reader, name) ⇒ Table
constructor
A new instance of Table.
- #load_columns ⇒ Object
- #load_indexes ⇒ Object
- #query_for_pager ⇒ Object
Constructor Details
#initialize(reader, name) ⇒ Table
Returns a new instance of Table.
14 15 16 17 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 14 def initialize(reader, name) @reader = reader @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 12 def name @name end |
Instance Method Details
#columns ⇒ Object
19 20 21 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 19 def columns @columns ||= load_columns end |
#convert_type(type) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 23 def convert_type(type) case type when /int.* unsigned/, /bigint/ 'bigint' when 'bit(1)', 'tinyint(1)' 'boolean' when /tinyint/ 'tinyint' when /int/ 'integer' when /varchar/, /set/ 'varchar' when /char/ 'char' when /decimal/ 'decimal' when /(float|double)/ 'double precision' else type end end |
#count_for_pager ⇒ Object
145 146 147 148 149 150 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 145 def count_for_pager query = id? ? 'MAX(id)' : 'COUNT(*)' @reader.query "SELECT #{query} FROM `#{name}`" do |res| return res.fetch_row[0].to_i end end |
#count_rows ⇒ Object
135 136 137 138 139 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 135 def count_rows @reader.query "SELECT COUNT(*) FROM `#{name}`" do |res| return res.fetch_row[0].to_i end end |
#foreign_keys ⇒ Object
83 84 85 86 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 83 def foreign_keys load_indexes unless @foreign_keys @foreign_keys end |
#id? ⇒ Boolean
141 142 143 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 141 def id? !!columns.find { |col| col[:name] == 'id' } end |
#indexes ⇒ Object
78 79 80 81 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 78 def indexes load_indexes unless @indexes @indexes end |
#load_columns ⇒ Object
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 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 46 def load_columns @reader.reconnect # mysql_flags = ::Mysql::Field.constants.select { |c| c.to_s.include?('FLAG') } fields = [] @reader.query "EXPLAIN `#{name}`" do |res| while (field = res.fetch_row) length = field[1][/\((\d+)\)/, 1] if field[1].match?(/\((\d+)\)/) length = field[1][/\((\d+),(\d+)\)/, 1] if field[1].match?(/\((\d+),(\d+)\)/) desc = { name: field[0], table_name: name, type: convert_type(field[1]), length: length&.to_i, decimals: field[1][/\((\d+),(\d+)\)/, 2], null: field[2] == 'YES', primary_key: field[3] == 'PRI', auto_increment: field[5] == 'auto_increment' } desc[:default] = field[4] unless field[4].nil? fields << desc end end fields.select { |field| field[:auto_increment] }.each do |field| @reader.query "SELECT max(`#{field[:name]}`) FROM `#{name}`" do |res| field[:maxval] = res.fetch_row[0].to_i end end fields end |
#load_indexes ⇒ Object
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 88 def load_indexes @indexes = [] @foreign_keys = [] @reader.query "SHOW CREATE TABLE `#{name}`" do |result| explain = result.fetch_row[1] explain.split("\n").each do |line| next unless line.include? ' KEY ' index = {} if (match_data = /CONSTRAINT `(\w+)` FOREIGN KEY \((.*?)\) REFERENCES `(\w+)` \((.*?)\)(.*)/.match(line)) index[:name] = "fk_#{name}_#{match_data[1]}" index[:column] = match_data[2].delete!('`').split(', ') index[:ref_table] = match_data[3] index[:ref_column] = match_data[4].delete!('`').split(', ') the_rest = match_data[5] if (match_data = /ON DELETE (SET NULL|SET DEFAULT|RESTRICT|NO ACTION|CASCADE)/.match(the_rest)) index[:on_delete] = match_data[1] else index[:on_delete] ||= 'RESTRICT' end if (match_data = /ON UPDATE (SET NULL|SET DEFAULT|RESTRICT|NO ACTION|CASCADE)/.match(the_rest)) index[:on_update] = match_data[1] else index[:on_update] ||= 'RESTRICT' end @foreign_keys << index elsif (match_data = /KEY `(\w+)` \((.*)\)/.match(line)) # index[:name] = 'idx_' + name + '_' + match_data[1] # with redmine we do not want prefix idx_tablename_ index[:name] = match_data[1] index[:columns] = match_data[2].split(',').map { |col| col[/`(\w+)`/, 1] } index[:unique] = true if line.include? 'UNIQUE' @indexes << index elsif (match_data = /PRIMARY KEY .*\((.*)\)/.match(line)) index[:primary] = true index[:columns] = match_data[1].split(',').map { |col| col.strip.delete '`' } @indexes << index end end end end |
#query_for_pager ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mysql2postgres/mysql_reader.rb', line 152 def query_for_pager query = id? ? 'WHERE id >= ? AND id < ?' : 'LIMIT ?,?' cols = columns.map do |c| if c[:type] == 'multipolygon' "AsWKT(`#{c[:name]}`) as `#{c[:name]}`" else "`#{c[:name]}`" end end "SELECT #{cols.join ', '} FROM `#{name}` #{query}" end |