Class: Flydata::TableDef::MysqlTableDef
- Inherits:
-
Object
- Object
- Flydata::TableDef::MysqlTableDef
- Defined in:
- lib/flydata/table_def/mysql_table_def.rb
Constant Summary collapse
- TYPE_MAP_M2F =
{ 'bigint' => 'int8', 'binary' => 'binary', 'blob' => 'varbinary(65535)', 'bool' => 'int1', 'boolean' => 'int1', 'char' => 'varchar', 'date' => 'date', 'datetime' => 'datetime', 'dec' => 'numeric', 'decimal' => 'numeric', 'double' => 'float8', 'double precision' => 'float8', 'enum' => 'enum', 'fixed' => 'numeric', 'float' => 'float4', 'int' => 'int4', 'integer' => 'int4', 'longblob' => 'varbinary(4294967295)', 'longtext' => 'text', 'mediumblob' => 'varbinary(16777215)', 'mediumint' => 'int3', 'mediumtext' => 'text', 'numeric' => 'numeric', 'smallint' => 'int2', 'text' => 'text', 'time' => 'time', 'timestamp' => 'datetime', 'tinyblob' => 'varbinary(255)', 'tinyint' => 'int1', 'tinytext' => 'text', 'varbinary' => 'varbinary', 'varchar' => 'varchar', }
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
- ._create(io) ⇒ Object
-
.check_and_set_varchar_length(type, mysql_type, flydata_type) ⇒ Object
Check and set the varchar(char) size which is converted from length to byte size.
- .convert_to_flydata_type(type) ⇒ Object
- .create(io) ⇒ Object
- .parse_one_column_def(query) ⇒ Object
Instance Method Summary collapse
-
#initialize(table_def, table_name, columns, default_charset, comment) ⇒ MysqlTableDef
constructor
A new instance of MysqlTableDef.
- #to_flydata_tabledef ⇒ Object
Constructor Details
#initialize(table_def, table_name, columns, default_charset, comment) ⇒ MysqlTableDef
Returns a new instance of MysqlTableDef.
70 71 72 73 74 75 76 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 70 def initialize(table_def, table_name, columns, default_charset, comment) @table_def = table_def @table_name = table_name @columns = columns @default_charset = default_charset @comment = comment end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
131 132 133 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 131 def columns @columns end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
131 132 133 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 131 def table_name @table_name end |
Class Method Details
._create(io) ⇒ Object
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 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 78 def self._create(io) table_def = '' table_name = nil columns = [] default_charset = nil comment = nil position = :before_create_table io.each_line do |line| case position when :before_create_table if line =~ /CREATE TABLE `(.*?)`/ position = :in_create_table table_name = $1 table_def += line.chomp next end when :in_create_table table_def += line.chomp stripped_line = line.strip # `col_smallint` smallint(6) DEFAULT NULL, if stripped_line.start_with?('`') columns << parse_one_column_def(line) # PRIMARY KEY (`id`) elsif stripped_line.start_with?("PRIMARY KEY") parse_key(line, columns) #) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='test table'; elsif stripped_line.start_with?(')') default_charset = $1 if line =~ /DEFAULT CHARSET\s*=\s*([^\s]+)/ comment = $1 if /COMMENT='((?:\\'|[^'])*)'/.match(line) position = :after_create_table elsif stripped_line.start_with?("KEY") # index creation. No action required. elsif stripped_line.start_with?("CONSTRAINT") # constraint definition. No acction required. elsif stripped_line.start_with?("UNIQUE KEY") parse_key(line, columns, :unique) else $stderr.puts "Unknown table definition. Skip. (#{line})" end when :after_create_table unless columns.any? {|column| column[:primary_key]} raise TableDefError, {error: "no primary key defined", table: table_name} end break end end position == :after_create_table ? [table_def, table_name, columns, default_charset, comment] : nil end |
.check_and_set_varchar_length(type, mysql_type, flydata_type) ⇒ Object
Check and set the varchar(char) size which is converted from length to byte size. On Mysql the record size of varchar(char) is a length of characters. ex) varchar(6) on mysql -> varchar(18) on flydata
55 56 57 58 59 60 61 62 63 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 55 def self.check_and_set_varchar_length(type, mysql_type, flydata_type) return type unless %w(char varchar).include?(mysql_type) if type =~ /\((\d+)\)/ # expect 3 byte UTF-8 character "#{flydata_type}(#{$1.to_i * 3})" else raise "Invalid varchar type. It must be a bug... type:#{type}" end end |
.convert_to_flydata_type(type) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 40 def self.convert_to_flydata_type(type) TYPE_MAP_M2F.each do |mysql_type, flydata_type| if /^#{mysql_type}\(|^#{mysql_type}$/.match(type) ret_type = type.gsub(/^#{mysql_type}/, flydata_type) ret_type = check_and_set_varchar_length(ret_type, mysql_type, flydata_type) return ret_type end end nil end |
.create(io) ⇒ Object
65 66 67 68 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 65 def self.create(io) params = _create(io) params ? self.new(*params) : nil end |
.parse_one_column_def(query) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 143 def self.parse_one_column_def(query) line = query.strip line = line[0..-2] if line.end_with?(',') pos = 0 cond = :column_name column = {} while pos < line.length case cond when :column_name #`column_name` ... pos = line.index(' ', 1) column[:column] = if line[0] == '`' line[1..pos-2] else line[0..pos-1] end cond = :column_type pos += 1 when :column_type #... formattype(,,,) ... pos += 1 until line[pos] != ' ' start_pos = pos pos += 1 until line[pos].nil? || line[pos] =~ /\s|\(/ # meta if line[pos] == '(' #TODO: implement better parser pos = line.index(')', pos) pos += 1 end # type type = line[start_pos..pos-1] column[:type] = convert_to_flydata_type(type) cond = :options when :options column[:type] += ' unsigned' if line =~ /unsigned/i column[:auto_increment] = true if line =~ /AUTO_INCREMENT/i column[:not_null] = true if line =~ /NOT NULL/i column[:unique] = true if line =~ /UNIQUE/i if /DEFAULT\s+((?:[^'\s]+\b)|(?:'(?:\\'|[^'])*'))/i.match(line) val = $1 val = val.slice(1..-1) if val.start_with?("'") val = val.slice(0..-2) if val.end_with?("'") column[:default] = val == "NULL" ? nil : val end if /COMMENT\s+'(((?:\\'|[^'])*))'/i.match(line) column[:comment] = $1 end if block_given? column = yield(column, query, pos) end break else raise "Invalid condition. It must be a bug..." end end column end |
Instance Method Details
#to_flydata_tabledef ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/flydata/table_def/mysql_table_def.rb', line 133 def to_flydata_tabledef tabledef = { table_name: @table_name, columns: @columns, } tabledef[:default_charset] = @default_charset if @default_charset tabledef[:comment] = @comment if @comment tabledef end |