PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hstore_plperl.c
Go to the documentation of this file.
1 #include "postgres.h"
2 
3 #undef _
4 
5 #include "fmgr.h"
6 #include "plperl.h"
7 #include "plperl_helpers.h"
8 #include "hstore.h"
9 
11 
12 extern void _PG_init(void);
13 
14 /* Linkage to functions in hstore module */
15 typedef HStore *(*hstoreUpgrade_t) (Datum orig);
17 typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
19 typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
21 typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
23 typedef size_t (*hstoreCheckValLen_t) (size_t len);
25 
26 
27 /*
28  * Module initialize function: fetch function pointers for cross-module calls.
29  */
30 void
31 _PG_init(void)
32 {
33  /* Asserts verify that typedefs above match original declarations */
36  load_external_function("$libdir/hstore", "hstoreUpgrade",
37  true, NULL);
40  load_external_function("$libdir/hstore", "hstoreUniquePairs",
41  true, NULL);
44  load_external_function("$libdir/hstore", "hstorePairs",
45  true, NULL);
48  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
49  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 
67 Datum
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 
99 
100 Datum
102 {
103  dTHX;
104  HV *hv = (HV *) SvRV((SV *) PG_GETARG_POINTER(0));
105  HE *he;
106  int32 buflen;
107  int32 i;
108  int32 pcount;
109  HStore *out;
110  Pairs *pairs;
111 
112  pcount = hv_iterinit(hv);
113 
114  pairs = palloc(pcount * sizeof(Pairs));
115 
116  i = 0;
117  while ((he = hv_iternext(hv)))
118  {
119  char *key = sv2cstr(HeSVKEY_force(he));
120  SV *value = HeVAL(he);
121 
122  pairs[i].key = pstrdup(key);
123  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
124  pairs[i].needfree = true;
125 
126  if (!SvOK(value))
127  {
128  pairs[i].val = NULL;
129  pairs[i].vallen = 0;
130  pairs[i].isnull = true;
131  }
132  else
133  {
134  pairs[i].val = pstrdup(sv2cstr(value));
135  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
136  pairs[i].isnull = false;
137  }
138 
139  i++;
140  }
141 
142  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
143  out = hstorePairs(pairs, pcount, buflen);
144  PG_RETURN_POINTER(out);
145 }
size_t(* hstoreCheckValLen_t)(size_t len)
Definition: hstore_plperl.c:23
#define hstorePairs
Definition: hstore_plperl.c:60
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
PG_MODULE_MAGIC
Definition: hstore_plperl.c:10
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1087
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define dTHX
Definition: ppport.h:3208
static hstorePairs_t hstorePairs_p
Definition: hstore_plperl.c:20
#define PointerGetDatum(X)
Definition: postgres.h:562
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
size_t vallen
Definition: hstore.h:166
#define hstoreCheckValLen
Definition: hstore_plperl.c:62
bool needfree
Definition: hstore.h:168
HStore *(* hstoreUpgrade_t)(Datum orig)
Definition: hstore_plperl.c:15
static hstoreCheckValLen_t hstoreCheckValLen_p
Definition: hstore_plperl.c:24
signed int int32
Definition: c.h:246
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
static struct @121 value
#define HS_COUNT(hsp_)
Definition: hstore.h:61
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
Definition: hstore_plperl.c:22
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_plperl.c:17
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
Datum hstore_to_plperl(PG_FUNCTION_ARGS)
Definition: hstore_plperl.c:68
#define hstoreUniquePairs
Definition: hstore_plperl.c:59
PG_FUNCTION_INFO_V1(hstore_to_plperl)
uintptr_t Datum
Definition: postgres.h:372
#define hstoreUpgrade
Definition: hstore_plperl.c:58
static SV * cstr2sv(const char *str)
size_t keylen
Definition: hstore.h:165
bool isnull
Definition: hstore.h:167
#define hstoreCheckKeyLen
Definition: hstore_plperl.c:61
Definition: hstore.h:18
void _PG_init(void)
Definition: hstore_plperl.c:31
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
Datum plperl_to_hstore(PG_FUNCTION_ARGS)
void * palloc(Size size)
Definition: mcxt.c:848
#define STRPTR(x)
Definition: hstore.h:76
int i
static hstoreUniquePairs_t hstoreUniquePairs_p
Definition: hstore_plperl.c:18
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define ARRPTR(x)
Definition: cube.c:26
static hstoreUpgrade_t hstoreUpgrade_p
Definition: hstore_plperl.c:16
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_plperl.c:19
Definition: hstore.h:161
long val
Definition: informix.c:689
static char * sv2cstr(SV *sv)
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:772
size_t(* hstoreCheckKeyLen_t)(size_t len)
Definition: hstore_plperl.c:21