PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_HS(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 
97  for (i = 0; i < count; i++)
98  {
99  PyObject *key;
100 
101  key = PyString_FromStringAndSize(HSTORE_KEY(entries, base, i),
102  HSTORE_KEYLEN(entries, i));
103  if (HSTORE_VALISNULL(entries, i))
104  PyDict_SetItem(dict, key, Py_None);
105  else
106  {
107  PyObject *value;
108 
109  value = PyString_FromStringAndSize(HSTORE_VAL(entries, base, i),
110  HSTORE_VALLEN(entries, i));
111  PyDict_SetItem(dict, key, value);
112  Py_XDECREF(value);
113  }
114  Py_XDECREF(key);
115  }
116 
117  return PointerGetDatum(dict);
118 }
119 
120 
122 
123 Datum
125 {
126  PyObject *dict;
127  volatile PyObject *items_v = NULL;
128  int32 pcount;
129  HStore *out;
130 
131  dict = (PyObject *) PG_GETARG_POINTER(0);
132  if (!PyMapping_Check(dict))
133  ereport(ERROR,
134  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
135  errmsg("not a Python mapping")));
136 
137  pcount = PyMapping_Size(dict);
138  items_v = PyMapping_Items(dict);
139 
140  PG_TRY();
141  {
142  int32 buflen;
143  int32 i;
144  Pairs *pairs;
145  PyObject *items = (PyObject *) items_v;
146 
147  pairs = palloc(pcount * sizeof(*pairs));
148 
149  for (i = 0; i < pcount; i++)
150  {
151  PyObject *tuple;
152  PyObject *key;
153  PyObject *value;
154 
155  tuple = PyList_GetItem(items, i);
156  key = PyTuple_GetItem(tuple, 0);
157  value = PyTuple_GetItem(tuple, 1);
158 
159  pairs[i].key = PLyObject_AsString(key);
160  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
161  pairs[i].needfree = true;
162 
163  if (value == Py_None)
164  {
165  pairs[i].val = NULL;
166  pairs[i].vallen = 0;
167  pairs[i].isnull = true;
168  }
169  else
170  {
171  pairs[i].val = PLyObject_AsString(value);
172  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
173  pairs[i].isnull = false;
174  }
175  }
176  Py_DECREF(items_v);
177 
178  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
179  out = hstorePairs(pairs, pcount, buflen);
180  }
181  PG_CATCH();
182  {
183  Py_DECREF(items_v);
184  PG_RE_THROW();
185  }
186  PG_END_TRY();
187 
188  PG_RETURN_POINTER(out);
189 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define hstoreUniquePairs
Definition: hstore.h:44
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
size_t(* hstoreCheckValLen_t)(size_t len)
char *(* PLyObject_AsString_t)(PyObject *plrv)
#define PointerGetDatum(X)
Definition: postgres.h:562
#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:241
HStore *(* hstoreUpgrade_t)(Datum orig)
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
int Py_ssize_t
Definition: plpython.h:64
signed int int32
Definition: c.h:256
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)
static struct @121 value
#define HS_COUNT(hsp_)
Definition: hstore.h:61
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
#define PG_GETARG_HS(x)
Definition: hstore.h:154
PG_MODULE_MAGIC
size_t(* hstoreCheckKeyLen_t)(size_t len)
#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:372
#define PLyObject_AsString
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)
#define NULL
Definition: c.h:229
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:849
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:158
#define ARRPTR(x)
Definition: cube.c:26
static hstorePairs_t hstorePairs_p
#define PG_TRY()
Definition: elog.h:284
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
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:783