PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hstore_plperl.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "hstore/hstore.h"
#include "miscadmin.h"
#include "plperl.h"
Include dependency graph for hstore_plperl.c:

Go to the source code of this file.

Macros

#define hstoreUpgrade   hstoreUpgrade_p
 
#define hstoreUniquePairs   hstoreUniquePairs_p
 
#define hstorePairs   hstorePairs_p
 
#define hstoreCheckKeyLen   hstoreCheckKeyLen_p
 
#define hstoreCheckValLen   hstoreCheckValLen_p
 

Typedefs

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_plperl",.version=PG_VERSION)
 
void _PG_init (void)
 
 PG_FUNCTION_INFO_V1 (hstore_to_plperl)
 
Datum hstore_to_plperl (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plperl_to_hstore)
 
Datum plperl_to_hstore (PG_FUNCTION_ARGS)
 

Variables

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

Macro Definition Documentation

◆ hstoreCheckKeyLen

#define hstoreCheckKeyLen   hstoreCheckKeyLen_p

Definition at line 61 of file hstore_plperl.c.

◆ hstoreCheckValLen

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 62 of file hstore_plperl.c.

◆ hstorePairs

#define hstorePairs   hstorePairs_p

Definition at line 60 of file hstore_plperl.c.

◆ hstoreUniquePairs

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 59 of file hstore_plperl.c.

◆ hstoreUpgrade

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 58 of file hstore_plperl.c.

Typedef Documentation

◆ hstoreCheckKeyLen_t

typedef size_t(* hstoreCheckKeyLen_t) (size_t len)

Definition at line 20 of file hstore_plperl.c.

◆ hstoreCheckValLen_t

typedef size_t(* hstoreCheckValLen_t) (size_t len)

Definition at line 22 of file hstore_plperl.c.

◆ hstorePairs_t

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

Definition at line 18 of file hstore_plperl.c.

◆ hstoreUniquePairs_t

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

Definition at line 16 of file hstore_plperl.c.

◆ hstoreUpgrade_t

typedef HStore *(* hstoreUpgrade_t) (Datum orig)

Definition at line 14 of file hstore_plperl.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 37 of file hstore_plperl.c.

38{
40 load_external_function("$libdir/hstore", "hstoreUpgrade",
41 true, NULL);
43 load_external_function("$libdir/hstore", "hstoreUniquePairs",
44 true, NULL);
46 load_external_function("$libdir/hstore", "hstorePairs",
47 true, NULL);
49 load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
50 true, NULL);
52 load_external_function("$libdir/hstore", "hstoreCheckValLen",
53 true, NULL);
54}
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)
static hstorePairs_t hstorePairs_p
static hstoreCheckValLen_t hstoreCheckValLen_p
static hstoreUniquePairs_t hstoreUniquePairs_p
size_t(* hstoreCheckValLen_t)(size_t len)
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, and load_external_function().

◆ hstore_to_plperl()

Datum hstore_to_plperl ( PG_FUNCTION_ARGS  )

Definition at line 68 of file hstore_plperl.c.

69{
70 dTHX;
72 int i;
73 int count = HS_COUNT(in);
74 char *base = STRPTR(in);
75 HEntry *entries = ARRPTR(in);
76 HV *hv;
77
78 hv = newHV();
79
80 for (i = 0; i < count; i++)
81 {
82 const char *key;
83 SV *value;
84
85 key = pnstrdup(HSTORE_KEY(entries, base, i),
86 HSTORE_KEYLEN(entries, i));
87 value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
88 cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
89 HSTORE_VALLEN(entries, i)));
90
91 (void) hv_store(hv, key, strlen(key), value, 0);
92 }
93
94 return PointerGetDatum(newRV((SV *) hv));
95}
#define ARRPTR(x)
Definition cube.c:28
#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 @177 value
int i
Definition isn.c:77
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1921
static SV * cstr2sv(const char *str)
Definition plperl.h:147
#define PointerGetDatum(X)
Definition postgres.h:354
#define dTHX
Definition ppport.h:11306

