From cd9ddc9142dac204c977436d95b3c424dc9a3ea3 Mon Sep 17 00:00:00 2001 From: Herwin Weststrate Date: Tue, 2 Oct 2018 15:14:48 +0200 Subject: [PATCH 1/3] Unit test for CREATE TEMPORARY TABLE --- spec/lib/pg_query/deparse_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index 73a48aab..d15f32b8 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -793,6 +793,12 @@ it { is_expected.to eq oneline_query } end + + context 'temporary table' do + let(:query) { 'CREATE TEMPORARY TABLE "temp" AS SELECT "c" FROM "t"' } + + it { is_expected.to eq oneline_query } + end end context 'DROP TABLE' do From f853707e03a31890dfa3bbc55bafa3a8e69e5d10 Mon Sep 17 00:00:00 2001 From: Herwin Weststrate Date: Tue, 2 Oct 2018 15:21:58 +0200 Subject: [PATCH 2/3] Allow CREATE TEMPORARY TABLE in deparse --- lib/pg_query/deparse.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 75afe5ff..d3670831 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -90,6 +90,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple deparse_create_function(node) when CREATE_STMT deparse_create_table(node) + when CREATE_TABLE_AS_STMT + deparse_create_table_as(node) when DEF_ELEM deparse_defelem(node) when DELETE_STMT @@ -669,6 +671,15 @@ def deparse_create_table(node) output.join(' ') end + def deparse_create_table_as(node) + output = [] + output << 'CREATE TEMPORARY TABLE' + output << deparse_item(node['into']['IntoClause']['rel']) + output << 'AS' + output << deparse_item(node['query']) + output.join(' ') + end + def deparse_when(node) output = ['WHEN'] output << deparse_item(node['expr']) From f5f3b6ef25c7e5c252de45c9cb09c6d5e0fb6613 Mon Sep 17 00:00:00 2001 From: Herwin Weststrate Date: Tue, 2 Oct 2018 15:25:27 +0200 Subject: [PATCH 3/3] Extract method deparse_into_clause This makes the code a bit more generic --- lib/pg_query/deparse.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index d3670831..51b12d29 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -92,6 +92,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple deparse_create_table(node) when CREATE_TABLE_AS_STMT deparse_create_table_as(node) + when INTO_CLAUSE + deparse_into_clause(node) when DEF_ELEM deparse_defelem(node) when DELETE_STMT @@ -674,12 +676,16 @@ def deparse_create_table(node) def deparse_create_table_as(node) output = [] output << 'CREATE TEMPORARY TABLE' - output << deparse_item(node['into']['IntoClause']['rel']) + output << deparse_item(node['into']) output << 'AS' output << deparse_item(node['query']) output.join(' ') end + def deparse_into_clause(node) + deparse_item(node['rel']) + end + def deparse_when(node) output = ['WHEN'] output << deparse_item(node['expr'])