PostgreSQL Source Code  git master
hstore_plperl.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "hstore/hstore.h"
#include "plperl.h"
#include "plperl_helpers.h"
Include dependency graph for hstore_plperl.c:

Go to the source code of this file.


#define hstoreUpgrade   hstoreUpgrade_p
#define hstoreUniquePairs   hstoreUniquePairs_p
#define hstorePairs   hstorePairs_p
#define hstoreCheckKeyLen   hstoreCheckKeyLen_p
#define hstoreCheckValLen   hstoreCheckValLen_p


typedef HStore *(* hstoreUpgrade_t) (Datum orig)
typedef int(* hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen)
typedef HStore *(* hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen)
typedef size_t(* hstoreCheckKeyLen_t) (size_t len)
typedef size_t(* hstoreCheckValLen_t) (size_t len)


void _PG_init (void)
 PG_FUNCTION_INFO_V1 (hstore_to_plperl)
Datum hstore_to_plperl (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (plperl_to_hstore)
Datum plperl_to_hstore (PG_FUNCTION_ARGS)


static hstoreUpgrade_t hstoreUpgrade_p
static hstoreUniquePairs_t hstoreUniquePairs_p
static hstorePairs_t hstorePairs_p
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static hstoreCheckValLen_t hstoreCheckValLen_p

Macro Definition Documentation

◆ hstoreCheckKeyLen

#define hstoreCheckKeyLen   hstoreCheckKeyLen_p

Definition at line 59 of file hstore_plperl.c.

Referenced by _PG_init(), and plperl_to_hstore().

◆ hstoreCheckValLen

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 60 of file hstore_plperl.c.

Referenced by _PG_init(), and plperl_to_hstore().

◆ hstorePairs

#define hstorePairs   hstorePairs_p

Definition at line 58 of file hstore_plperl.c.

Referenced by _PG_init(), hstore_slice_to_hstore(), and plperl_to_hstore().

◆ hstoreUniquePairs

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 57 of file hstore_plperl.c.

Referenced by _PG_init(), hstoreArrayToPairs(), and plperl_to_hstore().

◆ hstoreUpgrade

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 56 of file hstore_plperl.c.

Referenced by _PG_init().

Typedef Documentation

◆ hstoreCheckKeyLen_t

typedef size_t(* hstoreCheckKeyLen_t) (size_t len)

Definition at line 19 of file hstore_plperl.c.

◆ hstoreCheckValLen_t

typedef size_t(* hstoreCheckValLen_t) (size_t len)

Definition at line 21 of file hstore_plperl.c.

◆ hstorePairs_t

typedef HStore*(* hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen)

Definition at line 17 of file hstore_plperl.c.

◆ hstoreUniquePairs_t

typedef int(* hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen)

Definition at line 15 of file hstore_plperl.c.

◆ hstoreUpgrade_t

typedef HStore*(* hstoreUpgrade_t) (Datum orig)

Definition at line 13 of file hstore_plperl.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 29 of file hstore_plperl.c.

References AssertVariableIsOfType, hstoreCheckKeyLen, hstoreCheckKeyLen_p, hstoreCheckValLen, hstoreCheckValLen_p, hstorePairs, hstorePairs_p, hstoreUniquePairs, hstoreUniquePairs_p, hstoreUpgrade, hstoreUpgrade_p, and load_external_function().

30 {
31  /* Asserts verify that typedefs above match original declarations */
34  load_external_function("$libdir/hstore", "hstoreUpgrade",
35  true, NULL);
38  load_external_function("$libdir/hstore", "hstoreUniquePairs",
39  true, NULL);
42  load_external_function("$libdir/hstore", "hstorePairs",
43  true, NULL);
46  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
47  true, NULL);
50  load_external_function("$libdir/hstore", "hstoreCheckValLen",
51  true, NULL);
52 }
size_t(* hstoreCheckKeyLen_t)(size_t len)
Definition: hstore_plperl.c:19
#define hstorePairs
Definition: hstore_plperl.c:58
static hstorePairs_t hstorePairs_p
Definition: hstore_plperl.c:18
#define hstoreCheckValLen
Definition: hstore_plperl.c:60
static hstoreCheckValLen_t hstoreCheckValLen_p
Definition: hstore_plperl.c:22
size_t(* hstoreCheckValLen_t)(size_t len)
Definition: hstore_plperl.c:21
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
Definition: hstore_plperl.c:20
HStore *(* hstoreUpgrade_t)(Datum orig)
Definition: hstore_plperl.c:13
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:107
#define hstoreUniquePairs
Definition: hstore_plperl.c:57
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_plperl.c:17
#define hstoreUpgrade
Definition: hstore_plperl.c:56
#define hstoreCheckKeyLen
Definition: hstore_plperl.c:59
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_plperl.c:15
static hstoreUniquePairs_t hstoreUniquePairs_p
Definition: hstore_plperl.c:16
static hstoreUpgrade_t hstoreUpgrade_p
Definition: hstore_plperl.c:14
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:897

