Skip to content

Commit 75e4643

Browse files
committed
Handle "CALL fn()"
1 parent 24597b2 commit 75e4643

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/pg_query/parse.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
106106
statements = @tree.stmts.dup.to_a.map(&:stmt)
107107
from_clause_items = [] # types: select, dml, ddl
108108
subselect_items = []
109+
call_items = [] # CALL fn()
109110

110111
loop do
111112
statement = statements.shift
@@ -171,6 +172,8 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
171172
when :copy_stmt
172173
from_clause_items << { item: PgQuery::Node.new(range_var: statement.copy_stmt.relation), type: :dml } if statement.copy_stmt.relation
173174
statements << statement.copy_stmt.query
175+
when :call_stmt
176+
call_items << statement.call_stmt
174177
# The following statement types are DDL (changing table structure)
175178
when :alter_table_stmt
176179
case statement.alter_table_stmt.objtype
@@ -285,6 +288,7 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
285288
when :sub_link
286289
statements << next_item.sub_link.subselect
287290
when :func_call
291+
# See also CALL below
288292
subselect_items.concat(next_item.func_call.args.to_ary)
289293
@functions << {
290294
function: next_item.func_call.funcname.map { |f| f.string.sval }.join('.'),
@@ -299,6 +303,17 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
299303
end
300304
end
301305

306+
# CALL fn()
307+
next_item = call_items.shift
308+
if next_item
309+
# Treat as a sub-select. Note the difference in underscore in func_call versus the above.
310+
subselect_items.concat(next_item.funccall.args.to_ary)
311+
@functions << {
312+
function: next_item.funccall.funcname.map { |f| f.string.sval }.join('.'),
313+
type: :call
314+
}
315+
end
316+
302317
next_item = from_clause_items.shift
303318
if next_item && next_item[:item]
304319
case next_item[:item].node

spec/lib/parse_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,17 @@
12761276
expect(query.call_functions).to eq ['foo.testfunc']
12771277
end
12781278

1279+
it 'correctly finds functions invoked with CALL' do
1280+
query = described_class.parse(<<-SQL)
1281+
CALL foo.testfunc(1);
1282+
SQL
1283+
expect(query.tables).to eq []
1284+
expect(query.warnings).to eq []
1285+
expect(query.functions).to eq ['foo.testfunc']
1286+
expect(query.ddl_functions).to eq []
1287+
expect(query.call_functions).to eq ['foo.testfunc']
1288+
end
1289+
12791290
it 'correctly finds dropped functions' do
12801291
query = described_class.parse(<<-SQL)
12811292
DROP FUNCTION IF EXISTS foo.testfunc(x integer);

0 commit comments

Comments
 (0)