Branch data Line data Source code
1 : : #include "postgres.h"
2 : :
3 : : #include "fmgr.h"
4 : : #include "hstore/hstore.h"
5 : : #include "miscadmin.h"
6 : : #include "plperl.h"
7 : :
8 : 3 : PG_MODULE_MAGIC_EXT(
9 : : .name = "hstore_plperl",
10 : : .version = PG_VERSION
11 : : );
12 : :
13 : : /* Linkage to functions in hstore module */
14 : : typedef HStore *(*hstoreUpgrade_t) (Datum orig);
15 : : static hstoreUpgrade_t hstoreUpgrade_p;
16 : : typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, Size *buflen);
17 : : static hstoreUniquePairs_t hstoreUniquePairs_p;
18 : : typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, Size buflen);
19 : : static hstorePairs_t hstorePairs_p;
20 : : typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
21 : : static hstoreCheckKeyLen_t hstoreCheckKeyLen_p;
22 : : typedef size_t (*hstoreCheckValLen_t) (size_t len);
23 : : static hstoreCheckValLen_t hstoreCheckValLen_p;
24 : :
25 : : /* Static asserts verify that typedefs above match original declarations */
26 : : StaticAssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
27 : : StaticAssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
28 : : StaticAssertVariableIsOfType(&hstorePairs, hstorePairs_t);
29 : : StaticAssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
30 : : StaticAssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
31 : :
32 : :
33 : : /*
34 : : * Module initialize function: fetch function pointers for cross-module calls.
35 : : */
36 : : void
37 : 3 : _PG_init(void)
38 : : {
39 : 3 : hstoreUpgrade_p = (hstoreUpgrade_t)
40 : 3 : load_external_function("$libdir/hstore", "hstoreUpgrade",
41 : : true, NULL);
42 : 3 : hstoreUniquePairs_p = (hstoreUniquePairs_t)
43 : 3 : load_external_function("$libdir/hstore", "hstoreUniquePairs",
44 : : true, NULL);
45 : 3 : hstorePairs_p = (hstorePairs_t)
46 : 3 : load_external_function("$libdir/hstore", "hstorePairs",
47 : : true, NULL);
48 : 3 : hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
49 : 3 : load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
50 : : true, NULL);
51 : 3 : hstoreCheckValLen_p = (hstoreCheckValLen_t)
52 : 3 : load_external_function("$libdir/hstore", "hstoreCheckValLen",
53 : : true, NULL);
54 : 3 : }
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 : :
65 : 5 : PG_FUNCTION_INFO_V1(hstore_to_plperl);
66 : :
67 : : Datum
68 : 7 : hstore_to_plperl(PG_FUNCTION_ARGS)
69 : : {
70 : 7 : dTHX;
71 : 7 : HStore *in = PG_GETARG_HSTORE_P(0);
72 : : int i;
73 : 7 : int count = HS_COUNT(in);
74 : 7 : char *base = STRPTR(in);
75 : 7 : HEntry *entries = ARRPTR(in);
76 : : HV *hv;
77 : :
78 : 7 : hv = newHV();
79 : :
80 [ + + ]: 20 : for (i = 0; i < count; i++)
81 : : {
82 : : const char *key;
83 : : SV *value;
84 : :
85 [ + + ]: 13 : key = pnstrdup(HSTORE_KEY(entries, base, i),
86 [ + + ]: 13 : HSTORE_KEYLEN(entries, i));
87 [ + + ]: 13 : value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
88 [ + - ]: 7 : cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
89 [ - + ]: 7 : HSTORE_VALLEN(entries, i)));
90 : :
91 : 13 : (void) hv_store(hv, key, strlen(key), value, 0);
92 : : }
93 : :
94 : 7 : return PointerGetDatum(newRV((SV *) hv));
95 : : }
96 : :
97 : :
98 : 6 : PG_FUNCTION_INFO_V1(plperl_to_hstore);
99 : :
100 : : Datum
101 : 8 : plperl_to_hstore(PG_FUNCTION_ARGS)
102 : : {
103 : 8 : dTHX;
104 : 8 : SV *in = (SV *) PG_GETARG_POINTER(0);
105 : : HV *hv;
106 : : HE *he;
107 : : Size buflen;
108 : : int32 i;
109 : : int32 pcount;
110 : : HStore *out;
111 : : Pairs *pairs;
112 : :
113 : : /* Dereference references recursively. */
114 [ + - ]: 8 : plperl_materialize_sv(in);
115 [ + - + + ]: 15 : while (in && SvROK(in))
116 : : {
117 : : /*
118 : : * It's possible for circular references to make this an infinite
119 : : * loop. Checking for such a situation seems like much more trouble
120 : : * than it's worth, but let's provide a way to break out of the loop.
121 : : */
122 [ - + ]: 7 : CHECK_FOR_INTERRUPTS();
123 : 7 : in = SvRV(in);
124 [ + - ]: 7 : plperl_materialize_sv(in);
125 : : }
126 : :
127 : : /* Now we must have a hash. */
128 [ + - + + ]: 8 : if (!in || SvTYPE(in) != SVt_PVHV)
129 [ + - ]: 2 : ereport(ERROR,
130 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
131 : : errmsg("cannot transform non-hash Perl value to hstore")));
132 : 6 : hv = (HV *) in;
133 : :
134 : 6 : (void) hv_iterinit(hv);
135 : :
136 : 6 : pcount = 64; /* arbitrary initial guess */
137 : 6 : pairs = palloc_array(Pairs, pcount);
138 : :
139 : 6 : i = 0;
140 [ + + ]: 22 : while ((he = hv_iternext(hv)))
141 : : {
142 : 16 : char *key = hek2cstr(he);
143 : 16 : SV *value = hv_iterval(hv, he);
144 : :
145 [ + - ]: 16 : plperl_materialize_sv(value);
146 : :
147 [ - + ]: 16 : if (i >= pcount)
148 : : {
149 : 0 : pcount *= 2;
150 : 0 : pairs = repalloc_array(pairs, Pairs, pcount);
151 : : }
152 : :
153 : 16 : pairs[i].key = key;
154 : 16 : pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
155 : 16 : pairs[i].needfree = true;
156 : :
157 [ + - + + ]: 16 : if (!value || !SvOK(value))
158 : : {
159 : 5 : pairs[i].val = NULL;
160 : 5 : pairs[i].vallen = 0;
161 : 5 : pairs[i].isnull = true;
162 : : }
163 : : else
164 : : {
165 : 11 : pairs[i].val = sv2cstr(value);
166 : 11 : pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
167 : 11 : pairs[i].isnull = false;
168 : : }
169 : :
170 : 16 : i++;
171 : : }
172 : :
173 : 6 : pcount = hstoreUniquePairs(pairs, i, &buflen);
174 : 6 : out = hstorePairs(pairs, pcount, buflen);
175 : 6 : PG_RETURN_POINTER(out);
176 : : }
|