Skip to content

Commit

Permalink
Type Cast Parentheses Problem (#152)
Browse files Browse the repository at this point in the history
* Add support for CREATE DOMAIN

CREATE DOMAIN us_postal_code AS TEXT
CHECK(
   VALUE ~ '^\d{5}$'
OR VALUE ~ '^\d{5}-\d{4}$'
);

* Type Cast Parentheses Problem

Query:
SELECT * FROM kodsis WHERE create_date BETWEEN (CURRENT_DATE - 5)::DATE AND CURRENT_DATE

Deparser Result:
SELECT * FROM kodsis WHERE create_date BETWEEN CURRENT_DATE - 5::DATE AND CURRENT_DATE

Result is not same the original query. I fixed it.

New Deparser Result:
SELECT * FROM kodsis WHERE create_date BETWEEN (CURRENT_DATE - 5)::date AND CURRENT_DATE
  • Loading branch information
emin100 authored and lfittl committed Nov 11, 2019
1 parent 28bf123 commit 4fa328e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,8 @@ def deparse_typecast(node)
if deparse_item(node['typeName']) == 'boolean'
deparse_item(node['arg']) == "'t'" ? 'true' : 'false'
else
deparse_item(node['arg']) + '::' + deparse_typename(node['typeName'][TYPE_NAME])
context = true if node['arg']['A_Expr']
deparse_item(node['arg'], context) + '::' + deparse_typename(node['typeName'][TYPE_NAME])
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@
it { is_expected.to eq query }
end

context 'with parentheses' do
let(:query) { "SELECT (1 + 3)::int8" }

it { is_expected.to eq query }
end

context 'regclass' do
let(:query) { "SELECT ?::regclass" }

Expand Down

0 comments on commit 4fa328e

Please sign in to comment.