Fix header inclusion order in c.h.
[pgsql.git] / src / pl / plpython / plpy_planobject.c
blobbbef889329e8854750040548294bc6f18003d7ab
1 /*
2 * the PLyPlan class
4 * src/pl/plpython/plpy_planobject.c
5 */
7 #include "postgres.h"
9 #include "plpy_cursorobject.h"
10 #include "plpy_planobject.h"
11 #include "plpy_spi.h"
12 #include "plpython.h"
13 #include "utils/memutils.h"
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);
20 static char PLy_plan_doc[] = "Store a PostgreSQL plan";
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}
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,
39 void
40 PLy_plan_init_type(void)
42 if (PyType_Ready(&PLy_PlanType) < 0)
43 elog(ERROR, "could not initialize PLy_PlanType");
46 PyObject *
47 PLy_plan_new(void)
49 PLyPlanObject *ob;
51 if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
52 return NULL;
54 ob->plan = NULL;
55 ob->nargs = 0;
56 ob->types = NULL;
57 ob->values = NULL;
58 ob->args = NULL;
59 ob->mcxt = NULL;
61 return (PyObject *) ob;
64 bool
65 is_PLyPlanObject(PyObject *ob)
67 return ob->ob_type == &PLy_PlanType;
70 static void
71 PLy_plan_dealloc(PyObject *arg)
73 PLyPlanObject *ob = (PLyPlanObject *) arg;
75 if (ob->plan)
77 SPI_freeplan(ob->plan);
78 ob->plan = NULL;
80 if (ob->mcxt)
82 MemoryContextDelete(ob->mcxt);
83 ob->mcxt = NULL;
85 arg->ob_type->tp_free(arg);
89 static PyObject *
90 PLy_plan_cursor(PyObject *self, PyObject *args)
92 PyObject *planargs = NULL;
94 if (!PyArg_ParseTuple(args, "|O", &planargs))
95 return NULL;
97 return PLy_cursor_plan(self, planargs);
101 static PyObject *
102 PLy_plan_execute(PyObject *self, PyObject *args)
104 PyObject *list = NULL;
105 long limit = 0;
107 if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
108 return NULL;
110 return PLy_spi_execute_plan(self, list, limit);
114 static PyObject *
115 PLy_plan_status(PyObject *self, PyObject *args)
117 if (PyArg_ParseTuple(args, ":status"))
119 Py_INCREF(Py_True);
120 return Py_True;
121 /* return PyLong_FromLong(self->status); */
123 return NULL;