Class: Mysql2psql::MysqlReader::Table

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(reader, name) ⇒ Table

Returns a new instance of Table.



13
14
15
16
# File 'lib/mysql2psql/mysql_reader.rb', line 13

def initialize(reader, name)
  @reader = reader
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/mysql2psql/mysql_reader.rb', line 11

def name
  @name
end

Instance Method Details

#columnsObject



25
26
27
# File 'lib/mysql2psql/mysql_reader.rb', line 25

def columns
  @columns ||= load_columns
end

#convert_type(type) ⇒ Object



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
# File 'lib/mysql2psql/mysql_reader.rb', line 29

def convert_type(type)
  case type
  when /^int.* unsigned/
    "bigint"
  when /bigint/
    "bigint"
  when "bit(1)"
    "boolean"
  when /smallint.* unsigned/
    "integer"
  when /smallint/
    "smallint"
  when "tinyint(1)"
    "boolean"
  when /tinyint/
    "tinyint"
  when /int/
    "integer"
  when /varchar/
    "varchar"
  when /char/
    "char"
  when /decimal/
    "decimal"
  when /float/
    "float"
  when /real|double/
    "double precision"
  else
    type
  end
end

#count_for_pagerObject



160
161
162
163
164
165
# File 'lib/mysql2psql/mysql_reader.rb', line 160

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_rowsObject



150
151
152
153
154
# File 'lib/mysql2psql/mysql_reader.rb', line 150

def count_rows
  @reader.mysql.query("SELECT COUNT(*) FROM `#{name}`")  do |res|
    return res.fetch_row[0].to_i
  end
end

#foreign_keysObject



101
102
103
104
# File 'lib/mysql2psql/mysql_reader.rb', line 101

def foreign_keys
  load_indexes unless @foreign_keys
  @foreign_keys
end

#has_id?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/mysql2psql/mysql_reader.rb', line 156

def has_id?
  !!columns.find {|col| col[:name] == "id"}
end

#indexesObject



96
97
98
99
# File 'lib/mysql2psql/mysql_reader.rb', line 96

def indexes
  load_indexes unless @indexes
  @indexes
end

#load_columnsObject



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
# File 'lib/mysql2psql/mysql_reader.rb', line 62

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 = -1
      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_indexesObject



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mysql2psql/mysql_reader.rb', line 106

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 \((.*?)\) REFERENCES `(\w+)` \((.*?)\)(.*)/.match(line)
        index[:name] = match_data[1]
        index[:column] = match_data[2].parse_csv(:quote_char => '`',:col_sep => ', ')
        index[:ref_table] = match_data[3]
        index[:ref_column] = match_data[4].parse_csv(:quote_char => '`',:col_sep => ', ')
        
        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] = 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_pagerObject



167
168
169
170
# File 'lib/mysql2psql/mysql_reader.rb', line 167

def query_for_pager
  query = has_id? ? 'WHERE id >= ? AND id < ?' : 'LIMIT ?,?'
  "SELECT #{columns.map{|c| "`"+c[:name]+"`"}.join(", ")} FROM `#{name}` #{query}"
end