Class: Samidare::MySQL::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/samidare/mysql.rb

Constant Summary collapse

TYPE_MAPPINGS =
{
  'int' => 'integer',
  'tinyint' => 'integer',
  'smallint' => 'integer',
  'mediumint' => 'integer',
  'bigint' => 'integer',
  'float' => 'float',
  'double' => 'float',
  'decimal' => 'float',
  'char' => 'string',
  'varchar' => 'string',
  'tinytext' => 'string',
  'text' => 'string',
  'date' => 'timestamp',
  'datetime' => 'timestamp',
  'timestamp' => 'timestamp'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_name, data_type) ⇒ Column

Returns a new instance of Column.



86
87
88
89
# File 'lib/samidare/mysql.rb', line 86

def initialize(column_name, data_type)
  @column_name = column_name
  @data_type = data_type
end

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



66
67
68
# File 'lib/samidare/mysql.rb', line 66

def column_name
  @column_name
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



66
67
68
# File 'lib/samidare/mysql.rb', line 66

def data_type
  @data_type
end

Instance Method Details

#bigquery_data_typeObject



91
92
93
# File 'lib/samidare/mysql.rb', line 91

def bigquery_data_type
  TYPE_MAPPINGS[@data_type]
end

#converted_valueObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/samidare/mysql.rb', line 95

def converted_value
  if bigquery_data_type == 'timestamp'
    # time zone translate to UTC
    "UNIX_TIMESTAMP(#{escaped_column_name}) AS #{escaped_column_name}"
  elsif data_type == 'tinyint'
    # for MySQL tinyint(1) problem
    "CAST(#{escaped_column_name} AS signed) AS #{escaped_column_name}"
  else
    escaped_column_name
  end
end

#to_json(*a) ⇒ Object



107
108
109
# File 'lib/samidare/mysql.rb', line 107

def to_json(*a)
  { "name" => @column_name, "type" => bigquery_data_type }.to_json(*a)
end