Skip to content

Commit

Permalink
Fix plpgsql WHILE handling
Browse files Browse the repository at this point in the history
  • Loading branch information
svenklemm committed Oct 8, 2024
1 parent 3393f96 commit 2da7637
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/pgspot/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ def visit(self, node):
self.state,
"SELECT " + value["expr"]["PLpgSQL_expr"]["query"],
)
case "PLpgSQL_stmt_while":
if "cond" in value:
visit_sql(
self.state,
"SELECT " + value["cond"]["PLpgSQL_expr"]["query"],
)
if "body" in value:
self.visit(value["body"])
case _:
self.visit(value)

Expand Down
10 changes: 10 additions & 0 deletions testdata/expected/while.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PS002: Unsafe function creation: w1() at line 1
PS005: Function without explicit search_path: w1() at line 1
PS017: Unqualified object reference: bool in CAST(format1('%s', 'false') AS bool) at line 1
PS016: Unqualified function call: format1 at line 1
PS001: Unqualified operator: '<' in 0 < _count at line 1
PS001: Unqualified operator: '-' in _count - 1 at line 1
PS016: Unqualified function call: format2 at line 1

Errors: 1 Warnings: 6 Unknown: 0

13 changes: 13 additions & 0 deletions testdata/while.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
create or replace function w1() returns void as $func$
declare
_count bigint;
begin
select 100 into strict _count;
while format1('%s','false')::bool loop
end loop;
while 0 < _count loop
_count := _count - 1;
PERFORM format2('%s','true');
end loop;
end
$func$ language plpgsql;

0 comments on commit 2da7637

Please sign in to comment.