39 PyVarObject_HEAD_INIT(NULL, 0)
40 .tp_name =
"PLyCursor",
43 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
45 .tp_iter = PyObject_SelfIter,
54 elog(
ERROR,
"could not initialize PLy_CursorType");
62 PyObject *planargs = NULL;
64 if (PyArg_ParseTuple(
args,
"s", &query))
69 if (PyArg_ParseTuple(
args,
"O|O", &
plan, &planargs))
90 "PL/Python cursor context",
120 elog(
ERROR,
"SPI_cursor_open() failed: %s",
137 return (PyObject *)
cursor;
153 if (!PySequence_Check(
args) || PyUnicode_Check(
args))
155 PLy_exception_set(PyExc_TypeError,
"plpy.cursor takes a sequence as its second argument");
158 nargs = PySequence_Length(
args);
165 if (nargs !=
plan->nargs)
168 PyObject *so = PyObject_Str(
args);
174 "Expected sequence of %d argument, got %d: %s",
175 "Expected sequence of %d arguments, got %d: %s",
177 plan->nargs, nargs, sv);
185 cursor->portalname = NULL;
188 "PL/Python cursor context",
204 char *
volatile nulls;
208 nulls =
palloc(nargs *
sizeof(
char));
212 for (
j = 0;
j < nargs;
j++)
217 elem = PySequence_GetItem(
args,
j);
223 nulls[
j] = isnull ?
'n' :
' ';
235 elog(
ERROR,
"SPI_cursor_open() failed: %s",
249 for (k = 0; k < nargs; k++)
251 if (!
plan->args[k].typbyval &&
266 for (
i = 0;
i < nargs;
i++)
268 if (!
plan->args[
i].typbyval &&
277 return (PyObject *)
cursor;
304 arg->ob_type->tp_free(
arg);
329 "iterating a cursor in an aborted subtransaction");
343 PyErr_SetNone(PyExc_StopIteration);
380 if (!PyArg_ParseTuple(
args,
"i:fetch", &count))
395 "iterating a cursor in an aborted subtransaction");
415 Py_DECREF(ret->
nrows);
429 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
430 errmsg(
"query result has too many rows to fit in a Python list")));
432 Py_DECREF(ret->
rows);
451 PyList_SetItem(ret->
rows,
i, row);
467 return (PyObject *) ret;
482 "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