1 #include "postgres.h"
2 
3 #undef _
4 
5 #include "fmgr.h"
6 #include "plperl.h"
7 #include "plperl_helpers.h"
8 #include "hstore/hstore.h"
9 
10 PG_MODULE_MAGIC;
11 
12 extern void _PG_init(void);
13 
14 /* Linkage to functions in hstore module */
15 typedef HStore *(*hstoreUpgrade_t) (Datum orig);
16 static hstoreUpgrade_t hstoreUpgrade_p;
17 typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
18 static hstoreUniquePairs_t hstoreUniquePairs_p;
19 typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
20 static hstorePairs_t hstorePairs_p;
21 typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
22 static hstoreCheckKeyLen_t hstoreCheckKeyLen_p;
23 typedef size_t (*hstoreCheckValLen_t) (size_t len);
24 static hstoreCheckValLen_t hstoreCheckValLen_p;
25 
26 
27 /*
28  * Module initialize function: fetch function pointers for cross-module calls.
29  */
30 void
_PG_init(void)31 _PG_init(void)
32 {
33 	/* Asserts verify that typedefs above match original declarations */
34 	AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
35 	hstoreUpgrade_p = (hstoreUpgrade_t)
36 		load_external_function("$libdir/hstore", "hstoreUpgrade",
37 							   true, NULL);
38 	AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
39 	hstoreUniquePairs_p = (hstoreUniquePairs_t)
40 		load_external_function("$libdir/hstore", "hstoreUniquePairs",
41 							   true, NULL);
42 	AssertVariableIsOfType(&hstorePairs, hstorePairs_t);
43 	hstorePairs_p = (hstorePairs_t)
44 		load_external_function("$libdir/hstore", "hstorePairs",
45 							   true, NULL);
46 	AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
47 	hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
48 		load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
49 							   true, NULL);
50 	AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
51 	hstoreCheckValLen_p = (hstoreCheckValLen_t)
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 
65 PG_FUNCTION_INFO_V1(hstore_to_plperl);
66 
67 Datum
hstore_to_plperl(PG_FUNCTION_ARGS)68 hstore_to_plperl(PG_FUNCTION_ARGS)
69 {
70 	dTHX;
71 	HStore	   *in = PG_GETARG_HSTORE_P(0);
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 
98 PG_FUNCTION_INFO_V1(plperl_to_hstore);
99 
100 Datum
plperl_to_hstore(PG_FUNCTION_ARGS)101 plperl_to_hstore(PG_FUNCTION_ARGS)
102 {
103 	dTHX;
104 	SV		   *in = (SV *) PG_GETARG_POINTER(0);
105 	HV		   *hv;
106 	HE		   *he;
107 	int32		buflen;
108 	int32		i;
109 	int32		pcount;
110 	HStore	   *out;
111 	Pairs	   *pairs;
112 
113 	/* Dereference references recursively. */
114 	while (SvROK(in))
115 		in = SvRV(in);
116 
117 	/* Now we must have a hash. */
118 	if (SvTYPE(in) != SVt_PVHV)
119 		ereport(ERROR,
120 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
121 				 (errmsg("cannot transform non-hash Perl value to hstore"))));
122 	hv = (HV *) in;
123 
124 	pcount = hv_iterinit(hv);
125 
126 	pairs = palloc(pcount * sizeof(Pairs));
127 
128 	i = 0;
129 	while ((he = hv_iternext(hv)))
130 	{
131 		char	   *key = sv2cstr(HeSVKEY_force(he));
132 		SV		   *value = HeVAL(he);
133 
134 		pairs[i].key = pstrdup(key);
135 		pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
136 		pairs[i].needfree = true;
137 
138 		if (!SvOK(value))
139 		{
140 			pairs[i].val = NULL;
141 			pairs[i].vallen = 0;
142 			pairs[i].isnull = true;
143 		}
144 		else
145 		{
146 			pairs[i].val = pstrdup(sv2cstr(value));
147 			pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
148 			pairs[i].isnull = false;
149 		}
150 
151 		i++;
152 	}
153 
154 	pcount = hstoreUniquePairs(pairs, pcount, &buflen);
155 	out = hstorePairs(pairs, pcount, buflen);
156 	PG_RETURN_POINTER(out);
157 }
158