1 /*
2  * the PLyPlan class
3  *
4  * src/pl/plpython/plpy_planobject.c
5  */
6 
7 #include "postgres.h"
8 
9 #include "plpython.h"
10 
11 #include "plpy_planobject.h"
12 
13 #include "plpy_elog.h"
14 #include "utils/memutils.h"
15 
16 
17 static void PLy_plan_dealloc(PyObject *arg);
18 static PyObject *PLy_plan_status(PyObject *self, PyObject *args);
19 
20 static char PLy_plan_doc[] = {
21 	"Store a PostgreSQL plan"
22 };
23 
24 static PyMethodDef PLy_plan_methods[] = {
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 	"PLyPlan",					/* tp_name */
32 	sizeof(PLyPlanObject),		/* tp_size */
33 	0,							/* tp_itemsize */
34 
35 	/*
36 	 * methods
37 	 */
38 	PLy_plan_dealloc,			/* tp_dealloc */
39 	0,							/* tp_print */
40 	0,							/* tp_getattr */
41 	0,							/* tp_setattr */
42 	0,							/* tp_compare */
43 	0,							/* tp_repr */
44 	0,							/* tp_as_number */
45 	0,							/* tp_as_sequence */
46 	0,							/* tp_as_mapping */
47 	0,							/* tp_hash */
48 	0,							/* tp_call */
49 	0,							/* tp_str */
50 	0,							/* tp_getattro */
51 	0,							/* tp_setattro */
52 	0,							/* tp_as_buffer */
53 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/* tp_flags */
54 	PLy_plan_doc,				/* tp_doc */
55 	0,							/* tp_traverse */
56 	0,							/* tp_clear */
57 	0,							/* tp_richcompare */
58 	0,							/* tp_weaklistoffset */
59 	0,							/* tp_iter */
60 	0,							/* tp_iternext */
61 	PLy_plan_methods,			/* tp_tpmethods */
62 };
63 
64 void
PLy_plan_init_type(void)65 PLy_plan_init_type(void)
66 {
67 	if (PyType_Ready(&PLy_PlanType) < 0)
68 		elog(ERROR, "could not initialize PLy_PlanType");
69 }
70 
71 PyObject *
PLy_plan_new(void)72 PLy_plan_new(void)
73 {
74 	PLyPlanObject *ob;
75 
76 	if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
77 		return NULL;
78 
79 	ob->plan = NULL;
80 	ob->nargs = 0;
81 	ob->types = NULL;
82 	ob->values = NULL;
83 	ob->args = NULL;
84 	ob->mcxt = NULL;
85 
86 	return (PyObject *) ob;
87 }
88 
89 bool
is_PLyPlanObject(PyObject * ob)90 is_PLyPlanObject(PyObject *ob)
91 {
92 	return ob->ob_type == &PLy_PlanType;
93 }
94 
95 static void
PLy_plan_dealloc(PyObject * arg)96 PLy_plan_dealloc(PyObject *arg)
97 {
98 	PLyPlanObject *ob = (PLyPlanObject *) arg;
99 
100 	if (ob->plan)
101 	{
102 		SPI_freeplan(ob->plan);
103 		ob->plan = NULL;
104 	}
105 	if (ob->mcxt)
106 	{
107 		MemoryContextDelete(ob->mcxt);
108 		ob->mcxt = NULL;
109 	}
110 	arg->ob_type->tp_free(arg);
111 }
112 
113 
114 static PyObject *
PLy_plan_status(PyObject * self,PyObject * args)115 PLy_plan_status(PyObject *self, PyObject *args)
116 {
117 	if (PyArg_ParseTuple(args, ""))
118 	{
119 		Py_INCREF(Py_True);
120 		return Py_True;
121 		/* return PyInt_FromLong(self->status); */
122 	}
123 	PLy_exception_set(PLy_exc_error, "plan.status takes no arguments");
124 	return NULL;
125 }
126