Skip to content

Commit

Permalink
Fix RETURN QUERY EXECUTE handling
Browse files Browse the repository at this point in the history
Fixes #211
  • Loading branch information
svenklemm committed Jan 6, 2025
1 parent 320bbac commit 27965f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/pgspot/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ def visit(self, node):
self.state,
"SELECT " + value["expr"]["PLpgSQL_expr"]["query"],
)
case "PLpgSQL_stmt_return_query":
if "dynquery" in value:
query = (
"SELECT " + value["dynquery"]["PLpgSQL_expr"]["query"]
)
parsed = parse_sql(query)[0].stmt.targetList[0].val

# When the query is a string literal we can analyze it's content
if (
isinstance(parsed, ast.A_Const)
and parsed.isnull is False
and isinstance(parsed.val, ast.String)
):
visit_sql(
self.state,
parsed.val.sval,
)

case "PLpgSQL_stmt_while":
if "cond" in value:
visit_sql(
Expand Down
5 changes: 5 additions & 0 deletions testdata/expected/return_query.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PS005: Function without explicit search_path: ret_query(y integer) at line 2
PS016: Unqualified function call: format at line 2

Errors: 0 Warnings: 2 Unknown: 0

12 changes: 12 additions & 0 deletions testdata/return_query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

create function ret_query() returns table(y int)
as $func$
declare
a text;
begin
return query execute $$select format('abc',1,2)::int$$;
return query execute a;
return query execute a + 'def';
end;
$func$ language plpgsql;

0 comments on commit 27965f2

Please sign in to comment.