PostgreSQL Source Code git master
hstore_plpython.c
Go to the documentation of this file.
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
9 .name = "hstore_plpython",
10 .version = PG_VERSION
11);
12
13/* Linkage to functions in plpython module */
14typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
16typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
18
19/* Linkage to functions in hstore module */
20typedef HStore *(*hstoreUpgrade_t) (Datum orig);
22typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
24typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
26typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
28typedef size_t (*hstoreCheckValLen_t) (size_t len);
30
31
32/*
33 * Module initialize function: fetch function pointers for cross-module calls.
34 */
35void
37{
38 /* Asserts verify that typedefs above match original declarations */
41 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
42 true, NULL);
45 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
46 true, NULL);
49 load_external_function("$libdir/hstore", "hstoreUpgrade",
50 true, NULL);
53 load_external_function("$libdir/hstore", "hstoreUniquePairs",
54 true, NULL);
57 load_external_function("$libdir/hstore", "hstorePairs",
58 true, NULL);
61 load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
62 true, NULL);
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
81
84{
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)
95 (errcode(ERRCODE_OUT_OF_MEMORY),
96 errmsg("out of memory")));
97
98 for (i = 0; i < count; i++)
99 {
100 PyObject *key;
101
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
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
123
124Datum
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))
141 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
142 errmsg("not a Python mapping")));
143
144 pcount = PyMapping_Size(dict);
145 items = PyMapping_Items(dict);
146
147 PG_TRY();
148 {
149 int32 buflen;
150 Py_ssize_t i;
151 Pairs *pairs;
152
153 pairs = palloc(pcount * sizeof(*pairs));
154
155 for (i = 0; i < pcount; i++)
156 {
157 PyObject *tuple;
158 PyObject *key;
159 PyObject *value;
160
161 tuple = PyList_GetItem(items, i);
162 key = PyTuple_GetItem(tuple, 0);
163 value = PyTuple_GetItem(tuple, 1);
164
165 pairs[i].key = PLyObject_AsString(key);
166 pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
167 pairs[i].needfree = true;
168
169 if (value == Py_None)
170 {
171 pairs[i].val = NULL;
172 pairs[i].vallen = 0;
173 pairs[i].isnull = true;
174 }
175 else
176 {
177 pairs[i].val = PLyObject_AsString(value);
178 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
179 pairs[i].isnull = false;
180 }
181 }
182
183 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
184 out = hstorePairs(pairs, pcount, buflen);
185 }
186 PG_FINALLY();
187 {
188 Py_DECREF(items);
189 }
190 PG_END_TRY();
191
193}
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:952
int32_t int32
Definition: c.h:498
#define ARRPTR(x)
Definition: cube.c:28
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:95
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define PG_FINALLY(...)
Definition: elog.h:388
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define STRPTR(x)
Definition: hstore.h:76
#define hstoreUpgrade
Datum plpython_to_hstore(PG_FUNCTION_ARGS)
#define PLyUnicode_FromStringAndSize
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
static hstoreUpgrade_t hstoreUpgrade_p
void _PG_init(void)
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
HStore *(* hstoreUpgrade_t)(Datum orig)
#define hstoreCheckKeyLen
#define hstoreUniquePairs
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
static hstorePairs_t hstorePairs_p
static PLyObject_AsString_t PLyObject_AsString_p
PG_MODULE_MAGIC_EXT(.name="hstore_plpython",.version=PG_VERSION)
#define hstorePairs
Datum hstore_to_plpython(PG_FUNCTION_ARGS)
char *(* PLyObject_AsString_t)(PyObject *plrv)
static hstoreCheckValLen_t hstoreCheckValLen_p
static hstoreUniquePairs_t hstoreUniquePairs_p
size_t(* hstoreCheckValLen_t)(size_t len)
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
#define hstoreCheckValLen
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
PG_FUNCTION_INFO_V1(hstore_to_plpython)
size_t(* hstoreCheckKeyLen_t)(size_t len)
#define PLyObject_AsString
long val
Definition: informix.c:689
static struct @165 value
int a
Definition: isn.c:73
int i
Definition: isn.c:77
void * palloc(Size size)
Definition: mcxt.c:1945
const void size_t len
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
Definition: hstore.h:19
Definition: hstore.h:45
Definition: hstore.h:162
char * val
Definition: hstore.h:164
bool isnull
Definition: hstore.h:167
size_t keylen
Definition: hstore.h:165
char * key
Definition: hstore.h:163
bool needfree
Definition: hstore.h:168
size_t vallen
Definition: hstore.h:166
static ItemArray items
Definition: test_tidstore.c:48
const char * name