|
3 | 3 | * procedural language
|
4 | 4 | *
|
5 | 5 | * IDENTIFICATION
|
6 |
| - * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.138 2005/05/06 17:24:55 tgl Exp $ |
| 6 | + * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.139 2005/05/26 00:16:31 momjian Exp $ |
7 | 7 | *
|
8 | 8 | * This software is copyrighted by Jan Wieck - Hamburg.
|
9 | 9 | *
|
@@ -180,6 +180,7 @@ static Datum exec_simple_cast_value(Datum value, Oid valtype,
|
180 | 180 | static void exec_init_tuple_store(PLpgSQL_execstate *estate);
|
181 | 181 | static bool compatible_tupdesc(TupleDesc td1, TupleDesc td2);
|
182 | 182 | static void exec_set_found(PLpgSQL_execstate *estate, bool state);
|
| 183 | +static char *unpack_sql_state(int ssval); |
183 | 184 |
|
184 | 185 |
|
185 | 186 | /* ----------
|
@@ -747,6 +748,20 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
|
747 | 748 | int i;
|
748 | 749 | int n;
|
749 | 750 |
|
| 751 | + |
| 752 | + /* setup SQLSTATE and SQLERRM */ |
| 753 | + PLpgSQL_var *var; |
| 754 | + |
| 755 | + var = (PLpgSQL_var *) (estate->datums[block->sqlstate_varno]); |
| 756 | + var->isnull = false; |
| 757 | + var->freeval = true; |
| 758 | + var->value = DirectFunctionCall1(textin, CStringGetDatum("00000")); |
| 759 | + |
| 760 | + var = (PLpgSQL_var *) (estate->datums[block->sqlerrm_varno]); |
| 761 | + var->isnull = false; |
| 762 | + var->freeval = true; |
| 763 | + var->value = DirectFunctionCall1(textin, CStringGetDatum("Sucessful completion")); |
| 764 | + |
750 | 765 | /*
|
751 | 766 | * First initialize all variables declared in this block
|
752 | 767 | */
|
@@ -855,6 +870,16 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
|
855 | 870 | RollbackAndReleaseCurrentSubTransaction();
|
856 | 871 | MemoryContextSwitchTo(oldcontext);
|
857 | 872 | CurrentResourceOwner = oldowner;
|
| 873 | + |
| 874 | + /* set SQLSTATE and SQLERRM variables */ |
| 875 | + |
| 876 | + var = (PLpgSQL_var *) (estate->datums[block->sqlstate_varno]); |
| 877 | + pfree((void *) (var->value)); |
| 878 | + var->value = DirectFunctionCall1(textin, CStringGetDatum(unpack_sql_state(edata->sqlerrcode))); |
| 879 | + |
| 880 | + var = (PLpgSQL_var *) (estate->datums[block->sqlerrm_varno]); |
| 881 | + pfree((void *) (var->value)); |
| 882 | + var->value = DirectFunctionCall1(textin, CStringGetDatum(edata->message)); |
858 | 883 |
|
859 | 884 | /*
|
860 | 885 | * If AtEOSubXact_SPI() popped any SPI context of the subxact,
|
@@ -919,6 +944,26 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
|
919 | 944 | return PLPGSQL_RC_OK;
|
920 | 945 | }
|
921 | 946 |
|
| 947 | +/* |
| 948 | + * unpack MAKE_SQLSTATE code |
| 949 | + * This code is copied from backend/utils/error/elog.c. |
| 950 | + */ |
| 951 | +static char * |
| 952 | +unpack_sql_state(int ssval) |
| 953 | +{ |
| 954 | + static char tbuf[12]; |
| 955 | + int i; |
| 956 | + |
| 957 | + for (i = 0; i < 5; i++) |
| 958 | + { |
| 959 | + tbuf[i] = PGUNSIXBIT(ssval); |
| 960 | + ssval >>= 6; |
| 961 | + } |
| 962 | + tbuf[i] = '\0'; |
| 963 | + return tbuf; |
| 964 | +} |
| 965 | + |
| 966 | + |
922 | 967 |
|
923 | 968 | /* ----------
|
924 | 969 | * exec_stmts Iterate over a list of statements
|
|
0 commit comments