Skip to content

Commit aec8d3d

Browse files
committed
fix(plpgsql-deparser): filter out implicit sqlstate/sqlerrm variables
PostgreSQL automatically adds sqlstate and sqlerrm variables for exception handling. These should not be explicitly declared in the output DECLARE section. Updated snapshots to reflect the fix.
1 parent 13cff51 commit aec8d3d

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

packages/plpgsql-deparser/__tests__/__snapshots__/hydrate-demo.test.ts.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ exports[`hydrate demonstration with big-function.sql should parse, hydrate, modi
4949
v_sql text;
5050
v_rowcount int := 0;
5151
v_lock_key bigint := CAST(CAST('x' || substr(md5(p_org_id::text), 1, 16) AS pg_catalog.bit(64)) AS bigint);
52-
sqlstate CONSTANT text;
53-
sqlerrm CONSTANT text;
5452
BEGIN
5553
BEGIN
5654
IF p_org_id IS NULL

packages/plpgsql-deparser/src/plpgsql-deparser.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,11 @@ export class PLpgSQLDeparser {
342342
const localVars = datums.filter(datum => {
343343
if ('PLpgSQL_var' in datum) {
344344
const v = datum.PLpgSQL_var;
345-
// Skip internal variables
346-
if (v.refname === 'found' || v.refname.startsWith('__')) {
345+
// Skip internal variables:
346+
// - 'found' is the implicit FOUND variable
347+
// - 'sqlstate' and 'sqlerrm' are implicit exception handling variables
348+
// - variables starting with '__' are internal
349+
if (v.refname === 'found' || v.refname === 'sqlstate' || v.refname === 'sqlerrm' || v.refname.startsWith('__')) {
347350
return false;
348351
}
349352
// Skip variables without lineno (usually parameters or internal)

0 commit comments

Comments
 (0)