Skip to content

Commit

Permalink
Quote column name (#13)
Browse files Browse the repository at this point in the history
* Quote column name

* Fix require path

* Fix complex paths

* Change String literal syntax to %Q{}
  • Loading branch information
yancya authored Dec 12, 2019
1 parent 75efb54 commit 2768617
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 43 deletions.
7 changes: 0 additions & 7 deletions lib/rel_lib/relationizer.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/rel_lib/relationizer/version.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/relationizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_relative "relationizer/version"
require_relative "relationizer/postgresql"
require_relative "relationizer/big_query"

module Relationizer
end
4 changes: 2 additions & 2 deletions lib/relationizer/big_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def array_type(array)
def types_exp(names, types)
case names.length
when 1
%Q{ARRAY<STRUCT<#{names.first} #{types.first}, ___dummy STRING>>}
%Q{ARRAY<STRUCT<`#{names.first}` #{types.first}, `___dummy` STRING>>}
else
%Q{ARRAY<STRUCT<#{names.zip(types).map { |(name, type)| "#{name} #{type}" }.join(", ")}>>}
%Q{ARRAY<STRUCT<#{names.zip(types).map { |(name, type)| "`#{name}` #{type}" }.join(", ")}>>}
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/relationizer/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ def create_relation_literal(schema, tuples)

def select_exp(schema, tuples)
tuples.transpose.zip(schema.to_a).map { |(values, (name, type))|
next "#{name}::#{type.to_s.upcase}" if type
next %Q{"#{name}"::#{type.to_s.upcase}} if type

values.
map(&DEFAULT_TYPES).compact.uniq.
tap(&method(:empty_candidate_check)).
tap(&method(:many_candidate_check)).
first.
to_s.upcase.
tap { |fixed_type| break "#{name}::#{fixed_type}" }
tap { |fixed_type| break %Q{"#{name}"::#{fixed_type}} }
}.join(", ")
end

Expand Down
40 changes: 20 additions & 20 deletions test/big_query_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'test-unit'
require_relative '../lib/relationizer/big_query.rb'
require_relative '../lib/relationizer/big_query'
require_relative './to_one_line.rb'

class BigQueryTest < Test::Unit::TestCase
Expand All @@ -18,8 +18,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
name STRING>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`name` STRING>>
[(1, 'hoge'),
(2, 'fuga')])
SQL
Expand All @@ -36,9 +36,9 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
name STRING,
combination ARRAY<INT64>>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`name` STRING,
`combination` ARRAY<INT64>>>
[(1, 'hoge', [1, 2, 3]),
(2, 'fuga', [4, 5, 6])])
SQL
Expand All @@ -54,8 +54,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
ratio FLOAT64>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`ratio` FLOAT64>>
[(1, 1.002),
(2, 3.14)])
SQL
Expand All @@ -71,8 +71,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
birthday DATE>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`birthday` DATE>>
[(1, '1999-02-11'),
(2, '2000-01-15')])
SQL
Expand All @@ -88,8 +88,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
created_at TIMESTAMP>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`created_at` TIMESTAMP>>
[(1, '1999-02-11 12:00:33'),
(2, '2000-01-15 17:45:11')])
SQL
Expand All @@ -105,8 +105,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
usable BOOL>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`usable` BOOL>>
[(1, true),
(2, false)])
SQL
Expand All @@ -119,8 +119,8 @@ class BigQueryTest < Test::Unit::TestCase
[3]
],
<<~SQL.to_one_line
SELECT id FROM UNNEST(ARRAY<STRUCT<id INT64,
___dummy STRING>>
SELECT id FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`___dummy` STRING>>
[(1, NULL),
(2, NULL),
(3, NULL)])
Expand All @@ -137,8 +137,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
ratio FLOAT64>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`ratio` FLOAT64>>
[(1, 1),
(2, 3.14)])
SQL
Expand All @@ -154,8 +154,8 @@ class BigQueryTest < Test::Unit::TestCase
],
<<~SQL.to_one_line
SELECT *
FROM UNNEST(ARRAY<STRUCT<id INT64,
ratio BOOL>>
FROM UNNEST(ARRAY<STRUCT<`id` INT64,
`ratio` BOOL>>
[(1, true),
(2, false)])
SQL
Expand Down
18 changes: 9 additions & 9 deletions test/postgres_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test-unit'
require 'time'
require_relative '../lib/relationizer/postgresql.rb'
require_relative '../lib/relationizer/postgresql'

class PostgresqlTest < Test::Unit::TestCase
include Relationizer::Postgresql
Expand All @@ -9,42 +9,42 @@ class PostgresqlTest < Test::Unit::TestCase
"NULL" => [
{ id: nil },
[[1], [nil]],
%Q{SELECT id::INT8 FROM (VALUES('1'), (NULL)) AS t("id")}
%Q{SELECT "id"::INT8 FROM (VALUES('1'), (NULL)) AS t("id")}
],
"INT and TEXT" => [
{ id: nil, name: nil },
[[1, 'hoge'], [2, 'fuga']],
%Q{SELECT id::INT8, name::TEXT FROM (VALUES('1', 'hoge'), ('2', 'fuga')) AS t("id", "name")}
%Q{SELECT "id"::INT8, "name"::TEXT FROM (VALUES('1', 'hoge'), ('2', 'fuga')) AS t("id", "name")}
],
"Bignum" => [
{ id: nil },
[[2_147_483_648]], # 2_147_483_648.bit_length #=> 32
%Q{SELECT id::INT8 FROM (VALUES('2147483648')) AS t("id")}
%Q{SELECT "id"::INT8 FROM (VALUES('2147483648')) AS t("id")}
],
"Float" => [
{ f: nil },
[[0.0005]],
%Q{SELECT f::FLOAT8 FROM (VALUES('0.0005')) AS t("f")}
%Q{SELECT "f"::FLOAT8 FROM (VALUES('0.0005')) AS t("f")}
],
"Bool" => [
{ b: nil },
[[true], [false]],
%Q{SELECT b::BOOLEAN FROM (VALUES('true'), ('false')) AS t("b")}
%Q{SELECT "b"::BOOLEAN FROM (VALUES('true'), ('false')) AS t("b")}
],
"Date" => [
{ date: nil },
[[Date.parse('2017-04-25')]],
%Q{SELECT date::DATE FROM (VALUES('2017-04-25')) AS t("date")}
%Q{SELECT "date"::DATE FROM (VALUES('2017-04-25')) AS t("date")}
],
"Time" => [
{ time: nil },
[[Time.parse('2017-04-25 16:36:00+09:00')]],
%Q{SELECT time::TIMESTAMPTZ FROM (VALUES('2017-04-25 16:36:00 +0900')) AS t("time")}
%Q{SELECT "time"::TIMESTAMPTZ FROM (VALUES('2017-04-25 16:36:00 +0900')) AS t("time")}
],
"DateTime" => [
{ time: nil },
[[DateTime.parse('2017-04-25 16:36:00+09:00')]],
%Q{SELECT time::TIMESTAMPTZ FROM (VALUES('2017-04-25T16:36:00+09:00')) AS t("time")}
%Q{SELECT "time"::TIMESTAMPTZ FROM (VALUES('2017-04-25T16:36:00+09:00')) AS t("time")}
]
}

Expand Down

0 comments on commit 2768617

Please sign in to comment.