Class: Naginegi::PostgreSQL::Column

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

Constant Summary collapse

TYPE_MAPPINGS =
{
  'smallint' => 'INT64',
  'integer' => 'INT64',
  'bigint' => 'INT64',
  'smallserial' => 'INT64',
  'serial' => 'INT64',
  'bigserial' => 'INT64',
  'decimal' => 'FLOAT64',
  'numeric' => 'FLOAT64',
  'real' => 'FLOAT64',
  'double precision' => 'FLOAT64',
  'character' => 'STRING',
  'character varying' => 'STRING',
  'text' => 'STRING',
  'date' => 'TIMESTAMP',
  'timestamp' => 'TIMESTAMP',
  'timestamp with time zone' => 'TIMESTAMP',
  'boolean' => 'BOOL'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_name, data_type) ⇒ Column

Returns a new instance of Column.



64
65
66
67
# File 'lib/naginegi/postgresql.rb', line 64

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.



42
43
44
# File 'lib/naginegi/postgresql.rb', line 42

def column_name
  @column_name
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



42
43
44
# File 'lib/naginegi/postgresql.rb', line 42

def data_type
  @data_type
end

Instance Method Details

#bigquery_data_typeObject



69
70
71
# File 'lib/naginegi/postgresql.rb', line 69

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

#converted_valueObject



73
74
75
76
77
78
79
80
# File 'lib/naginegi/postgresql.rb', line 73

def converted_value
  if bigquery_data_type == 'TIMESTAMP'
    # time zone translate to UTC
    "EXTRACT(EPOCH FROM #{escaped_column_name}) AS #{escaped_column_name}"
  else
    escaped_column_name
  end
end

#to_json(*a) ⇒ Object



82
83
84
# File 'lib/naginegi/postgresql.rb', line 82

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