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-11-21 08:14:44 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->values = NULL;
      58          52 :     ob->args = NULL;
      59          52 :     ob->mcxt = NULL;
      60             : 
      61          52 :     return (PyObject *) ob;
      62             : }
      63             : 
      64             : bool
      65          42 : is_PLyPlanObject(PyObject *ob)
      66             : {
      67          42 :     return ob->ob_type == &PLy_PlanType;
      68             : }
      69             : 
      70             : static void
      71          44 : PLy_plan_dealloc(PyObject *arg)
      72             : {
      73          44 :     PLyPlanObject *ob = (PLyPlanObject *) arg;
      74             : 
      75          44 :     if (ob->plan)
      76             :     {
      77          38 :         SPI_freeplan(ob->plan);
      78          38 :         ob->plan = NULL;
      79             :     }
      80          44 :     if (ob->mcxt)
      81             :     {
      82          44 :         MemoryContextDelete(ob->mcxt);
      83          44 :         ob->mcxt = NULL;
      84             :     }
      85          44 :     arg->ob_type->tp_free(arg);
      86          44 : }
      87             : 
      88             : 
      89             : static PyObject *
      90           2 : PLy_plan_cursor(PyObject *self, PyObject *args)
      91             : {
      92           2 :     PyObject   *planargs = NULL;
      93             : 
      94           2 :     if (!PyArg_ParseTuple(args, "|O", &planargs))
      95           0 :         return NULL;
      96             : 
      97           2 :     return PLy_cursor_plan(self, planargs);
      98             : }
      99             : 
     100             : 
     101             : static PyObject *
     102           2 : PLy_plan_execute(PyObject *self, PyObject *args)
     103             : {
     104           2 :     PyObject   *list = NULL;
     105           2 :     long        limit = 0;
     106             : 
     107           2 :     if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
     108           0 :         return NULL;
     109             : 
     110           2 :     return PLy_spi_execute_plan(self, list, limit);
     111             : }
     112             : 
     113             : 
     114             : static PyObject *
     115           4 : PLy_plan_status(PyObject *self, PyObject *args)
     116             : {
     117           4 :     if (PyArg_ParseTuple(args, ":status"))
     118             :     {
     119           4 :         Py_INCREF(Py_True);
     120           4 :         return Py_True;
     121             :         /* return PyLong_FromLong(self->status); */
     122             :     }
     123           0 :     return NULL;
     124             : }

Generated by: LCOV version 1.14