condition value

fix_bug
This commit is contained in:
nnuanyang
2022-12-21 00:15:10 -08:00
parent ba442ff864
commit e6e765493e
3 changed files with 92 additions and 3 deletions

View File

@ -772,10 +772,31 @@ declare_stmt : T_DECLARE_CURSOR decl_varname K_CURSOR opt_scrollable
condition_value : K_SQLSTATE
{
/* next token should be a string literal */
char *sqlstatestr;
if (yylex() != SCONST)
yyerror("syntax error");
yylex();
if (strcmp(yylval.str, "value") ==0) {
yylex();
}
sqlstatestr = yylval.str;
if (strlen(sqlstatestr) != 5)
yyerror("invalid SQLSTATE code");
if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
yyerror("invalid SQLSTATE code");
if (strncmp(sqlstatestr, "00", 2) == 0) {
const char* message = "bad SQLSTATE";
InsertErrorMessage(message, plpgsql_yylloc);
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION),
errmsg("bad SQLSTATE '%s'",sqlstatestr)));
}
$$ = MAKE_SQLSTATE(sqlstatestr[0],
sqlstatestr[1],
sqlstatestr[2],
sqlstatestr[3],
sqlstatestr[4]);
sqlstatestr = yylval.str;
if (strlen(sqlstatestr) != 5)

View File

@ -402,6 +402,46 @@ END;
/
ERROR: bad SQLSTATE '00000'
CONTEXT: compilation of PL/pgSQL function "test_condition_6" near line 3
create or replace procedure test_condition_1 as
declare
a int;
BEGIN
declare DIVISION_ZERO condition for SQLSTATE value '22012';
a := 1/0;
exception
when DIVISION_ZERO then
BEGIN
RAISE NOTICE 'SQLSTATE = %, SQLERRM = %', SQLSTATE,SQLERRM;
END;
END;
/
call test_condition_1();
NOTICE: SQLSTATE = 22012, SQLERRM = division by zero
test_condition_1
------------------
(1 row)
create or replace procedure test_condition_1 as
declare
a int;
BEGIN
declare DIVISION_ZERO condition for SQLSTATE "22012";
a := 1/0;
exception
when DIVISION_ZERO then
BEGIN
RAISE NOTICE 'SQLSTATE = %, SQLERRM = %', SQLSTATE,SQLERRM;
END;
END;
/
call test_condition_1();
NOTICE: SQLSTATE = 22012, SQLERRM = division by zero
test_condition_1
------------------
(1 row)
\c regression
drop trigger animal_trigger1;
ERROR: drop trigger without table name only support in B-format database

View File

@ -310,6 +310,34 @@ BEGIN
RAISE NOTICE 'declare condition successed';
END;
/
create or replace procedure test_condition_1 as
declare
a int;
BEGIN
declare DIVISION_ZERO condition for SQLSTATE value '22012';
a := 1/0;
exception
when DIVISION_ZERO then
BEGIN
RAISE NOTICE 'SQLSTATE = %, SQLERRM = %', SQLSTATE,SQLERRM;
END;
END;
/
call test_condition_1();
create or replace procedure test_condition_1 as
declare
a int;
BEGIN
declare DIVISION_ZERO condition for SQLSTATE "22012";
a := 1/0;
exception
when DIVISION_ZERO then
BEGIN
RAISE NOTICE 'SQLSTATE = %, SQLERRM = %', SQLSTATE,SQLERRM;
END;
END;
/
call test_condition_1();
\c regression
drop trigger animal_trigger1;