Class: Mysql2psql::MysqlReader::Table
- Inherits:
-
Object
- Object
- Mysql2psql::MysqlReader::Table
- Defined in:
- lib/mysql2psql/mysql_reader.rb
Constant Summary collapse
- @@types =
%w(tiny enum decimal short long float double null timestamp longlong int24 date time datetime year set blob string var_string char).inject({}) do |list, type| list[eval("::Mysql::Field::TYPE_#{type.upcase}")] = type list end
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
- #has_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.
12 13 14 15 |
# File 'lib/mysql2psql/mysql_reader.rb', line 12 def initialize(reader, name) @reader = reader @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/mysql2psql/mysql_reader.rb', line 10 def name @name end |
Instance Method Details
#columns ⇒ Object
24 25 26 |
# File 'lib/mysql2psql/mysql_reader.rb', line 24 def columns @columns ||= load_columns end |
#convert_type(type) ⇒ 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 |
# File 'lib/mysql2psql/mysql_reader.rb', line 28 def convert_type(type) case type when /int.* unsigned/ "bigint" when /bigint/ "bigint" when "bit(1)" "boolean" when "tinyint(1)" "boolean" when /tinyint/ "tinyint" when /int/ "integer" when /varchar/ "varchar" when /char/ "char" when /(float|decimal)/ "decimal" when /double/ "double precision" else type end end |
#count_for_pager ⇒ Object
137 138 139 140 141 142 |
# File 'lib/mysql2psql/mysql_reader.rb', line 137 def count_for_pager query = has_id? ? 'MAX(id)' : 'COUNT(*)' @reader.mysql.query("SELECT #{query} FROM `#{name}`") do |res| return res.fetch_row[0].to_i end end |
#count_rows ⇒ Object
127 128 129 130 131 |
# File 'lib/mysql2psql/mysql_reader.rb', line 127 def count_rows @reader.mysql.query("SELECT COUNT(*) FROM `#{name}`") do |res| return res.fetch_row[0].to_i end end |
#foreign_keys ⇒ Object
93 94 95 96 |
# File 'lib/mysql2psql/mysql_reader.rb', line 93 def foreign_keys load_indexes unless @foreign_keys @foreign_keys end |
#has_id? ⇒ Boolean
133 134 135 |
# File 'lib/mysql2psql/mysql_reader.rb', line 133 def has_id? !!columns.find {|col| col[:name] == "id"} end |
#indexes ⇒ Object
88 89 90 91 |
# File 'lib/mysql2psql/mysql_reader.rb', line 88 def indexes load_indexes unless @indexes @indexes end |
#load_columns ⇒ Object
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 |
# File 'lib/mysql2psql/mysql_reader.rb', line 55 def load_columns @reader.reconnect result = @reader.mysql.list_fields(name) mysql_flags = ::Mysql::Field.constants.select {|c| c =~ /FLAG/} fields = [] @reader.mysql.query("EXPLAIN `#{name}`") do |res| while field = res.fetch_row do length = field[1][/\((\d+)\)/, 1] if field[1] =~ /\((\d+)\)/ length = field[1][/\((\d+),(\d+)\)/, 1] if field[1] =~ /\((\d+),(\d+)\)/ desc = { :name => field[0], :table_name => name, :type => convert_type(field[1]), :length => 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.mysql.query("SELECT max(`#{field[:name]}`) FROM `#{name}`") do |res| field[:maxval] = res.fetch_row[0].to_i end end fields end |
#load_indexes ⇒ Object
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 |
# File 'lib/mysql2psql/mysql_reader.rb', line 98 def load_indexes @indexes = [] @foreign_keys = [] @reader.mysql.query("SHOW CREATE TABLE `#{name}`") do |result| explain = result.fetch_row[1] explain.split(/\n/).each do |line| next unless line =~ / KEY / index = {} if match_data = /CONSTRAINT `(\w+)` FOREIGN KEY \(`(\w+)`\) REFERENCES `(\w+)` \(`(\w+)`\)/.match(line) index[:name] = match_data[1] index[:column] = match_data[2] index[:ref_table] = match_data[3] index[:ref_column] = match_data[4] @foreign_keys << index elsif match_data = /KEY `(\w+)` \((.*)\)/.match(line) index[:name] = match_data[1] index[:columns] = match_data[2].split(",").map {|col| col[/`(\w+)`/, 1]} index[:unique] = true if line =~ /UNIQUE/ @indexes << index elsif match_data = /PRIMARY KEY .*\((.*)\)/.match(line) index[:primary] = true index[:columns] = match_data[1].split(",").map {|col| col.strip.gsub(/`/, "")} @indexes << index end end end end |
#query_for_pager ⇒ Object
144 145 146 147 |
# File 'lib/mysql2psql/mysql_reader.rb', line 144 def query_for_pager query = has_id? ? 'WHERE id >= ? AND id < ?' : 'LIMIT ?,?' "SELECT #{columns.map{|c| "`"+c[:name]+"`"}.join(", ")} FROM `#{name}` #{query}" end |