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 "plpy_util.h"
13#include "utils/memutils.h"
14
15static void PLy_plan_dealloc(PLyPlanObject *self);
16static PyObject *PLy_plan_cursor(PyObject *self, PyObject *args);
17static PyObject *PLy_plan_execute(PyObject *self, PyObject *args);
18static PyObject *PLy_plan_status(PyObject *self, PyObject *args);
19
20static char PLy_plan_doc[] = "Store a PostgreSQL plan";
21
22static 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
29static PyType_Slot PLyPlan_slots[] =
30{
31 {
32 Py_tp_dealloc, PLy_plan_dealloc
33 },
34 {
35 Py_tp_doc, (char *) PLy_plan_doc
36 },
37 {
38 Py_tp_methods, PLy_plan_methods
39 },
40 {
41 0, NULL
42 }
43};
44
45static PyType_Spec PLyPlan_spec =
46{
47 .name = "PLyPlan",
48 .basicsize = sizeof(PLyPlanObject),
49 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
50 .slots = PLyPlan_slots,
51};
52
53static PyTypeObject *PLy_PlanType;
54
55void
57{
58 PLy_PlanType = (PyTypeObject *) PyType_FromSpec(&PLyPlan_spec);
59 if (!PLy_PlanType)
60 elog(ERROR, "could not initialize PLy_PlanType");
61}
62
63PyObject *
65{
66 PLyPlanObject *ob;
67
68 if ((ob = PyObject_New(PLyPlanObject, PLy_PlanType)) == NULL)
69 return NULL;
70#if PY_VERSION_HEX < 0x03080000
71 /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
72 Py_INCREF(PLy_PlanType);
73#endif
74
75 ob->plan = NULL;
76 ob->nargs = 0;
77 ob->types = NULL;
78 ob->args = NULL;
79 ob->mcxt = NULL;
80
81 return (PyObject *) ob;
82}
83
84bool
85is_PLyPlanObject(PyObject *ob)
86{
87 return ob->ob_type == PLy_PlanType;
88}
89
90static void
92{
93#if PY_VERSION_HEX >= 0x03080000
94 PyTypeObject *tp = Py_TYPE(self);
95#endif
96
97 if (self->plan)
98 {
99 SPI_freeplan(self->plan);
100 self->plan = NULL;
101 }
102 if (self->mcxt)
103 {
105 self->mcxt = NULL;
106 }
107
108 PyObject_Free(self);
109#if PY_VERSION_HEX >= 0x03080000
110 /* This was not needed before Python 3.8 (Python issue 35810) */
111 Py_DECREF(tp);
112#endif
113}
114
115
116static PyObject *
117PLy_plan_cursor(PyObject *self, PyObject *args)
118{
119 PyObject *planargs = NULL;
120
121 if (!PyArg_ParseTuple(args, "|O", &planargs))
122 return NULL;
123
124 return PLy_cursor_plan(self, planargs);
125}
126
127
128static PyObject *
129PLy_plan_execute(PyObject *self, PyObject *args)
130{
131 PyObject *list = NULL;
132 long limit = 0;
133
134 if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
135 return NULL;
136
137 return PLy_spi_execute_plan(self, list, limit);
138}
139
140
141static PyObject *
142PLy_plan_status(PyObject *self, PyObject *args)
143{
144 if (PyArg_ParseTuple(args, ":status"))
145 {
146 Py_INCREF(Py_True);
147 return Py_True;
148 /* return PyLong_FromLong(self->status); */
149 }
150 return NULL;
151}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:485
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)
static PyObject * PLy_plan_status(PyObject *self, PyObject *args)
static PyObject * PLy_plan_cursor(PyObject *self, PyObject *args)
PyObject * PLy_plan_new(void)
static void PLy_plan_dealloc(PLyPlanObject *self)
void PLy_plan_init_type(void)
static char PLy_plan_doc[]
static PyType_Slot PLyPlan_slots[]
static PyObject * PLy_plan_execute(PyObject *self, PyObject *args)
static PyType_Spec PLyPlan_spec
bool is_PLyPlanObject(PyObject *ob)
static PyMethodDef PLy_plan_methods[]
static PyTypeObject * PLy_PlanType
struct PLyPlanObject PLyPlanObject
PyObject * PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
Definition: plpy_spi.c:171
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1026
PyObject_HEAD SPIPlanPtr plan
MemoryContext mcxt
PLyObToDatum * args