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 "plpython.h"
7 
9 
10 /* Linkage to functions in plpython module */
11 typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
13 typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
15 
16 /* Linkage to functions in hstore module */
17 typedef HStore *(*hstoreUpgrade_t) (Datum orig);
19 typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
21 typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
23 typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
25 typedef size_t (*hstoreCheckValLen_t) (size_t len);
27 
28 
29 /*
30  * Module initialize function: fetch function pointers for cross-module calls.
31  */
32 void
33 _PG_init(void)
34 {
35  /* Asserts verify that typedefs above match original declarations */
38  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
39  true, NULL);
42  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
43  true, NULL);
46  load_external_function("$libdir/hstore", "hstoreUpgrade",
47  true, NULL);
50  load_external_function("$libdir/hstore", "hstoreUniquePairs",
51  true, NULL);
54  load_external_function("$libdir/hstore", "hstorePairs",
55  true, NULL);
58  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
59  true, NULL);
62  load_external_function("$libdir/hstore", "hstoreCheckValLen",
63  true, NULL);
64 }
65 
66 
67 /* These defines must be after the module init function */
68 #define PLyObject_AsString PLyObject_AsString_p
69 #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
70 #define hstoreUpgrade hstoreUpgrade_p
71 #define hstoreUniquePairs hstoreUniquePairs_p
72 #define hstorePairs hstorePairs_p
73 #define hstoreCheckKeyLen hstoreCheckKeyLen_p
74 #define hstoreCheckValLen hstoreCheckValLen_p
75 
76 
78 
79 Datum
81 {
82  HStore *in = PG_GETARG_HSTORE_P(0);
83  int i;
84  int count = HS_COUNT(in);
85  char *base = STRPTR(in);
86  HEntry *entries = ARRPTR(in);
87  PyObject *dict;
88 
89  dict = PyDict_New();
90  if (!dict)
91  ereport(ERROR,
92  (errcode(ERRCODE_OUT_OF_MEMORY),
93  errmsg("out of memory")));
94 
95  for (i = 0; i < count; i++)
96  {
97  PyObject *key;
98 
99  key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i),
100  HSTORE_KEYLEN(entries, i));
101  if (HSTORE_VALISNULL(entries, i))
102  PyDict_SetItem(dict, key, Py_None);
103  else
104  {
105  PyObject *value;
106 
108  HSTORE_VALLEN(entries, i));
109  PyDict_SetItem(dict, key, value);
110  Py_XDECREF(value);
111  }
112  Py_XDECREF(key);
113  }
114 
115  return PointerGetDatum(dict);
116 }
117 
118 
120 
121 Datum
123 {
124  PyObject *dict;
125  PyObject *volatile items;
126  Py_ssize_t pcount;
127  HStore *volatile out;
128 
129  dict = (PyObject *) PG_GETARG_POINTER(0);
130 
131  /*
132  * As of Python 3, PyMapping_Check() is unreliable unless one first checks
133  * that the object isn't a sequence. (Cleaner solutions exist, but not
134  * before Python 3.10, which we're not prepared to require yet.)
135  */
136  if (PySequence_Check(dict) || !PyMapping_Check(dict))
137  ereport(ERROR,
138  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
139  errmsg("not a Python mapping")));
140 
141  pcount = PyMapping_Size(dict);
142  items = PyMapping_Items(dict);
143 
144  PG_TRY();
145  {
146  int32 buflen;
147  Py_ssize_t i;
148  Pairs *pairs;
149 
150  pairs = palloc(pcount * sizeof(*pairs));
151 
152  for (i = 0; i < pcount; i++)
153  {
154  PyObject *tuple;
155  PyObject *key;
156  PyObject *value;
157 
158  tuple = PyList_GetItem(items, i);
159  key = PyTuple_GetItem(tuple, 0);
160  value = PyTuple_GetItem(tuple, 1);
161 
162  pairs[i].key = PLyObject_AsString(key);
163  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
164  pairs[i].needfree = true;
165 
166  if (value == Py_None)
167  {
168  pairs[i].val = NULL;
169  pairs[i].vallen = 0;
170  pairs[i].isnull = true;
171  }
172  else
173  {
174  pairs[i].val = PLyObject_AsString(value);
175  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
176  pairs[i].isnull = false;
177  }
178  }
179 
180  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
181  out = hstorePairs(pairs, pcount, buflen);
182  }
183  PG_FINALLY();
184  {
185  Py_DECREF(items);
186  }
187  PG_END_TRY();
188 
189  PG_RETURN_POINTER(out);
190 }
signed int int32
Definition: c.h:497
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:984
#define ARRPTR(x)
Definition: cube.c:25
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#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
HStore *(* hstoreUpgrade_t)(Datum orig)
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)
char *(* PLyObject_AsString_t)(PyObject *plrv)
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
#define hstoreCheckKeyLen
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
#define hstoreUniquePairs
PG_MODULE_MAGIC
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
static hstorePairs_t hstorePairs_p
static PLyObject_AsString_t PLyObject_AsString_p
#define hstorePairs
Datum hstore_to_plpython(PG_FUNCTION_ARGS)
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
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 @157 value
int a
Definition: isn.c:69
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static pg_noinline void Size size
Definition: slab.c:607
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:49