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 extern void _PG_init(void);
11 
12 /* Linkage to functions in plpython module */
13 typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
15 typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
17 
18 /* Linkage to functions in hstore module */
19 typedef HStore *(*hstoreUpgrade_t) (Datum orig);
21 typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
23 typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
25 typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
27 typedef size_t (*hstoreCheckValLen_t) (size_t len);
29 
30 
31 /*
32  * Module initialize function: fetch function pointers for cross-module calls.
33  */
34 void
35 _PG_init(void)
36 {
37  /* Asserts verify that typedefs above match original declarations */
40  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
41  true, NULL);
44  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
45  true, NULL);
48  load_external_function("$libdir/hstore", "hstoreUpgrade",
49  true, NULL);
52  load_external_function("$libdir/hstore", "hstoreUniquePairs",
53  true, NULL);
56  load_external_function("$libdir/hstore", "hstorePairs",
57  true, NULL);
60  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
61  true, NULL);
64  load_external_function("$libdir/hstore", "hstoreCheckValLen",
65  true, NULL);
66 }
67 
68 
69 /* These defines must be after the module init function */
70 #define PLyObject_AsString PLyObject_AsString_p
71 #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
72 #define hstoreUpgrade hstoreUpgrade_p
73 #define hstoreUniquePairs hstoreUniquePairs_p
74 #define hstorePairs hstorePairs_p
75 #define hstoreCheckKeyLen hstoreCheckKeyLen_p
76 #define hstoreCheckValLen hstoreCheckValLen_p
77 
78 
80 
81 Datum
83 {
84  HStore *in = PG_GETARG_HSTORE_P(0);
85  int i;
86  int count = HS_COUNT(in);
87  char *base = STRPTR(in);
88  HEntry *entries = ARRPTR(in);
89  PyObject *dict;
90 
91  dict = PyDict_New();
92  if (!dict)
93  ereport(ERROR,
94  (errcode(ERRCODE_OUT_OF_MEMORY),
95  errmsg("out of memory")));
96 
97  for (i = 0; i < count; i++)
98  {
99  PyObject *key;
100 
101  key = PLyUnicode_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 
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  PyObject *volatile items;
128  Py_ssize_t pcount;
129  HStore *volatile 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 = PyMapping_Items(dict);
139 
140  PG_TRY();
141  {
142  int32 buflen;
143  Py_ssize_t i;
144  Pairs *pairs;
145 
146  pairs = palloc(pcount * sizeof(*pairs));
147 
148  for (i = 0; i < pcount; i++)
149  {
150  PyObject *tuple;
151  PyObject *key;
152  PyObject *value;
153 
154  tuple = PyList_GetItem(items, i);
155  key = PyTuple_GetItem(tuple, 0);
156  value = PyTuple_GetItem(tuple, 1);
157 
158  pairs[i].key = PLyObject_AsString(key);
159  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
160  pairs[i].needfree = true;
161 
162  if (value == Py_None)
163  {
164  pairs[i].val = NULL;
165  pairs[i].vallen = 0;
166  pairs[i].isnull = true;
167  }
168  else
169  {
170  pairs[i].val = PLyObject_AsString(value);
171  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
172  pairs[i].isnull = false;
173  }
174  }
175 
176  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
177  out = hstorePairs(pairs, pcount, buflen);
178  }
179  PG_FINALLY();
180  {
181  Py_DECREF(items);
182  }
183  PG_END_TRY();
184 
185  PG_RETURN_POINTER(out);
186 }
signed int int32
Definition: c.h:429
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:963
#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:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define PG_END_TRY()
Definition: elog.h:324
#define PG_TRY()
Definition: elog.h:299
#define PG_FINALLY()
Definition: elog.h:316
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#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
static struct @151 value
long val
Definition: informix.c:664
int a
Definition: isn.c:69
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1068
const void size_t len
uintptr_t Datum
Definition: postgres.h:411
#define PointerGetDatum(X)
Definition: postgres.h:600
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