◆ hstore_to_plperl()

Datum hstore_to_plperl ( PG_FUNCTION_ARGS  )

Definition at line 66 of file hstore_plperl.c.

References ARRPTR, cstr2sv(), dTHX, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, sort-test::key, PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, plperl_to_hstore(), pnstrdup(), PointerGetDatum, STRPTR, and value.

67 {
68  dTHX;
69  HStore *in = PG_GETARG_HSTORE_P(0);
70  int i;
71  int count = HS_COUNT(in);
72  char *base = STRPTR(in);
73  HEntry *entries = ARRPTR(in);
74  HV *hv;
76  hv = newHV();
78  for (i = 0; i < count; i++)
79  {
80  const char *key;
81  SV *value;
83  key = pnstrdup(HSTORE_KEY(entries, base, i),
84  HSTORE_KEYLEN(entries, i));
85  value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
86  cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
87  HSTORE_VALLEN(entries, i)));
89  (void) hv_store(hv, key, strlen(key), value, 0);
90  }
92  return PointerGetDatum(newRV((SV *) hv));
93 }
Definition: hstore.h:44
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
Definition: hstore.h:154
#define dTHX
Definition: ppport.h:3208
#define PointerGetDatum(X)
Definition: postgres.h:556
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
static SV * cstr2sv(const char *str)
static struct @143 value
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:24


PG_FUNCTION_INFO_V1 ( hstore_to_plperl  )

Referenced by hstore_to_plperl().


PG_FUNCTION_INFO_V1 ( plperl_to_hstore  )

◆ plperl_to_hstore()

Datum plperl_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 99 of file hstore_plperl.c.

References dTHX, ereport, errcode(), errmsg(), ERROR, hstoreCheckKeyLen, hstoreCheckValLen, hstorePairs, hstoreUniquePairs, i, Pairs::isnull, sort-test::key, Pairs::key, Pairs::keylen, Pairs::needfree, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, pstrdup(), sv2cstr(), Pairs::val, val, Pairs::vallen, and value.

Referenced by hstore_to_plperl().

100 {
101  dTHX;
102  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;
111  /* Dereference references recursively. */
112  while (SvROK(in))
113  in = SvRV(in);
115  /* Now we must have a hash. */
116  if (SvTYPE(in) != SVt_PVHV)
117  ereport(ERROR,
119  errmsg("cannot transform non-hash Perl value to hstore")));
120  hv = (HV *) in;
122  pcount = hv_iterinit(hv);
124  pairs = palloc(pcount * sizeof(Pairs));
126  i = 0;
127  while ((he = hv_iternext(hv)))
128  {
129  char *key = sv2cstr(HeSVKEY_force(he));
130  SV *value = HeVAL(he);
132  pairs[i].key = pstrdup(key);
133  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
134  pairs[i].needfree = true;
136  if (!SvOK(value))
137  {
138  pairs[i].val = NULL;
139  pairs[i].vallen = 0;
140  pairs[i].isnull = true;
141  }
142  else
143  {
144  pairs[i].val = pstrdup(sv2cstr(value));
145  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
146  pairs[i].isnull = false;
147  }
149  i++;
150  }
152  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
153  out = hstorePairs(pairs, pcount, buflen);
155 }
#define hstorePairs
Definition: hstore_plperl.c:58
Definition: fmgr.h:360
Definition: hstore.h:44
#define dTHX
Definition: ppport.h:3208
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: fmgr.h:276
size_t vallen
Definition: hstore.h:166
#define hstoreCheckValLen
Definition: hstore_plperl.c:60
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:355
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define ERROR
Definition: elog.h:43
#define hstoreUniquePairs
Definition: hstore_plperl.c:57
size_t keylen
Definition: hstore.h:165
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:144
bool isnull
Definition: hstore.h:167
#define hstoreCheckKeyLen
Definition: hstore_plperl.c:59
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
Definition: hstore.h:161
long val
Definition: informix.c:664
static char * sv2cstr(SV *sv)

Variable Documentation

◆ hstoreCheckKeyLen_p

hstoreCheckKeyLen_t hstoreCheckKeyLen_p

Definition at line 20 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreCheckValLen_p

hstoreCheckValLen_t hstoreCheckValLen_p

Definition at line 22 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstorePairs_p

hstorePairs_t hstorePairs_p

Definition at line 18 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreUniquePairs_p

hstoreUniquePairs_t hstoreUniquePairs_p

Definition at line 16 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreUpgrade_p

hstoreUpgrade_t hstoreUpgrade_p

Definition at line 14 of file hstore_plperl.c.

Referenced by _PG_init().



Definition at line 8 of file hstore_plperl.c.