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.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/bigshift/redshift_table_schema.rb', line 41

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



41
42
43
# File 'lib/bigshift/redshift_table_schema.rb', line 41

def type
  @type
end

Instance Method Details

#nullable?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/bigshift/redshift_table_schema.rb', line 49

def nullable?
  @nullable
end

#to_big_queryObject



53
54
55
56
57
58
59
# 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_sqlObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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