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_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[] = {
22  "Store a PostgreSQL plan"
23 };
24 
25 static PyMethodDef PLy_plan_methods[] = {
26  {"cursor", PLy_plan_cursor, METH_VARARGS, NULL},
27  {"execute", PLy_plan_execute, METH_VARARGS, NULL},
28  {"status", PLy_plan_status, METH_VARARGS, NULL},
29  {NULL, NULL, 0, NULL}
30 };
31 
32 static PyTypeObject PLy_PlanType = {
33  PyVarObject_HEAD_INIT(NULL, 0)
34  .tp_name = "PLyPlan",
35  .tp_basicsize = sizeof(PLyPlanObject),
36  .tp_dealloc = PLy_plan_dealloc,
37  .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
38  .tp_doc = PLy_plan_doc,
39  .tp_methods = PLy_plan_methods,
40 };
41 
42 void
44 {
45  if (PyType_Ready(&PLy_PlanType) < 0)
46  elog(ERROR, "could not initialize PLy_PlanType");
47 }
48 
49 PyObject *
51 {
52  PLyPlanObject *ob;
53 
54  if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
55  return NULL;
56 
57  ob->plan = NULL;
58  ob->nargs = 0;
59  ob->types = NULL;
60  ob->values = NULL;
61  ob->args = NULL;
62  ob->mcxt = NULL;
63 
64  return (PyObject *) ob;
65 }
66 
67 bool
68 is_PLyPlanObject(PyObject *ob)
69 {
70  return ob->ob_type == &PLy_PlanType;
71 }
72 
73 static void
75 {
76  PLyPlanObject *ob = (PLyPlanObject *) arg;
77 
78  if (ob->plan)
79  {
80  SPI_freeplan(ob->plan);
81  ob->plan = NULL;
82  }
83  if (ob->mcxt)
84  {
86  ob->mcxt = NULL;
87  }
88  arg->ob_type->tp_free(arg);
89 }
90 
91 
92 static PyObject *
93 PLy_plan_cursor(PyObject *self, PyObject *args)
94 {
95  PyObject *planargs = NULL;
96 
97  if (!PyArg_ParseTuple(args, "|O", &planargs))
98  return NULL;
99 
100  return PLy_cursor_plan(self, planargs);
101 }
102 
103 
104 static PyObject *
105 PLy_plan_execute(PyObject *self, PyObject *args)
106 {
107  PyObject *list = NULL;
108  long limit = 0;
109 
110  if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
111  return NULL;
112 
113  return PLy_spi_execute_plan(self, list, limit);
114 }
115 
116 
117 static PyObject *
118 PLy_plan_status(PyObject *self, PyObject *args)
119 {
120  if (PyArg_ParseTuple(args, ":status"))
121  {
122  Py_INCREF(Py_True);
123  return Py_True;
124  /* return PyInt_FromLong(self->status); */
125  }
126  return NULL;
127 }
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:177
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:228
void * arg