From 871f9478e3640a6dbf866ed62842241af0becf1f Mon Sep 17 00:00:00 2001 From: Herwin Weststrate Date: Wed, 24 Oct 2018 09:53:23 +0200 Subject: [PATCH 1/3] Unit test for COPY with subquery --- 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 bf4dcb2f..b61a56d7 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -1116,6 +1116,12 @@ it { is_expected.to eq oneline_query } end + + context 'SUBQUERY' do + let(:query) { 'COPY (SELECT 1 FROM "foo") TO STDOUT' } + + it { is_expected.to eq oneline_query } + end end context 'DO' do From 3bac10486f140a1b5d08b817f7e6f9a635bbbd08 Mon Sep 17 00:00:00 2001 From: Herwin Weststrate Date: Wed, 24 Oct 2018 09:56:12 +0200 Subject: [PATCH 2/3] Add subquery to COPY statement Still needs refactoring to keep Rubocop satisfied. --- lib/pg_query/deparse.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index b7d03766..6b6b89e1 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -658,7 +658,11 @@ def deparse_constraint(node) # rubocop:disable Metrics/CyclomaticComplexity def deparse_copy(node) output = ['COPY'] - output << deparse_item(node['relation']) + if node.key?('relation') + output << deparse_item(node['relation']) + elsif node.key?('query') + output << "(#{deparse_item(node['query'])})" + end columns = node.fetch('attlist', []).map { |column| deparse_item(column) } output << "(#{columns.join(', ')})" unless columns.empty? output << (node['is_from'] ? 'FROM' : 'TO') From 72e4f7d4d31c9c37c68b7e42bfe27c74407ffaf8 Mon Sep 17 00:00:00 2001 From: Herwin Weststrate Date: Wed, 24 Oct 2018 10:00:32 +0200 Subject: [PATCH 3/3] Refactor deparse_copy This keeps Rubocop satisfied --- lib/pg_query/deparse.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 6b6b89e1..760de418 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -667,14 +667,16 @@ def deparse_copy(node) output << "(#{columns.join(', ')})" unless columns.empty? output << (node['is_from'] ? 'FROM' : 'TO') output << 'PROGRAM' if node['is_program'] - output << if node.key?('filename') - "'#{node['filename']}'" - else - node['is_from'] ? 'STDIN' : 'STDOUT' - end + output << deparse_copy_output(node) output.join(' ') end + def deparse_copy_output(node) + return "'#{node['filename']}'" if node.key?('filename') + return 'STDIN' if node['is_from'] + 'STDOUT' + end + def deparse_create_function(node) output = [] output << 'CREATE'