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

Deparse vacuum #97

Merged
merged 3 commits into from Sep 20, 2018
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
22 changes: 22 additions & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple
deparse_viewstmt(node)
when VARIABLE_SET_STMT
deparse_variable_set_stmt(node)
when VACUUM_STMT
deparse_vacuum_stmt(node)
when STRING
if context == A_CONST
format("'%s'", node['str'].gsub("'", "''"))
Expand Down Expand Up @@ -529,6 +531,26 @@ def deparse_variable_set_stmt(node)
output.join(' ')
end

def deparse_vacuum_stmt(node)
output = []
output << 'VACUUM'
output.concat(deparse_vacuum_options(node))
output << deparse_item(node['relation']) if node.key?('relation')
if node.key?('va_cols')
output << "(#{node['va_cols'].map(&method(:deparse_item)).join(', ')})"
end
output.join(' ')
end

def deparse_vacuum_options(node)
output = []
output << 'FULL' if node['options'][4] == 1
output << 'FREEZE' if node['options'][3] == 1
output << 'VERBOSE' if node['options'][2] == 1
output << 'ANALYZE' if node['options'][1] == 1
output
end

def deparse_cte(node)
output = []
output << node['ctename']
Expand Down
50 changes: 50 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,56 @@
it { is_expected.to eq "SET search_path TO 10000" }
end
end

context 'VACUUM' do
context 'without anything' do
let(:query) { 'VACUUM' }

it { is_expected.to eq oneline_query }
end

context 'with table name' do
let(:query) { 'VACUUM "t"' }

it { is_expected.to eq oneline_query }
end

context 'full' do
let(:query) { 'VACUUM FULL "t"' }

it { is_expected.to eq oneline_query }
end

context 'freeze' do
let(:query) { 'VACUUM FREEZE "t"' }

it { is_expected.to eq oneline_query }
end

context 'verbose' do
let(:query) { 'VACUUM VERBOSE "t"' }

it { is_expected.to eq oneline_query }
end

context 'analyze' do
let(:query) { 'VACUUM ANALYZE "t"' }

it { is_expected.to eq oneline_query }
end

context 'combine operations' do
let(:query) { 'VACUUM FULL FREEZE VERBOSE ANALYZE' }

it { is_expected.to eq oneline_query }
end

context 'adding column names' do
let(:query) { 'VACUUM ANALYZE "t" ("a", "b")' }

it { is_expected.to eq oneline_query }
end
end
end

describe '#deparse' do
Expand Down