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 "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
47 {
48  if (PyType_Ready(&PLy_PlanType) < 0)
49  elog(ERROR, "could not initialize PLy_PlanType");
50 }
51 
52 PyObject *
54 {
55  PLyPlanObject *ob;
56 
57  if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
58  return NULL;
59 
60  ob->plan = NULL;
61  ob->nargs = 0;
62  ob->types = NULL;
63  ob->values = NULL;
64  ob->args = NULL;
65  ob->mcxt = NULL;
66 
67  return (PyObject *) ob;
68 }
69 
70 bool
71 is_PLyPlanObject(PyObject *ob)
72 {
73  return ob->ob_type == &PLy_PlanType;
74 }
75 
76 static void
78 {
79  PLyPlanObject *ob = (PLyPlanObject *) arg;
80 
81  if (ob->plan)
82  {
83  SPI_freeplan(ob->plan);
84  ob->plan = NULL;
85  }
86  if (ob->mcxt)
87  {
89  ob->mcxt = NULL;
90  }
91  arg->ob_type->tp_free(arg);
92 }
93 
94 
95 static PyObject *
96 PLy_plan_cursor(PyObject *self, PyObject *args)
97 {
98  PyObject *planargs = NULL;
99 
100  if (!PyArg_ParseTuple(args, "|O", &planargs))
101  return NULL;
102 
103  return PLy_cursor_plan(self, planargs);
104 }
105 
106 
107 static PyObject *
108 PLy_plan_execute(PyObject *self, PyObject *args)
109 {
110  PyObject *list = NULL;
111  long limit = 0;
112 
113  if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
114  return NULL;
115 
116  return PLy_spi_execute_plan(self, list, limit);
117 }
118 
119 
120 static PyObject *
121 PLy_plan_status(PyObject *self, PyObject *args)
122 {
123  if (PyArg_ParseTuple(args, ":status"))
124  {
125  Py_INCREF(Py_True);
126  return Py_True;
127  /* return PyInt_FromLong(self->status); */
128  }
129  return NULL;
130 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
void PLy_plan_init_type(void)
#define PyVarObject_HEAD_INIT(type, size)
Definition: plpython.h:113
static PyObject * PLy_plan_status(PyObject *self, PyObject *args)
bool is_PLyPlanObject(PyObject *ob)
#define ERROR
Definition: elog.h:43
static PyTypeObject PLy_PlanType
PLyObToDatum * args
static char PLy_plan_doc[]
PyObject_HEAD SPIPlanPtr plan
MemoryContext mcxt
PyObject * PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
Definition: plpy_spi.c:181
static PyMethodDef PLy_plan_methods[]
PyObject * PLy_plan_new(void)
static PyObject * PLy_plan_execute(PyObject *self, PyObject *args)
static PyObject * PLy_plan_cursor(PyObject *self, PyObject *args)
static void PLy_plan_dealloc(PyObject *arg)
struct PLyPlanObject PLyPlanObject
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:801
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)
#define elog(elevel,...)
Definition: elog.h:226
void * arg