6#include "utils/fmgrprotos.h"
13typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
29typedef PyObject *(*PLyUnicode_FromStringAndSize_t)
30 (
const char *s, Py_ssize_t
size);
55#define PLyObject_AsString (PLyObject_AsString_p)
56#define PLyUnicode_FromStringAndSize (PLyUnicode_FromStringAndSize_p)
58#define PLy_elog (PLy_elog_impl_p)
83 jbvElem->
val.string.len = strlen(jbvElem->
val.string.val);
94 switch (jsonbValue->
type)
117 if (jsonbValue->
val.boolean)
123 elog(
ERROR,
"unexpected jsonb value type: %d", jsonbValue->
type);
147 if (v.
val.array.rawScalar)
154 elog(
ERROR,
"unexpected jsonb token: %d", r);
160 PyObject *
volatile elem = NULL;
162 result = PyList_New(0);
175 PyList_Append(result, elem);
192 PyObject *
volatile result_v = PyDict_New();
193 PyObject *
volatile key = NULL;
194 PyObject *
volatile val = NULL;
209 Py_XDECREF(result_v);
215 elog(
ERROR,
"unexpected jsonb token: %d", r);
222 Py_XDECREF(result_v);
227 PyDict_SetItem(result_v,
key,
val);
237 Py_XDECREF(result_v);
249 elog(
ERROR,
"unexpected jsonb token: %d", r);
265 PyObject *
volatile items;
268 pcount = PyMapping_Size(obj);
269 items = PyMapping_Items(obj);
277 for (
i = 0;
i < pcount;
i++)
280 PyObject *item = PyList_GetItem(
items,
i);
281 PyObject *
key = PyTuple_GetItem(item, 0);
282 PyObject *
value = PyTuple_GetItem(item, 1);
288 jbvKey.
val.string.len = 0;
289 jbvKey.
val.string.val =
"";
323 PyObject *
volatile value = NULL;
325 pcount = PySequence_Size(obj);
331 for (
i = 0;
i < pcount;
i++)
333 value = PySequence_GetItem(obj,
i);
375 (
errcode(ERRCODE_DATATYPE_MISMATCH),
376 errmsg(
"could not convert value \"%s\" to jsonb",
str)));
388 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
389 errmsg(
"cannot convert NaN to jsonb")));
392 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
393 errmsg(
"cannot convert infinity to jsonb")));
396 jbvNum->
val.numeric = num;
411 if (!PyUnicode_Check(obj))
413 if (PySequence_Check(obj))
415 else if (PyMapping_Check(obj))
423 else if (PyUnicode_Check(obj))
430 else if (PyBool_Check(obj))
433 out->
val.boolean = (obj == Py_True);
435 else if (PyNumber_Check(obj))
439 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
440 errmsg(
"Python type \"%s\" cannot be transformed to jsonb",
444 return (*jsonb_state ?
486 PyObject *decimal_module = PyImport_ImportModule(
"cdecimal");
491 decimal_module = PyImport_ImportModule(
"decimal");
499 PLy_elog(
ERROR,
"transformation from jsonb to Python failed");
Datum numeric_out(PG_FUNCTION_ARGS)
Datum numeric_in(PG_FUNCTION_ARGS)
bool numeric_is_nan(Numeric num)
bool numeric_is_inf(Numeric num)
#define AssertVariableIsOfType(varname, typename)
#define Assert(condition)
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_GETARG_POINTER(n)
#define DirectFunctionCall1(func, arg1)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define PG_RETURN_POINTER(x)
#define PG_GETARG_JSONB_P(x)
static JsonbValue * PLyObject_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state, bool is_elem)
static JsonbValue * PLyMapping_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state)
#define PLyUnicode_FromStringAndSize
static PLy_elog_impl_t PLy_elog_impl_p
static JsonbValue * PLyNumber_ToJsonbValue(PyObject *obj, JsonbValue *jbvNum)
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
Datum plpython_to_jsonb(PG_FUNCTION_ARGS)
void(* PLy_elog_impl_t)(int elevel, const char *fmt,...)
static PyObject * PLyObject_FromJsonbValue(JsonbValue *jsonbValue)
PG_FUNCTION_INFO_V1(plpython_to_jsonb)
static PLyObject_AsString_t PLyObject_AsString_p
char *(* PLyObject_AsString_t)(PyObject *plrv)
static PyObject * decimal_constructor
Datum jsonb_to_plpython(PG_FUNCTION_ARGS)
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
static void PLyUnicode_ToJsonbValue(PyObject *obj, JsonbValue *jbvElem)
static PyObject * PLyObject_FromJsonbContainer(JsonbContainer *jsonb)
#define PLyObject_AsString
static JsonbValue * PLySequence_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state)
static PyObject * PLyUnicode_FromJsonbValue(JsonbValue *jbv)
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
static void const char * fmt
void pfree(void *pointer)
static Numeric DatumGetNumeric(Datum X)
static Datum NumericGetDatum(Numeric X)
void PLy_elog_impl(int elevel, const char *fmt,...)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static char * DatumGetCString(Datum X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
static pg_noinline void Size size