PostgreSQL Source Code  git master
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

◆ hstoreCheckKeyLen

#define hstoreCheckKeyLen   hstoreCheckKeyLen_p

Definition at line 79 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

◆ hstoreCheckValLen

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 80 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

◆ hstorePairs

#define hstorePairs   hstorePairs_p

Definition at line 78 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

◆ hstoreUniquePairs

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 77 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

◆ hstoreUpgrade

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 76 of file hstore_plpython.c.

Referenced by _PG_init().

◆ PLyObject_AsString

#define PLyObject_AsString   PLyObject_AsString_p

Definition at line 74 of file hstore_plpython.c.

Referenced by _PG_init(), and plpython_to_hstore().

◆ PLyUnicode_FromStringAndSize

#define PLyUnicode_FromStringAndSize   PLyUnicode_FromStringAndSize_p

Definition at line 75 of file hstore_plpython.c.

Referenced by _PG_init(), and PLyUnicode_AsString().

Typedef Documentation

◆ hstoreCheckKeyLen_t

typedef size_t(* hstoreCheckKeyLen_t) (size_t len)

Definition at line 27 of file hstore_plpython.c.

◆ hstoreCheckValLen_t

typedef size_t(* hstoreCheckValLen_t) (size_t len)

Definition at line 29 of file hstore_plpython.c.

◆ hstorePairs_t

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

Definition at line 25 of file hstore_plpython.c.

◆ hstoreUniquePairs_t

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

Definition at line 23 of file hstore_plpython.c.

◆ hstoreUpgrade_t

typedef HStore*(* hstoreUpgrade_t) (Datum orig)

Definition at line 21 of file hstore_plpython.c.

◆ PLyObject_AsString_t

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

Definition at line 13 of file hstore_plpython.c.

Function Documentation

◆ _PG_init()

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

◆ hstore_to_plpython()

Datum hstore_to_plpython ( PG_FUNCTION_ARGS  )

Definition at line 86 of file hstore_plpython.c.

References ARRPTR, ereport, errcode(), errmsg(), ERROR, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, plpython_to_hstore(), PointerGetDatum, STRPTR, and value.

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 }
Definition: hstore.h:44
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define PointerGetDatum(X)
Definition: postgres.h:562
#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 HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ERROR
Definition: elog.h:43
static struct @121 value
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:26

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( hstore_to_plpython  )

Referenced by hstore_to_plpython().

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( plpython_to_hstore  )

◆ plpython_to_hstore()

Datum plpython_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 128 of file hstore_plpython.c.

References ereport, errcode(), errmsg(), ERROR, hstoreCheckKeyLen, hstoreCheckValLen, hstorePairs, hstoreUniquePairs, i, Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, 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.

Referenced by hstore_to_plpython().

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: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:284
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define ERROR
Definition: elog.h:43
static struct @121 value
#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
bool isnull
Definition: hstore.h:167
#define hstoreCheckKeyLen
#define PG_RE_THROW()
Definition: elog.h:314
void * palloc(Size size)
Definition: mcxt.c:848
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_p

hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static

Definition at line 28 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreCheckValLen_p

hstoreCheckValLen_t hstoreCheckValLen_p
static

Definition at line 30 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstorePairs_p

hstorePairs_t hstorePairs_p
static

Definition at line 26 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreUniquePairs_p

hstoreUniquePairs_t hstoreUniquePairs_p
static

Definition at line 24 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreUpgrade_p

hstoreUpgrade_t hstoreUpgrade_p
static

Definition at line 22 of file hstore_plpython.c.

Referenced by _PG_init().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 8 of file hstore_plpython.c.

◆ PLyObject_AsString_p

PLyObject_AsString_t PLyObject_AsString_p
static

Definition at line 14 of file hstore_plpython.c.

Referenced by _PG_init().