37 PyVarObject_HEAD_INIT(NULL, 0)
38 .tp_name =
"PLyCursor",
41 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
43 .tp_iter = PyObject_SelfIter,
52 elog(
ERROR,
"could not initialize PLy_CursorType");
60 PyObject *planargs = NULL;
62 if (PyArg_ParseTuple(
args,
"s", &query))
67 if (PyArg_ParseTuple(
args,
"O|O", &
plan, &planargs))
88 "PL/Python cursor context",
118 elog(
ERROR,
"SPI_cursor_open() failed: %s",
135 return (PyObject *)
cursor;
151 if (!PySequence_Check(
args) || PyUnicode_Check(
args))
153 PLy_exception_set(PyExc_TypeError,
"plpy.cursor takes a sequence as its second argument");
156 nargs = PySequence_Length(
args);
163 if (nargs !=
plan->nargs)
166 PyObject *so = PyObject_Str(
args);
172 "Expected sequence of %d argument, got %d: %s",
173 "Expected sequence of %d arguments, got %d: %s",
175 plan->nargs, nargs, sv);
183 cursor->portalname = NULL;
186 "PL/Python cursor context",
202 char *
volatile nulls;
206 nulls =
palloc(nargs *
sizeof(
char));
210 for (
j = 0;
j < nargs;
j++)
215 elem = PySequence_GetItem(
args,
j);
221 nulls[
j] = isnull ?
'n' :
' ';
233 elog(
ERROR,
"SPI_cursor_open() failed: %s",
247 for (k = 0; k < nargs; k++)
249 if (!
plan->args[k].typbyval &&
264 for (
i = 0;
i < nargs;
i++)
266 if (!
plan->args[
i].typbyval &&
275 return (PyObject *)
cursor;
302 arg->ob_type->tp_free(
arg);
327 "iterating a cursor in an aborted subtransaction");
341 PyErr_SetNone(PyExc_StopIteration);
378 if (!PyArg_ParseTuple(
args,
"i:fetch", &count))
393 "iterating a cursor in an aborted subtransaction");
413 Py_DECREF(ret->
nrows);
427 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
428 errmsg(
"query result has too many rows to fit in a Python list")));
430 Py_DECREF(ret->
rows);
449 PyList_SetItem(ret->
rows,
i, row);
465 return (PyObject *) ret;
480 "closing a cursor in an aborted subtransaction");
#define Assert(condition)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
void pfree(void *pointer)
MemoryContext TopMemoryContext
MemoryContext CurrentMemoryContext
char * MemoryContextStrdup(MemoryContext context, const char *string)
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
PyObject * PLy_cursor(PyObject *self, PyObject *args)
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)
static PyMethodDef PLy_cursor_methods[]
static PyObject * PLy_cursor_query(const char *query)
static PyObject * PLy_cursor_fetch(PyObject *self, PyObject *args)
static PyObject * PLy_cursor_iternext(PyObject *self)
static PyTypeObject PLy_CursorType
static PyObject * PLy_cursor_close(PyObject *self, PyObject *unused)
static void PLy_cursor_dealloc(PyObject *arg)
void PLy_cursor_init_type(void)
static const char PLy_cursor_doc[]
struct PLyCursorObject PLyCursorObject
void PLy_exception_set(PyObject *exc, const char *fmt,...)
void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
PLyExecutionContext * PLy_current_execution_context(void)
PyObject * PLy_result_new(void)
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
PyObject * PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
char * PLyUnicode_AsString(PyObject *unicode)
void PinPortal(Portal portal)
void UnpinPortal(Portal portal)
Portal GetPortalByName(const char *name)
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
ResourceOwner CurrentResourceOwner
int SPI_freeplan(SPIPlanPtr plan)
SPITupleTable * SPI_tuptable
const char * SPI_result_code_string(int code)
void SPI_cursor_fetch(Portal portal, bool forward, long count)
void SPI_freetuptable(SPITupleTable *tuptable)
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
void SPI_cursor_close(Portal portal)
PyObject_HEAD PyObject * nrows