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

Array indirection problem fixed. #162

Merged
merged 3 commits into from
Dec 27, 2020
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
6 changes: 5 additions & 1 deletion lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ def deparse_a_star(_node)
def deparse_a_indirection(node)
output = []
arg = deparse_item(node['arg'])
output << if node['arg'].key?(FUNC_CALL) || node['arg'].key?(SUB_LINK)
array_indirection = []
if node['indirection']
array_indirection = node['indirection'].select { |a| a.key?(A_INDICES) }
end
output << if node['arg'].key?(FUNC_CALL) || node['arg'].key?(A_EXPR) || (node['arg'].key?(SUB_LINK) && array_indirection.count.zero?)
"(#{arg})."
else
arg
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@
it { is_expected.to eq query }
end

context 'array indirection' do
let(:query) { "SELECT \"proname\", (SELECT regexp_split_to_array(\"proargtypes\"::text, ' ') )[\"idx\"] AS argtype, \"proargnames\"[\"idx\"] AS argname FROM \"pg_proc\"" }

it { is_expected.to eq oneline_query }
end

context 'sub query indirection' do
let(:query) { "SELECT COALESCE(((SELECT customer.sp_person(\"n\".\"id\") AS sp_person)).\"city_id\", NULL::int) AS city_id FROM \"customer\".\"tb_customer\" n" }

Expand All @@ -346,6 +352,12 @@
it { is_expected.to eq query }
end

context 'indirection with star' do
let(:query) { 'SELECT ("k" #= hstore(\'{id}\'::text[], ARRAY[1::text])).* FROM "test" k' }

it { is_expected.to eq query }
end

context 'NOT' do
let(:query) { 'SELECT * FROM "x" WHERE NOT "y"' }

Expand Down