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 "miscadmin.h"
6#include "plperl.h"
7
9 .name = "hstore_plperl",
10 .version = PG_VERSION
11);
12
13/* Linkage to functions in hstore module */
14typedef HStore *(*hstoreUpgrade_t) (Datum orig);
16typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
18typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
20typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
22typedef size_t (*hstoreCheckValLen_t) (size_t len);
24
25/* Static asserts verify that typedefs above match original declarations */
31
32
33/*
34 * Module initialize function: fetch function pointers for cross-module calls.
35 */
36void
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}
55
56
57/* These defines must be after the module init function */
58#define hstoreUpgrade hstoreUpgrade_p
59#define hstoreUniquePairs hstoreUniquePairs_p
60#define hstorePairs hstorePairs_p
61#define hstoreCheckKeyLen hstoreCheckKeyLen_p
62#define hstoreCheckValLen hstoreCheckValLen_p
63
64
66
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}
96
97
99
100Datum
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:676
#define StaticAssertVariableIsOfType(varname, typename)
Definition c.h:1124
#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: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_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 @175 value
int a
Definition isn.c:73
int i
Definition isn.c:77
char * pstrdup(const char *in)
Definition mcxt.c:1910
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1921
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
static char * errmsg
const void size_t len
static char * sv2cstr(SV *sv)
Definition plperl.h:89
static SV * cstr2sv(const char *str)
Definition plperl.h:147
uint64_t Datum
Definition postgres.h:70
#define PointerGetDatum(X)
Definition postgres.h:354
#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