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 87 of file plpy_cursorobject.c.

References PLy_cursor_plan(), PLy_cursor_query(), PLy_exc_error, and PLy_exception_set().

88 {
89  char *query;
90  PyObject *plan;
91  PyObject *planargs = NULL;
92 
93  if (PyArg_ParseTuple(args, "s", &query))
94  return PLy_cursor_query(query);
95 
96  PyErr_Clear();
97 
98  if (PyArg_ParseTuple(args, "O|O", &plan, &planargs))
99  return PLy_cursor_plan(plan, planargs);
100 
101  PLy_exception_set(PLy_exc_error, "plpy.cursor expected a query or a plan");
102  return NULL;
103 }
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
static PyObject * PLy_cursor_query(const char *query)
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)

◆ PLy_cursor_init_type()

void PLy_cursor_init_type ( void  )

Definition at line 80 of file plpy_cursorobject.c.

References elog, ERROR, and PLy_CursorType.

Referenced by PLy_init_plpy().

81 {
82  if (PyType_Ready(&PLy_CursorType) < 0)
83  elog(ERROR, "could not initialize PLy_CursorType");
84 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219
static PyTypeObject PLy_CursorType

◆ PLy_cursor_plan()

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

Definition at line 168 of file plpy_cursorobject.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), arg, PLyPlanObject::args, Assert, PLyCursorObject::closed, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, DatumGetPointer, elog, ERROR, PLyProcedure::fn_readonly, i, PLyCursorObject::mcxt, MemoryContextStrdup(), PortalData::name, PLyPlanObject::nargs, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLyPlanObject::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(), PointerGetDatum, PLyCursorObject::portalname, RECORDOID, PLyCursorObject::result, SPI_cursor_open(), SPI_result, SPI_result_code_string(), TopMemoryContext, PLyObToDatum::typbyval, and PLyPlanObject::values.

Referenced by PLy_cursor(), and PLy_plan_cursor().

169 {
171  volatile int nargs;
172  int i;
173  PLyPlanObject *plan;
175  volatile MemoryContext oldcontext;
176  volatile ResourceOwner oldowner;
177 
178  if (args)
179  {
180  if (!PySequence_Check(args) || PyString_Check(args) || PyUnicode_Check(args))
181  {
182  PLy_exception_set(PyExc_TypeError, "plpy.cursor takes a sequence as its second argument");
183  return NULL;
184  }
185  nargs = PySequence_Length(args);
186  }
187  else
188  nargs = 0;
189 
190  plan = (PLyPlanObject *) ob;
191 
192  if (nargs != plan->nargs)
193  {
194  char *sv;
195  PyObject *so = PyObject_Str(args);
196 
197  if (!so)
198  PLy_elog(ERROR, "could not execute plan");
199  sv = PyString_AsString(so);
200  PLy_exception_set_plural(PyExc_TypeError,
201  "Expected sequence of %d argument, got %d: %s",
202  "Expected sequence of %d arguments, got %d: %s",
203  plan->nargs,
204  plan->nargs, nargs, sv);
205  Py_DECREF(so);
206 
207  return NULL;
208  }
209 
210  if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
211  return NULL;
212  cursor->portalname = NULL;
213  cursor->closed = false;
215  "PL/Python cursor context",
217 
218  /* Initialize for converting result tuples to Python */
219  PLy_input_setup_func(&cursor->result, cursor->mcxt,
220  RECORDOID, -1,
221  exec_ctx->curr_proc);
222 
223  oldcontext = CurrentMemoryContext;
224  oldowner = CurrentResourceOwner;
225 
226  PLy_spi_subtransaction_begin(oldcontext, oldowner);
227 
228  PG_TRY();
229  {
230  Portal portal;
231  char *volatile nulls;
232  volatile int j;
233 
234  if (nargs > 0)
235  nulls = palloc(nargs * sizeof(char));
236  else
237  nulls = NULL;
238 
239  for (j = 0; j < nargs; j++)
240  {
241  PLyObToDatum *arg = &plan->args[j];
242  PyObject *elem;
243 
244  elem = PySequence_GetItem(args, j);
245  PG_TRY();
246  {
247  bool isnull;
248 
249  plan->values[j] = PLy_output_convert(arg, elem, &isnull);
250  nulls[j] = isnull ? 'n' : ' ';
251  }
252  PG_CATCH();
253  {
254  Py_DECREF(elem);
255  PG_RE_THROW();
256  }
257  PG_END_TRY();
258  Py_DECREF(elem);
259  }
260 
261  portal = SPI_cursor_open(NULL, plan->plan, plan->values, nulls,
262  exec_ctx->curr_proc->fn_readonly);
263  if (portal == NULL)
264  elog(ERROR, "SPI_cursor_open() failed: %s",
266 
267  cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
268 
269  PLy_spi_subtransaction_commit(oldcontext, oldowner);
270  }
271  PG_CATCH();
272  {
273  int k;
274 
275  /* cleanup plan->values array */
276  for (k = 0; k < nargs; k++)
277  {
278  if (!plan->args[k].typbyval &&
279  (plan->values[k] != PointerGetDatum(NULL)))
280  {
281  pfree(DatumGetPointer(plan->values[k]));
282  plan->values[k] = PointerGetDatum(NULL);
283  }
284  }
285 
286  Py_DECREF(cursor);
287 
288  PLy_spi_subtransaction_abort(oldcontext, oldowner);
289  return NULL;
290  }
291  PG_END_TRY();
292 
293  for (i = 0; i < nargs; i++)
294  {
295  if (!plan->args[i].typbyval &&
296  (plan->values[i] != PointerGetDatum(NULL)))
297  {
298  pfree(DatumGetPointer(plan->values[i]));
299  plan->values[i] = PointerGetDatum(NULL);
300  }
301  }
302 
303  Assert(cursor->portalname != NULL);
304  return (PyObject *) cursor;
305 }
void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: plpy_elog.c:514
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:123
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:506
PyObject_HEAD char * portalname
#define PointerGetDatum(X)
Definition: postgres.h:562
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void PLy_elog(int elevel, const char *fmt,...)
Definition: plpy_elog.c:47
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1037
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:403
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
PLyDatumToOb result
int SPI_result
Definition: spi.c:42
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:489
void pfree(void *pointer)
Definition: mcxt.c:949
const char * name
Definition: portal.h:117
#define ERROR
Definition: elog.h:43
MemoryContext mcxt
const char * SPI_result_code_string(int code)
Definition: spi.c:1521
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
PLyObToDatum * args
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:421
Definition: type.h:124
PyObject_HEAD SPIPlanPtr plan
#define RECORDOID
Definition: pg_type.h:680
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext TopMemoryContext
Definition: mcxt.c:43
PLyProcedure * curr_proc
Definition: plpy_main.h:20
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:670
#define PG_RE_THROW()
Definition: elog.h:314
#define DatumGetPointer(X)
Definition: postgres.h:555
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:497
void * palloc(Size size)
Definition: mcxt.c:848
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1063
int i
void * arg
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static PyTypeObject PLy_CursorType
#define PG_END_TRY()
Definition: elog.h:300