Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create table as problem. Support without TEMP. #149

Merged
merged 6 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,22 @@ def deparse_create_table(node)

def deparse_create_table_as(node)
output = []
output << 'CREATE TEMPORARY TABLE'
output << 'CREATE'

into = node['into']['IntoClause']
persistence = relpersistence(into['rel'])
output << persistence if persistence

output << 'TABLE'

output << deparse_item(node['into'])

if into['onCommit'] > 0
output << 'ON COMMIT'
output << 'DELETE ROWS' if into['onCommit'] == 2
output << 'DROP' if into['onCommit'] == 3
end

output << 'AS'
output << deparse_item(node['query'])
output.join(' ')
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,18 @@

it { is_expected.to eq oneline_query }
end

context 'create table as' do
let(:query) { 'CREATE TABLE "films2" AS SELECT * FROM "films"' }

it { is_expected.to eq oneline_query }
end

context 'create table ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP }' do
let(:query) { 'CREATE TEMPORARY TABLE "films_recent" ON COMMIT DROP AS SELECT * FROM "films" WHERE "date_prod" > $1' }

it { is_expected.to eq oneline_query }
end
end

context 'DROP TABLE' do
Expand Down