From 043e15ee99154079f34c425da93ae4cbd508d1f9 Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 2 Oct 2018 19:41:17 +0200 Subject: [PATCH] Create table as stmt (#102) * Unit test for CREATE TEMPORARY TABLE * Allow CREATE TEMPORARY TABLE in deparse * Extract method deparse_into_clause This makes the code a bit more generic --- lib/pg_query/deparse.rb | 17 +++++++++++++++++ spec/lib/pg_query/deparse_spec.rb | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index f02dc97d..34169563 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -90,6 +90,10 @@ 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 INTO_CLAUSE + deparse_into_clause(node) when DEF_ELEM deparse_defelem(node) when DELETE_STMT @@ -681,6 +685,19 @@ def deparse_create_table(node) output.join(' ') end + def deparse_create_table_as(node) + output = [] + output << 'CREATE TEMPORARY TABLE' + 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']) diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index 3a61b981..239b94f6 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -805,6 +805,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