class BigShift::RedshiftTableSchema::Column

Attributes

name[R]
type[R]

Public Class Methods

new(name, type, nullable) click to toggle source
# File lib/bigshift/redshift_table_schema.rb, line 43
def initialize(name, type, nullable)
  @name = name
  @type = type
  @nullable = nullable
end

Public Instance Methods

nullable?() click to toggle source
# File lib/bigshift/redshift_table_schema.rb, line 49
def nullable?
  @nullable
end
to_big_query() click to toggle source
# File lib/bigshift/redshift_table_schema.rb, line 53
def to_big_query
  Google::Apis::BigqueryV2::TableFieldSchema.new(
    name: @name,
    type: big_query_type,
    mode: @nullable ? 'NULLABLE' : 'REQUIRED'
  )
end
to_sql() click to toggle source
# File lib/bigshift/redshift_table_schema.rb, line 61
def to_sql
  case @type
  when /^numeric/, /int/, /^double/, 'real', /^timestamp/
    sprintf('"%s"', @name)
  when /^character/
    sprintf(%q<('"' || REPLACE(REPLACE(REPLACE("%s", '"', '""'), '\\n', '\\\\n'), '\\r', '\\\\r') || '"')>, @name)
  when 'date'
    sprintf(%q<(TO_CHAR("%s", 'YYYY-MM-DD'))>, @name)
  when 'boolean'
    if nullable?
      sprintf('(CASE WHEN "%s" IS NULL THEN NULL WHEN "%s" THEN 1 ELSE 0 END)', @name, @name)
    else
      sprintf('(CASE WHEN "%s" THEN 1 ELSE 0 END)', @name)
    end
  else
    raise sprintf('Unsupported column type: %s', type.inspect)
  end
end

Private Instance Methods

big_query_type() click to toggle source
# File lib/bigshift/redshift_table_schema.rb, line 82
def big_query_type
  case @type
  when /^character/, /^numeric/, 'date' then 'STRING'
  when /^timestamp/ then 'TIMESTAMP'
  when /int/ then 'INTEGER'
  when 'boolean' then 'BOOLEAN'
  when /^double/, 'real' then 'FLOAT'
  else
    raise sprintf('Unsupported column type: %s', type.inspect)
  end
end