From 4fa328ef0b92dc9f56a7d9d9e3be8ccd5fe16c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Emin=20KARAKA=C5=9E?= Date: Mon, 11 Nov 2019 05:46:08 +0300 Subject: [PATCH] Type Cast Parentheses Problem (#152) * 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 --- lib/pg_query/deparse.rb | 3 ++- spec/lib/pg_query/deparse_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pg_query/deparse.rb b/lib/pg_query/deparse.rb index 2e160df8..8009af03 100644 --- a/lib/pg_query/deparse.rb +++ b/lib/pg_query/deparse.rb @@ -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 diff --git a/spec/lib/pg_query/deparse_spec.rb b/spec/lib/pg_query/deparse_spec.rb index ea605639..201b62c1 100644 --- a/spec/lib/pg_query/deparse_spec.rb +++ b/spec/lib/pg_query/deparse_spec.rb @@ -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" }