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

Go to the source code of this file.

Macros

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

Typedefs

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)
 

Functions

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)
 

Variables

 PG_MODULE_MAGIC
 
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 56 of file hstore_plperl.c.

◆ hstoreCheckValLen

#define hstoreCheckValLen   hstoreCheckValLen_p

Definition at line 57 of file hstore_plperl.c.

◆ hstorePairs

#define hstorePairs   hstorePairs_p

Definition at line 55 of file hstore_plperl.c.

◆ hstoreUniquePairs

#define hstoreUniquePairs   hstoreUniquePairs_p

Definition at line 54 of file hstore_plperl.c.

◆ hstoreUpgrade

#define hstoreUpgrade   hstoreUpgrade_p

Definition at line 53 of file hstore_plperl.c.

Typedef Documentation

◆ hstoreCheckKeyLen_t

typedef size_t(* hstoreCheckKeyLen_t) (size_t len)

Definition at line 16 of file hstore_plperl.c.

◆ hstoreCheckValLen_t

typedef size_t(* hstoreCheckValLen_t) (size_t len)

Definition at line 18 of file hstore_plperl.c.

◆ hstorePairs_t

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

Definition at line 14 of file hstore_plperl.c.

◆ hstoreUniquePairs_t

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

Definition at line 12 of file hstore_plperl.c.

◆ hstoreUpgrade_t

typedef HStore*(* hstoreUpgrade_t) (Datum orig)

Definition at line 10 of file hstore_plperl.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 26 of file hstore_plperl.c.

27 {
28  /* Asserts verify that typedefs above match original declarations */
31  load_external_function("$libdir/hstore", "hstoreUpgrade",
32  true, NULL);
35  load_external_function("$libdir/hstore", "hstoreUniquePairs",
36  true, NULL);
39  load_external_function("$libdir/hstore", "hstorePairs",
40  true, NULL);
43  load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
44  true, NULL);
47  load_external_function("$libdir/hstore", "hstoreCheckValLen",
48  true, NULL);
49 }
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:981
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
#define hstoreUpgrade
Definition: hstore_plperl.c:53
HStore *(* hstoreUpgrade_t)(Datum orig)
Definition: hstore_plperl.c:10
int(* hstoreUniquePairs_t)(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_plperl.c:12
static hstoreUpgrade_t hstoreUpgrade_p
Definition: hstore_plperl.c:11
static hstoreCheckKeyLen_t hstoreCheckKeyLen_p
Definition: hstore_plperl.c:17
#define hstoreCheckKeyLen
Definition: hstore_plperl.c:56
#define hstoreUniquePairs
Definition: hstore_plperl.c:54
HStore *(* hstorePairs_t)(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_plperl.c:14
static hstorePairs_t hstorePairs_p
Definition: hstore_plperl.c:15
#define hstorePairs
Definition: hstore_plperl.c:55
static hstoreCheckValLen_t hstoreCheckValLen_p
Definition: hstore_plperl.c:19
static hstoreUniquePairs_t hstoreUniquePairs_p
Definition: hstore_plperl.c:13
size_t(* hstoreCheckValLen_t)(size_t len)
Definition: hstore_plperl.c:18
#define hstoreCheckValLen
Definition: hstore_plperl.c:57
size_t(* hstoreCheckKeyLen_t)(size_t len)
Definition: hstore_plperl.c:16

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

◆ hstore_to_plperl()

Datum hstore_to_plperl ( PG_FUNCTION_ARGS  )

Definition at line 63 of file hstore_plperl.c.

64 {
65  dTHX;
66  HStore *in = PG_GETARG_HSTORE_P(0);
67  int i;
68  int count = HS_COUNT(in);
69  char *base = STRPTR(in);
70  HEntry *entries = ARRPTR(in);
71  HV *hv;
72 
73  hv = newHV();
74 
75  for (i = 0; i < count; i++)
76  {
77  const char *key;
78  SV *value;
79 
80  key = pnstrdup(HSTORE_KEY(entries, base, i),
81  HSTORE_KEYLEN(entries, i));
82  value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
83  cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
84  HSTORE_VALLEN(entries, i)));
85 
86  (void) hv_store(hv, key, strlen(key), value, 0);
87  }
88 
89  return PointerGetDatum(newRV((SV *) hv));
90 }
#define ARRPTR(x)
Definition: cube.c:25
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
#define STRPTR(x)
Definition: hstore.h:76
static struct @155 value
int i
Definition: isn.c:73
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1706
static SV * cstr2sv(const char *str)
Definition: plperl.h:147
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
#define dTHX
Definition: ppport.h:11306
Definition: hstore.h:19
Definition: hstore.h:45

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

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( hstore_to_plperl  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( plperl_to_hstore  )

◆ plperl_to_hstore()

Datum plperl_to_hstore ( PG_FUNCTION_ARGS  )

Definition at line 96 of file hstore_plperl.c.

97 {
98  dTHX;
99  SV *in = (SV *) PG_GETARG_POINTER(0);
100  HV *hv;
101  HE *he;
102  int32 buflen;
103  int32 i;
104  int32 pcount;
105  HStore *out;
106  Pairs *pairs;
107 
108  /* Dereference references recursively. */
109  while (SvROK(in))
110  in = SvRV(in);
111 
112  /* Now we must have a hash. */
113  if (SvTYPE(in) != SVt_PVHV)
114  ereport(ERROR,
115  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
116  errmsg("cannot transform non-hash Perl value to hstore")));
117  hv = (HV *) in;
118 
119  pcount = hv_iterinit(hv);
120 
121  pairs = palloc(pcount * sizeof(Pairs));
122 
123  i = 0;
124  while ((he = hv_iternext(hv)))
125  {
126  char *key = sv2cstr(HeSVKEY_force(he));
127  SV *value = HeVAL(he);
128 
129  pairs[i].key = pstrdup(key);
130  pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
131  pairs[i].needfree = true;
132 
133  if (!SvOK(value))
134  {
135  pairs[i].val = NULL;
136  pairs[i].vallen = 0;
137  pairs[i].isnull = true;
138  }
139  else
140  {
141  pairs[i].val = pstrdup(sv2cstr(value));
142  pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
143  pairs[i].isnull = false;
144  }
145 
146  i++;
147  }
148 
149  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
150  out = hstorePairs(pairs, pcount, buflen);
151  PG_RETURN_POINTER(out);
152 }
signed int int32
Definition: c.h:494
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
long val
Definition: informix.c:670
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
static char * sv2cstr(SV *sv)
Definition: plperl.h:89
Definition: hstore.h:162
char * val
Definition: hstore.h:164
bool isnull
Definition: hstore.h:167
size_t keylen
Definition: hstore.h:165
char * key
Definition: hstore.h:163
bool needfree
Definition: hstore.h:168
size_t vallen
Definition: hstore.h:166

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

Variable Documentation

◆ hstoreCheckKeyLen_p

hstoreCheckKeyLen_t hstoreCheckKeyLen_p
static

Definition at line 17 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreCheckValLen_p

hstoreCheckValLen_t hstoreCheckValLen_p
static

Definition at line 19 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstorePairs_p

hstorePairs_t hstorePairs_p
static

Definition at line 15 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreUniquePairs_p

hstoreUniquePairs_t hstoreUniquePairs_p
static

Definition at line 13 of file hstore_plperl.c.

Referenced by _PG_init().

◆ hstoreUpgrade_p

hstoreUpgrade_t hstoreUpgrade_p
static

Definition at line 11 of file hstore_plperl.c.

Referenced by _PG_init().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 7 of file hstore_plperl.c.