Age Owner Branch data TLA Line data Source code
1 : : #include "postgres.h"
2 : :
3 : : #include "fmgr.h"
4 : : #include "hstore/hstore.h"
5 : : #include "plperl.h"
6 : :
164 tgl@sss.pgh.pa.us 7 :CBC 3 : PG_MODULE_MAGIC_EXT(
8 : : .name = "hstore_plperl",
9 : : .version = PG_VERSION
10 : : );
11 : :
12 : : /* Linkage to functions in hstore module */
13 : : typedef HStore *(*hstoreUpgrade_t) (Datum orig);
14 : : static hstoreUpgrade_t hstoreUpgrade_p;
15 : : typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
16 : : static hstoreUniquePairs_t hstoreUniquePairs_p;
17 : : typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
18 : : static hstorePairs_t hstorePairs_p;
19 : : typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
20 : : static hstoreCheckKeyLen_t hstoreCheckKeyLen_p;
21 : : typedef size_t (*hstoreCheckValLen_t) (size_t len);
22 : : static hstoreCheckValLen_t hstoreCheckValLen_p;
23 : :
24 : :
25 : : /*
26 : : * Module initialize function: fetch function pointers for cross-module calls.
27 : : */
28 : : void
3259 29 : 3 : _PG_init(void)
30 : : {
31 : : /* Asserts verify that typedefs above match original declarations */
32 : : AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
33 : 3 : hstoreUpgrade_p = (hstoreUpgrade_t)
34 : 3 : load_external_function("$libdir/hstore", "hstoreUpgrade",
35 : : true, NULL);
36 : : AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
37 : 3 : hstoreUniquePairs_p = (hstoreUniquePairs_t)
38 : 3 : load_external_function("$libdir/hstore", "hstoreUniquePairs",
39 : : true, NULL);
40 : : AssertVariableIsOfType(&hstorePairs, hstorePairs_t);
41 : 3 : hstorePairs_p = (hstorePairs_t)
42 : 3 : load_external_function("$libdir/hstore", "hstorePairs",
43 : : true, NULL);
44 : : AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
45 : 3 : hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
46 : 3 : load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
47 : : true, NULL);
48 : : AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
49 : 3 : hstoreCheckValLen_p = (hstoreCheckValLen_t)
50 : 3 : load_external_function("$libdir/hstore", "hstoreCheckValLen",
51 : : true, NULL);
52 : 3 : }
53 : :
54 : :
55 : : /* These defines must be after the module init function */
56 : : #define hstoreUpgrade hstoreUpgrade_p
57 : : #define hstoreUniquePairs hstoreUniquePairs_p
58 : : #define hstorePairs hstorePairs_p
59 : : #define hstoreCheckKeyLen hstoreCheckKeyLen_p
60 : : #define hstoreCheckValLen hstoreCheckValLen_p
61 : :
62 : :
3786 peter_e@gmx.net 63 : 5 : PG_FUNCTION_INFO_V1(hstore_to_plperl);
64 : :
65 : : Datum
66 : 7 : hstore_to_plperl(PG_FUNCTION_ARGS)
67 : : {
2962 tgl@sss.pgh.pa.us 68 : 7 : dTHX;
2910 69 : 7 : HStore *in = PG_GETARG_HSTORE_P(0);
70 : : int i;
3786 peter_e@gmx.net 71 : 7 : int count = HS_COUNT(in);
72 : 7 : char *base = STRPTR(in);
73 : 7 : HEntry *entries = ARRPTR(in);
74 : : HV *hv;
75 : :
76 : 7 : hv = newHV();
77 : :
78 [ + + ]: 20 : for (i = 0; i < count; i++)
79 : : {
80 : : const char *key;
81 : : SV *value;
82 : :
3579 tgl@sss.pgh.pa.us 83 [ + + ]: 13 : key = pnstrdup(HSTORE_KEY(entries, base, i),
84 [ + + ]: 13 : HSTORE_KEYLEN(entries, i));
85 [ + + ]: 13 : value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
86 [ + - ]: 7 : cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
87 [ - + ]: 7 : HSTORE_VALLEN(entries, i)));
88 : :
3786 peter_e@gmx.net 89 : 13 : (void) hv_store(hv, key, strlen(key), value, 0);
90 : : }
91 : :
92 : 7 : return PointerGetDatum(newRV((SV *) hv));
93 : : }
94 : :
95 : :
96 : 6 : PG_FUNCTION_INFO_V1(plperl_to_hstore);
97 : :
98 : : Datum
99 : 7 : plperl_to_hstore(PG_FUNCTION_ARGS)
100 : : {
2962 tgl@sss.pgh.pa.us 101 : 7 : dTHX;
2637 102 : 7 : SV *in = (SV *) PG_GETARG_POINTER(0);
103 : : HV *hv;
104 : : HE *he;
105 : : int32 buflen;
106 : : int32 i;
107 : : int32 pcount;
108 : : HStore *out;
109 : : Pairs *pairs;
110 : :
111 : : /* Dereference references recursively. */
112 [ + + ]: 13 : while (SvROK(in))
113 : 6 : in = SvRV(in);
114 : :
115 : : /* Now we must have a hash. */
116 [ + + ]: 7 : if (SvTYPE(in) != SVt_PVHV)
117 [ + - ]: 2 : ereport(ERROR,
118 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
119 : : errmsg("cannot transform non-hash Perl value to hstore")));
120 : 5 : hv = (HV *) in;
121 : :
3786 peter_e@gmx.net 122 : 5 : pcount = hv_iterinit(hv);
123 : :
124 : 5 : pairs = palloc(pcount * sizeof(Pairs));
125 : :
126 : 5 : i = 0;
127 [ + + ]: 18 : while ((he = hv_iternext(hv)))
128 : : {
3759 bruce@momjian.us 129 [ + - - + ]: 13 : char *key = sv2cstr(HeSVKEY_force(he));
130 : 13 : SV *value = HeVAL(he);
131 : :
3786 peter_e@gmx.net 132 : 13 : pairs[i].key = pstrdup(key);
133 : 13 : pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
134 : 13 : pairs[i].needfree = true;
135 : :
136 [ + + ]: 13 : if (!SvOK(value))
137 : : {
138 : 4 : pairs[i].val = NULL;
139 : 4 : pairs[i].vallen = 0;
140 : 4 : pairs[i].isnull = true;
141 : : }
142 : : else
143 : : {
144 : 9 : pairs[i].val = pstrdup(sv2cstr(value));
145 : 9 : pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
146 : 9 : pairs[i].isnull = false;
147 : : }
148 : :
149 : 13 : i++;
150 : : }
151 : :
152 : 5 : pcount = hstoreUniquePairs(pairs, pcount, &buflen);
153 : 5 : out = hstorePairs(pairs, pcount, buflen);
154 : 5 : PG_RETURN_POINTER(out);
155 : : }
|