| 1 |
#include "postgres.h" |
| 2 |
|
| 3 |
#include "fmgr.h" |
| 4 |
#include "hstore/hstore.h" |
| 5 |
#include "miscadmin.h" |
| 6 |
#include "plperl.h" |
| 7 |
|
| 8 |
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, int32 *buflen); |
| 17 |
static hstoreUniquePairs_t hstoreUniquePairs_p; |
| 18 |
typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 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 |
|
| 26 |
/* |
| 27 |
* Module initialize function: fetch function pointers for cross-module calls. |
| 28 |
*/ |
| 29 |
void |
| 30 |
_PG_init(void) |
| 31 |
{ |
| 32 |
/* Asserts verify that typedefs above match original declarations */ |
| 33 |
AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t); |
| 34 |
hstoreUpgrade_p = (hstoreUpgrade_t) |
| 35 |
load_external_function("$libdir/hstore", "hstoreUpgrade", |
| 36 |
true, NULL); |
| 37 |
AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t); |
| 38 |
hstoreUniquePairs_p = (hstoreUniquePairs_t) |
| 39 |
load_external_function("$libdir/hstore", "hstoreUniquePairs", |
| 40 |
true, NULL); |
| 41 |
AssertVariableIsOfType(&hstorePairs, hstorePairs_t); |
| 42 |
hstorePairs_p = (hstorePairs_t) |
| 43 |
load_external_function("$libdir/hstore", "hstorePairs", |
| 44 |
true, NULL); |
| 45 |
AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t); |
| 46 |
hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t) |
| 47 |
load_external_function("$libdir/hstore", "hstoreCheckKeyLen", |
| 48 |
true, NULL); |
| 49 |
AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t); |
| 50 |
hstoreCheckValLen_p = (hstoreCheckValLen_t) |
| 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 |
|
| 64 |
PG_FUNCTION_INFO_V1(hstore_to_plperl); |
| 65 |
|
| 66 |
Datum |
| 67 |
hstore_to_plperl(PG_FUNCTION_ARGS) |
| 68 |
{ |
| 69 |
dTHX; |
| 70 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 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 |
|
| 97 |
PG_FUNCTION_INFO_V1(plperl_to_hstore); |
| 98 |
|
| 99 |
Datum |
| 100 |
plperl_to_hstore(PG_FUNCTION_ARGS) |
| 101 |
{ |
| 102 |
dTHX; |
| 103 |
SV *in = (SV *) PG_GETARG_POINTER(0); |
| 104 |
HV *hv; |
| 105 |
HE *he; |
| 106 |
int32 buflen; |
| 107 |
int32 i; |
| 108 |
int32 pcount; |
| 109 |
HStore *out; |
| 110 |
Pairs *pairs; |
| 111 |
|
| 112 |
/* Dereference references recursively. */ |
| 113 |
while (SvROK(in)) |
| 114 |
{ |
| 115 |
/* |
| 116 |
* It's possible for circular references to make this an infinite |
| 117 |
* loop. Checking for such a situation seems like much more trouble |
| 118 |
* than it's worth, but let's provide a way to break out of the loop. |
| 119 |
*/ |
| 120 |
CHECK_FOR_INTERRUPTS(); |
| 121 |
in = SvRV(in); |
| 122 |
} |
| 123 |
|
| 124 |
/* Now we must have a hash. */ |
| 125 |
if (SvTYPE(in) != SVt_PVHV) |
| 126 |
ereport(ERROR, |
| 127 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 128 |
errmsg("cannot transform non-hash Perl value to hstore"))); |
| 129 |
hv = (HV *) in; |
| 130 |
|
| 131 |
(void) hv_iterinit(hv); |
| 132 |
|
| 133 |
pcount = 64; /* arbitrary initial guess */ |
| 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 |
if (i >= pcount) |
| 143 |
{ |
| 144 |
pcount *= 2; |
| 145 |
pairs = repalloc_array(pairs, Pairs, pcount); |
| 146 |
} |
| 147 |
|
| 148 |
pairs[i].key = pstrdup(key); |
| 149 |
pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key)); |
| 150 |
pairs[i].needfree = true; |
| 151 |
|
| 152 |
if (!SvOK(value)) |
| 153 |
{ |
| 154 |
pairs[i].val = NULL; |
| 155 |
pairs[i].vallen = 0; |
| 156 |
pairs[i].isnull = true; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
pairs[i].val = pstrdup(sv2cstr(value)); |
| 161 |
pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val)); |
| 162 |
pairs[i].isnull = false; |
| 163 |
} |
| 164 |
|
| 165 |
i++; |
| 166 |
} |
| 167 |
|
| 168 |
pcount = hstoreUniquePairs(pairs, i, &buflen); |
| 169 |
out = hstorePairs(pairs, pcount, buflen); |
| 170 |
PG_RETURN_POINTER(out); |
| 171 |
} |