Class: BigShift::RedshiftTableSchema::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/bigshift/redshift_table_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, nullable) ⇒ Column

Returns a new instance of Column.



33
34
35
36
37
# File 'lib/bigshift/redshift_table_schema.rb', line 33

def initialize(name, type, nullable)
  @name = name
  @type = type
  @nullable = nullable
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/bigshift/redshift_table_schema.rb', line 31

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



31
32
33
# File 'lib/bigshift/redshift_table_schema.rb', line 31

def type
  @type
end

Instance Method Details

#nullable?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/bigshift/redshift_table_schema.rb', line 39

def nullable?
  @nullable
end

#to_big_queryObject



43
44
45
46
47
48
49
# File 'lib/bigshift/redshift_table_schema.rb', line 43

def to_big_query
  Google::Apis::BigqueryV2::TableFieldSchema.new(
    name: @name,
    type: big_query_type,
    mode: @nullable ? 'NULLABLE' : 'REQUIRED'
  )
end

#to_sqlObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bigshift/redshift_table_schema.rb', line 51

def to_sql
  case @type
  when /^numeric/, /int/, /^double/, 'real'
    sprintf('"%s"', @name)
  when /^character/
    sprintf(%q<('"' || REPLACE(REPLACE(REPLACE("%s", '"', '""'), '\\n', '\\\\n'), '\\r', '\\\\r') || '"')>, @name)
  when /^timestamp/
    sprintf('(EXTRACT(epoch FROM "%s") + EXTRACT(milliseconds FROM "%s")/1000.0)', @name, @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