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_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
41 {
42  if (PyType_Ready(&PLy_PlanType) < 0)
43  elog(ERROR, "could not initialize PLy_PlanType");
44 }
45 
46 PyObject *
48 {
49  PLyPlanObject *ob;
50 
51  if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
52  return NULL;
53 
54  ob->plan = NULL;
55  ob->nargs = 0;
56  ob->types = NULL;
57  ob->values = NULL;
58  ob->args = NULL;
59  ob->mcxt = NULL;
60 
61  return (PyObject *) ob;
62 }
63 
64 bool
65 is_PLyPlanObject(PyObject *ob)
66 {
67  return ob->ob_type == &PLy_PlanType;
68 }
69 
70 static void
72 {
74 
75  if (ob->plan)
76  {
77  SPI_freeplan(ob->plan);
78  ob->plan = NULL;
79  }
80  if (ob->mcxt)
81  {
83  ob->mcxt = NULL;
84  }
85  arg->ob_type->tp_free(arg);
86 }
87 
88 
89 static PyObject *
90 PLy_plan_cursor(PyObject *self, PyObject *args)
91 {
92  PyObject *planargs = NULL;
93 
94  if (!PyArg_ParseTuple(args, "|O", &planargs))
95  return NULL;
96 
97  return PLy_cursor_plan(self, planargs);
98 }
99 
100 
101 static PyObject *
102 PLy_plan_execute(PyObject *self, PyObject *args)
103 {
104  PyObject *list = NULL;
105  long limit = 0;
106 
107  if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
108  return NULL;
109 
110  return PLy_spi_execute_plan(self, list, limit);
111 }
112 
113 
114 static PyObject *
115 PLy_plan_status(PyObject *self, PyObject *args)
116 {
117  if (PyArg_ParseTuple(args, ":status"))
118  {
119  Py_INCREF(Py_True);
120  return Py_True;
121  /* return PyLong_FromLong(self->status); */
122  }
123  return NULL;
124 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
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:172
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1025
PyObject_HEAD SPIPlanPtr plan
MemoryContext mcxt
PLyObToDatum * args