Class: Naginegi::MySQL::Column

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

Constant Summary collapse

TYPE_MAPPINGS =
{
  'int' => 'INT64',
  'tinyint' => 'INT64',
  'smallint' => 'INT64',
  'mediumint' => 'INT64',
  'bigint' => 'INT64',
  'float' => 'FLOAT64',
  'double' => 'FLOAT64',
  'decimal' => 'FLOAT64',
  'char' => 'STRING',
  'varchar' => 'STRING',
  'tinytext' => 'STRING',
  'text' => 'STRING',
  'date' => 'TIMESTAMP',
  'datetime' => 'TIMESTAMP',
  'timestamp' => 'TIMESTAMP'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_name, data_type) ⇒ Column

Returns a new instance of Column.



63
64
65
66
# File 'lib/naginegi/mysql.rb', line 63

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.



43
44
45
# File 'lib/naginegi/mysql.rb', line 43

def column_name
  @column_name
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



43
44
45
# File 'lib/naginegi/mysql.rb', line 43

def data_type
  @data_type
end

Instance Method Details

#bigquery_data_typeObject



68
69
70
# File 'lib/naginegi/mysql.rb', line 68

def bigquery_data_type
  TYPE_MAPPINGS[@data_type] || 'STRING'
end

#converted_valueObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/naginegi/mysql.rb', line 72

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



84
85
86
# File 'lib/naginegi/mysql.rb', line 84

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