Skip to content

Commit

Permalink
Deparse DO statements (#109)
Browse files Browse the repository at this point in the history
* Unit test for DO statement

* Implement deparse of DO statement
  • Loading branch information
herwinw authored and lfittl committed Oct 4, 2018
1 parent d8c31be commit 4784ff1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple
deparse_variable_set_stmt(node)
when VACUUM_STMT
deparse_vacuum_stmt(node)
when DO_STMT
deparse_do_stmt(node)
when STRING
if context == A_CONST
format("'%s'", node['str'].gsub("'", "''"))
Expand Down Expand Up @@ -571,6 +573,15 @@ def deparse_vacuum_options(node)
output
end

def deparse_do_stmt(node)
output = []
output << 'DO'
statement, *rest = node['args']
output << "$$#{statement['DefElem']['arg']['String']['str']}$$"
output += rest.map { |item| deparse_item(item) }
output.join(' ')
end

def deparse_cte(node)
output = []
output << node['ctename']
Expand Down
1 change: 1 addition & 0 deletions lib/pg_query/node_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PgQuery
DECLARE_CURSOR_STMT = 'DeclareCursorStmt'.freeze
DEF_ELEM = 'DefElem'.freeze
DELETE_STMT = 'DeleteStmt'.freeze
DO_STMT = 'DoStmt'.freeze
DROP_STMT = 'DropStmt'.freeze
EXECUTE_STMT = 'ExecuteStmt'.freeze
EXPLAIN_STMT = 'ExplainStmt'.freeze
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,20 @@
it { is_expected.to eq oneline_query }
end
end

context 'DO' do
context 'basic statement' do
let(:query) { 'DO $$BEGIN PERFORM * FROM information_schema.tables; END$$' }

it { is_expected.to eq oneline_query }
end

context 'with language' do
let(:query) { 'DO $$ BEGIN PERFORM * FROM information_schema.tables; END $$ language "plpgsql"' }

it { is_expected.to eq oneline_query }
end
end
end

describe '#deparse' do
Expand Down

0 comments on commit 4784ff1

Please sign in to comment.