PostgreSQL Source Code  git master
plpy_planobject.c
Go to the documentation of this file.
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
42 {
43  if (PyType_Ready(&PLy_PlanType) < 0)
44  elog(ERROR, "could not initialize PLy_PlanType");
45 }
46 
47 PyObject *
49 {
50  PLyPlanObject *ob;
51 
52  if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
53  return NULL;
54 
55  ob->plan = NULL;
56  ob->nargs = 0;
57  ob->types = NULL;
58  ob->values = NULL;
59  ob->args = NULL;
60  ob->mcxt = NULL;
61 
62  return (PyObject *) ob;
63 }
64 
65 bool
66 is_PLyPlanObject(PyObject *ob)
67 {
68  return ob->ob_type == &PLy_PlanType;
69 }
70 
71 static void
73 {
75 
76  if (ob->plan)
77  {
78  SPI_freeplan(ob->plan);
79  ob->plan = NULL;
80  }
81  if (ob->mcxt)
82  {
84  ob->mcxt = NULL;
85  }
86  arg->ob_type->tp_free(arg);
87 }
88 
89 
90 static PyObject *
91 PLy_plan_cursor(PyObject *self, PyObject *args)
92 {
93  PyObject *planargs = NULL;
94 
95  if (!PyArg_ParseTuple(args, "|O", &planargs))
96  return NULL;
97 
98  return PLy_cursor_plan(self, planargs);
99 }
100 
101 
102 static PyObject *
103 PLy_plan_execute(PyObject *self, PyObject *args)
104 {
105  PyObject *list = NULL;
106  long limit = 0;
107 
108  if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
109  return NULL;
110 
111  return PLy_spi_execute_plan(self, list, limit);
112 }
113 
114 
115 static PyObject *
116 PLy_plan_status(PyObject *self, PyObject *args)
117 {
118  if (PyArg_ParseTuple(args, ":status"))
119  {
120  Py_INCREF(Py_True);
121  return Py_True;
122  /* return PyLong_FromLong(self->status); */
123  }
124  return NULL;
125 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
void * arg
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)
static PyTypeObject PLy_PlanType
static PyObject * PLy_plan_status(PyObject *self, PyObject *args)
static PyObject * PLy_plan_cursor(PyObject *self, PyObject *args)
static void PLy_plan_dealloc(PyObject *arg)
PyObject * PLy_plan_new(void)
void PLy_plan_init_type(void)
static char PLy_plan_doc[]
static PyObject * PLy_plan_execute(PyObject *self, PyObject *args)
bool is_PLyPlanObject(PyObject *ob)
static PyMethodDef PLy_plan_methods[]
struct PLyPlanObject PLyPlanObject
PyObject * PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
Definition: plpy_spi.c:175
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1022
PyObject_HEAD SPIPlanPtr plan
MemoryContext mcxt
PLyObToDatum * args