PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hstore_plpython.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "hstore/hstore.h"
#include "plpy_typeio.h"
#include "plpy_util.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

 PG_MODULE_MAGIC_EXT (.name="hstore_plpython",.version=PG_VERSION)
 
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

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
 
StaticAssertVariableIsOfTypePLyObject_AsString
 
StaticAssertVariableIsOfTypePLyUnicode_FromStringAndSize
 
StaticAssertVariableIsOfTypehstoreUpgrade
 
StaticAssertVariableIsOfTypehstoreUniquePairs
 
StaticAssertVariableIsOfTypehstorePairs
 
StaticAssertVariableIsOfTypehstoreCheckKeyLen
 
StaticAssertVariableIsOfTypehstoreCheckValLen
 

Macro Definition Documentation

◆ hstoreCheckKeyLen

#define hstoreCheckKeyLen   hstoreCheckKeyLen_p

Definition at line 77 of file hstore_plpython.c.

◆ hstoreCheckValLen

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 78 of file hstore_plpython.c.

◆ hstorePairs

#define hstorePairs   hstorePairs_p

Definition at line 76 of file hstore_plpython.c.

◆ hstoreUniquePairs

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 75 of file hstore_plpython.c.

◆ hstoreUpgrade

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 74 of file hstore_plpython.c.

◆ PLyObject_AsString

#define PLyObject_AsString   PLyObject_AsString_p

Definition at line 72 of file hstore_plpython.c.

◆ PLyUnicode_FromStringAndSize

#define PLyUnicode_FromStringAndSize   PLyUnicode_FromStringAndSize_p

Definition at line 73 of file hstore_plpython.c.

Typedef Documentation

◆ hstoreCheckKeyLen_t

typedef size_t(* hstoreCheckKeyLen_t) (size_t len)

Definition at line 26 of file hstore_plpython.c.

◆ hstoreCheckValLen_t

typedef size_t(* hstoreCheckValLen_t) (size_t len)

Definition at line 28 of file hstore_plpython.c.

◆ hstorePairs_t

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

Definition at line 24 of file hstore_plpython.c.

◆ hstoreUniquePairs_t

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

Definition at line 22 of file hstore_plpython.c.

◆ hstoreUpgrade_t

typedef HStore *(* hstoreUpgrade_t) (Datum orig)

Definition at line 20 of file hstore_plpython.c.

◆ PLyObject_AsString_t

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

Definition at line 14 of file hstore_plpython.c.

◆ PLyUnicode_FromStringAndSize_t

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

Definition at line 16 of file hstore_plpython.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 45 of file hstore_plpython.c.

46{
48 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
49 true, NULL);
51 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
52 true, NULL);
54 load_external_function("$libdir/hstore", "hstoreUpgrade",
55 true, NULL);
57 load_external_function("$libdir/hstore", "hstoreUniquePairs",
58 true, NULL);
60 load_external_function("$libdir/hstore", "hstorePairs",
61 true, NULL);
63 load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
64 true, NULL);
66 load_external_function("$libdir/hstore", "hstoreCheckValLen",
67 true, NULL);
68}
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition dfmgr.c:95
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
static hstoreUpgrade_t hstoreUpgrade_p
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
HStore *(* hstoreUpgrade_t)(Datum orig)
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
static hstorePairs_t hstorePairs_p
static PLyObject_AsString_t PLyObject_AsString_p
char *(* PLyObject_AsString_t)(PyObject *plrv)
static hstoreCheckValLen_t hstoreCheckValLen_p
static hstoreUniquePairs_t hstoreUniquePairs_p
size_t(* hstoreCheckValLen_t)(size_t len)
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
size_t(* hstoreCheckKeyLen_t)(size_t len)
static int fb(int x)

References fb(), hstoreCheckKeyLen_p, hstoreCheckValLen_p, hstorePairs_p, hstoreUniquePairs_p, hstoreUpgrade_p, load_external_function(), PLyObject_AsString_p, and PLyUnicode_FromStringAndSize_p.

◆ hstore_to_plpython()

Datum hstore_to_plpython ( PG_FUNCTION_ARGS  )

Definition at line 84 of file hstore_plpython.c.

