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
PLy_plan_init_type(void)41 PLy_plan_init_type(void)
42 {
43 if (PyType_Ready(&PLy_PlanType) < 0)
44 elog(ERROR, "could not initialize PLy_PlanType");
45 }
46
47 PyObject *
PLy_plan_new(void)48 PLy_plan_new(void)
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
is_PLyPlanObject(PyObject * ob)66 is_PLyPlanObject(PyObject *ob)
67 {
68 return ob->ob_type == &PLy_PlanType;
69 }
70
71 static void
PLy_plan_dealloc(PyObject * arg)72 PLy_plan_dealloc(PyObject *arg)
73 {
74 PLyPlanObject *ob = (PLyPlanObject *) arg;
75
76 if (ob->plan)
77 {
78 SPI_freeplan(ob->plan);
79 ob->plan = NULL;
80 }
81 if (ob->mcxt)
82 {
83 MemoryContextDelete(ob->mcxt);
84 ob->mcxt = NULL;
85 }
86 arg->ob_type->tp_free(arg);
87 }
88
89
90 static PyObject *
PLy_plan_cursor(PyObject * self,PyObject * args)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 *
PLy_plan_execute(PyObject * self,PyObject * args)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 *
PLy_plan_status(PyObject * self,PyObject * args)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 PyInt_FromLong(self->status); */
123 }
124 return NULL;
125 }
126