Skip to content

Commit

Permalink
Create table as stmt (#102)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
herwinw authored and lfittl committed Oct 2, 2018
1 parent b6d8558 commit 043e15e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 043e15e

Please sign in to comment.