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

Generated by: LCOV version 1.14