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 "plpython.h"
5 #include "plpy_typeio.h"
6 #include "hstore.h"
7 
9 
10 extern void _PG_init(void);
11 
12 /* Linkage to functions in plpython module */
13 typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
15 #if PY_MAJOR_VERSION >= 3
16 typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
17 static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
18 #endif
19 
20 /* Linkage to functions in hstore module */
21 typedef HStore *(*hstoreUpgrade_t) (Datum orig);
23 typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
25 typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
27 typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
29 typedef size_t (*hstoreCheckValLen_t) (size_t len);
31 
32 
33 /*
34  * Module initialize function: fetch function pointers for cross-module calls.
35  */
36 void
37 _PG_init(void)
38 {
39  /* Asserts verify that typedefs above match original declarations */
42  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
43  true, NULL);
44 #if PY_MAJOR_VERSION >= 3
45  AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
46  PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
47  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
48  true, NULL);
49 #endif
52  load_external_function("$libdir/hstore", "hstoreUpgrade",
53  true, NULL);
56  load_external_function("$libdir/hstore", "hstoreUniquePairs",
57  true, NULL);
60  load_external_function("$libdir/hstore", "hstorePairs",
61  true, NULL);
64  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
65  true, NULL);
68  load_external_function("$libdir/hstore", "hstoreCheckValLen",
69  true, NULL);
70 }
71 
72 
73 /* These defines must be after the module init function */
74 #define PLyObject_AsString PLyObject_AsString_p
75 #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
76 #define hstoreUpgrade hstoreUpgrade_p
77 #define hstoreUniquePairs hstoreUniquePairs_p
78 #define hstorePairs hstorePairs_p
79 #define hstoreCheckKeyLen hstoreCheckKeyLen_p
80 #define hstoreCheckValLen hstoreCheckValLen_p
81 
82 
84 
85 Datum
87 {
88  HStore *in = PG_GETARG_HSTORE_P(0);
89  int i;
90  int count = HS_COUNT(in);
91  char *base = STRPTR(in);
92  HEntry *entries = ARRPTR(in);
93  PyObject *dict;
94 
95  dict = PyDict_New();
96  if (!dict)
97  ereport(ERROR,
98  (errcode(ERRCODE_OUT_OF_MEMORY),
99  errmsg("out of memory")));
100 
101  for (i = 0; i < count; i++)
102  {
103  PyObject *key;
104 
105  key = PyString_FromStringAndSize(HSTORE_KEY(entries, base, i),
106  HSTORE_KEYLEN(entries, i));
107  if (HSTORE_VALISNULL(entries, i))
108  PyDict_SetItem(dict, key, Py_None);
109  else
110  {
111  PyObject *value;
112 
113  value = PyString_FromStringAndSize(HSTORE_VAL(entries, base, i),
114  HSTORE_VALLEN(entries, i));
115  PyDict_SetItem(dict, key, value);
116  Py_XDECREF(value);
117  }
118  Py_XDECREF(key);
119  }
120 
121  return PointerGetDatum(dict);
122 }
123 
124 
126 
127 Datum
129 {
130  PyObject *dict;
131  volatile PyObject *items_v = NULL;
132  int32 pcount;
133  HStore *out;
134 
135  dict = (PyObject *) PG_GETARG_POINTER(0);
136  if (!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_v = PyMapping_Items(dict);
143 
144  PG_TRY();
145  {
146  int32 buflen;
147  int32 i;
148  Pairs *pairs;
149  PyObject *items = (PyObject *) items_v;
150 
151  pairs = palloc(pcount * sizeof(*pairs));
152 
153  for (i = 0; i < pcount; i++)
154  {
155  PyObject *tuple;
156  PyObject *key;
157  PyObject *value;
158 
159  tuple = PyList_GetItem(items, i);
160  key = PyTuple_GetItem(tuple, 0);
161  value = PyTuple_GetItem(tuple, 1);
162 
163  pairs[i].key = PLyObject_AsString(key);
164  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
165  pairs[i].needfree = true;
166 
167  if (value == Py_None)
168  {
169  pairs[i].val = NULL;
170  pairs[i].vallen = 0;
171  pairs[i].isnull = true;
172  }
173  else
174  {
175  pairs[i].val = PLyObject_AsString(value);
176  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
177  pairs[i].isnull = false;
178  }
179  }
180  Py_DECREF(items_v);
181 
182  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
183  out = hstorePairs(pairs, pcount, buflen);
184  }
185  PG_CATCH();
186  {
187  Py_DECREF(items_v);
188  PG_RE_THROW();
189  }
190  PG_END_TRY();
191 
192  PG_RETURN_POINTER(out);
193 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
#define hstoreUniquePairs
Definition: hstore.h:44
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define PointerGetDatum(X)
Definition: postgres.h:541
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define hstoreUpgrade
int errcode(int sqlerrcode)
Definition: elog.c:575
static PLyObject_AsString_t PLyObject_AsString_p
static hstoreUpgrade_t hstoreUpgrade_p
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
int Py_ssize_t
Definition: plpython.h:64
size_t(* hstoreCheckKeyLen_t)(size_t len)
char *(* PLyObject_AsString_t)(PyObject *plrv)
signed int int32
Definition: c.h:313
char * val
Definition: hstore.h:164
static hstoreCheckValLen_t hstoreCheckValLen_p
char * key
Definition: hstore.h:163
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ERROR
Definition: elog.h:43
void _PG_init(void)
size_t(* hstoreCheckValLen_t)(size_t len)
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
#define HS_COUNT(hsp_)
Definition: hstore.h:61
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
HStore *(* hstoreUpgrade_t)(Datum orig)
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
PG_MODULE_MAGIC
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:367
#define PLyObject_AsString
static struct @131 value
size_t keylen
Definition: hstore.h:165
#define hstorePairs
#define PG_CATCH()
Definition: elog.h:293
Datum plpython_to_hstore(PG_FUNCTION_ARGS)
Datum hstore_to_plpython(PG_FUNCTION_ARGS)
bool isnull
Definition: hstore.h:167
#define hstoreCheckKeyLen
Definition: hstore.h:18
static hstoreUniquePairs_t hstoreUniquePairs_p
#define PG_RE_THROW()
Definition: elog.h:314
#define PLyUnicode_FromStringAndSize
PG_FUNCTION_INFO_V1(hstore_to_plpython)
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define STRPTR(x)
Definition: hstore.h:76
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:163
#define ARRPTR(x)
Definition: cube.c:26
static hstorePairs_t hstorePairs_p
#define PG_TRY()
Definition: elog.h:284
Definition: hstore.h:161
long val
Definition: informix.c:689
#define hstoreCheckValLen
#define PG_END_TRY()
Definition: elog.h:300
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:834