| 1 |
#include "postgres.h" |
| 2 |
|
| 3 |
#include "fmgr.h" |
| 4 |
#include "hstore/hstore.h" |
| 5 |
#include "plpy_typeio.h" |
| 6 |
#include "plpy_util.h" |
| 7 |
|
| 8 |
PG_MODULE_MAGIC_EXT( |
| 9 |
.name = "hstore_plpython", |
| 10 |
.version = PG_VERSION |
| 11 |
); |
| 12 |
|
| 13 |
/* Linkage to functions in plpython module */ |
| 14 |
typedef char *(*PLyObject_AsString_t) (PyObject *plrv); |
| 15 |
static PLyObject_AsString_t PLyObject_AsString_p; |
| 16 |
typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size); |
| 17 |
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p; |
| 18 |
|
| 19 |
/* Linkage to functions in hstore module */ |
| 20 |
typedef HStore *(*hstoreUpgrade_t) (Datum orig); |
| 21 |
static hstoreUpgrade_t hstoreUpgrade_p; |
| 22 |
typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen); |
| 23 |
static hstoreUniquePairs_t hstoreUniquePairs_p; |
| 24 |
typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen); |
| 25 |
static hstorePairs_t hstorePairs_p; |
| 26 |
typedef size_t (*hstoreCheckKeyLen_t) (size_t len); |
| 27 |
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p; |
| 28 |
typedef size_t (*hstoreCheckValLen_t) (size_t len); |
| 29 |
static hstoreCheckValLen_t hstoreCheckValLen_p; |
| 30 |
|
| 31 |
|
| 32 |
/* |
| 33 |
* Module initialize function: fetch function pointers for cross-module calls. |
| 34 |
*/ |
| 35 |
void |
| 36 |
_PG_init(void) |
| 37 |
{ |
| 38 |
/* Asserts verify that typedefs above match original declarations */ |
| 39 |
AssertVariableIsOfType(&PLyObject_AsString, PLyObject_AsString_t); |
| 40 |
PLyObject_AsString_p = (PLyObject_AsString_t) |
| 41 |
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString", |
| 42 |
true, NULL); |
| 43 |
AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t); |
| 44 |
PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t) |
| 45 |
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize", |
| 46 |
true, NULL); |
| 47 |
AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t); |
| 48 |
hstoreUpgrade_p = (hstoreUpgrade_t) |
| 49 |
load_external_function("$libdir/hstore", "hstoreUpgrade", |
| 50 |
true, NULL); |
| 51 |
AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t); |
| 52 |
hstoreUniquePairs_p = (hstoreUniquePairs_t) |
| 53 |
load_external_function("$libdir/hstore", "hstoreUniquePairs", |
| 54 |
true, NULL); |
| 55 |
AssertVariableIsOfType(&hstorePairs, hstorePairs_t); |
| 56 |
hstorePairs_p = (hstorePairs_t) |
| 57 |
load_external_function("$libdir/hstore", "hstorePairs", |
| 58 |
true, NULL); |
| 59 |
AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t); |
| 60 |
hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t) |
| 61 |
load_external_function("$libdir/hstore", "hstoreCheckKeyLen", |
| 62 |
true, NULL); |
| 63 |
AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t); |
| 64 |
hstoreCheckValLen_p = (hstoreCheckValLen_t) |
| 65 |
load_external_function("$libdir/hstore", "hstoreCheckValLen", |
| 66 |
true, NULL); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/* These defines must be after the module init function */ |
| 71 |
#define PLyObject_AsString PLyObject_AsString_p |
| 72 |
#define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p |
| 73 |
#define hstoreUpgrade hstoreUpgrade_p |
| 74 |
#define hstoreUniquePairs hstoreUniquePairs_p |
| 75 |
#define hstorePairs hstorePairs_p |
| 76 |
#define hstoreCheckKeyLen hstoreCheckKeyLen_p |
| 77 |
#define hstoreCheckValLen hstoreCheckValLen_p |
| 78 |
|
| 79 |
|
| 80 |
PG_FUNCTION_INFO_V1(hstore_to_plpython); |
| 81 |
|
| 82 |
Datum |
| 83 |
hstore_to_plpython(PG_FUNCTION_ARGS) |
| 84 |
{ |
| 85 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 86 |
int i; |
| 87 |
int count = HS_COUNT(in); |
| 88 |
char *base = STRPTR(in); |
| 89 |
HEntry *entries = ARRPTR(in); |
| 90 |
PyObject *dict; |
| 91 |
|
| 92 |
dict = PyDict_New(); |
| 93 |
if (!dict) |
| 94 |
ereport(ERROR, |
| 95 |
(errcode(ERRCODE_OUT_OF_MEMORY), |
| 96 |
errmsg("out of memory"))); |
| 97 |
|
| 98 |
for (i = 0; i < count; i++) |
| 99 |
{ |
| 100 |
PyObject *key; |
| 101 |
|
| 102 |
key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i), |
| 103 |
HSTORE_KEYLEN(entries, i)); |
| 104 |
if (HSTORE_VALISNULL(entries, i)) |
| 105 |
PyDict_SetItem(dict, key, Py_None); |
| 106 |
else |
| 107 |
{ |
| 108 |
PyObject *value; |
| 109 |
|
| 110 |
value = PLyUnicode_FromStringAndSize(HSTORE_VAL(entries, base, i), |
| 111 |
HSTORE_VALLEN(entries, i)); |
| 112 |
PyDict_SetItem(dict, key, value); |
| 113 |
Py_XDECREF(value); |
| 114 |
} |
| 115 |
Py_XDECREF(key); |
| 116 |
} |
| 117 |
|
| 118 |
return PointerGetDatum(dict); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
PG_FUNCTION_INFO_V1(plpython_to_hstore); |
| 123 |
|
| 124 |
Datum |
| 125 |
plpython_to_hstore(PG_FUNCTION_ARGS) |
| 126 |
{ |
| 127 |
PyObject *dict; |
| 128 |
PyObject *volatile items; |
| 129 |
Py_ssize_t pcount; |
| 130 |
HStore *volatile out; |
| 131 |
|
| 132 |
dict = (PyObject *) PG_GETARG_POINTER(0); |
| 133 |
|
| 134 |
/* |
| 135 |
* As of Python 3, PyMapping_Check() is unreliable unless one first checks |
| 136 |
* that the object isn't a sequence. (Cleaner solutions exist, but not |
| 137 |
* before Python 3.10, which we're not prepared to require yet.) |
| 138 |
*/ |
| 139 |
if (PySequence_Check(dict) || !PyMapping_Check(dict)) |
| 140 |
ereport(ERROR, |
| 141 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 142 |
errmsg("not a Python mapping"))); |
| 143 |
|
| 144 |
pcount = PyMapping_Size(dict); |
| 145 |
if (pcount < 0) |
| 146 |
ereport(ERROR, |
| 147 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 148 |
errmsg("could not get size of Python mapping"))); |
| 149 |
|
| 150 |
items = PyMapping_Items(dict); |
| 151 |
if (items == NULL) |
| 152 |
ereport(ERROR, |
| 153 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 154 |
errmsg("could not get items from Python mapping"))); |
| 155 |
|
| 156 |
PG_TRY(); |
| 157 |
{ |
| 158 |
int32 buflen; |
| 159 |
Py_ssize_t i; |
| 160 |
Pairs *pairs; |
| 161 |
|
| 162 |
pairs = palloc_array(Pairs, pcount); |
| 163 |
|
| 164 |
for (i = 0; i < pcount; i++) |
| 165 |
{ |
| 166 |
PyObject *tuple; |
| 167 |
PyObject *key; |
| 168 |
PyObject *value; |
| 169 |
|
| 170 |
tuple = PyList_GetItem(items, i); |
| 171 |
|
| 172 |
/* The mapping's items() must yield key/value pairs */ |
| 173 |
if (tuple == NULL || !PyTuple_Check(tuple) || PyTuple_Size(tuple) < 2) |
| 174 |
ereport(ERROR, |
| 175 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 176 |
errmsg("items() of a Python mapping must return key/value pairs"))); |
| 177 |
|
| 178 |
key = PyTuple_GetItem(tuple, 0); |
| 179 |
value = PyTuple_GetItem(tuple, 1); |
| 180 |
|
| 181 |
pairs[i].key = PLyObject_AsString(key); |
| 182 |
pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key)); |
| 183 |
pairs[i].needfree = true; |
| 184 |
|
| 185 |
if (value == Py_None) |
| 186 |
{ |
| 187 |
pairs[i].val = NULL; |
| 188 |
pairs[i].vallen = 0; |
| 189 |
pairs[i].isnull = true; |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
pairs[i].val = PLyObject_AsString(value); |
| 194 |
pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val)); |
| 195 |
pairs[i].isnull = false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
pcount = hstoreUniquePairs(pairs, pcount, &buflen); |
| 200 |
out = hstorePairs(pairs, pcount, buflen); |
| 201 |
} |
| 202 |
PG_FINALLY(); |
| 203 |
{ |
| 204 |
Py_DECREF(items); |
| 205 |
} |
| 206 |
PG_END_TRY(); |
| 207 |
|
| 208 |
PG_RETURN_POINTER(out); |
| 209 |
} |