PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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/* Static asserts verify that typedefs above match original declarations */
39
40
41/*
42 * Module initialize function: fetch function pointers for cross-module calls.
43 */
44void
46{
48 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
49 true, NULL);
51 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
52 true, NULL);
54 load_external_function("$libdir/hstore", "hstoreUpgrade",
55 true, NULL);
57 load_external_function("$libdir/hstore", "hstoreUniquePairs",
58 true, NULL);
60 load_external_function("$libdir/hstore", "hstorePairs",
61 true, NULL);
63 load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
64 true, NULL);
66 load_external_function("$libdir/hstore", "hstoreCheckValLen",
67 true, NULL);
68}
69
70
71/* These defines must be after the module init function */
72#define PLyObject_AsString PLyObject_AsString_p
73#define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
74#define hstoreUpgrade hstoreUpgrade_p
75#define hstoreUniquePairs hstoreUniquePairs_p
76#define hstorePairs hstorePairs_p
77#define hstoreCheckKeyLen hstoreCheckKeyLen_p
78#define hstoreCheckValLen hstoreCheckValLen_p
79
80
82
85{
87 int i;
88 int count = HS_COUNT(in);
89 char *base = STRPTR(in);
90 HEntry *entries = ARRPTR(in);
92
93 dict = PyDict_New();
94 if (!dict)
97 errmsg("out of memory")));
98
99 for (i = 0; i < count; i++)
100 {
101 PyObject *key;
102
103 key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i),
104 HSTORE_KEYLEN(entries, i));
105 if (HSTORE_VALISNULL(entries, i))
107 else
108 {
110
112 HSTORE_VALLEN(entries, i));
115 }
116 Py_XDECREF(key);
117 }
118
119 return PointerGetDatum(dict);
120}
121
122
124
125Datum
127{
128 PyObject *dict;
129 PyObject *volatile items;
131 HStore *volatile out;
132
134
135 /*
136 * As of Python 3, PyMapping_Check() is unreliable unless one first checks
137 * that the object isn't a sequence. (Cleaner solutions exist, but not
138 * before Python 3.10, which we're not prepared to require yet.)
139 */
143 errmsg("not a Python mapping")));
144
147
148 PG_TRY();
149 {
150 int32 buflen;
152 Pairs *pairs;
153
154 pairs = palloc(pcount * sizeof(*pairs));
155
156 for (i = 0; i < pcount; i++)
157 {
158 PyObject *tuple;
159 PyObject *key;
161
162 tuple = PyList_GetItem(items, i);
163 key = PyTuple_GetItem(tuple, 0);
164 value = PyTuple_GetItem(tuple, 1);
165
166 pairs[i].key = PLyObject_AsString(key);
167 pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
168 pairs[i].needfree = true;
169
170 if (value == Py_None)
171 {
172 pairs[i].val = NULL;
173 pairs[i].vallen = 0;
174 pairs[i].isnull = true;
175 }
176 else
177 {
178 pairs[i].val = PLyObject_AsString(value);
179 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
180 pairs[i].isnull = false;
181 }
182 }
183
184 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
185 out = hstorePairs(pairs, pcount, buflen);
186 }
187 PG_FINALLY();
188 {
190 }
191 PG_END_TRY();
192
194}
int32_t int32
Definition c.h:542
#define StaticAssertVariableIsOfType(varname, typename)
Definition c.h:974
#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:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_FINALLY(...)
Definition elog.h:389
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#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
#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)
size_t(* hstoreCheckKeyLen_t)(size_t len)
#define PLyObject_AsString
long val
Definition informix.c:689
static struct @172 value
int a
Definition isn.c:73
int i
Definition isn.c:77
void * palloc(Size size)
Definition mcxt.c:1387
const void size_t len
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
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
const char * name