27 uint64 rows,
int status);
39 PyObject *
list = NULL;
40 PyObject *
volatile optr = NULL;
47 if (!PyArg_ParseTuple(
args,
"s|O:prepare", &query, &
list))
50 if (
list && (!PySequence_Check(
list)))
53 "second argument of plpy.prepare must be a sequence");
61 "PL/Python plan context",
65 nargs =
list ? PySequence_Length(
list) : 0;
83 for (
i = 0;
i < nargs;
i++)
89 optr = PySequence_GetItem(
list,
i);
90 if (PyUnicode_Check(optr))
95 (
errmsg(
"plpy.prepare: type name at ordinal position %d is not a string",
i)));
115 plan->types[
i] = typeId;
123 if (
plan->plan == NULL)
144 return (PyObject *)
plan;
155 PyObject *
list = NULL;
158 if (PyArg_ParseTuple(
args,
"s|l", &query, &limit))
163 if (PyArg_ParseTuple(
args,
"O|Ol", &
plan, &
list, &limit) &&
184 if (!PySequence_Check(
list) || PyUnicode_Check(
list))
186 PLy_exception_set(PyExc_TypeError,
"plpy.execute takes a sequence as its second argument");
189 nargs = PySequence_Length(
list);
196 if (nargs !=
plan->nargs)
199 PyObject *so = PyObject_Str(
list);
205 "Expected sequence of %d argument, got %d: %s",
206 "Expected sequence of %d arguments, got %d: %s",
208 plan->nargs, nargs, sv);
222 char *
volatile nulls;
226 nulls =
palloc(nargs *
sizeof(
char));
230 for (
j = 0;
j < nargs;
j++)
235 elem = PySequence_GetItem(
list,
j);
241 nulls[
j] = isnull ?
'n' :
' ';
266 for (k = 0; k < nargs; k++)
268 if (!
plan->args[k].typbyval &&
281 for (
i = 0;
i < nargs;
i++)
283 if (!
plan->args[
i].typbyval &&
294 "SPI_execute_plan failed: %s",
308 PyObject *ret = NULL;
336 "SPI_execute failed: %s",
357 Py_DECREF(result->
status);
358 result->
status = PyLong_FromLong(status);
360 if (status > 0 && tuptable == NULL)
362 Py_DECREF(result->
nrows);
363 result->
nrows = PyLong_FromUnsignedLongLong(rows);
365 else if (status > 0 && tuptable != NULL)
370 Py_DECREF(result->
nrows);
371 result->
nrows = PyLong_FromUnsignedLongLong(rows);
374 "PL/Python temp context",
395 if (rows > (uint64) PY_SSIZE_T_MAX)
397 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
398 errmsg(
"query result has too many rows to fit in a Python list")));
400 Py_DECREF(result->
rows);
401 result->
rows = PyList_New(rows);
407 for (
i = 0;
i < rows;
i++)
414 PyList_SetItem(result->
rows,
i, row);
451 return (PyObject *) result;
628 PyObject *
args = NULL;
629 PyObject *spierror = NULL;
630 PyObject *spidata = NULL;
637 spierror = PyObject_CallObject(excclass,
args);
648 if (PyObject_SetAttrString(spierror,
"spidata", spidata) == -1)
651 PyErr_SetObject(excclass, spierror);
660 Py_XDECREF(spierror);
662 elog(
ERROR,
"could not convert SPI error to Python exception");
#define Assert(condition)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
void FreeErrorData(ErrorData *edata)
void FlushErrorState(void)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
ErrorData * CopyErrorData(void)
#define ereport(elevel,...)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
void pfree(void *pointer)
MemoryContext TopMemoryContext
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
bool parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, Node *escontext)
PyObject * PLy_exc_spi_error
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_plan_new(void)
bool is_PLyPlanObject(PyObject *ob)
HTAB * PLy_spi_exceptions
PyObject * PLy_result_new(void)
PyObject * PLy_spi_prepare(PyObject *self, PyObject *args)
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
PyObject * PLy_spi_execute(PyObject *self, PyObject *args)
PyObject * PLy_rollback(PyObject *self, PyObject *args)
static PyObject * PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
PyObject * PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
static PyObject * PLy_spi_execute_query(char *query, long limit)
PyObject * PLy_commit(PyObject *self, PyObject *args)
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
static void PLy_spi_exception_set(PyObject *excclass, ErrorData *edata)
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_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
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)
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
MemoryContextSwitchTo(old_ctx)
ResourceOwner CurrentResourceOwner
SPITupleTable * SPI_tuptable
const char * SPI_result_code_string(int code)
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
void SPI_freetuptable(SPITupleTable *tuptable)
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
int SPI_keepplan(SPIPlanPtr plan)
int SPI_execute(const char *src, bool read_only, long tcount)
MemoryContext scratch_ctx
PyObject_HEAD PyObject * nrows
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
void BeginInternalSubTransaction(const char *name)
void RollbackAndReleaseCurrentSubTransaction(void)
void ReleaseCurrentSubTransaction(void)