Skip to content

Commit 4aa0b6a

Browse files
authored
Merge pull request IvorySQL#1152 from rophy/fix/1151
fix: allow SQLERRM in procedures when package has EXCEPTION variables
2 parents 3a40b6a + 128aab0 commit 4aa0b6a

3 files changed

Lines changed: 84 additions & 5 deletions

File tree

src/pl/plisql/src/expected/plisql_exception.out

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,45 @@ SELECT 'PRAGMA EXCEPTION_INIT negative error code tests completed' AS result;
340340
PRAGMA EXCEPTION_INIT negative error code tests completed
341341
(1 row)
342342

343+
--
344+
-- Test 12: SQLERRM in procedures when package has EXCEPTION variables (issue #1151)
345+
-- When a package declares user-defined EXCEPTION variables at package scope,
346+
-- passing SQLERRM to procedures from exception handlers should work.
347+
--
348+
CREATE OR REPLACE PACKAGE test_sqlerrm_with_exc IS
349+
my_exception EXCEPTION; -- User-defined exception at package scope
350+
PROCEDURE log_error(p_msg VARCHAR2);
351+
PROCEDURE test_sqlerrm;
352+
END test_sqlerrm_with_exc;
353+
/
354+
CREATE OR REPLACE PACKAGE BODY test_sqlerrm_with_exc IS
355+
PROCEDURE log_error(p_msg VARCHAR2) IS
356+
BEGIN
357+
RAISE INFO 'Logged: %', p_msg;
358+
END log_error;
359+
PROCEDURE test_sqlerrm IS
360+
BEGIN
361+
RAISE my_exception;
362+
EXCEPTION
363+
WHEN my_exception THEN
364+
-- This should work: passing SQLERRM to procedure
365+
log_error(SQLERRM);
366+
END test_sqlerrm;
367+
END test_sqlerrm_with_exc;
368+
/
369+
-- Test execution - this used to fail with "exception variables cannot be used in this context"
370+
BEGIN
371+
test_sqlerrm_with_exc.test_sqlerrm();
372+
END;
373+
/
374+
INFO: Logged: User-Defined Exception
375+
SELECT 'SQLERRM with package EXCEPTION test passed (issue #1151)' AS result;
376+
result
377+
----------------------------------------------------------
378+
SQLERRM with package EXCEPTION test passed (issue #1151)
379+
(1 row)
380+
381+
DROP PACKAGE test_sqlerrm_with_exc;
343382
-- Cleanup
344383
DROP PACKAGE test_pragma_init;
345384
DROP PACKAGE test_pragma_raise;

src/pl/plisql/src/pl_package.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,11 +3068,12 @@ is_const_datum(PLiSQL_execstate *estate, PLiSQL_datum *datum)
30683068
}
30693069
break;
30703070
case PLISQL_DTYPE_EXCEPTION:
3071-
3072-
/* Exception variables cannot be used here */
3073-
3074-
elog(ERROR, "exception variables cannot be used in this context");
3075-
3071+
/*
3072+
* Exception variables are constant identifiers - they cannot be
3073+
* assigned to. Return true to indicate they are constant and
3074+
* should be skipped by callers that iterate over datums.
3075+
*/
3076+
isconst = true;
30763077
break;
30773078

30783079

src/pl/plisql/src/sql/plisql_exception.sql

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,45 @@ END test_pragma_zero;
315315

316316
SELECT 'PRAGMA EXCEPTION_INIT negative error code tests completed' AS result;
317317

318+
--
319+
-- Test 12: SQLERRM in procedures when package has EXCEPTION variables (issue #1151)
320+
-- When a package declares user-defined EXCEPTION variables at package scope,
321+
-- passing SQLERRM to procedures from exception handlers should work.
322+
--
323+
CREATE OR REPLACE PACKAGE test_sqlerrm_with_exc IS
324+
my_exception EXCEPTION; -- User-defined exception at package scope
325+
PROCEDURE log_error(p_msg VARCHAR2);
326+
PROCEDURE test_sqlerrm;
327+
END test_sqlerrm_with_exc;
328+
/
329+
330+
CREATE OR REPLACE PACKAGE BODY test_sqlerrm_with_exc IS
331+
PROCEDURE log_error(p_msg VARCHAR2) IS
332+
BEGIN
333+
RAISE INFO 'Logged: %', p_msg;
334+
END log_error;
335+
336+
PROCEDURE test_sqlerrm IS
337+
BEGIN
338+
RAISE my_exception;
339+
EXCEPTION
340+
WHEN my_exception THEN
341+
-- This should work: passing SQLERRM to procedure
342+
log_error(SQLERRM);
343+
END test_sqlerrm;
344+
END test_sqlerrm_with_exc;
345+
/
346+
347+
-- Test execution - this used to fail with "exception variables cannot be used in this context"
348+
BEGIN
349+
test_sqlerrm_with_exc.test_sqlerrm();
350+
END;
351+
/
352+
353+
SELECT 'SQLERRM with package EXCEPTION test passed (issue #1151)' AS result;
354+
355+
DROP PACKAGE test_sqlerrm_with_exc;
356+
318357
-- Cleanup
319358
DROP PACKAGE test_pragma_init;
320359
DROP PACKAGE test_pragma_raise;

0 commit comments

Comments
 (0)