References ARRPTR, cstr2sv(), dTHX, fb(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, PG_GETARG_HSTORE_P, pnstrdup(), PointerGetDatum, STRPTR, and value.

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( hstore_to_plperl  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( plperl_to_hstore  )

◆ PG_MODULE_MAGIC_EXT()

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

◆ plperl_to_hstore()

Datum plperl_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 101 of file hstore_plperl.c.

102{
103 dTHX;
104 SV *in = (SV *) PG_GETARG_POINTER(0);
105 HV *hv;
106 HE *he;
107 int32 buflen;
108 int32 i;
110 HStore *out;
111 Pairs *pairs;
112
113 /* Dereference references recursively. */
114 while (SvROK(in))
115 {
116 /*
117 * It's possible for circular references to make this an infinite
118 * loop. Checking for such a situation seems like much more trouble
119 * than it's worth, but let's provide a way to break out of the loop.
120 */
122 in = SvRV(in);
123 }
124
125 /* Now we must have a hash. */
126 if (SvTYPE(in) != SVt_PVHV)
129 errmsg("cannot transform non-hash Perl value to hstore")));
130 hv = (HV *) in;
131
133
134 pairs = palloc_array(Pairs, pcount);
135
136 i = 0;
137 while ((he = hv_iternext(hv)))
138 {
139 char *key = sv2cstr(HeSVKEY_force(he));
140 SV *value = HeVAL(he);
141
142 pairs[i].key = pstrdup(key);
143 pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
144 pairs[i].needfree = true;
145
146 if (!SvOK(value))
147 {
148 pairs[i].val = NULL;
149 pairs[i].vallen = 0;
150 pairs[i].isnull = true;
151 }
152 else
153 {
154 pairs[i].val = pstrdup(sv2cstr(value));
155 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
156 pairs[i].isnull = false;
157 }
158
159 i++;
160 }
161
162 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
163 out = hstorePairs(pairs, pcount, buflen);
165}
int32_t int32
Definition c.h:620
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#define palloc_array(type, count)
Definition fe_memutils.h:91
#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
long val
Definition informix.c:689
char * pstrdup(const char *in)
Definition mcxt.c:1910
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
static char * errmsg
static char * sv2cstr(SV *sv)
Definition plperl.h:89
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 CHECK_FOR_INTERRUPTS, dTHX, ereport, errcode(), errmsg, ERROR, fb(), hstoreCheckKeyLen, hstoreCheckValLen, hstorePairs, hstoreUniquePairs, i, Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, palloc_array, PG_GETARG_POINTER, PG_RETURN_POINTER, pstrdup(), sv2cstr(), Pairs::val, val, Pairs::vallen, and value.

Variable Documentation

◆ hstoreCheckKeyLen

StaticAssertVariableIsOfType& hstoreCheckKeyLen

Definition at line 29 of file hstore_plperl.c.

◆ hstoreCheckKeyLen_p

hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static

Definition at line 21 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreCheckValLen

StaticAssertVariableIsOfType& hstoreCheckValLen

Definition at line 30 of file hstore_plperl.c.

◆ hstoreCheckValLen_p

hstoreCheckValLen_t hstoreCheckValLen_p
static

Definition at line 23 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstorePairs

Definition at line 28 of file hstore_plperl.c.

◆ hstorePairs_p

hstorePairs_t hstorePairs_p
static

Definition at line 19 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreUniquePairs

StaticAssertVariableIsOfType& hstoreUniquePairs

Definition at line 27 of file hstore_plperl.c.

◆ hstoreUniquePairs_p

hstoreUniquePairs_t hstoreUniquePairs_p
static

Definition at line 17 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreUpgrade

Definition at line 26 of file hstore_plperl.c.

◆ hstoreUpgrade_p

hstoreUpgrade_t hstoreUpgrade_p
static

Definition at line 15 of file hstore_plperl.c.

Referenced by _PG_init().