From 107173382f8434c0b4d9460d7d7e4780a3452759 Mon Sep 17 00:00:00 2001 From: Chris Winslett Date: Mon, 9 May 2016 17:21:39 -0500 Subject: [PATCH] complete the VARIABLE_SET_STMT deparsing --- lib/pg_query/deparse.rb | 12 ++++++++++ spec/lib/pg_query/deparse_spec.rb | 37 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 0bb84710..ccae19d6 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -136,6 +136,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple deparse_with_clause(node) when VIEW_STMT deparse_viewstmt(node) + when VARIABLE_SET_STMT + deparse_variable_set_stmt(node) when STRING if context == A_CONST format("'%s'", node['str'].gsub("'", "''")) @@ -440,6 +442,16 @@ def deparse_viewstmt(node) output.join(' ') end + def deparse_variable_set_stmt(node) + output = [] + output << "SET" + output << "LOCAL" if node["is_local"] + output << node['name'] + output << "TO" + output << node['args'].map { |arg| deparse_item(arg) }.join(', ') + output.join(' ') + end + def deparse_cte(node) output = [] output << node['ctename'] diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index 98be1f99..ba88caaa 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -566,6 +566,43 @@ end end end + + context 'SET' do + context 'with integer value' do + let(:query) do + ''' + SET statement_timeout TO 10000; + ''' + end + it { is_expected.to eq oneline_query } + end + context 'with string value' do + let(:query) do + ''' + SET search_path TO \'my_schema\', \'public\'; + ''' + end + it { is_expected.to eq oneline_query } + end + context 'with local scope' do + let(:query) do + ''' + SET LOCAL search_path TO \'my_schema\', \'public\'; + ''' + end + it { is_expected.to eq oneline_query } + end + # Because SESSION is default, it is removed by the query parser. + context 'with session scope' do + let(:query) do + ''' + SET SESSION search_path TO 10000; + ''' + end + it { is_expected.to eq "SET search_path TO 10000" } + end + end + end describe '#deparse' do