@@ -188,6 +188,20 @@ static HTAB *pltcl_proc_htab = NULL;
188188static FunctionCallInfo pltcl_current_fcinfo = NULL ;
189189static pltcl_proc_desc * pltcl_current_prodesc = NULL ;
190190
191+ /**********************************************************************
192+ * Lookup table for SQLSTATE condition names
193+ **********************************************************************/
194+ typedef struct
195+ {
196+ const char * label ;
197+ int sqlerrstate ;
198+ } TclExceptionNameMap ;
199+
200+ static const TclExceptionNameMap exception_name_map [] = {
201+ #include "pltclerrcodes.h" /* pgrminclude ignore */
202+ {NULL , 0 }
203+ };
204+
191205/**********************************************************************
192206 * Forward declarations
193207 **********************************************************************/
@@ -213,6 +227,7 @@ static pltcl_proc_desc *compile_pltcl_function(Oid fn_oid, Oid tgreloid,
213227static int pltcl_elog (ClientData cdata , Tcl_Interp * interp ,
214228 int objc , Tcl_Obj * const objv []);
215229static void pltcl_construct_errorCode (Tcl_Interp * interp , ErrorData * edata );
230+ static const char * pltcl_get_condition_name (int sqlstate );
216231static int pltcl_quote (ClientData cdata , Tcl_Interp * interp ,
217232 int objc , Tcl_Obj * const objv []);
218233static int pltcl_argisnull (ClientData cdata , Tcl_Interp * interp ,
@@ -1681,6 +1696,10 @@ pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata)
16811696 Tcl_NewStringObj ("SQLSTATE" , -1 ));
16821697 Tcl_ListObjAppendElement (interp , obj ,
16831698 Tcl_NewStringObj (unpack_sql_state (edata -> sqlerrcode ), -1 ));
1699+ Tcl_ListObjAppendElement (interp , obj ,
1700+ Tcl_NewStringObj ("condition" , -1 ));
1701+ Tcl_ListObjAppendElement (interp , obj ,
1702+ Tcl_NewStringObj (pltcl_get_condition_name (edata -> sqlerrcode ), -1 ));
16841703 Tcl_ListObjAppendElement (interp , obj ,
16851704 Tcl_NewStringObj ("message" , -1 ));
16861705 UTF_BEGIN ;
@@ -1806,6 +1825,23 @@ pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata)
18061825}
18071826
18081827
1828+ /**********************************************************************
1829+ * pltcl_get_condition_name() - find name for SQLSTATE
1830+ **********************************************************************/
1831+ static const char *
1832+ pltcl_get_condition_name (int sqlstate )
1833+ {
1834+ int i ;
1835+
1836+ for (i = 0 ; exception_name_map [i ].label != NULL ; i ++ )
1837+ {
1838+ if (exception_name_map [i ].sqlerrstate == sqlstate )
1839+ return exception_name_map [i ].label ;
1840+ }
1841+ return "unrecognized_sqlstate" ;
1842+ }
1843+
1844+
18091845/**********************************************************************
18101846 * pltcl_quote() - quote literal strings that are to
18111847 * be used in SPI_execute query strings
0 commit comments