PostgreSQL Source Code git master
plpy_cursorobject.h File Reference
#include "plpy_typeio.h"
Include dependency graph for plpy_cursorobject.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PLyCursorObject
 

Typedefs

typedef struct PLyCursorObject PLyCursorObject
 

Functions

void PLy_cursor_init_type (void)
 
PyObject * PLy_cursor (PyObject *self, PyObject *args)
 
PyObject * PLy_cursor_plan (PyObject *ob, PyObject *args)
 

Typedef Documentation

◆ PLyCursorObject

Function Documentation

◆ PLy_cursor()

PyObject * PLy_cursor ( PyObject *  self,
PyObject *  args 
)

Definition at line 56 of file plpy_cursorobject.c.

57{
58 char *query;
59 PyObject *plan;
60 PyObject *planargs = NULL;
61
62 if (PyArg_ParseTuple(args, "s", &query))
63 return PLy_cursor_query(query);
64
65 PyErr_Clear();
66
67 if (PyArg_ParseTuple(args, "O|O", &plan, &planargs))
68 return PLy_cursor_plan(plan, planargs);
69
70 PLy_exception_set(PLy_exc_error, "plpy.cursor expected a query or a plan");
71 return NULL;
72}
#define plan(x)
Definition: pg_regress.c:161
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)
static PyObject * PLy_cursor_query(const char *query)
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:472

References generate_unaccent_rules::args, plan, PLy_cursor_plan(), PLy_cursor_query(), PLy_exc_error, and PLy_exception_set().

◆ PLy_cursor_init_type()

void PLy_cursor_init_type ( void  )

Definition at line 49 of file plpy_cursorobject.c.

50{
51 if (PyType_Ready(&PLy_CursorType) < 0)
52 elog(ERROR, "could not initialize PLy_CursorType");
53}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
static PyTypeObject PLy_CursorType

References elog, ERROR, and PLy_CursorType.

Referenced by PLy_init_plpy().

◆ PLy_cursor_plan()

PyObject * PLy_cursor_plan ( PyObject *  ob,
PyObject *  args 
)

Definition at line 139 of file plpy_cursorobject.c.

140{
142 volatile int nargs;
145 volatile MemoryContext oldcontext;
146 volatile ResourceOwner oldowner;
147
148 if (args)
149 {
150 if (!PySequence_Check(args) || PyUnicode_Check(args))
151 {
152 PLy_exception_set(PyExc_TypeError, "plpy.cursor takes a sequence as its second argument");
153 return NULL;
154 }
155 nargs = PySequence_Length(args);
156 }
157 else
158 nargs = 0;
159
160 plan = (PLyPlanObject *) ob;
161
162 if (nargs != plan->nargs)
163 {
164 char *sv;
165 PyObject *so = PyObject_Str(args);
166
167 if (!so)
168 PLy_elog(ERROR, "could not execute plan");
169 sv = PLyUnicode_AsString(so);
170 PLy_exception_set_plural(PyExc_TypeError,
171 "Expected sequence of %d argument, got %d: %s",
172 "Expected sequence of %d arguments, got %d: %s",
173 plan->nargs,
174 plan->nargs, nargs, sv);
175 Py_DECREF(so);
176
177 return NULL;
178 }
179
180 if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
181 return NULL;
182 cursor->portalname = NULL;
183 cursor->closed = false;
185 "PL/Python cursor context",
187
188 /* Initialize for converting result tuples to Python */
189 PLy_input_setup_func(&cursor->result, cursor->mcxt,
190 RECORDOID, -1,
191 exec_ctx->curr_proc);
192
193 oldcontext = CurrentMemoryContext;
194 oldowner = CurrentResourceOwner;
195
196 PLy_spi_subtransaction_begin(oldcontext, oldowner);
197
198 PG_TRY();
199 {
200 Portal portal;
201 MemoryContext tmpcontext;
202 Datum *volatile values;
203 char *volatile nulls;
204 volatile int j;
205
206 /*
207 * Converted arguments and associated cruft will be in this context,
208 * which is local to our subtransaction.
209 */
211 "PL/Python temporary context",
213 MemoryContextSwitchTo(tmpcontext);
214
215 if (nargs > 0)
216 {
217 values = (Datum *) palloc(nargs * sizeof(Datum));
218 nulls = (char *) palloc(nargs * sizeof(char));
219 }
220 else
221 {
222 values = NULL;
223 nulls = NULL;
224 }
225
226 for (j = 0; j < nargs; j++)
227 {
228 PLyObToDatum *arg = &plan->args[j];
229 PyObject *elem;
230
231 elem = PySequence_GetItem(args, j);
232 PG_TRY(2);
233 {
234 bool isnull;
235
236 values[j] = PLy_output_convert(arg, elem, &isnull);
237 nulls[j] = isnull ? 'n' : ' ';
238 }
239 PG_FINALLY(2);
240 {
241 Py_DECREF(elem);
242 }
243 PG_END_TRY(2);
244 }
245
246 MemoryContextSwitchTo(oldcontext);
247
248 portal = SPI_cursor_open(NULL, plan->plan, values, nulls,
249 exec_ctx->curr_proc->fn_readonly);
250 if (portal == NULL)
251 elog(ERROR, "SPI_cursor_open() failed: %s",
253
254 cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
255
256 PinPortal(portal);
257
258 MemoryContextDelete(tmpcontext);
259 PLy_spi_subtransaction_commit(oldcontext, oldowner);
260 }
261 PG_CATCH();
262 {
263 Py_DECREF(cursor);
264 /* Subtransaction abort will remove the tmpcontext */
265 PLy_spi_subtransaction_abort(oldcontext, oldowner);
266 return NULL;
267 }
268 PG_END_TRY();
269
270 Assert(cursor->portalname != NULL);
271 return (PyObject *) cursor;
272}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define Assert(condition)
Definition: c.h:815
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
#define PG_FINALLY(...)
Definition: elog.h:388
int j
Definition: isn.c:73
#define PLy_elog
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1683
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContext CurTransactionContext
Definition: mcxt.c:155
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: plpy_elog.c:486
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:365
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:573
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:582
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:565
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:418
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:120
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:82
void PinPortal(Portal portal)
Definition: portalmem.c:371
uintptr_t Datum
Definition: postgres.h:69
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
const char * SPI_result_code_string(int code)
Definition: spi.c:1972
int SPI_result
Definition: spi.c:46
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1445
PLyProcedure * curr_proc
Definition: plpy_main.h:20
const char * name
Definition: portal.h:118
Definition: type.h:138

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, arg, generate_unaccent_rules::args, Assert, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, CurTransactionContext, elog, ERROR, PLyProcedure::fn_readonly, j, MemoryContextDelete(), MemoryContextStrdup(), MemoryContextSwitchTo(), PortalData::name, palloc(), PG_CATCH, PG_END_TRY, PG_FINALLY, PG_TRY, PinPortal(), plan, PLy_current_execution_context(), PLy_CursorType, PLy_elog, PLy_exception_set(), PLy_exception_set_plural(), PLy_input_setup_func(), PLy_output_convert(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), PLyUnicode_AsString(), SPI_cursor_open(), SPI_result, SPI_result_code_string(), TopMemoryContext, and values.

Referenced by PLy_cursor(), and PLy_plan_cursor().