4 #include "plpy_typeio.h"
10 PG_FUNCTION_INFO_V1(hstore_to_plpython);
11 Datum hstore_to_plpython(PG_FUNCTION_ARGS);
14 hstore_to_plpython(PG_FUNCTION_ARGS)
16 HStore *in = PG_GETARG_HS(0);
18 int count = HS_COUNT(in);
19 char *base = STRPTR(in);
20 HEntry *entries = ARRPTR(in);
25 for (i = 0; i < count; i++)
29 key = PyString_FromStringAndSize(HS_KEY(entries, base, i), HS_KEYLEN(entries, i));
30 if (HS_VALISNULL(entries, i))
31 PyDict_SetItem(dict, key, Py_None);
36 value = PyString_FromStringAndSize(HS_VAL(entries, base,i), HS_VALLEN(entries, i));
37 PyDict_SetItem(dict, key, value);
43 return PointerGetDatum(dict);
47 PG_FUNCTION_INFO_V1(plpython_to_hstore);
48 Datum plpython_to_hstore(PG_FUNCTION_ARGS);
51 plpython_to_hstore(PG_FUNCTION_ARGS)
54 volatile PyObject *items_v = NULL;
58 dict = (PyObject *) PG_GETARG_POINTER(0);
59 if (!PyMapping_Check(dict))
61 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
62 errmsg("not a Python mapping")));
64 pcount = PyMapping_Size(dict);
65 items_v = PyMapping_Items(dict);
72 PyObject *items = (PyObject *) items_v;
74 pairs = palloc(pcount * sizeof(*pairs));
76 for (i = 0; i < pcount; i++)
82 tuple = PyList_GetItem(items, i);
83 key = PyTuple_GetItem(tuple, 0);
84 value = PyTuple_GetItem(tuple, 1);
86 pairs[i].key = PLyObject_AsString(key);
87 pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
88 pairs[i].needfree = true;
94 pairs[i].isnull = true;
98 pairs[i].val = PLyObject_AsString(value);
99 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
100 pairs[i].isnull = false;
105 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
106 out = hstorePairs(pairs, pcount, buflen);
115 PG_RETURN_POINTER(out);