PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hstore_plperl.c
Go to the documentation of this file.
1#include "postgres.h"
2
3#include "fmgr.h"
4#include "hstore/hstore.h"
5#include "plperl.h"
6
8 .name = "hstore_plperl",
9 .version = PG_VERSION
10);
11
12/* Linkage to functions in hstore module */
13typedef HStore *(*hstoreUpgrade_t) (Datum orig);
15typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
17typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
19typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
21typedef size_t (*hstoreCheckValLen_t) (size_t len);
23
24/* Static asserts verify that typedefs above match original declarations */
30
31
32/*
33 * Module initialize function: fetch function pointers for cross-module calls.
34 */
35void
37{
39 load_external_function("$libdir/hstore", "hstoreUpgrade",
40 true, NULL);
42 load_external_function("$libdir/hstore", "hstoreUniquePairs",
43 true, NULL);
45 load_external_function("$libdir/hstore", "hstorePairs",
46 true, NULL);
48 load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
49 true, NULL);
51 load_external_function("$libdir/hstore", "hstoreCheckValLen",
52 true, NULL);
53}
54
55
56/* These defines must be after the module init function */
57#define hstoreUpgrade hstoreUpgrade_p
58#define hstoreUniquePairs hstoreUniquePairs_p
59#define hstorePairs hstorePairs_p
60#define hstoreCheckKeyLen hstoreCheckKeyLen_p
61#define hstoreCheckValLen hstoreCheckValLen_p
62
63
65
68{
69 dTHX;
71 int i;
72 int count = HS_COUNT(in);
73 char *base = STRPTR(in);
74 HEntry *entries = ARRPTR(in);
75 HV *hv;
76
77 hv = newHV();
78
79 for (i = 0; i < count; i++)
80 {
81 const char *key;
82 SV *value;
83
84 key = pnstrdup(HSTORE_KEY(entries, base, i),
85 HSTORE_KEYLEN(entries, i));
86 value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
87 cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
88 HSTORE_VALLEN(entries, i)));
89
90 (void) hv_store(hv, key, strlen(key), value, 0);
91 }
92
93 return PointerGetDatum(newRV((SV *) hv));
94}
95
96
98
101{
102 dTHX;
103 SV *in = (SV *) PG_GETARG_POINTER(0);
104 HV *hv;
105 HE *he;
106 int32 buflen;
107 int32 i;
109 HStore *out;
110 Pairs *pairs;
111
112 /* Dereference references recursively. */
113 while (SvROK(in))
114 in = SvRV(in);
115
116 /* Now we must have a hash. */
117 if (SvTYPE(in) != SVt_PVHV)
120 errmsg("cannot transform non-hash Perl value to hstore")));
121 hv = (HV *) in;
122
124
125 pairs = palloc(pcount * sizeof(Pairs));
126
127 i = 0;
128 while ((he = hv_iternext(hv)))
129 {
130 char *key = sv2cstr(HeSVKEY_force(he));
131 SV *value = HeVAL(he);
132
133 pairs[i].key = pstrdup(key);
134 pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
135 pairs[i].needfree = true;
136
137 if (!SvOK(value))
138 {
139 pairs[i].val = NULL;
140 pairs[i].vallen = 0;
141 pairs[i].isnull = true;
142 }
143 else
144 {
145 pairs[i].val = pstrdup(sv2cstr(value));
146 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
147 pairs[i].isnull = false;
148 }
149
150 i++;
151 }
152
153 pcount = hstoreUniquePairs(pairs, pcount, &buflen);
154 out = hstorePairs(pairs, pcount, buflen);
156}
int32_t int32
Definition c.h:542
#define StaticAssertVariableIsOfType(varname, typename)
Definition c.h:974
#define ARRPTR(x)
Definition cube.c:28
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition dfmgr.c:95
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 PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#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 hstoreUpgrade
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
static hstoreUpgrade_t hstoreUpgrade_p
void _PG_init(void)
Datum hstore_to_plperl(PG_FUNCTION_ARGS)
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
HStore *(* hstoreUpgrade_t)(Datum orig)
#define hstoreCheckKeyLen
#define hstoreUniquePairs
static hstorePairs_t hstorePairs_p
#define hstorePairs
static hstoreCheckValLen_t hstoreCheckValLen_p
static hstoreUniquePairs_t hstoreUniquePairs_p
size_t(* hstoreCheckValLen_t)(size_t len)
#define hstoreCheckValLen
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
size_t(* hstoreCheckKeyLen_t)(size_t len)
Datum plperl_to_hstore(PG_FUNCTION_ARGS)
long val
Definition informix.c:689
static struct @170 value
int a
Definition isn.c:73
int i
Definition isn.c:77
char * pstrdup(const char *in)
Definition mcxt.c:1781
void * palloc(Size size)
Definition mcxt.c:1387
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1792
const void size_t len
static char * sv2cstr(SV *sv)
Definition plperl.h:89
static SV * cstr2sv(const char *str)
Definition plperl.h:147
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
#define dTHX
Definition ppport.h:11306
static int fb(int x)
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
const char * name