1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#include "postgres.h"
#include "fmgr.h"
#include "plpython.h"
#include "plpy_typeio.h"
#include "hstore.h"
PG_MODULE_MAGIC;
extern void _PG_init(void);
/* Linkage to functions in plpython module */
typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
static PLyObject_AsString_t PLyObject_AsString_p;
/* Linkage to functions in hstore module */
typedef HStore *(*hstoreUpgrade_t) (Datum orig);
typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
typedef size_t (*hstoreCheckValLen_t) (size_t len);
static hstoreUpgrade_t hstoreUpgrade_p;
static hstoreUniquePairs_t hstoreUniquePairs_p;
static hstorePairs_t hstorePairs_p;
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p;
static hstoreCheckValLen_t hstoreCheckValLen_p;
/*
* Module initialize function: fetch function pointers for cross-module calls.
*/
void
_PG_init(void)
{
/* These asserts verify that typedefs above match original declarations */
AssertVariableIsOfType(&PLyObject_AsString, PLyObject_AsString_t);
AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
AssertVariableIsOfType(&hstorePairs, hstorePairs_t);
AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
PLyObject_AsString_p = (PLyObject_AsString_t)
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
true, NULL);
hstoreUpgrade_p = (hstoreUpgrade_t)
load_external_function("$libdir/hstore", "hstoreUpgrade",
true, NULL);
hstoreUniquePairs_p = (hstoreUniquePairs_t)
load_external_function("$libdir/hstore", "hstoreUniquePairs",
true, NULL);
hstorePairs_p = (hstorePairs_t)
load_external_function("$libdir/hstore", "hstorePairs",
true, NULL);
hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
true, NULL);
hstoreCheckValLen_p = (hstoreCheckValLen_t)
load_external_function("$libdir/hstore", "hstoreCheckValLen",
true, NULL);
}
/* These defines must be after the module init function */
#define PLyObject_AsString PLyObject_AsString_p
#define hstoreUpgrade hstoreUpgrade_p
#define hstoreUniquePairs hstoreUniquePairs_p
#define hstorePairs hstorePairs_p
#define hstoreCheckKeyLen hstoreCheckKeyLen_p
#define hstoreCheckValLen hstoreCheckValLen_p
PG_FUNCTION_INFO_V1(hstore_to_plpython);
Datum
hstore_to_plpython(PG_FUNCTION_ARGS)
{
HStore *in = PG_GETARG_HS(0);
int i;
int count = HS_COUNT(in);
char *base = STRPTR(in);
HEntry *entries = ARRPTR(in);
PyObject *dict;
dict = PyDict_New();
for (i = 0; i < count; i++)
{
PyObject *key;
key = PyString_FromStringAndSize(HSTORE_KEY(entries, base, i),
HSTORE_KEYLEN(entries, i));
if (HSTORE_VALISNULL(entries, i))
PyDict_SetItem(dict, key, Py_None);
else
{
PyObject *value;
value = PyString_FromStringAndSize(HSTORE_VAL(entries, base, i),
HSTORE_VALLEN(entries, i));
PyDict_SetItem(dict, key, value);
Py_XDECREF(value);
}
Py_XDECREF(key);
}
return PointerGetDatum(dict);
}
PG_FUNCTION_INFO_V1(plpython_to_hstore);
Datum
plpython_to_hstore(PG_FUNCTION_ARGS)
{
PyObject *dict;
volatile PyObject *items_v = NULL;
int32 pcount;
HStore *out;
dict = (PyObject *) PG_GETARG_POINTER(0);
if (!PyMapping_Check(dict))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("not a Python mapping")));
pcount = PyMapping_Size(dict);
items_v = PyMapping_Items(dict);
PG_TRY();
{
int32 buflen;
int32 i;
Pairs *pairs;
PyObject *items = (PyObject *) items_v;
pairs = palloc(pcount * sizeof(*pairs));
for (i = 0; i < pcount; i++)
{
PyObject *tuple;
PyObject *key;
PyObject *value;
tuple = PyList_GetItem(items, i);
key = PyTuple_GetItem(tuple, 0);
value = PyTuple_GetItem(tuple, 1);
pairs[i].key = PLyObject_AsString(key);
pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
pairs[i].needfree = true;
if (value == Py_None)
{
pairs[i].val = NULL;
pairs[i].vallen = 0;
pairs[i].isnull = true;
}
else
{
pairs[i].val = PLyObject_AsString(value);
pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
pairs[i].isnull = false;
}
}
Py_DECREF(items_v);
pcount = hstoreUniquePairs(pairs, pcount, &buflen);
out = hstorePairs(pairs, pcount, buflen);
}
PG_CATCH();
{
Py_DECREF(items_v);
PG_RE_THROW();
}
PG_END_TRY();
PG_RETURN_POINTER(out);
}
|