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

Enable SET deparsing #48

Merged
merged 1 commit into from
May 10, 2016
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
12 changes: 12 additions & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("'", "''"))
Expand Down Expand Up @@ -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']
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down