PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hstore_plpython.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "plpython.h"
#include "plpy_typeio.h"
#include "hstore.h"
Include dependency graph for hstore_plpython.c:

Go to the source code of this file.

Macros

#define PLyObject_AsString   PLyObject_AsString_p
 
#define PLyUnicode_FromStringAndSize   PLyUnicode_FromStringAndSize_p
 
#define hstoreUpgrade   hstoreUpgrade_p
 
#define hstoreUniquePairs   hstoreUniquePairs_p
 
#define hstorePairs   hstorePairs_p
 
#define hstoreCheckKeyLen   hstoreCheckKeyLen_p
 
#define hstoreCheckValLen   hstoreCheckValLen_p
 

Typedefs

typedef char *(* PLyObject_AsString_t )(PyObject *plrv)
 
typedef HStore *(* hstoreUpgrade_t )(Datum orig)
 
typedef int(* hstoreUniquePairs_t )(Pairs *a, int32 l, int32 *buflen)
 
typedef HStore *(* hstorePairs_t )(Pairs *pairs, int32 pcount, int32 buflen)
 
typedef size_t(* hstoreCheckKeyLen_t )(size_t len)
 
typedef size_t(* hstoreCheckValLen_t )(size_t len)
 

Functions

void _PG_init (void)
 
 PG_FUNCTION_INFO_V1 (hstore_to_plpython)
 
Datum hstore_to_plpython (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plpython_to_hstore)
 
Datum plpython_to_hstore (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static PLyObject_AsString_t PLyObject_AsString_p
 
static hstoreUpgrade_t hstoreUpgrade_p
 
static hstoreUniquePairs_t hstoreUniquePairs_p
 
static hstorePairs_t hstorePairs_p
 
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
 
static hstoreCheckValLen_t hstoreCheckValLen_p
 

Macro Definition Documentation

#define hstoreCheckKeyLen   hstoreCheckKeyLen_p

Definition at line 79 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 80 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

#define hstorePairs   hstorePairs_p

Definition at line 78 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 77 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 76 of file hstore_plpython.c.

Referenced by _PG_init().

#define PLyObject_AsString   PLyObject_AsString_p

Definition at line 74 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

#define PLyUnicode_FromStringAndSize   PLyUnicode_FromStringAndSize_p

Definition at line 75 of file hstore_plpython.c.

Referenced by _PG_init().

Typedef Documentation

typedef size_t(* hstoreCheckKeyLen_t)(size_t len)

Definition at line 27 of file hstore_plpython.c.

typedef size_t(* hstoreCheckValLen_t)(size_t len)

Definition at line 29 of file hstore_plpython.c.

typedef HStore*(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)

Definition at line 25 of file hstore_plpython.c.

typedef int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)

Definition at line 23 of file hstore_plpython.c.

typedef HStore*(* hstoreUpgrade_t)(Datum orig)

Definition at line 21 of file hstore_plpython.c.

typedef char*(* PLyObject_AsString_t)(PyObject *plrv)

Definition at line 13 of file hstore_plpython.c.

Function Documentation

void _PG_init ( void  )

Definition at line 37 of file hstore_plpython.c.

References AssertVariableIsOfType, hstoreCheckKeyLen, hstoreCheckKeyLen_p, hstoreCheckValLen, hstoreCheckValLen_p, hstorePairs, hstorePairs_p, hstoreUniquePairs, hstoreUniquePairs_p, hstoreUpgrade, hstoreUpgrade_p, load_external_function(), NULL, PLyObject_AsString, PLyObject_AsString_p, and PLyUnicode_FromStringAndSize.

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 }
#define hstoreUniquePairs
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
size_t(* hstoreCheckValLen_t)(size_t len)
char *(* PLyObject_AsString_t)(PyObject *plrv)
#define hstoreUpgrade
static PLyObject_AsString_t PLyObject_AsString_p
static hstoreUpgrade_t hstoreUpgrade_p
HStore *(* hstoreUpgrade_t)(Datum orig)
static hstoreCheckValLen_t hstoreCheckValLen_p
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
size_t(* hstoreCheckKeyLen_t)(size_t len)
PGFunction load_external_function(char *filename, char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
#define PLyObject_AsString
#define hstorePairs
#define NULL
Definition: c.h:229
#define hstoreCheckKeyLen
static hstoreUniquePairs_t hstoreUniquePairs_p
#define PLyUnicode_FromStringAndSize
static hstorePairs_t hstorePairs_p
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
#define hstoreCheckValLen
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:783
Datum hstore_to_plpython ( PG_FUNCTION_ARGS  )

Definition at line 86 of file hstore_plpython.c.

References ARRPTR, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, PG_GETARG_HS, PointerGetDatum, STRPTR, and value.

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 }
Definition: hstore.h:44
#define PointerGetDatum(X)
Definition: postgres.h:562
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
static struct @114 value
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define PG_GETARG_HS(x)
Definition: hstore.h:154
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:26
PG_FUNCTION_INFO_V1 ( hstore_to_plpython  )
PG_FUNCTION_INFO_V1 ( plpython_to_hstore  )
Datum plpython_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 124 of file hstore_plpython.c.

References ereport, errcode(), errmsg(), ERROR, hstoreCheckKeyLen, hstoreCheckValLen, hstorePairs, hstoreUniquePairs, i, Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, NULL, palloc(), PG_CATCH, PG_END_TRY, PG_GETARG_POINTER, PG_RE_THROW, PG_RETURN_POINTER, PG_TRY, PLyObject_AsString, Pairs::val, val, Pairs::vallen, and value.

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
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:256
char * val
Definition: hstore.h:164
static struct @114 value
char * key
Definition: hstore.h:163
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PLyObject_AsString
size_t keylen
Definition: hstore.h:165
#define hstorePairs
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
bool isnull
Definition: hstore.h:167
#define hstoreCheckKeyLen
#define PG_RE_THROW()
Definition: elog.h:314
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#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

Variable Documentation

hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static

Definition at line 28 of file hstore_plpython.c.

Referenced by _PG_init().

hstoreCheckValLen_t hstoreCheckValLen_p
static

Definition at line 30 of file hstore_plpython.c.

Referenced by _PG_init().

hstorePairs_t hstorePairs_p
static

Definition at line 26 of file hstore_plpython.c.

Referenced by _PG_init().

hstoreUniquePairs_t hstoreUniquePairs_p
static

Definition at line 24 of file hstore_plpython.c.

Referenced by _PG_init().

hstoreUpgrade_t hstoreUpgrade_p
static

Definition at line 22 of file hstore_plpython.c.

Referenced by _PG_init().

PG_MODULE_MAGIC

Definition at line 8 of file hstore_plpython.c.

PLyObject_AsString_t PLyObject_AsString_p
static

Definition at line 14 of file hstore_plpython.c.

Referenced by _PG_init().