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 #include "fmgr.h"
3 #include "plpython.h"
4 #include "plpy_typeio.h"
5 #include "hstore.h"
6 
8 
9 
11 
12 Datum
14 {
15  HStore *in = PG_GETARG_HS(0);
16  int i;
17  int count = HS_COUNT(in);
18  char *base = STRPTR(in);
19  HEntry *entries = ARRPTR(in);
20  PyObject *dict;
21 
22  dict = PyDict_New();
23 
24  for (i = 0; i < count; i++)
25  {
26  PyObject *key;
27 
28  key = PyString_FromStringAndSize(HSTORE_KEY(entries, base, i),
29  HSTORE_KEYLEN(entries, i));
30  if (HSTORE_VALISNULL(entries, i))
31  PyDict_SetItem(dict, key, Py_None);
32  else
33  {
34  PyObject *value;
35 
36  value = PyString_FromStringAndSize(HSTORE_VAL(entries, base, i),
37  HSTORE_VALLEN(entries, i));
38  PyDict_SetItem(dict, key, value);
39  Py_XDECREF(value);
40  }
41  Py_XDECREF(key);
42  }
43 
44  return PointerGetDatum(dict);
45 }
46 
47 
49 
50 Datum
52 {
53  PyObject *dict;
54  volatile PyObject *items_v = NULL;
55  int32 pcount;
56  HStore *out;
57 
58  dict = (PyObject *) PG_GETARG_POINTER(0);
59  if (!PyMapping_Check(dict))
60  ereport(ERROR,
61  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
62  errmsg("not a Python mapping")));
63 
64  pcount = PyMapping_Size(dict);
65  items_v = PyMapping_Items(dict);
66 
67  PG_TRY();
68  {
69  int32 buflen;
70  int32 i;
71  Pairs *pairs;
72  PyObject *items = (PyObject *) items_v;
73 
74  pairs = palloc(pcount * sizeof(*pairs));
75 
76  for (i = 0; i < pcount; i++)
77  {
78  PyObject *tuple;
79  PyObject *key;
80  PyObject *value;
81 
82  tuple = PyList_GetItem(items, i);
83  key = PyTuple_GetItem(tuple, 0);
84  value = PyTuple_GetItem(tuple, 1);
85 
86  pairs[i].key = PLyObject_AsString(key);
87  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
88  pairs[i].needfree = true;
89 
90  if (value == Py_None)
91  {
92  pairs[i].val = NULL;
93  pairs[i].vallen = 0;
94  pairs[i].isnull = true;
95  }
96  else
97  {
98  pairs[i].val = PLyObject_AsString(value);
99  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
100  pairs[i].isnull = false;
101  }
102  }
103  Py_DECREF(items_v);
104 
105  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
106  out = hstorePairs(pairs, pcount, buflen);
107  }
108  PG_CATCH();
109  {
110  Py_DECREF(items_v);
111  PG_RE_THROW();
112  }
113  PG_END_TRY();
114 
115  PG_RETURN_POINTER(out);
116 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
Definition: hstore.h:44
static struct @76 value
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
#define PointerGetDatum(X)
Definition: postgres.h:564
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:253
char * val
Definition: hstore.h:164
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
char * key
Definition: hstore.h:163
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ERROR
Definition: elog.h:43
char * PLyObject_AsString(PyObject *plrv)
Definition: plpy_typeio.c:789
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
PG_MODULE_MAGIC
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define ereport(elevel, rest)
Definition: elog.h:122
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
uintptr_t Datum
Definition: postgres.h:374
size_t keylen
Definition: hstore.h:165
#define PG_CATCH()
Definition: elog.h:292
Datum plpython_to_hstore(PG_FUNCTION_ARGS)
Datum hstore_to_plpython(PG_FUNCTION_ARGS)
#define NULL
Definition: c.h:226
bool isnull
Definition: hstore.h:167
Definition: hstore.h:18
#define PG_RE_THROW()
Definition: elog.h:313
PG_FUNCTION_INFO_V1(hstore_to_plpython)
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:894
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define STRPTR(x)
Definition: hstore.h:76
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define ARRPTR(x)
Definition: cube.c:26
#define PG_TRY()
Definition: elog.h:283
Definition: hstore.h:161
long val
Definition: informix.c:689
#define PG_END_TRY()
Definition: elog.h:299