LCOV - code coverage report
Current view: top level - src/pl/plpython - plpy_plpymodule.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 179 197 90.9 %
Date: 2020-12-05 18:06:08 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * the plpy module
       3             :  *
       4             :  * src/pl/plpython/plpy_plpymodule.c
       5             :  */
       6             : 
       7             : #include "postgres.h"
       8             : 
       9             : #include "access/xact.h"
      10             : #include "mb/pg_wchar.h"
      11             : #include "plpy_cursorobject.h"
      12             : #include "plpy_elog.h"
      13             : #include "plpy_main.h"
      14             : #include "plpy_planobject.h"
      15             : #include "plpy_plpymodule.h"
      16             : #include "plpy_resultobject.h"
      17             : #include "plpy_spi.h"
      18             : #include "plpy_subxactobject.h"
      19             : #include "plpython.h"
      20             : #include "utils/builtins.h"
      21             : #include "utils/snapmgr.h"
      22             : 
      23             : HTAB       *PLy_spi_exceptions = NULL;
      24             : 
      25             : 
      26             : static void PLy_add_exceptions(PyObject *plpy);
      27             : static PyObject *PLy_create_exception(char *name,
      28             :                                       PyObject *base, PyObject *dict,
      29             :                                       const char *modname, PyObject *mod);
      30             : static void PLy_generate_spi_exceptions(PyObject *mod, PyObject *base);
      31             : 
      32             : /* module functions */
      33             : static PyObject *PLy_debug(PyObject *self, PyObject *args, PyObject *kw);
      34             : static PyObject *PLy_log(PyObject *self, PyObject *args, PyObject *kw);
      35             : static PyObject *PLy_info(PyObject *self, PyObject *args, PyObject *kw);
      36             : static PyObject *PLy_notice(PyObject *self, PyObject *args, PyObject *kw);
      37             : static PyObject *PLy_warning(PyObject *self, PyObject *args, PyObject *kw);
      38             : static PyObject *PLy_error(PyObject *self, PyObject *args, PyObject *kw);
      39             : static PyObject *PLy_fatal(PyObject *self, PyObject *args, PyObject *kw);
      40             : static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
      41             : static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
      42             : static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
      43             : static PyObject *PLy_commit(PyObject *self, PyObject *args);
      44             : static PyObject *PLy_rollback(PyObject *self, PyObject *args);
      45             : 
      46             : 
      47             : /* A list of all known exceptions, generated from backend/utils/errcodes.txt */
      48             : typedef struct ExceptionMap
      49             : {
      50             :     char       *name;
      51             :     char       *classname;
      52             :     int         sqlstate;
      53             : } ExceptionMap;
      54             : 
      55             : static const ExceptionMap exception_map[] = {
      56             : #include "spiexceptions.h"
      57             :     {NULL, NULL, 0}
      58             : };
      59             : 
      60             : static PyMethodDef PLy_methods[] = {
      61             :     /*
      62             :      * logging methods
      63             :      */
      64             :     {"debug", (PyCFunction) (pg_funcptr_t) PLy_debug, METH_VARARGS | METH_KEYWORDS, NULL},
      65             :     {"log", (PyCFunction) (pg_funcptr_t) PLy_log, METH_VARARGS | METH_KEYWORDS, NULL},
      66             :     {"info", (PyCFunction) (pg_funcptr_t) PLy_info, METH_VARARGS | METH_KEYWORDS, NULL},
      67             :     {"notice", (PyCFunction) (pg_funcptr_t) PLy_notice, METH_VARARGS | METH_KEYWORDS, NULL},
      68             :     {"warning", (PyCFunction) (pg_funcptr_t) PLy_warning, METH_VARARGS | METH_KEYWORDS, NULL},
      69             :     {"error", (PyCFunction) (pg_funcptr_t) PLy_error, METH_VARARGS | METH_KEYWORDS, NULL},
      70             :     {"fatal", (PyCFunction) (pg_funcptr_t) PLy_fatal, METH_VARARGS | METH_KEYWORDS, NULL},
      71             : 
      72             :     /*
      73             :      * create a stored plan
      74             :      */
      75             :     {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
      76             : 
      77             :     /*
      78             :      * execute a plan or query
      79             :      */
      80             :     {"execute", PLy_spi_execute, METH_VARARGS, NULL},
      81             : 
      82             :     /*
      83             :      * escaping strings
      84             :      */
      85             :     {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL},
      86             :     {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL},
      87             :     {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL},
      88             : 
      89             :     /*
      90             :      * create the subtransaction context manager
      91             :      */
      92             :     {"subtransaction", PLy_subtransaction_new, METH_NOARGS, NULL},
      93             : 
      94             :     /*
      95             :      * create a cursor
      96             :      */
      97             :     {"cursor", PLy_cursor, METH_VARARGS, NULL},
      98             : 
      99             :     /*
     100             :      * transaction control
     101             :      */
     102             :     {"commit", PLy_commit, METH_NOARGS, NULL},
     103             :     {"rollback", PLy_rollback, METH_NOARGS, NULL},
     104             : 
     105             :     {NULL, NULL, 0, NULL}
     106             : };
     107             : 
     108             : static PyMethodDef PLy_exc_methods[] = {
     109             :     {NULL, NULL, 0, NULL}
     110             : };
     111             : 
     112             : #if PY_MAJOR_VERSION >= 3
     113             : static PyModuleDef PLy_module = {
     114             :     PyModuleDef_HEAD_INIT,
     115             :     .m_name = "plpy",
     116             :     .m_size = -1,
     117             :     .m_methods = PLy_methods,
     118             : };
     119             : 
     120             : static PyModuleDef PLy_exc_module = {
     121             :     PyModuleDef_HEAD_INIT,
     122             :     .m_name = "spiexceptions",
     123             :     .m_size = -1,
     124             :     .m_methods = PLy_exc_methods,
     125             : };
     126             : 
     127             : /*
     128             :  * Must have external linkage, because PyMODINIT_FUNC does dllexport on
     129             :  * Windows-like platforms.
     130             :  */
     131             : PyMODINIT_FUNC
     132             : PyInit_plpy(void)
     133             : {
     134             :     PyObject   *m;
     135             : 
     136             :     m = PyModule_Create(&PLy_module);
     137             :     if (m == NULL)
     138             :         return NULL;
     139             : 
     140             :     PLy_add_exceptions(m);
     141             : 
     142             :     return m;
     143             : }
     144             : #endif                          /* PY_MAJOR_VERSION >= 3 */
     145             : 
     146             : void
     147          46 : PLy_init_plpy(void)
     148             : {
     149             :     PyObject   *main_mod,
     150             :                *main_dict,
     151             :                *plpy_mod;
     152             : 
     153             : #if PY_MAJOR_VERSION < 3
     154             :     PyObject   *plpy;
     155             : #endif
     156             : 
     157             :     /*
     158             :      * initialize plpy module
     159             :      */
     160          46 :     PLy_plan_init_type();
     161          46 :     PLy_result_init_type();
     162          46 :     PLy_subtransaction_init_type();
     163          46 :     PLy_cursor_init_type();
     164             : 
     165             : #if PY_MAJOR_VERSION >= 3
     166             :     PyModule_Create(&PLy_module);
     167             :     /* for Python 3 we initialized the exceptions in PyInit_plpy */
     168             : #else
     169          46 :     plpy = Py_InitModule("plpy", PLy_methods);
     170          46 :     PLy_add_exceptions(plpy);
     171             : #endif
     172             : 
     173             :     /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
     174             : 
     175             :     /*
     176             :      * initialize main module, and add plpy
     177             :      */
     178          46 :     main_mod = PyImport_AddModule("__main__");
     179          46 :     main_dict = PyModule_GetDict(main_mod);
     180          46 :     plpy_mod = PyImport_AddModule("plpy");
     181          46 :     if (plpy_mod == NULL)
     182           0 :         PLy_elog(ERROR, "could not import \"plpy\" module");
     183          46 :     PyDict_SetItemString(main_dict, "plpy", plpy_mod);
     184          46 :     if (PyErr_Occurred())
     185           0 :         PLy_elog(ERROR, "could not import \"plpy\" module");
     186          46 : }
     187             : 
     188             : static void
     189          46 : PLy_add_exceptions(PyObject *plpy)
     190             : {
     191             :     PyObject   *excmod;
     192             :     HASHCTL     hash_ctl;
     193             : 
     194             : #if PY_MAJOR_VERSION < 3
     195          46 :     excmod = Py_InitModule("spiexceptions", PLy_exc_methods);
     196             : #else
     197             :     excmod = PyModule_Create(&PLy_exc_module);
     198             : #endif
     199          46 :     if (excmod == NULL)
     200           0 :         PLy_elog(ERROR, "could not create the spiexceptions module");
     201             : 
     202             :     /*
     203             :      * PyModule_AddObject does not add a refcount to the object, for some odd
     204             :      * reason; we must do that.
     205             :      */
     206          46 :     Py_INCREF(excmod);
     207          46 :     if (PyModule_AddObject(plpy, "spiexceptions", excmod) < 0)
     208           0 :         PLy_elog(ERROR, "could not add the spiexceptions module");
     209             : 
     210          46 :     PLy_exc_error = PLy_create_exception("plpy.Error", NULL, NULL,
     211             :                                          "Error", plpy);
     212          46 :     PLy_exc_fatal = PLy_create_exception("plpy.Fatal", NULL, NULL,
     213             :                                          "Fatal", plpy);
     214          46 :     PLy_exc_spi_error = PLy_create_exception("plpy.SPIError", NULL, NULL,
     215             :                                              "SPIError", plpy);
     216             : 
     217          46 :     memset(&hash_ctl, 0, sizeof(hash_ctl));
     218          46 :     hash_ctl.keysize = sizeof(int);
     219          46 :     hash_ctl.entrysize = sizeof(PLyExceptionEntry);
     220          46 :     PLy_spi_exceptions = hash_create("PL/Python SPI exceptions", 256,
     221             :                                      &hash_ctl, HASH_ELEM | HASH_BLOBS);
     222             : 
     223          46 :     PLy_generate_spi_exceptions(excmod, PLy_exc_spi_error);
     224          46 : }
     225             : 
     226             : /*
     227             :  * Create an exception object and add it to the module
     228             :  */
     229             : static PyObject *
     230       11500 : PLy_create_exception(char *name, PyObject *base, PyObject *dict,
     231             :                      const char *modname, PyObject *mod)
     232             : {
     233             :     PyObject   *exc;
     234             : 
     235       11500 :     exc = PyErr_NewException(name, base, dict);
     236       11500 :     if (exc == NULL)
     237           0 :         PLy_elog(ERROR, NULL);
     238             : 
     239             :     /*
     240             :      * PyModule_AddObject does not add a refcount to the object, for some odd
     241             :      * reason; we must do that.
     242             :      */
     243       11500 :     Py_INCREF(exc);
     244       11500 :     PyModule_AddObject(mod, modname, exc);
     245             : 
     246             :     /*
     247             :      * The caller will also store a pointer to the exception object in some
     248             :      * permanent variable, so add another ref to account for that.  This is
     249             :      * probably excessively paranoid, but let's be sure.
     250             :      */
     251       11500 :     Py_INCREF(exc);
     252       11500 :     return exc;
     253             : }
     254             : 
     255             : /*
     256             :  * Add all the autogenerated exceptions as subclasses of SPIError
     257             :  */
     258             : static void
     259          46 : PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
     260             : {
     261             :     int         i;
     262             : 
     263       11408 :     for (i = 0; exception_map[i].name != NULL; i++)
     264             :     {
     265             :         bool        found;
     266             :         PyObject   *exc;
     267             :         PLyExceptionEntry *entry;
     268             :         PyObject   *sqlstate;
     269       11362 :         PyObject   *dict = PyDict_New();
     270             : 
     271       11362 :         if (dict == NULL)
     272           0 :             PLy_elog(ERROR, NULL);
     273             : 
     274       11362 :         sqlstate = PyString_FromString(unpack_sql_state(exception_map[i].sqlstate));
     275       11362 :         if (sqlstate == NULL)
     276           0 :             PLy_elog(ERROR, "could not generate SPI exceptions");
     277             : 
     278       11362 :         PyDict_SetItemString(dict, "sqlstate", sqlstate);
     279       11362 :         Py_DECREF(sqlstate);
     280             : 
     281       11362 :         exc = PLy_create_exception(exception_map[i].name, base, dict,
     282       11362 :                                    exception_map[i].classname, mod);
     283             : 
     284       11362 :         entry = hash_search(PLy_spi_exceptions, &exception_map[i].sqlstate,
     285             :                             HASH_ENTER, &found);
     286             :         Assert(!found);
     287       11362 :         entry->exc = exc;
     288             :     }
     289          46 : }
     290             : 
     291             : 
     292             : /*
     293             :  * the python interface to the elog function
     294             :  * don't confuse these with PLy_elog
     295             :  */
     296             : static PyObject *PLy_output(volatile int level, PyObject *self,
     297             :                             PyObject *args, PyObject *kw);
     298             : 
     299             : static PyObject *
     300           4 : PLy_debug(PyObject *self, PyObject *args, PyObject *kw)
     301             : {
     302           4 :     return PLy_output(DEBUG2, self, args, kw);
     303             : }
     304             : 
     305             : static PyObject *
     306           4 : PLy_log(PyObject *self, PyObject *args, PyObject *kw)
     307             : {
     308           4 :     return PLy_output(LOG, self, args, kw);
     309             : }
     310             : 
     311             : static PyObject *
     312         222 : PLy_info(PyObject *self, PyObject *args, PyObject *kw)
     313             : {
     314         222 :     return PLy_output(INFO, self, args, kw);
     315             : }
     316             : 
     317             : static PyObject *
     318         406 : PLy_notice(PyObject *self, PyObject *args, PyObject *kw)
     319             : {
     320         406 :     return PLy_output(NOTICE, self, args, kw);
     321             : }
     322             : 
     323             : static PyObject *
     324          10 : PLy_warning(PyObject *self, PyObject *args, PyObject *kw)
     325             : {
     326          10 :     return PLy_output(WARNING, self, args, kw);
     327             : }
     328             : 
     329             : static PyObject *
     330          22 : PLy_error(PyObject *self, PyObject *args, PyObject *kw)
     331             : {
     332          22 :     return PLy_output(ERROR, self, args, kw);
     333             : }
     334             : 
     335             : static PyObject *
     336           0 : PLy_fatal(PyObject *self, PyObject *args, PyObject *kw)
     337             : {
     338           0 :     return PLy_output(FATAL, self, args, kw);
     339             : }
     340             : 
     341             : static PyObject *
     342          12 : PLy_quote_literal(PyObject *self, PyObject *args)
     343             : {
     344             :     const char *str;
     345             :     char       *quoted;
     346             :     PyObject   *ret;
     347             : 
     348          12 :     if (!PyArg_ParseTuple(args, "s:quote_literal", &str))
     349           0 :         return NULL;
     350             : 
     351          12 :     quoted = quote_literal_cstr(str);
     352          12 :     ret = PyString_FromString(quoted);
     353          12 :     pfree(quoted);
     354             : 
     355          12 :     return ret;
     356             : }
     357             : 
     358             : static PyObject *
     359          12 : PLy_quote_nullable(PyObject *self, PyObject *args)
     360             : {
     361             :     const char *str;
     362             :     char       *quoted;
     363             :     PyObject   *ret;
     364             : 
     365          12 :     if (!PyArg_ParseTuple(args, "z:quote_nullable", &str))
     366           0 :         return NULL;
     367             : 
     368          12 :     if (str == NULL)
     369           2 :         return PyString_FromString("NULL");
     370             : 
     371          10 :     quoted = quote_literal_cstr(str);
     372          10 :     ret = PyString_FromString(quoted);
     373          10 :     pfree(quoted);
     374             : 
     375          10 :     return ret;
     376             : }
     377             : 
     378             : static PyObject *
     379           6 : PLy_quote_ident(PyObject *self, PyObject *args)
     380             : {
     381             :     const char *str;
     382             :     const char *quoted;
     383             :     PyObject   *ret;
     384             : 
     385           6 :     if (!PyArg_ParseTuple(args, "s:quote_ident", &str))
     386           0 :         return NULL;
     387             : 
     388           6 :     quoted = quote_identifier(str);
     389           6 :     ret = PyString_FromString(quoted);
     390             : 
     391           6 :     return ret;
     392             : }
     393             : 
     394             : /* enforce cast of object to string */
     395             : static char *
     396         116 : object_to_string(PyObject *obj)
     397             : {
     398         116 :     if (obj)
     399             :     {
     400         116 :         PyObject   *so = PyObject_Str(obj);
     401             : 
     402         116 :         if (so != NULL)
     403             :         {
     404             :             char       *str;
     405             : 
     406         116 :             str = pstrdup(PyString_AsString(so));
     407         116 :             Py_DECREF(so);
     408             : 
     409         116 :             return str;
     410             :         }
     411             :     }
     412             : 
     413           0 :     return NULL;
     414             : }
     415             : 
     416             : static PyObject *
     417         668 : PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
     418             : {
     419         668 :     int         sqlstate = 0;
     420         668 :     char       *volatile sqlstatestr = NULL;
     421         668 :     char       *volatile message = NULL;
     422         668 :     char       *volatile detail = NULL;
     423         668 :     char       *volatile hint = NULL;
     424         668 :     char       *volatile column_name = NULL;
     425         668 :     char       *volatile constraint_name = NULL;
     426         668 :     char       *volatile datatype_name = NULL;
     427         668 :     char       *volatile table_name = NULL;
     428         668 :     char       *volatile schema_name = NULL;
     429             :     volatile MemoryContext oldcontext;
     430             :     PyObject   *key,
     431             :                *value;
     432             :     PyObject   *volatile so;
     433         668 :     Py_ssize_t  pos = 0;
     434             : 
     435         668 :     if (PyTuple_Size(args) == 1)
     436             :     {
     437             :         /*
     438             :          * Treat single argument specially to avoid undesirable ('tuple',)
     439             :          * decoration.
     440             :          */
     441             :         PyObject   *o;
     442             : 
     443         496 :         if (!PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o))
     444           0 :             PLy_elog(ERROR, "could not unpack arguments in plpy.elog");
     445         496 :         so = PyObject_Str(o);
     446             :     }
     447             :     else
     448         172 :         so = PyObject_Str(args);
     449             : 
     450         668 :     if (so == NULL || ((message = PyString_AsString(so)) == NULL))
     451             :     {
     452           0 :         level = ERROR;
     453           0 :         message = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
     454             :     }
     455         668 :     message = pstrdup(message);
     456             : 
     457         668 :     Py_XDECREF(so);
     458             : 
     459         668 :     if (kw != NULL)
     460             :     {
     461         160 :         while (PyDict_Next(kw, &pos, &key, &value))
     462             :         {
     463         122 :             char       *keyword = PyString_AsString(key);
     464             : 
     465         122 :             if (strcmp(keyword, "message") == 0)
     466             :             {
     467             :                 /* the message should not be overwritten */
     468          18 :                 if (PyTuple_Size(args) != 0)
     469             :                 {
     470           4 :                     PLy_exception_set(PyExc_TypeError, "argument 'message' given by name and position");
     471           4 :                     return NULL;
     472             :                 }
     473             : 
     474          14 :                 if (message)
     475          14 :                     pfree(message);
     476          14 :                 message = object_to_string(value);
     477             :             }
     478         104 :             else if (strcmp(keyword, "detail") == 0)
     479          30 :                 detail = object_to_string(value);
     480          74 :             else if (strcmp(keyword, "hint") == 0)
     481          14 :                 hint = object_to_string(value);
     482          60 :             else if (strcmp(keyword, "sqlstate") == 0)
     483          14 :                 sqlstatestr = object_to_string(value);
     484          46 :             else if (strcmp(keyword, "schema_name") == 0)
     485           8 :                 schema_name = object_to_string(value);
     486          38 :             else if (strcmp(keyword, "table_name") == 0)
     487          10 :                 table_name = object_to_string(value);
     488          28 :             else if (strcmp(keyword, "column_name") == 0)
     489           8 :                 column_name = object_to_string(value);
     490          20 :             else if (strcmp(keyword, "datatype_name") == 0)
     491          10 :                 datatype_name = object_to_string(value);
     492          10 :             else if (strcmp(keyword, "constraint_name") == 0)
     493           8 :                 constraint_name = object_to_string(value);
     494             :             else
     495             :             {
     496           2 :                 PLy_exception_set(PyExc_TypeError,
     497             :                                   "'%s' is an invalid keyword argument for this function",
     498             :                                   keyword);
     499           2 :                 return NULL;
     500             :             }
     501             :         }
     502             :     }
     503             : 
     504         662 :     if (sqlstatestr != NULL)
     505             :     {
     506          14 :         if (strlen(sqlstatestr) != 5)
     507             :         {
     508           2 :             PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
     509           2 :             return NULL;
     510             :         }
     511             : 
     512          12 :         if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
     513             :         {
     514           0 :             PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
     515           0 :             return NULL;
     516             :         }
     517             : 
     518          12 :         sqlstate = MAKE_SQLSTATE(sqlstatestr[0],
     519             :                                  sqlstatestr[1],
     520             :                                  sqlstatestr[2],
     521             :                                  sqlstatestr[3],
     522             :                                  sqlstatestr[4]);
     523             :     }
     524             : 
     525         660 :     oldcontext = CurrentMemoryContext;
     526         660 :     PG_TRY();
     527             :     {
     528         660 :         if (message != NULL)
     529         660 :             pg_verifymbstr(message, strlen(message), false);
     530         660 :         if (detail != NULL)
     531          30 :             pg_verifymbstr(detail, strlen(detail), false);
     532         660 :         if (hint != NULL)
     533          14 :             pg_verifymbstr(hint, strlen(hint), false);
     534         660 :         if (schema_name != NULL)
     535           8 :             pg_verifymbstr(schema_name, strlen(schema_name), false);
     536         660 :         if (table_name != NULL)
     537          10 :             pg_verifymbstr(table_name, strlen(table_name), false);
     538         660 :         if (column_name != NULL)
     539           8 :             pg_verifymbstr(column_name, strlen(column_name), false);
     540         660 :         if (datatype_name != NULL)
     541          10 :             pg_verifymbstr(datatype_name, strlen(datatype_name), false);
     542         660 :         if (constraint_name != NULL)
     543           8 :             pg_verifymbstr(constraint_name, strlen(constraint_name), false);
     544             : 
     545         660 :         ereport(level,
     546             :                 ((sqlstate != 0) ? errcode(sqlstate) : 0,
     547             :                  (message != NULL) ? errmsg_internal("%s", message) : 0,
     548             :                  (detail != NULL) ? errdetail_internal("%s", detail) : 0,
     549             :                  (hint != NULL) ? errhint("%s", hint) : 0,
     550             :                  (column_name != NULL) ?
     551             :                  err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
     552             :                  (constraint_name != NULL) ?
     553             :                  err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
     554             :                  (datatype_name != NULL) ?
     555             :                  err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
     556             :                  (table_name != NULL) ?
     557             :                  err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
     558             :                  (schema_name != NULL) ?
     559             :                  err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
     560             :     }
     561          22 :     PG_CATCH();
     562             :     {
     563             :         ErrorData  *edata;
     564             : 
     565          22 :         MemoryContextSwitchTo(oldcontext);
     566          22 :         edata = CopyErrorData();
     567          22 :         FlushErrorState();
     568             : 
     569          22 :         PLy_exception_set_with_details(PLy_exc_error, edata);
     570          22 :         FreeErrorData(edata);
     571             : 
     572          22 :         return NULL;
     573             :     }
     574         638 :     PG_END_TRY();
     575             : 
     576             :     /*
     577             :      * return a legal object so the interpreter will continue on its merry way
     578             :      */
     579         638 :     Py_RETURN_NONE;
     580             : }
     581             : 
     582             : static PyObject *
     583          48 : PLy_commit(PyObject *self, PyObject *args)
     584             : {
     585          48 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     586             : 
     587          48 :     SPI_commit();
     588          40 :     SPI_start_transaction();
     589             : 
     590             :     /* was cleared at transaction end, reset pointer */
     591          40 :     exec_ctx->scratch_ctx = NULL;
     592             : 
     593          40 :     Py_RETURN_NONE;
     594             : }
     595             : 
     596             : static PyObject *
     597          34 : PLy_rollback(PyObject *self, PyObject *args)
     598             : {
     599          34 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     600             : 
     601          34 :     SPI_rollback();
     602          34 :     SPI_start_transaction();
     603             : 
     604             :     /* was cleared at transaction end, reset pointer */
     605          34 :     exec_ctx->scratch_ctx = NULL;
     606             : 
     607          34 :     Py_RETURN_NONE;
     608             : }

Generated by: LCOV version 1.13