Skip to content

Commit 4056401

Browse files
committed
Fix RETURN QUERY EXECUTE handling
Fixes #211
1 parent 320bbac commit 4056401

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/pgspot/visitors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ def visit(self, node):
191191
self.state,
192192
"SELECT " + value["expr"]["PLpgSQL_expr"]["query"],
193193
)
194+
case "PLpgSQL_stmt_return_query":
195+
if "dynquery" in value:
196+
query = (
197+
"SELECT " + value["dynquery"]["PLpgSQL_expr"]["query"]
198+
)
199+
parsed = parse_sql(query)[0].stmt.targetList[0].val
200+
201+
# When the query is a string literal we can analyze it's content
202+
if (
203+
isinstance(parsed, ast.A_Const)
204+
and parsed.isnull is False
205+
and isinstance(parsed.val, ast.String)
206+
):
207+
visit_sql(
208+
self.state,
209+
parsed.val.sval,
210+
)
211+
194212
case "PLpgSQL_stmt_while":
195213
if "cond" in value:
196214
visit_sql(

testdata/expected/return_query.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PS005: Function without explicit search_path: ret_query(y integer) at line 2
2+
PS016: Unqualified function call: format at line 2
3+
4+
Errors: 0 Warnings: 2 Unknown: 0
5+

testdata/return_query.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
create function ret_query() returns table(y int)
3+
as $func$
4+
declare
5+
a text;
6+
begin
7+
return query execute $$select format('abc',1,2)::int$$;
8+
return query execute a;
9+
return query execute a + 'def';
10+
end;
11+
$func$ language plpgsql;
12+

0 commit comments

Comments
 (0)