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

Generated by: LCOV version 1.14