85{
87 int i;
88 int count = HS_COUNT(in);
89 char *base = STRPTR(in);
90 HEntry *entries = ARRPTR(in);
92
93 dict = PyDict_New();
94 if (!dict)
97 errmsg("out of memory")));
98
99 for (i = 0; i < count; i++)
100 {
101 PyObject *key;
102
104 HSTORE_KEYLEN(entries, i));
105 if (HSTORE_VALISNULL(entries, i))
107 else
108 {
110
112 HSTORE_VALLEN(entries, i));
115 }
116 Py_XDECREF(key);
117 }
118
119 return PointerGetDatum(dict);
120}
#define ARRPTR(x)
Definition cube.c:28
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#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 PLyUnicode_FromStringAndSize
static struct @172 value
int i
Definition isn.c:77
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352

References ARRPTR, ereport, errcode(), errmsg(), ERROR, fb(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, 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  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "hstore_plpython",
version = PG_VERSION 
)

◆ plpython_to_hstore()

Datum plpython_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 126 of file hstore_plpython.c.

127{
128 PyObject *dict;
129 PyObject *volatile items;
131 HStore *volatile out;
132
134
135 /*
136 * As of Python 3, PyMapping_Check() is unreliable unless one first checks
137 * that the object isn't a sequence. (Cleaner solutions exist, but not
138 * before Python 3.10, which we're not prepared to require yet.)
139 */
143 errmsg("not a Python mapping")));
144
147
148 PG_TRY();
149 {
150 int32 buflen;
152 Pairs *pairs;
153
154 pairs = palloc(pcount * sizeof(*pairs));
155
156 for (i = 0; i < pcount; i++)
157 {
158 PyObject *tuple;
159 PyObject *key;
161
162 tuple = PyList_GetItem(items, i);
163 key = PyTuple_GetItem(tuple, 0);
164 value = PyTuple_GetItem(tuple, 1);
165
166 pairs[i].key = PLyObject_AsString(key);
167 pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
168 pairs[i].needfree = true;
169
170 if (value == Py_None)
171 {
172 pairs[i].val = NULL;
173 pairs[i].vallen = 0;
174 pairs[i].isnull = true;
175 }
176 else
177 {
178 pairs[i].val = PLyObject_AsString(value);
179 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
180 pairs[i].isnull = false;
181 }
182 }
183
184 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
185 out = hstorePairs(pairs, pcount, buflen);
186 }
187 PG_FINALLY();
188 {
190 }
191 PG_END_TRY();
192
194}
int32_t int32
Definition c.h:542
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define PG_FINALLY(...)
Definition elog.h:389
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define hstoreCheckKeyLen
#define hstoreUniquePairs
#define hstorePairs
#define hstoreCheckValLen
#define PLyObject_AsString
long val
Definition informix.c:689
void * palloc(Size size)
Definition mcxt.c:1387
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
static ItemArray items

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

StaticAssertVariableIsOfType& hstoreCheckKeyLen

Definition at line 37 of file hstore_plpython.c.

◆ hstoreCheckKeyLen_p

hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static

Definition at line 27 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreCheckValLen

StaticAssertVariableIsOfType& hstoreCheckValLen

Definition at line 38 of file hstore_plpython.c.

◆ hstoreCheckValLen_p

hstoreCheckValLen_t hstoreCheckValLen_p
static

Definition at line 29 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstorePairs

Definition at line 36 of file hstore_plpython.c.

◆ hstorePairs_p

hstorePairs_t hstorePairs_p
static

Definition at line 25 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreUniquePairs

StaticAssertVariableIsOfType& hstoreUniquePairs

Definition at line 35 of file hstore_plpython.c.

◆ hstoreUniquePairs_p

hstoreUniquePairs_t hstoreUniquePairs_p
static

Definition at line 23 of file hstore_plpython.c.

Referenced by _PG_init().

◆ hstoreUpgrade

Definition at line 34 of file hstore_plpython.c.

◆ hstoreUpgrade_p

hstoreUpgrade_t hstoreUpgrade_p
static

Definition at line 21 of file hstore_plpython.c.

Referenced by _PG_init().

◆ PLyObject_AsString

StaticAssertVariableIsOfType& PLyObject_AsString

Definition at line 32 of file hstore_plpython.c.

◆ PLyObject_AsString_p

PLyObject_AsString_t PLyObject_AsString_p
static

Definition at line 15 of file hstore_plpython.c.

Referenced by _PG_init().

◆ PLyUnicode_FromStringAndSize

StaticAssertVariableIsOfType& PLyUnicode_FromStringAndSize

Definition at line 33 of file hstore_plpython.c.

◆ PLyUnicode_FromStringAndSize_p

PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
static

Definition at line 17 of file hstore_plpython.c.

Referenced by _PG_init().