Class: Flydata::TableDef::MysqlTableDef

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/table_def/mysql_table_def.rb

Constant Summary collapse

TYPE_MAP_M2F =
{
  'bigint' => 'int8',
  'binary' => 'binary',
  'blob' => 'varbinary',
  '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',
  'longblob' => 'varbinary',
  'longtext' => 'text',
  'mediumblob' => 'varbinary',
  'mediumint' => 'int3',
  'mediumtext' => 'text',
  'numeric' => 'numeric',
  'smallint' => 'int2',
  'text' => 'text',
  'time' => 'time',
  'timestamp' => 'datetime',
  'tinyblob' => 'varbinary',
  'tinyint' => 'int1',
  'tinytext' => 'text',
  'varbinary' => 'varbinary',
  'varchar' => 'varchar',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_def, table_name, columns, default_charset, comment) ⇒ MysqlTableDef

Returns a new instance of MysqlTableDef.



44
45
46
47
48
49
50
# File 'lib/flydata/table_def/mysql_table_def.rb', line 44

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

#columnsObject (readonly)

Returns the value of attribute columns.



98
99
100
# File 'lib/flydata/table_def/mysql_table_def.rb', line 98

def columns
  @columns
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



98
99
100
# File 'lib/flydata/table_def/mysql_table_def.rb', line 98

def table_name
  @table_name
end

Class Method Details

._create(io) ⇒ Object



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
# File 'lib/flydata/table_def/mysql_table_def.rb', line 52

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

      # `col_smallint` smallint(6) DEFAULT NULL,
      if line.strip.start_with?('`')
        columns << parse_column_line(line)
      # PRIMARY KEY (`id`)
      elsif line.strip.start_with?("PRIMARY KEY")
        parse_primary_key(line, columns)
      #) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='test table';
      elsif line.strip.start_with?(')')
        default_charset = $1 if line =~ /DEFAULT CHARSET\s*=\s*([^\s]+)/
        comment = $1 if /COMMENT='((?:\\'|[^'])*)'/.match(line)
        position = :after_create_table
      else
        raise "Invalid mysqldump file. It must be a bug...\n#{line}"
      end

    when :after_create_table
      unless columns.any? {|column| column[:primary_key]}
        raise "Primary key must be defined."
      end
      break
    end
  end
  position == :after_create_table ? [table_def, table_name, columns, default_charset, comment] : nil
end

.create(io) ⇒ Object



39
40
41
42
# File 'lib/flydata/table_def/mysql_table_def.rb', line 39

def self.create(io)
  params = _create(io)
  params ? self.new(*params) : nil
end

Instance Method Details

#to_flydata_tabledefObject



100
101
102
103
104
105
106
107
108
# File 'lib/flydata/table_def/mysql_table_def.rb', line 100

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