LCOV - code coverage report
Current view: top level - src/pl/plpython - plpy_planobject.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 37 42 88.1 %
Date: 2019-11-21 15:06:52 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * the PLyPlan class
       3             :  *
       4             :  * src/pl/plpython/plpy_planobject.c
       5             :  */
       6             : 
       7             : #include "postgres.h"
       8             : 
       9             : #include "plpython.h"
      10             : 
      11             : #include "plpy_planobject.h"
      12             : 
      13             : #include "plpy_cursorobject.h"
      14             : #include "plpy_elog.h"
      15             : #include "plpy_spi.h"
      16             : #include "utils/memutils.h"
      17             : 
      18             : 
      19             : static void PLy_plan_dealloc(PyObject *arg);
      20             : static PyObject *PLy_plan_cursor(PyObject *self, PyObject *args);
      21             : static PyObject *PLy_plan_execute(PyObject *self, PyObject *args);
      22             : static PyObject *PLy_plan_status(PyObject *self, PyObject *args);
      23             : 
      24             : static char PLy_plan_doc[] = {
      25             :     "Store a PostgreSQL plan"
      26             : };
      27             : 
      28             : static PyMethodDef PLy_plan_methods[] = {
      29             :     {"cursor", PLy_plan_cursor, METH_VARARGS, NULL},
      30             :     {"execute", PLy_plan_execute, METH_VARARGS, NULL},
      31             :     {"status", PLy_plan_status, METH_VARARGS, NULL},
      32             :     {NULL, NULL, 0, NULL}
      33             : };
      34             : 
      35             : static PyTypeObject PLy_PlanType = {
      36             :     PyVarObject_HEAD_INIT(NULL, 0)
      37             :     .tp_name = "PLyPlan",
      38             :     .tp_basicsize = sizeof(PLyPlanObject),
      39             :     .tp_dealloc = PLy_plan_dealloc,
      40             :     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
      41             :     .tp_doc = PLy_plan_doc,
      42             :     .tp_methods = PLy_plan_methods,
      43             : };
      44             : 
      45             : void
      46          46 : PLy_plan_init_type(void)
      47             : {
      48          46 :     if (PyType_Ready(&PLy_PlanType) < 0)
      49           0 :         elog(ERROR, "could not initialize PLy_PlanType");
      50          46 : }
      51             : 
      52             : PyObject *
      53          50 : PLy_plan_new(void)
      54             : {
      55             :     PLyPlanObject *ob;
      56             : 
      57          50 :     if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
      58           0 :         return NULL;
      59             : 
      60          50 :     ob->plan = NULL;
      61          50 :     ob->nargs = 0;
      62          50 :     ob->types = NULL;
      63          50 :     ob->values = NULL;
      64          50 :     ob->args = NULL;
      65          50 :     ob->mcxt = NULL;
      66             : 
      67          50 :     return (PyObject *) ob;
      68             : }
      69             : 
      70             : bool
      71          42 : is_PLyPlanObject(PyObject *ob)
      72             : {
      73          42 :     return ob->ob_type == &PLy_PlanType;
      74             : }
      75             : 
      76             : static void
      77          42 : PLy_plan_dealloc(PyObject *arg)
      78             : {
      79          42 :     PLyPlanObject *ob = (PLyPlanObject *) arg;
      80             : 
      81          42 :     if (ob->plan)
      82             :     {
      83          36 :         SPI_freeplan(ob->plan);
      84          36 :         ob->plan = NULL;
      85             :     }
      86          42 :     if (ob->mcxt)
      87             :     {
      88          42 :         MemoryContextDelete(ob->mcxt);
      89          42 :         ob->mcxt = NULL;
      90             :     }
      91          42 :     arg->ob_type->tp_free(arg);
      92          42 : }
      93             : 
      94             : 
      95             : static PyObject *
      96           2 : PLy_plan_cursor(PyObject *self, PyObject *args)
      97             : {
      98           2 :     PyObject   *planargs = NULL;
      99             : 
     100           2 :     if (!PyArg_ParseTuple(args, "|O", &planargs))
     101           0 :         return NULL;
     102             : 
     103           2 :     return PLy_cursor_plan(self, planargs);
     104             : }
     105             : 
     106             : 
     107             : static PyObject *
     108           2 : PLy_plan_execute(PyObject *self, PyObject *args)
     109             : {
     110           2 :     PyObject   *list = NULL;
     111           2 :     long        limit = 0;
     112             : 
     113           2 :     if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
     114           0 :         return NULL;
     115             : 
     116           2 :     return PLy_spi_execute_plan(self, list, limit);
     117             : }
     118             : 
     119             : 
     120             : static PyObject *
     121           4 : PLy_plan_status(PyObject *self, PyObject *args)
     122             : {
     123           4 :     if (PyArg_ParseTuple(args, ":status"))
     124             :     {
     125           4 :         Py_INCREF(Py_True);
     126           4 :         return Py_True;
     127             :         /* return PyInt_FromLong(self->status); */
     128             :     }
     129           0 :     return NULL;
     130             : }

Generated by: LCOV version 1.13