Class: Relationize::BqRelationizer
Constant Summary
collapse
- BQ_TS_FMT =
"%Y-%m-%d %H:%M:%S %:z"
- DEFAULT_TYPES =
{
Integer => :integer,
Fixnum => :integer,
Bignum => :integer,
BigDecimal => :integer,
Float => :float,
String => :string,
TrueClass => :boolean,
FalseClass => :boolean,
Date => :string,
Time => :timestamp
}
- CAST =
{
"INTEGER" => -> (val, col) { "INTEGER(#{to_text_literal(val)}) AS #{col}"},
"FLOAT" => -> (val, col) { "FLOAT(#{to_text_literal(val)}) AS #{col}" },
"BOOLEAN" => -> (val, col) { "BOOLEAN(#{val.nil? ? 'NULL' : (val ? 1 : 0)}) AS #{col}" },
"STRING" => -> (val, col) { "STRING(#{to_text_literal(val)}) AS #{col}"},
"TIMESTAMP" => -> (val, col) { "TIMESTAMP(#{to_timestamp_string(val)}) AS #{col}"}
}
Class Method Summary
collapse
Instance Method Summary
collapse
#initialize
Class Method Details
.to_text_literal(obj) ⇒ Object
22
23
24
|
# File 'lib/relationize/bq_relationizer.rb', line 22
def self.to_text_literal(obj)
obj.nil? ? 'NULL' : obj.to_s.gsub(/'/, "\\'").tap { |s| break "'#{s}'" }
end
|
.to_timestamp_string(obj) ⇒ Object
26
27
28
|
# File 'lib/relationize/bq_relationizer.rb', line 26
def self.to_timestamp_string(obj)
to_text_literal(obj.is_a?(Time) ? obj.strftime(BQ_TS_FMT) : obj)
end
|
Instance Method Details
#to_row(val, col), type) ⇒ Object
45
46
47
|
# File 'lib/relationize/bq_relationizer.rb', line 45
def to_row(((val, col), type))
CAST[type][val, col]
end
|
#to_s ⇒ Object
38
39
40
41
42
43
|
# File 'lib/relationize/bq_relationizer.rb', line 38
def to_s
rows = @tuples.map { |tuple|
"(SELECT #{tuple.zip(@columns).zip(oriented_types).map(&method(:to_row)).join(', ')})"
}.join(', ')
"SELECT * FROM (SELECT * FROM #{rows}) AS #{@name}"
end
|