PostgreSQL Source Code  git master
hstore_plpython.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "hstore/hstore.h"
#include "plpy_typeio.h"
#include "plpython.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 PyObject *(* PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size)
 
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 PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_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 73 of file hstore_plpython.c.

◆ hstoreCheckValLen

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 74 of file hstore_plpython.c.

◆ hstorePairs

#define hstorePairs   hstorePairs_p

Definition at line 72 of file hstore_plpython.c.

◆ hstoreUniquePairs

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 71 of file hstore_plpython.c.

◆ hstoreUpgrade

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 70 of file hstore_plpython.c.

◆ PLyObject_AsString

#define PLyObject_AsString   PLyObject_AsString_p

Definition at line 68 of file hstore_plpython.c.

◆ PLyUnicode_FromStringAndSize

#define PLyUnicode_FromStringAndSize   PLyUnicode_FromStringAndSize_p

Definition at line 69 of file hstore_plpython.c.

Typedef Documentation

◆ hstoreCheckKeyLen_t

typedef size_t(* hstoreCheckKeyLen_t) (size_t len)

Definition at line 23 of file hstore_plpython.c.

◆ hstoreCheckValLen_t

typedef size_t(* hstoreCheckValLen_t) (size_t len)

Definition at line 25 of file hstore_plpython.c.

◆ hstorePairs_t

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

Definition at line 21 of file hstore_plpython.c.

◆ hstoreUniquePairs_t

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

Definition at line 19 of file hstore_plpython.c.

◆ hstoreUpgrade_t

typedef HStore*(* hstoreUpgrade_t) (Datum orig)

Definition at line 17 of file hstore_plpython.c.

◆ PLyObject_AsString_t

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

Definition at line 11 of file hstore_plpython.c.

◆ PLyUnicode_FromStringAndSize_t

typedef PyObject*(* PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size)

Definition at line 13 of file hstore_plpython.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 33 of file hstore_plpython.c.

34 {
35  /* Asserts verify that typedefs above match original declarations */
38  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
39  true, NULL);
42  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
43  true, NULL);
46  load_external_function("$libdir/hstore", "hstoreUpgrade",
47  true, NULL);
50  load_external_function("$libdir/hstore", "hstoreUniquePairs",
51  true, NULL);
54  load_external_function("$libdir/hstore", "hstorePairs",
55  true, NULL);
58  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
59  true, NULL);
62  load_external_function("$libdir/hstore", "hstoreCheckValLen",
63  true, NULL);
64 }
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:965
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
#define hstoreUpgrade
HStore *(* hstoreUpgrade_t)(Datum orig)
#define PLyUnicode_FromStringAndSize
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
static hstoreUpgrade_t hstoreUpgrade_p
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
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
static hstorePairs_t hstorePairs_p
static PLyObject_AsString_t PLyObject_AsString_p
#define hstorePairs
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
size_t(* hstoreCheckKeyLen_t)(size_t len)
#define PLyObject_AsString

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, PLyUnicode_FromStringAndSize, and PLyUnicode_FromStringAndSize_p.

◆ hstore_to_plpython()

Datum hstore_to_plpython ( PG_FUNCTION_ARGS  )

Definition at line 80 of file hstore_plpython.c.

81 {
82  HStore *in = PG_GETARG_HSTORE_P(0);
83  int i;
84  int count = HS_COUNT(in);
85  char *base = STRPTR(in);
86  HEntry *entries = ARRPTR(in);
87  PyObject *dict;
88 
89  dict = PyDict_New();
90  if (!dict)
91  ereport(ERROR,
92  (errcode(ERRCODE_OUT_OF_MEMORY),
93  errmsg("out of memory")));
94 
95  for (i = 0; i < count; i++)
96  {
97  PyObject *key;
98 
99  key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i),
100  HSTORE_KEYLEN(entries, i));
101  if (HSTORE_VALISNULL(entries, i))
102  PyDict_SetItem(dict, key, Py_None);
103  else
104  {
105  PyObject *value;
106 
108  HSTORE_VALLEN(entries, i));
109  PyDict_SetItem(dict, key, value);
110  Py_XDECREF(value);
111  }
112  Py_XDECREF(key);
113  }
114 
115  return PointerGetDatum(dict);
116 }
#define ARRPTR(x)
Definition: cube.c:25
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#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
static struct @143 value
int i
Definition: isn.c:73
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
Definition: hstore.h:19
Definition: hstore.h:45

References ARRPTR, ereport, errcode(), errmsg(), ERROR, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, sort-test::key, PG_GETARG_HSTORE_P, PLyUnicode_FromStringAndSize, PointerGetDatum(), STRPTR, and value.

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( 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 122 of file hstore_plpython.c.

123 {
124  PyObject *dict;
125  PyObject *volatile items;
126  Py_ssize_t pcount;
127  HStore *volatile out;
128 
129  dict = (PyObject *) PG_GETARG_POINTER(0);
130  if (!PyMapping_Check(dict))
131  ereport(ERROR,
132  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
133  errmsg("not a Python mapping")));
134 
135  pcount = PyMapping_Size(dict);
136  items = PyMapping_Items(dict);
137 
138  PG_TRY();
139  {
140  int32 buflen;
141  Py_ssize_t i;
142  Pairs *pairs;
143 
144  pairs = palloc(pcount * sizeof(*pairs));
145 
146  for (i = 0; i < pcount; i++)
147  {
148  PyObject *tuple;
149  PyObject *key;
150  PyObject *value;
151 
152  tuple = PyList_GetItem(items, i);
153  key = PyTuple_GetItem(tuple, 0);
154  value = PyTuple_GetItem(tuple, 1);
155 
156  pairs[i].key = PLyObject_AsString(key);
157  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
158  pairs[i].needfree = true;
159 
160  if (value == Py_None)
161  {
162  pairs[i].val = NULL;
163  pairs[i].vallen = 0;
164  pairs[i].isnull = true;
165  }
166  else
167  {
168  pairs[i].val = PLyObject_AsString(value);
169  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
170  pairs[i].isnull = false;
171  }
172  }
173 
174  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
175  out = hstorePairs(pairs, pcount, buflen);
176  }
177  PG_FINALLY();
178  {
179  Py_DECREF(items);
180  }
181  PG_END_TRY();
182 
183  PG_RETURN_POINTER(out);
184 }
signed int int32
Definition: c.h:478
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define PG_FINALLY(...)
Definition: elog.h:387
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
long val
Definition: informix.c:664
void * palloc(Size size)
Definition: mcxt.c:1210
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

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

Variable Documentation

◆ hstoreCheckKeyLen_p

hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static

Definition at line 24 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreCheckValLen_p

hstoreCheckValLen_t hstoreCheckValLen_p
static

Definition at line 26 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstorePairs_p

hstorePairs_t hstorePairs_p
static

Definition at line 22 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreUniquePairs_p

hstoreUniquePairs_t hstoreUniquePairs_p
static

Definition at line 20 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreUpgrade_p

hstoreUpgrade_t hstoreUpgrade_p
static

Definition at line 18 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 12 of file hstore_plpython.c.

Referenced by _PG_init().

◆ PLyUnicode_FromStringAndSize_p

PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
static

Definition at line 14 of file hstore_plpython.c.

Referenced by _PG_init().