Skip to content

Commit

Permalink
Merge pull request #97 from Quarantainenet/deparse_vacuum
Browse files Browse the repository at this point in the history
Deparse vacuum
  • Loading branch information
lfittl authored Sep 20, 2018
2 parents 6150b7d + 1f9277b commit d2fc31f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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

0 comments on commit d2fc31f

Please sign in to comment.