PostgreSQL Source Code  git master
hstore_io.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/json.h"
#include "utils/jsonapi.h"
#include "utils/jsonb.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/typcache.h"
#include "hstore.h"
Include dependency graph for hstore_io.c:

Go to the source code of this file.

Data Structures

struct  HSParser
 
struct  ColumnIOData
 
struct  RecordIOData
 

Macros

#define RESIZEPRSBUF
 
#define GV_WAITVAL   0
 
#define GV_INVAL   1
 
#define GV_INESCVAL   2
 
#define GV_WAITESCIN   3
 
#define GV_WAITESCESCIN   4
 
#define WKEY   0
 
#define WVAL   1
 
#define WEQ   2
 
#define WGT   3
 
#define WDEL   4
 

Typedefs

typedef struct ColumnIOData ColumnIOData
 
typedef struct RecordIOData RecordIOData
 

Functions

 HSTORE_POLLUTE (hstore_from_text, tconvert)
 
static bool get_val (HSParser *state, bool ignoreeq, bool *escaped)
 
static void parse_hstore (HSParser *state)
 
static int comparePairs (const void *a, const void *b)
 
int hstoreUniquePairs (Pairs *a, int32 l, int32 *buflen)
 
size_t hstoreCheckKeyLen (size_t len)
 
size_t hstoreCheckValLen (size_t len)
 
HStorehstorePairs (Pairs *pairs, int32 pcount, int32 buflen)
 
 PG_FUNCTION_INFO_V1 (hstore_in)
 
Datum hstore_in (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_recv)
 
Datum hstore_recv (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_from_text)
 
Datum hstore_from_text (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_from_arrays)
 
Datum hstore_from_arrays (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_from_array)
 
Datum hstore_from_array (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_from_record)
 
Datum hstore_from_record (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_populate_record)
 
Datum hstore_populate_record (PG_FUNCTION_ARGS)
 
static char * cpw (char *dst, char *src, int len)
 
 PG_FUNCTION_INFO_V1 (hstore_out)
 
Datum hstore_out (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_send)
 
Datum hstore_send (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_to_json_loose)
 
Datum hstore_to_json_loose (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_to_json)
 
Datum hstore_to_json (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_to_jsonb)
 
Datum hstore_to_jsonb (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (hstore_to_jsonb_loose)
 
Datum hstore_to_jsonb_loose (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ GV_INESCVAL

#define GV_INESCVAL   2

Definition at line 56 of file hstore_io.c.

Referenced by get_val().

◆ GV_INVAL

#define GV_INVAL   1

Definition at line 55 of file hstore_io.c.

Referenced by get_val().

◆ GV_WAITESCESCIN

#define GV_WAITESCESCIN   4

Definition at line 58 of file hstore_io.c.

Referenced by get_val().

◆ GV_WAITESCIN

#define GV_WAITESCIN   3

Definition at line 57 of file hstore_io.c.

Referenced by get_val().

◆ GV_WAITVAL

#define GV_WAITVAL   0

Definition at line 54 of file hstore_io.c.

Referenced by get_val().

◆ RESIZEPRSBUF

#define RESIZEPRSBUF
Value:
do { \
if ( state->cur - state->word + 1 >= state->wordlen ) \
{ \
int32 clen = state->cur - state->word; \
state->wordlen *= 2; \
state->word = (char*)repalloc( (void*)state->word, state->wordlen ); \
state->cur = state->word + clen; \
} \
} while (0)
Definition: regguts.h:298
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962

Definition at line 42 of file hstore_io.c.

Referenced by get_val().

◆ WDEL

#define WDEL   4

Definition at line 179 of file hstore_io.c.

Referenced by parse_hstore().

◆ WEQ

#define WEQ   2

Definition at line 177 of file hstore_io.c.

Referenced by parse_hstore().

◆ WGT

#define WGT   3

Definition at line 178 of file hstore_io.c.

Referenced by parse_hstore().

◆ WKEY

#define WKEY   0

Definition at line 175 of file hstore_io.c.

Referenced by parse_hstore().

◆ WVAL

#define WVAL   1

Definition at line 176 of file hstore_io.c.

Referenced by parse_hstore().

Typedef Documentation

◆ ColumnIOData

◆ RecordIOData

Function Documentation

◆ comparePairs()

static int comparePairs ( const void *  a,
const void *  b 
)
static

Definition at line 282 of file hstore_io.c.

References Pairs::key, Pairs::keylen, and Pairs::needfree.

Referenced by hstoreUniquePairs().

283 {
284  const Pairs *pa = a;
285  const Pairs *pb = b;
286 
287  if (pa->keylen == pb->keylen)
288  {
289  int res = memcmp(pa->key, pb->key, pa->keylen);
290 
291  if (res)
292  return res;
293 
294  /* guarantee that needfree will be later */
295  if (pb->needfree == pa->needfree)
296  return 0;
297  else if (pa->needfree)
298  return 1;
299  else
300  return -1;
301  }
302  return (pa->keylen > pb->keylen) ? 1 : -1;
303 }
bool needfree
Definition: hstore.h:168
char * key
Definition: hstore.h:163
size_t keylen
Definition: hstore.h:165
Definition: hstore.h:161

◆ cpw()

static char* cpw ( char *  dst,
char *  src,
int  len 
)
static

Definition at line 1139 of file hstore_io.c.

References hstore_out(), and PG_FUNCTION_INFO_V1().

Referenced by hstore_out().

1140 {
1141  char *ptr = src;
1142 
1143  while (ptr - src < len)
1144  {
1145  if (*ptr == '"' || *ptr == '\\')
1146  *dst++ = '\\';
1147  *dst++ = *ptr++;
1148  }
1149  return dst;
1150 }

◆ get_val()

static bool get_val ( HSParser state,
bool  ignoreeq,
bool escaped 
)
static

Definition at line 61 of file hstore_io.c.

References HSParser::begin, HSParser::cur, elog, ERROR, GV_INESCVAL, GV_INVAL, GV_WAITESCESCIN, GV_WAITESCIN, GV_WAITVAL, palloc(), HSParser::ptr, RESIZEPRSBUF, HSParser::word, and HSParser::wordlen.

Referenced by parse_hstore().

62 {
63  int st = GV_WAITVAL;
64 
65  state->wordlen = 32;
66  state->cur = state->word = palloc(state->wordlen);
67  *escaped = false;
68 
69  while (1)
70  {
71  if (st == GV_WAITVAL)
72  {
73  if (*(state->ptr) == '"')
74  {
75  *escaped = true;
76  st = GV_INESCVAL;
77  }
78  else if (*(state->ptr) == '\0')
79  {
80  return false;
81  }
82  else if (*(state->ptr) == '=' && !ignoreeq)
83  {
84  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
85  }
86  else if (*(state->ptr) == '\\')
87  {
88  st = GV_WAITESCIN;
89  }
90  else if (!isspace((unsigned char) *(state->ptr)))
91  {
92  *(state->cur) = *(state->ptr);
93  state->cur++;
94  st = GV_INVAL;
95  }
96  }
97  else if (st == GV_INVAL)
98  {
99  if (*(state->ptr) == '\\')
100  {
101  st = GV_WAITESCIN;
102  }
103  else if (*(state->ptr) == '=' && !ignoreeq)
104  {
105  state->ptr--;
106  return true;
107  }
108  else if (*(state->ptr) == ',' && ignoreeq)
109  {
110  state->ptr--;
111  return true;
112  }
113  else if (isspace((unsigned char) *(state->ptr)))
114  {
115  return true;
116  }
117  else if (*(state->ptr) == '\0')
118  {
119  state->ptr--;
120  return true;
121  }
122  else
123  {
124  RESIZEPRSBUF;
125  *(state->cur) = *(state->ptr);
126  state->cur++;
127  }
128  }
129  else if (st == GV_INESCVAL)
130  {
131  if (*(state->ptr) == '\\')
132  {
133  st = GV_WAITESCESCIN;
134  }
135  else if (*(state->ptr) == '"')
136  {
137  return true;
138  }
139  else if (*(state->ptr) == '\0')
140  {
141  elog(ERROR, "Unexpected end of string");
142  }
143  else
144  {
145  RESIZEPRSBUF;
146  *(state->cur) = *(state->ptr);
147  state->cur++;
148  }
149  }
150  else if (st == GV_WAITESCIN)
151  {
152  if (*(state->ptr) == '\0')
153  elog(ERROR, "Unexpected end of string");
154  RESIZEPRSBUF;
155  *(state->cur) = *(state->ptr);
156  state->cur++;
157  st = GV_INVAL;
158  }
159  else if (st == GV_WAITESCESCIN)
160  {
161  if (*(state->ptr) == '\0')
162  elog(ERROR, "Unexpected end of string");
163  RESIZEPRSBUF;
164  *(state->cur) = *(state->ptr);
165  state->cur++;
166  st = GV_INESCVAL;
167  }
168  else
169  elog(ERROR, "Unknown state %d at position line %d in file '%s'", st, __LINE__, __FILE__);
170 
171  state->ptr++;
172  }
173 }
#define GV_WAITVAL
Definition: hstore_io.c:54
char * word
Definition: hstore_io.c:34
signed int int32
Definition: c.h:284
#define ERROR
Definition: elog.h:43
char * cur
Definition: hstore_io.c:33
#define GV_INVAL
Definition: hstore_io.c:55
#define GV_INESCVAL
Definition: hstore_io.c:56
#define RESIZEPRSBUF
Definition: hstore_io.c:42
#define GV_WAITESCESCIN
Definition: hstore_io.c:58
char * begin
Definition: hstore_io.c:31
char * ptr
Definition: hstore_io.c:32
void * palloc(Size size)
Definition: mcxt.c:848
int wordlen
Definition: hstore_io.c:35
#define GV_WAITESCIN
Definition: hstore_io.c:57
#define elog
Definition: elog.h:219

◆ hstore_from_array()

Datum hstore_from_array ( PG_FUNCTION_ARGS  )

Definition at line 645 of file hstore_io.c.

References ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, Assert, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, hstoreCheckKeyLen(), hstoreCheckValLen(), hstorePairs(), hstoreUniquePairs(), i, Pairs::isnull, Pairs::key, Pairs::keylen, MaxAllocSize, Pairs::needfree, palloc(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_POINTER, TEXTOID, Pairs::val, Pairs::vallen, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by hstore_from_arrays().

646 {
647  ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
648  int ndims = ARR_NDIM(in_array);
649  int count;
650  int32 buflen;
651  HStore *out;
652  Pairs *pairs;
653  Datum *in_datums;
654  bool *in_nulls;
655  int in_count;
656  int i;
657 
658  Assert(ARR_ELEMTYPE(in_array) == TEXTOID);
659 
660  switch (ndims)
661  {
662  case 0:
663  out = hstorePairs(NULL, 0, 0);
664  PG_RETURN_POINTER(out);
665 
666  case 1:
667  if ((ARR_DIMS(in_array)[0]) % 2)
668  ereport(ERROR,
669  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
670  errmsg("array must have even number of elements")));
671  break;
672 
673  case 2:
674  if ((ARR_DIMS(in_array)[1]) != 2)
675  ereport(ERROR,
676  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
677  errmsg("array must have two columns")));
678  break;
679 
680  default:
681  ereport(ERROR,
682  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
683  errmsg("wrong number of array subscripts")));
684  }
685 
686  deconstruct_array(in_array,
687  TEXTOID, -1, false, 'i',
688  &in_datums, &in_nulls, &in_count);
689 
690  count = in_count / 2;
691 
692  /* see discussion in hstoreArrayToPairs() */
693  if (count > MaxAllocSize / sizeof(Pairs))
694  ereport(ERROR,
695  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
696  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
697  count, (int) (MaxAllocSize / sizeof(Pairs)))));
698 
699  pairs = palloc(count * sizeof(Pairs));
700 
701  for (i = 0; i < count; ++i)
702  {
703  if (in_nulls[i * 2])
704  ereport(ERROR,
705  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
706  errmsg("null value not allowed for hstore key")));
707 
708  if (in_nulls[i * 2 + 1])
709  {
710  pairs[i].key = VARDATA(in_datums[i * 2]);
711  pairs[i].val = NULL;
712  pairs[i].keylen =
713  hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
714  pairs[i].vallen = 4;
715  pairs[i].isnull = true;
716  pairs[i].needfree = false;
717  }
718  else
719  {
720  pairs[i].key = VARDATA(in_datums[i * 2]);
721  pairs[i].val = VARDATA(in_datums[i * 2 + 1]);
722  pairs[i].keylen =
723  hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
724  pairs[i].vallen =
725  hstoreCheckValLen(VARSIZE(in_datums[i * 2 + 1]) - VARHDRSZ);
726  pairs[i].isnull = false;
727  pairs[i].needfree = false;
728  }
729  }
730 
731  count = hstoreUniquePairs(pairs, count, &buflen);
732 
733  out = hstorePairs(pairs, count, buflen);
734 
735  PG_RETURN_POINTER(out);
736 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
#define VARDATA(PTR)
Definition: postgres.h:303
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:493
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
int errcode(int sqlerrcode)
Definition: elog.c:575
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:284
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:279
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
#define ereport(elevel, rest)
Definition: elog.h:122
#define MaxAllocSize
Definition: memutils.h:40
uintptr_t Datum
Definition: postgres.h:372
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
size_t keylen
Definition: hstore.h:165
#define Assert(condition)
Definition: c.h:670
bool isnull
Definition: hstore.h:167
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: hstore.h:161
#define ARR_ELEMTYPE(a)
Definition: array.h:277

◆ hstore_from_arrays()

Datum hstore_from_arrays ( PG_FUNCTION_ARGS  )

Definition at line 525 of file hstore_io.c.

References ARR_DIMS, ARR_ELEMTYPE, ARR_LBOUND, ARR_NDIM, Assert, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, hstore_from_array(), hstoreCheckKeyLen(), hstoreCheckValLen(), hstorePairs(), hstoreUniquePairs(), i, Pairs::isnull, Pairs::key, Pairs::keylen, MaxAllocSize, Pairs::needfree, palloc(), PG_ARGISNULL, PG_FUNCTION_INFO_V1(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_NULL, PG_RETURN_POINTER, TEXTOID, Pairs::val, Pairs::vallen, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by hstore_from_text().

526 {
527  int32 buflen;
528  HStore *out;
529  Pairs *pairs;
530  Datum *key_datums;
531  bool *key_nulls;
532  int key_count;
533  Datum *value_datums;
534  bool *value_nulls;
535  int value_count;
536  ArrayType *key_array;
537  ArrayType *value_array;
538  int i;
539 
540  if (PG_ARGISNULL(0))
541  PG_RETURN_NULL();
542 
543  key_array = PG_GETARG_ARRAYTYPE_P(0);
544 
545  Assert(ARR_ELEMTYPE(key_array) == TEXTOID);
546 
547  /*
548  * must check >1 rather than != 1 because empty arrays have 0 dimensions,
549  * not 1
550  */
551 
552  if (ARR_NDIM(key_array) > 1)
553  ereport(ERROR,
554  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
555  errmsg("wrong number of array subscripts")));
556 
557  deconstruct_array(key_array,
558  TEXTOID, -1, false, 'i',
559  &key_datums, &key_nulls, &key_count);
560 
561  /* see discussion in hstoreArrayToPairs() */
562  if (key_count > MaxAllocSize / sizeof(Pairs))
563  ereport(ERROR,
564  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
565  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
566  key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
567 
568  /* value_array might be NULL */
569 
570  if (PG_ARGISNULL(1))
571  {
572  value_array = NULL;
573  value_count = key_count;
574  value_datums = NULL;
575  value_nulls = NULL;
576  }
577  else
578  {
579  value_array = PG_GETARG_ARRAYTYPE_P(1);
580 
581  Assert(ARR_ELEMTYPE(value_array) == TEXTOID);
582 
583  if (ARR_NDIM(value_array) > 1)
584  ereport(ERROR,
585  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
586  errmsg("wrong number of array subscripts")));
587 
588  if ((ARR_NDIM(key_array) > 0 || ARR_NDIM(value_array) > 0) &&
589  (ARR_NDIM(key_array) != ARR_NDIM(value_array) ||
590  ARR_DIMS(key_array)[0] != ARR_DIMS(value_array)[0] ||
591  ARR_LBOUND(key_array)[0] != ARR_LBOUND(value_array)[0]))
592  ereport(ERROR,
593  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
594  errmsg("arrays must have same bounds")));
595 
596  deconstruct_array(value_array,
597  TEXTOID, -1, false, 'i',
598  &value_datums, &value_nulls, &value_count);
599 
600  Assert(key_count == value_count);
601  }
602 
603  pairs = palloc(key_count * sizeof(Pairs));
604 
605  for (i = 0; i < key_count; ++i)
606  {
607  if (key_nulls[i])
608  ereport(ERROR,
609  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
610  errmsg("null value not allowed for hstore key")));
611 
612  if (!value_nulls || value_nulls[i])
613  {
614  pairs[i].key = VARDATA(key_datums[i]);
615  pairs[i].val = NULL;
616  pairs[i].keylen =
617  hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
618  pairs[i].vallen = 4;
619  pairs[i].isnull = true;
620  pairs[i].needfree = false;
621  }
622  else
623  {
624  pairs[i].key = VARDATA(key_datums[i]);
625  pairs[i].val = VARDATA(value_datums[i]);
626  pairs[i].keylen =
627  hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
628  pairs[i].vallen =
629  hstoreCheckValLen(VARSIZE(value_datums[i]) - VARHDRSZ);
630  pairs[i].isnull = false;
631  pairs[i].needfree = false;
632  }
633  }
634 
635  key_count = hstoreUniquePairs(pairs, key_count, &buflen);
636 
637  out = hstorePairs(pairs, key_count, buflen);
638 
639  PG_RETURN_POINTER(out);
640 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
#define VARDATA(PTR)
Definition: postgres.h:303
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:493
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
int errcode(int sqlerrcode)
Definition: elog.c:575
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:284
char * val
Definition: hstore.h:164
#define ARR_LBOUND(a)
Definition: array.h:281
char * key
Definition: hstore.h:163
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:279
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
#define ereport(elevel, rest)
Definition: elog.h:122
#define MaxAllocSize
Definition: memutils.h:40
uintptr_t Datum
Definition: postgres.h:372
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
size_t keylen
Definition: hstore.h:165
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:670
bool isnull
Definition: hstore.h:167
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Definition: hstore.h:161
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ hstore_from_record()

Datum hstore_from_record ( PG_FUNCTION_ARGS  )

Definition at line 763 of file hstore_io.c.

References Assert, ColumnIOData::column_type, RecordIOData::columns, fmgr_info_cxt(), FmgrInfo::fn_mcxt, get_fn_expr_argtype(), getTypeOutputInfo(), heap_deform_tuple(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, hstore_populate_record(), hstoreCheckKeyLen(), hstoreCheckValLen(), hstorePairs(), hstoreUniquePairs(), i, InvalidOid, Pairs::isnull, ItemPointerSetInvalid, Pairs::key, Pairs::keylen, lookup_rowtype_tupdesc_domain(), MaxTupleAttributeNumber, MemoryContextAlloc(), MemSet, NameStr, tupleDesc::natts, RecordIOData::ncolumns, Pairs::needfree, offsetof, OutputFunctionCall(), palloc(), PG_ARGISNULL, PG_FUNCTION_INFO_V1(), PG_GETARG_HEAPTUPLEHEADER, PG_RETURN_POINTER, ColumnIOData::proc, RecordIOData::record_type, RecordIOData::record_typmod, ReleaseTupleDesc, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TupleDescAttr, ColumnIOData::typiofunc, Pairs::val, Pairs::vallen, value, and values.

764 {
765  HeapTupleHeader rec;
766  int32 buflen;
767  HStore *out;
768  Pairs *pairs;
769  Oid tupType;
770  int32 tupTypmod;
771  TupleDesc tupdesc;
772  HeapTupleData tuple;
773  RecordIOData *my_extra;
774  int ncolumns;
775  int i,
776  j;
777  Datum *values;
778  bool *nulls;
779 
780  if (PG_ARGISNULL(0))
781  {
782  Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
783 
784  /*
785  * We have no tuple to look at, so the only source of type info is the
786  * argtype --- which might be domain over composite, but we don't care
787  * here, since we have no need to be concerned about domain
788  * constraints. The lookup_rowtype_tupdesc_domain call below will
789  * error out if we don't have a known composite type oid here.
790  */
791  tupType = argtype;
792  tupTypmod = -1;
793 
794  rec = NULL;
795  }
796  else
797  {
798  rec = PG_GETARG_HEAPTUPLEHEADER(0);
799 
800  /*
801  * Extract type info from the tuple itself -- this will work even for
802  * anonymous record types.
803  */
804  tupType = HeapTupleHeaderGetTypeId(rec);
805  tupTypmod = HeapTupleHeaderGetTypMod(rec);
806  }
807 
808  tupdesc = lookup_rowtype_tupdesc_domain(tupType, tupTypmod, false);
809  ncolumns = tupdesc->natts;
810 
811  /*
812  * We arrange to look up the needed I/O info just once per series of
813  * calls, assuming the record type doesn't change underneath us.
814  */
815  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
816  if (my_extra == NULL ||
817  my_extra->ncolumns != ncolumns)
818  {
819  fcinfo->flinfo->fn_extra =
820  MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
821  offsetof(RecordIOData, columns) +
822  ncolumns * sizeof(ColumnIOData));
823  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
824  my_extra->record_type = InvalidOid;
825  my_extra->record_typmod = 0;
826  }
827 
828  if (my_extra->record_type != tupType ||
829  my_extra->record_typmod != tupTypmod)
830  {
831  MemSet(my_extra, 0,
832  offsetof(RecordIOData, columns) +
833  ncolumns * sizeof(ColumnIOData));
834  my_extra->record_type = tupType;
835  my_extra->record_typmod = tupTypmod;
836  my_extra->ncolumns = ncolumns;
837  }
838 
839  Assert(ncolumns <= MaxTupleAttributeNumber); /* thus, no overflow */
840  pairs = palloc(ncolumns * sizeof(Pairs));
841 
842  if (rec)
843  {
844  /* Build a temporary HeapTuple control structure */
846  ItemPointerSetInvalid(&(tuple.t_self));
847  tuple.t_tableOid = InvalidOid;
848  tuple.t_data = rec;
849 
850  values = (Datum *) palloc(ncolumns * sizeof(Datum));
851  nulls = (bool *) palloc(ncolumns * sizeof(bool));
852 
853  /* Break down the tuple into fields */
854  heap_deform_tuple(&tuple, tupdesc, values, nulls);
855  }
856  else
857  {
858  values = NULL;
859  nulls = NULL;
860  }
861 
862  for (i = 0, j = 0; i < ncolumns; ++i)
863  {
864  ColumnIOData *column_info = &my_extra->columns[i];
865  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
866  Oid column_type = att->atttypid;
867  char *value;
868 
869  /* Ignore dropped columns in datatype */
870  if (att->attisdropped)
871  continue;
872 
873  pairs[j].key = NameStr(att->attname);
874  pairs[j].keylen = hstoreCheckKeyLen(strlen(NameStr(att->attname)));
875 
876  if (!nulls || nulls[i])
877  {
878  pairs[j].val = NULL;
879  pairs[j].vallen = 4;
880  pairs[j].isnull = true;
881  pairs[j].needfree = false;
882  ++j;
883  continue;
884  }
885 
886  /*
887  * Convert the column value to text
888  */
889  if (column_info->column_type != column_type)
890  {
891  bool typIsVarlena;
892 
893  getTypeOutputInfo(column_type,
894  &column_info->typiofunc,
895  &typIsVarlena);
896  fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
897  fcinfo->flinfo->fn_mcxt);
898  column_info->column_type = column_type;
899  }
900 
901  value = OutputFunctionCall(&column_info->proc, values[i]);
902 
903  pairs[j].val = value;
904  pairs[j].vallen = hstoreCheckValLen(strlen(value));
905  pairs[j].isnull = false;
906  pairs[j].needfree = false;
907  ++j;
908  }
909 
910  ncolumns = hstoreUniquePairs(pairs, j, &buflen);
911 
912  out = hstorePairs(pairs, ncolumns, buflen);
913 
914  ReleaseTupleDesc(tupdesc);
915 
916  PG_RETURN_POINTER(out);
917 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
FmgrInfo proc
Definition: hstore_io.c:748
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2646
MemoryContext fn_mcxt
Definition: fmgr.h:65
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
Oid record_type
Definition: hstore_io.c:753
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
#define MemSet(start, val, len)
Definition: c.h:853
size_t vallen
Definition: hstore.h:166
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:79
bool needfree
Definition: hstore.h:168
int32 record_typmod
Definition: hstore_io.c:754
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1674
signed int int32
Definition: c.h:284
char * val
Definition: hstore.h:164
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1662
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:460
char * key
Definition: hstore.h:163
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
static struct @121 value
ItemPointerData t_self
Definition: htup.h:65
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
uint32 t_len
Definition: htup.h:64
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
Oid t_tableOid
Definition: htup.h:66
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER]
Definition: hstore_io.c:758
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:450
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
#define InvalidOid
Definition: postgres_ext.h:36
size_t keylen
Definition: hstore.h:165
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:670
bool isnull
Definition: hstore.h:167
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
static Datum values[MAXATTR]
Definition: bootstrap.c:164
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
void * palloc(Size size)
Definition: mcxt.c:848
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
int i
#define NameStr(name)
Definition: c.h:547
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:121
Definition: hstore.h:161
#define offsetof(type, field)
Definition: c.h:593
Oid column_type
Definition: hstore_io.c:745
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:444

◆ hstore_from_text()

Datum hstore_from_text ( PG_FUNCTION_ARGS  )

Definition at line 489 of file hstore_io.c.

References hstore_from_arrays(), hstoreCheckKeyLen(), hstoreCheckValLen(), hstorePairs(), Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, PG_ARGISNULL, PG_FUNCTION_INFO_V1(), PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_POINTER, Pairs::val, val, Pairs::vallen, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by hstore_recv().

490 {
491  text *key;
492  text *val = NULL;
493  Pairs p;
494  HStore *out;
495 
496  if (PG_ARGISNULL(0))
497  PG_RETURN_NULL();
498 
499  p.needfree = false;
500  key = PG_GETARG_TEXT_PP(0);
501  p.key = VARDATA_ANY(key);
503 
504  if (PG_ARGISNULL(1))
505  {
506  p.vallen = 0;
507  p.isnull = true;
508  }
509  else
510  {
511  val = PG_GETARG_TEXT_PP(1);
512  p.val = VARDATA_ANY(val);
514  p.isnull = false;
515  }
516 
517  out = hstorePairs(&p, 1, p.keylen + p.vallen);
518 
519  PG_RETURN_POINTER(out);
520 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
char * val
Definition: hstore.h:164
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
char * key
Definition: hstore.h:163
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
size_t keylen
Definition: hstore.h:165
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
bool isnull
Definition: hstore.h:167
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:487
Definition: hstore.h:161
long val
Definition: informix.c:689
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ hstore_in()

Datum hstore_in ( PG_FUNCTION_ARGS  )

Definition at line 406 of file hstore_io.c.

References HSParser::begin, hstore_recv(), hstorePairs(), hstoreUniquePairs(), HSParser::pairs, parse_hstore(), HSParser::pcur, PG_FUNCTION_INFO_V1(), PG_GETARG_CSTRING, and PG_RETURN_POINTER.

Referenced by hstorePairs().

407 {
408  HSParser state;
409  int32 buflen;
410  HStore *out;
411 
412  state.begin = PG_GETARG_CSTRING(0);
413 
414  parse_hstore(&state);
415 
416  state.pcur = hstoreUniquePairs(state.pairs, state.pcur, &buflen);
417 
418  out = hstorePairs(state.pairs, state.pcur, buflen);
419 
420  PG_RETURN_POINTER(out);
421 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
signed int int32
Definition: c.h:284
static void parse_hstore(HSParser *state)
Definition: hstore_io.c:183
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
int pcur
Definition: hstore_io.c:38
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
Definition: regguts.h:298
Pairs * pairs
Definition: hstore_io.c:37
char * begin
Definition: hstore_io.c:31
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242

◆ hstore_out()

Datum hstore_out ( PG_FUNCTION_ARGS  )

Definition at line 1154 of file hstore_io.c.

References ARRPTR, cpw(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, hstore_send(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, palloc(), PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, PG_RETURN_CSTRING, pstrdup(), and STRPTR.

Referenced by cpw().

1155 {
1156  HStore *in = PG_GETARG_HSTORE_P(0);
1157  int buflen,
1158  i;
1159  int count = HS_COUNT(in);
1160  char *out,
1161  *ptr;
1162  char *base = STRPTR(in);
1163  HEntry *entries = ARRPTR(in);
1164 
1165  if (count == 0)
1167 
1168  buflen = 0;
1169 
1170  /*
1171  * this loop overestimates due to pessimistic assumptions about escaping,
1172  * so very large hstore values can't be output. this could be fixed, but
1173  * many other data types probably have the same issue. This replaced code
1174  * that used the original varlena size for calculations, which was wrong
1175  * in some subtle ways.
1176  */
1177 
1178  for (i = 0; i < count; i++)
1179  {
1180  /* include "" and => and comma-space */
1181  buflen += 6 + 2 * HSTORE_KEYLEN(entries, i);
1182  /* include "" only if nonnull */
1183  buflen += 2 + (HSTORE_VALISNULL(entries, i)
1184  ? 2
1185  : 2 * HSTORE_VALLEN(entries, i));
1186  }
1187 
1188  out = ptr = palloc(buflen);
1189 
1190  for (i = 0; i < count; i++)
1191  {
1192  *ptr++ = '"';
1193  ptr = cpw(ptr, HSTORE_KEY(entries, base, i), HSTORE_KEYLEN(entries, i));
1194  *ptr++ = '"';
1195  *ptr++ = '=';
1196  *ptr++ = '>';
1197  if (HSTORE_VALISNULL(entries, i))
1198  {
1199  *ptr++ = 'N';
1200  *ptr++ = 'U';
1201  *ptr++ = 'L';
1202  *ptr++ = 'L';
1203  }
1204  else
1205  {
1206  *ptr++ = '"';
1207  ptr = cpw(ptr, HSTORE_VAL(entries, base, i), HSTORE_VALLEN(entries, i));
1208  *ptr++ = '"';
1209  }
1210 
1211  if (i + 1 != count)
1212  {
1213  *ptr++ = ',';
1214  *ptr++ = ' ';
1215  }
1216  }
1217  *ptr = '\0';
1218 
1219  PG_RETURN_CSTRING(out);
1220 }
Definition: hstore.h:44
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
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
static char * cpw(char *dst, char *src, int len)
Definition: hstore_io.c:1139
#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
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Definition: hstore.h:18
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void * palloc(Size size)
Definition: mcxt.c:848
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:26

◆ HSTORE_POLLUTE()

HSTORE_POLLUTE ( hstore_from_text  ,
tconvert   
)

◆ hstore_populate_record()

Datum hstore_populate_record ( PG_FUNCTION_ARGS  )

Definition at line 922 of file hstore_io.c.

References ARRPTR, ColumnIOData::column_type, RecordIOData::columns, domain_check(), RecordIOData::domain_info, ereport, errcode(), errmsg(), ERROR, fmgr_info_cxt(), FmgrInfo::fn_mcxt, get_fn_expr_argtype(), getTypeInputInfo(), heap_deform_tuple(), heap_form_tuple(), HeapTupleGetDatum, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, HS_COUNT, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, hstoreFindKey(), i, idx(), InputFunctionCall(), InvalidOid, ItemPointerSetInvalid, lookup_rowtype_tupdesc_domain(), MemoryContextAlloc(), MemSet, NameStr, tupleDesc::natts, RecordIOData::ncolumns, offsetof, palloc(), PG_ARGISNULL, PG_GETARG_HEAPTUPLEHEADER, PG_GETARG_HSTORE_P, PG_RETURN_DATUM, PG_RETURN_NULL, PG_RETURN_POINTER, ColumnIOData::proc, RecordIOData::record_type, RecordIOData::record_typmod, ReleaseTupleDesc, STRPTR, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, tupleDesc::tdtypeid, TupleDescAttr, type_is_rowtype(), ColumnIOData::typiofunc, ColumnIOData::typioparam, value, and values.

Referenced by hstore_from_record().

923 {
924  Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
925  HStore *hs;
926  HEntry *entries;
927  char *ptr;
928  HeapTupleHeader rec;
929  Oid tupType;
930  int32 tupTypmod;
931  TupleDesc tupdesc;
932  HeapTupleData tuple;
933  HeapTuple rettuple;
934  RecordIOData *my_extra;
935  int ncolumns;
936  int i;
937  Datum *values;
938  bool *nulls;
939 
940  if (!type_is_rowtype(argtype))
941  ereport(ERROR,
942  (errcode(ERRCODE_DATATYPE_MISMATCH),
943  errmsg("first argument must be a rowtype")));
944 
945  if (PG_ARGISNULL(0))
946  {
947  if (PG_ARGISNULL(1))
948  PG_RETURN_NULL();
949 
950  rec = NULL;
951 
952  /*
953  * We have no tuple to look at, so the only source of type info is the
954  * argtype. The lookup_rowtype_tupdesc_domain call below will error
955  * out if we don't have a known composite type oid here.
956  */
957  tupType = argtype;
958  tupTypmod = -1;
959  }
960  else
961  {
962  rec = PG_GETARG_HEAPTUPLEHEADER(0);
963 
964  if (PG_ARGISNULL(1))
965  PG_RETURN_POINTER(rec);
966 
967  /*
968  * Extract type info from the tuple itself -- this will work even for
969  * anonymous record types.
970  */
971  tupType = HeapTupleHeaderGetTypeId(rec);
972  tupTypmod = HeapTupleHeaderGetTypMod(rec);
973  }
974 
975  hs = PG_GETARG_HSTORE_P(1);
976  entries = ARRPTR(hs);
977  ptr = STRPTR(hs);
978 
979  /*
980  * if the input hstore is empty, we can only skip the rest if we were
981  * passed in a non-null record, since otherwise there may be issues with
982  * domain nulls.
983  */
984 
985  if (HS_COUNT(hs) == 0 && rec)
986  PG_RETURN_POINTER(rec);
987 
988  /*
989  * Lookup the input record's tupdesc. For the moment, we don't worry
990  * about whether it is a domain over composite.
991  */
992  tupdesc = lookup_rowtype_tupdesc_domain(tupType, tupTypmod, false);
993  ncolumns = tupdesc->natts;
994 
995  if (rec)
996  {
997  /* Build a temporary HeapTuple control structure */
999  ItemPointerSetInvalid(&(tuple.t_self));
1000  tuple.t_tableOid = InvalidOid;
1001  tuple.t_data = rec;
1002  }
1003 
1004  /*
1005  * We arrange to look up the needed I/O info just once per series of
1006  * calls, assuming the record type doesn't change underneath us.
1007  */
1008  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
1009  if (my_extra == NULL ||
1010  my_extra->ncolumns != ncolumns)
1011  {
1012  fcinfo->flinfo->fn_extra =
1013  MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
1014  offsetof(RecordIOData, columns) +
1015  ncolumns * sizeof(ColumnIOData));
1016  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
1017  my_extra->record_type = InvalidOid;
1018  my_extra->record_typmod = 0;
1019  my_extra->domain_info = NULL;
1020  }
1021 
1022  if (my_extra->record_type != tupType ||
1023  my_extra->record_typmod != tupTypmod)
1024  {
1025  MemSet(my_extra, 0,
1026  offsetof(RecordIOData, columns) +
1027  ncolumns * sizeof(ColumnIOData));
1028  my_extra->record_type = tupType;
1029  my_extra->record_typmod = tupTypmod;
1030  my_extra->ncolumns = ncolumns;
1031  }
1032 
1033  values = (Datum *) palloc(ncolumns * sizeof(Datum));
1034  nulls = (bool *) palloc(ncolumns * sizeof(bool));
1035 
1036  if (rec)
1037  {
1038  /* Break down the tuple into fields */
1039  heap_deform_tuple(&tuple, tupdesc, values, nulls);
1040  }
1041  else
1042  {
1043  for (i = 0; i < ncolumns; ++i)
1044  {
1045  values[i] = (Datum) 0;
1046  nulls[i] = true;
1047  }
1048  }
1049 
1050  for (i = 0; i < ncolumns; ++i)
1051  {
1052  ColumnIOData *column_info = &my_extra->columns[i];
1053  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1054  Oid column_type = att->atttypid;
1055  char *value;
1056  int idx;
1057  int vallen;
1058 
1059  /* Ignore dropped columns in datatype */
1060  if (att->attisdropped)
1061  {
1062  nulls[i] = true;
1063  continue;
1064  }
1065 
1066  idx = hstoreFindKey(hs, 0,
1067  NameStr(att->attname),
1068  strlen(NameStr(att->attname)));
1069 
1070  /*
1071  * we can't just skip here if the key wasn't found since we might have
1072  * a domain to deal with. If we were passed in a non-null record
1073  * datum, we assume that the existing values are valid (if they're
1074  * not, then it's not our fault), but if we were passed in a null,
1075  * then every field which we don't populate needs to be run through
1076  * the input function just in case it's a domain type.
1077  */
1078  if (idx < 0 && rec)
1079  continue;
1080 
1081  /*
1082  * Prepare to convert the column value from text
1083  */
1084  if (column_info->column_type != column_type)
1085  {
1086  getTypeInputInfo(column_type,
1087  &column_info->typiofunc,
1088  &column_info->typioparam);
1089  fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
1090  fcinfo->flinfo->fn_mcxt);
1091  column_info->column_type = column_type;
1092  }
1093 
1094  if (idx < 0 || HSTORE_VALISNULL(entries, idx))
1095  {
1096  /*
1097  * need InputFunctionCall to happen even for nulls, so that domain
1098  * checks are done
1099  */
1100  values[i] = InputFunctionCall(&column_info->proc, NULL,
1101  column_info->typioparam,
1102  att->atttypmod);
1103  nulls[i] = true;
1104  }
1105  else
1106  {
1107  vallen = HSTORE_VALLEN(entries, idx);
1108  value = palloc(1 + vallen);
1109  memcpy(value, HSTORE_VAL(entries, ptr, idx), vallen);
1110  value[vallen] = 0;
1111 
1112  values[i] = InputFunctionCall(&column_info->proc, value,
1113  column_info->typioparam,
1114  att->atttypmod);
1115  nulls[i] = false;
1116  }
1117  }
1118 
1119  rettuple = heap_form_tuple(tupdesc, values, nulls);
1120 
1121  /*
1122  * If the target type is domain over composite, all we know at this point
1123  * is that we've made a valid value of the base composite type. Must
1124  * check domain constraints before deciding we're done.
1125  */
1126  if (argtype != tupdesc->tdtypeid)
1127  domain_check(HeapTupleGetDatum(rettuple), false,
1128  argtype,
1129  &my_extra->domain_info,
1130  fcinfo->flinfo->fn_mcxt);
1131 
1132  ReleaseTupleDesc(tupdesc);
1133 
1135 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
FmgrInfo proc
Definition: hstore_io.c:748
Oid tdtypeid
Definition: tupdesc.h:80
MemoryContext fn_mcxt
Definition: fmgr.h:65
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
Oid record_type
Definition: hstore_io.c:753
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
void * domain_info
Definition: hstore_io.c:756
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:853
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:79
int32 record_typmod
Definition: hstore_io.c:754
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1674
signed int int32
Definition: c.h:284
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:460
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define ERROR
Definition: elog.h:43
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
static struct @121 value
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
#define HS_COUNT(hsp_)
Definition: hstore.h:61
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
Oid t_tableOid
Definition: htup.h:66
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2405
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2613
#define ereport(elevel, rest)
Definition: elog.h:122
ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER]
Definition: hstore_io.c:758
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:450
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1618
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: hstore.h:18
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:230
Oid typioparam
Definition: hstore_io.c:747
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define STRPTR(x)
Definition: hstore.h:76
int i
#define NameStr(name)
Definition: c.h:547
#define ARRPTR(x)
Definition: cube.c:26
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:121
#define PG_RETURN_NULL()
Definition: fmgr.h:305
#define offsetof(type, field)
Definition: c.h:593
Oid column_type
Definition: hstore_io.c:745
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:444

◆ hstore_recv()

Datum hstore_recv ( PG_FUNCTION_ARGS  )

Definition at line 426 of file hstore_io.c.

References buf, ereport, errcode(), errmsg(), ERROR, hstore_from_text(), hstoreCheckKeyLen(), hstoreCheckValLen(), hstorePairs(), hstoreUniquePairs(), i, Pairs::isnull, Pairs::key, Pairs::keylen, MaxAllocSize, Pairs::needfree, palloc(), PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RETURN_POINTER, pq_getmsgint(), pq_getmsgtext(), Pairs::val, and Pairs::vallen.

Referenced by hstore_in().

427 {
428  int32 buflen;
429  HStore *out;
430  Pairs *pairs;
431  int32 i;
432  int32 pcount;
434 
435  pcount = pq_getmsgint(buf, 4);
436 
437  if (pcount == 0)
438  {
439  out = hstorePairs(NULL, 0, 0);
440  PG_RETURN_POINTER(out);
441  }
442 
443  if (pcount < 0 || pcount > MaxAllocSize / sizeof(Pairs))
444  ereport(ERROR,
445  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
446  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
447  pcount, (int) (MaxAllocSize / sizeof(Pairs)))));
448  pairs = palloc(pcount * sizeof(Pairs));
449 
450  for (i = 0; i < pcount; ++i)
451  {
452  int rawlen = pq_getmsgint(buf, 4);
453  int len;
454 
455  if (rawlen < 0)
456  ereport(ERROR,
457  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
458  errmsg("null value not allowed for hstore key")));
459 
460  pairs[i].key = pq_getmsgtext(buf, rawlen, &len);
461  pairs[i].keylen = hstoreCheckKeyLen(len);
462  pairs[i].needfree = true;
463 
464  rawlen = pq_getmsgint(buf, 4);
465  if (rawlen < 0)
466  {
467  pairs[i].val = NULL;
468  pairs[i].vallen = 0;
469  pairs[i].isnull = true;
470  }
471  else
472  {
473  pairs[i].val = pq_getmsgtext(buf, rawlen, &len);
474  pairs[i].vallen = hstoreCheckValLen(len);
475  pairs[i].isnull = false;
476  }
477  }
478 
479  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
480 
481  out = hstorePairs(pairs, pcount, buflen);
482 
483  PG_RETURN_POINTER(out);
484 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
StringInfoData * StringInfo
Definition: stringinfo.h:43
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:284
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define ERROR
Definition: elog.h:43
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:122
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
#define MaxAllocSize
Definition: memutils.h:40
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
size_t keylen
Definition: hstore.h:165
bool isnull
Definition: hstore.h:167
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
Definition: hstore.h:161

◆ hstore_send()

Datum hstore_send ( PG_FUNCTION_ARGS  )

Definition at line 1225 of file hstore_io.c.

References ARRPTR, buf, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, hstore_to_json_loose(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint32(), pq_sendtext(), and STRPTR.

Referenced by hstore_out().

1226 {
1227  HStore *in = PG_GETARG_HSTORE_P(0);
1228  int i;
1229  int count = HS_COUNT(in);
1230  char *base = STRPTR(in);
1231  HEntry *entries = ARRPTR(in);
1233 
1234  pq_begintypsend(&buf);
1235 
1236  pq_sendint32(&buf, count);
1237 
1238  for (i = 0; i < count; i++)
1239  {
1240  int32 keylen = HSTORE_KEYLEN(entries, i);
1241 
1242  pq_sendint32(&buf, keylen);
1243  pq_sendtext(&buf, HSTORE_KEY(entries, base, i), keylen);
1244  if (HSTORE_VALISNULL(entries, i))
1245  {
1246  pq_sendint32(&buf, -1);
1247  }
1248  else
1249  {
1250  int32 vallen = HSTORE_VALLEN(entries, i);
1251 
1252  pq_sendint32(&buf, vallen);
1253  pq_sendtext(&buf, HSTORE_VAL(entries, base, i), vallen);
1254  }
1255  }
1256 
1258 }
Definition: hstore.h:44
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
signed int int32
Definition: c.h:284
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
static char * buf
Definition: pg_test_fsync.c:67
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
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:26

◆ hstore_to_json()

Datum hstore_to_json ( PG_FUNCTION_ARGS  )

Definition at line 1326 of file hstore_io.c.

References appendBinaryStringInfo(), appendStringInfoChar(), appendStringInfoString(), ARRPTR, cstring_to_text(), cstring_to_text_with_len(), StringInfoData::data, escape_json(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, hstore_to_jsonb(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, initStringInfo(), PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, PG_RETURN_TEXT_P, resetStringInfo(), and STRPTR.

Referenced by hstore_to_json_loose().

1327 {
1328  HStore *in = PG_GETARG_HSTORE_P(0);
1329  int i;
1330  int count = HS_COUNT(in);
1331  char *base = STRPTR(in);
1332  HEntry *entries = ARRPTR(in);
1333  StringInfoData tmp,
1334  dst;
1335 
1336  if (count == 0)
1338 
1339  initStringInfo(&tmp);
1340  initStringInfo(&dst);
1341 
1342  appendStringInfoChar(&dst, '{');
1343 
1344  for (i = 0; i < count; i++)
1345  {
1346  resetStringInfo(&tmp);
1347  appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
1348  HSTORE_KEYLEN(entries, i));
1349  escape_json(&dst, tmp.data);
1350  appendStringInfoString(&dst, ": ");
1351  if (HSTORE_VALISNULL(entries, i))
1352  appendStringInfoString(&dst, "null");
1353  else
1354  {
1355  resetStringInfo(&tmp);
1356  appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1357  HSTORE_VALLEN(entries, i));
1358  escape_json(&dst, tmp.data);
1359  }
1360 
1361  if (i + 1 != count)
1362  appendStringInfoString(&dst, ", ");
1363  }
1364  appendStringInfoChar(&dst, '}');
1365 
1367 }
Definition: hstore.h:44
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2397
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#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
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define HS_COUNT(hsp_)
Definition: hstore.h:61
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
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:26
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ hstore_to_json_loose()

Datum hstore_to_json_loose ( PG_FUNCTION_ARGS  )

Definition at line 1271 of file hstore_io.c.

References appendBinaryStringInfo(), appendStringInfoChar(), appendStringInfoString(), ARRPTR, cstring_to_text(), cstring_to_text_with_len(), StringInfoData::data, escape_json(), HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, hstore_to_json(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, initStringInfo(), IsValidJsonNumber(), StringInfoData::len, PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, PG_RETURN_TEXT_P, resetStringInfo(), and STRPTR.

Referenced by hstore_send().

1272 {
1273  HStore *in = PG_GETARG_HSTORE_P(0);
1274  int i;
1275  int count = HS_COUNT(in);
1276  char *base = STRPTR(in);
1277  HEntry *entries = ARRPTR(in);
1278  StringInfoData tmp,
1279  dst;
1280 
1281  if (count == 0)
1283 
1284  initStringInfo(&tmp);
1285  initStringInfo(&dst);
1286 
1287  appendStringInfoChar(&dst, '{');
1288 
1289  for (i = 0; i < count; i++)
1290  {
1291  resetStringInfo(&tmp);
1292  appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
1293  HSTORE_KEYLEN(entries, i));
1294  escape_json(&dst, tmp.data);
1295  appendStringInfoString(&dst, ": ");
1296  if (HSTORE_VALISNULL(entries, i))
1297  appendStringInfoString(&dst, "null");
1298  /* guess that values of 't' or 'f' are booleans */
1299  else if (HSTORE_VALLEN(entries, i) == 1 &&
1300  *(HSTORE_VAL(entries, base, i)) == 't')
1301  appendStringInfoString(&dst, "true");
1302  else if (HSTORE_VALLEN(entries, i) == 1 &&
1303  *(HSTORE_VAL(entries, base, i)) == 'f')
1304  appendStringInfoString(&dst, "false");
1305  else
1306  {
1307  resetStringInfo(&tmp);
1308  appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1309  HSTORE_VALLEN(entries, i));
1310  if (IsValidJsonNumber(tmp.data, tmp.len))
1311  appendBinaryStringInfo(&dst, tmp.data, tmp.len);
1312  else
1313  escape_json(&dst, tmp.data);
1314  }
1315 
1316  if (i + 1 != count)
1317  appendStringInfoString(&dst, ", ");
1318  }
1319  appendStringInfoChar(&dst, '}');
1320 
1322 }
Definition: hstore.h:44
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2397
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
#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
bool IsValidJsonNumber(const char *str, int len)
Definition: json.c:193
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define HS_COUNT(hsp_)
Definition: hstore.h:61
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
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:26
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208

◆ hstore_to_jsonb()

Datum hstore_to_jsonb ( PG_FUNCTION_ARGS  )

Definition at line 1371 of file hstore_io.c.

References ARRPTR, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, hstore_to_jsonb_loose(), HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, jbvNull, jbvString, JsonbValueToJsonb(), PG_FUNCTION_INFO_V1(), PG_GETARG_HSTORE_P, PG_RETURN_POINTER, pushJsonbValue(), STRPTR, JsonbValue::type, JsonbValue::val, val, WJB_BEGIN_OBJECT, WJB_END_OBJECT, WJB_KEY, and WJB_VALUE.

Referenced by hstore_to_json().

1372 {
1373  HStore *in = PG_GETARG_HSTORE_P(0);
1374  int i;
1375  int count = HS_COUNT(in);
1376  char *base = STRPTR(in);
1377  HEntry *entries = ARRPTR(in);
1378  JsonbParseState *state = NULL;
1379  JsonbValue *res;
1380 
1381  (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
1382 
1383  for (i = 0; i < count; i++)
1384  {
1385  JsonbValue key,
1386  val;
1387 
1388  key.type = jbvString;
1389  key.val.string.len = HSTORE_KEYLEN(entries, i);
1390  key.val.string.val = HSTORE_KEY(entries, base, i);
1391 
1392  (void) pushJsonbValue(&state, WJB_KEY, &key);
1393 
1394  if (HSTORE_VALISNULL(entries, i))
1395  {
1396  val.type = jbvNull;
1397  }
1398  else
1399  {
1400  val.type = jbvString;
1401  val.val.string.len = HSTORE_VALLEN(entries, i);
1402  val.val.string.val = HSTORE_VAL(entries, base, i);
1403  }
1404  (void) pushJsonbValue(&state, WJB_VALUE, &val);
1405  }
1406 
1407  res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
1408 
1410 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:79
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
char * val
Definition: jsonb.h:259
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
Definition: jsonb.h:231
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:517
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
#define HS_COUNT(hsp_)
Definition: hstore.h:61
Definition: jsonb.h:23
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
Definition: regguts.h:298
Definition: hstore.h:18
enum jbvType type
Definition: jsonb.h:250
#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:26
long val
Definition: informix.c:689

◆ hstore_to_jsonb_loose()

Datum hstore_to_jsonb_loose ( PG_FUNCTION_ARGS  )

Definition at line 1414 of file hstore_io.c.

References appendBinaryStringInfo(), ARRPTR, CStringGetDatum, StringInfoData::data, DatumGetNumeric, DirectFunctionCall3, HS_COUNT, HSTORE_KEY, HSTORE_KEYLEN, HSTORE_VAL, HSTORE_VALISNULL, HSTORE_VALLEN, i, initStringInfo(), IsValidJsonNumber(), jbvBool, jbvNull, jbvNumeric, jbvString, JsonbValueToJsonb(), StringInfoData::len, numeric_in(), PG_GETARG_HSTORE_P, PG_RETURN_POINTER, pushJsonbValue(), resetStringInfo(), STRPTR, JsonbValue::type, JsonbValue::val, val, WJB_BEGIN_OBJECT, WJB_END_OBJECT, WJB_KEY, and WJB_VALUE.

Referenced by hstore_to_jsonb().

1415 {
1416  HStore *in = PG_GETARG_HSTORE_P(0);
1417  int i;
1418  int count = HS_COUNT(in);
1419  char *base = STRPTR(in);
1420  HEntry *entries = ARRPTR(in);
1421  JsonbParseState *state = NULL;
1422  JsonbValue *res;
1423  StringInfoData tmp;
1424 
1425  initStringInfo(&tmp);
1426 
1427  (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
1428 
1429  for (i = 0; i < count; i++)
1430  {
1431  JsonbValue key,
1432  val;
1433 
1434  key.type = jbvString;
1435  key.val.string.len = HSTORE_KEYLEN(entries, i);
1436  key.val.string.val = HSTORE_KEY(entries, base, i);
1437 
1438  (void) pushJsonbValue(&state, WJB_KEY, &key);
1439 
1440  if (HSTORE_VALISNULL(entries, i))
1441  {
1442  val.type = jbvNull;
1443  }
1444  /* guess that values of 't' or 'f' are booleans */
1445  else if (HSTORE_VALLEN(entries, i) == 1 &&
1446  *(HSTORE_VAL(entries, base, i)) == 't')
1447  {
1448  val.type = jbvBool;
1449  val.val.boolean = true;
1450  }
1451  else if (HSTORE_VALLEN(entries, i) == 1 &&
1452  *(HSTORE_VAL(entries, base, i)) == 'f')
1453  {
1454  val.type = jbvBool;
1455  val.val.boolean = false;
1456  }
1457  else
1458  {
1459  resetStringInfo(&tmp);
1460  appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1461  HSTORE_VALLEN(entries, i));
1462  if (IsValidJsonNumber(tmp.data, tmp.len))
1463  {
1464  val.type = jbvNumeric;
1465  val.val.numeric = DatumGetNumeric(
1467  CStringGetDatum(tmp.data), 0, -1));
1468  }
1469  else
1470  {
1471  val.type = jbvString;
1472  val.val.string.len = HSTORE_VALLEN(entries, i);
1473  val.val.string.val = HSTORE_VAL(entries, base, i);
1474  }
1475  }
1476  (void) pushJsonbValue(&state, WJB_VALUE, &val);
1477  }
1478 
1479  res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
1480 
1482 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Definition: hstore.h:44
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:79
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
char * val
Definition: jsonb.h:259
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
Definition: jsonb.h:234
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
Definition: jsonb.h:231
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:517
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
bool IsValidJsonNumber(const char *str, int len)
Definition: json.c:193
#define HS_COUNT(hsp_)
Definition: hstore.h:61
#define CStringGetDatum(X)
Definition: postgres.h:584
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:569
Definition: jsonb.h:23
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:589
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define DatumGetNumeric(X)
Definition: numeric.h:49
Definition: regguts.h:298
Definition: hstore.h:18
enum jbvType type
Definition: jsonb.h:250
#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:26
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
long val
Definition: informix.c:689

◆ hstoreCheckKeyLen()

size_t hstoreCheckKeyLen ( size_t  len)

Definition at line 354 of file hstore_io.c.

References ereport, errcode(), errmsg(), ERROR, and HSTORE_MAX_KEY_LEN.

Referenced by hstore_from_array(), hstore_from_arrays(), hstore_from_record(), hstore_from_text(), hstore_recv(), and parse_hstore().

355 {
356  if (len > HSTORE_MAX_KEY_LEN)
357  ereport(ERROR,
358  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
359  errmsg("string too long for hstore key")));
360  return len;
361 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define HSTORE_MAX_KEY_LEN
Definition: hstore.h:41
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ hstoreCheckValLen()

size_t hstoreCheckValLen ( size_t  len)

Definition at line 364 of file hstore_io.c.

References ereport, errcode(), errmsg(), ERROR, and HSTORE_MAX_VALUE_LEN.

Referenced by hstore_from_array(), hstore_from_arrays(), hstore_from_record(), hstore_from_text(), hstore_recv(), and parse_hstore().

365 {
366  if (len > HSTORE_MAX_VALUE_LEN)
367  ereport(ERROR,
368  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
369  errmsg("string too long for hstore value")));
370  return len;
371 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HSTORE_MAX_VALUE_LEN
Definition: hstore.h:42

◆ hstorePairs()

HStore* hstorePairs ( Pairs pairs,
int32  pcount,
int32  buflen 
)

Definition at line 375 of file hstore_io.c.

References ARRPTR, buf, CALCDATASIZE, HS_ADDITEM, HS_FINALIZE, HS_SETCOUNT, hstore_in(), i, palloc(), PG_FUNCTION_INFO_V1(), SET_VARSIZE, and STRPTR.

Referenced by hstore_from_array(), hstore_from_arrays(), hstore_from_record(), hstore_from_text(), hstore_in(), and hstore_recv().

376 {
377  HStore *out;
378  HEntry *entry;
379  char *ptr;
380  char *buf;
381  int32 len;
382  int32 i;
383 
384  len = CALCDATASIZE(pcount, buflen);
385  out = palloc(len);
386  SET_VARSIZE(out, len);
387  HS_SETCOUNT(out, pcount);
388 
389  if (pcount == 0)
390  return out;
391 
392  entry = ARRPTR(out);
393  buf = ptr = STRPTR(out);
394 
395  for (i = 0; i < pcount; i++)
396  HS_ADDITEM(entry, buf, ptr, pairs[i]);
397 
398  HS_FINALIZE(out, pcount, buf, ptr);
399 
400  return out;
401 }
Definition: hstore.h:44
signed int int32
Definition: c.h:284
static char * buf
Definition: pg_test_fsync.c:67
#define HS_ADDITEM(dent_, dbuf_, dptr_, pair_)
Definition: hstore.h:112
#define HS_FINALIZE(hsp_, count_, buf_, ptr_)
Definition: hstore.h:129
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
Definition: hstore.h:18
void * palloc(Size size)
Definition: mcxt.c:848
#define STRPTR(x)
Definition: hstore.h:76
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ARRPTR(x)
Definition: cube.c:26
#define HS_SETCOUNT(hsp_, c_)
Definition: hstore.h:62

◆ hstoreUniquePairs()

int hstoreUniquePairs ( Pairs a,
int32  l,
int32 buflen 
)

Definition at line 312 of file hstore_io.c.

References comparePairs(), Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, pfree(), qsort, Pairs::val, and Pairs::vallen.

Referenced by hstore_from_array(), hstore_from_arrays(), hstore_from_record(), hstore_in(), and hstore_recv().

313 {
314  Pairs *ptr,
315  *res;
316 
317  *buflen = 0;
318  if (l < 2)
319  {
320  if (l == 1)
321  *buflen = a->keylen + ((a->isnull) ? 0 : a->vallen);
322  return l;
323  }
324 
325  qsort((void *) a, l, sizeof(Pairs), comparePairs);
326  ptr = a + 1;
327  res = a;
328  while (ptr - a < l)
329  {
330  if (ptr->keylen == res->keylen &&
331  memcmp(ptr->key, res->key, res->keylen) == 0)
332  {
333  if (ptr->needfree)
334  {
335  pfree(ptr->key);
336  pfree(ptr->val);
337  }
338  }
339  else
340  {
341  *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
342  res++;
343  memcpy(res, ptr, sizeof(Pairs));
344  }
345 
346  ptr++;
347  }
348 
349  *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
350  return res + 1 - a;
351 }
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
void pfree(void *pointer)
Definition: mcxt.c:949
static int comparePairs(const void *a, const void *b)
Definition: hstore_io.c:282
size_t keylen
Definition: hstore.h:165
bool isnull
Definition: hstore.h:167
#define qsort(a, b, c, d)
Definition: port.h:408
Definition: hstore.h:161

◆ parse_hstore()

static void parse_hstore ( HSParser state)
static

Definition at line 183 of file hstore_io.c.

References HSParser::begin, HSParser::cur, elog, ERROR, get_val(), hstoreCheckKeyLen(), hstoreCheckValLen(), Pairs::isnull, Pairs::key, Pairs::keylen, Pairs::needfree, HSParser::pairs, palloc(), HSParser::pcur, pg_strcasecmp(), HSParser::plen, HSParser::ptr, repalloc(), Pairs::val, Pairs::vallen, WDEL, WEQ, WGT, WKEY, HSParser::word, and WVAL.

Referenced by hstore_in().

184 {
185  int st = WKEY;
186  bool escaped = false;
187 
188  state->plen = 16;
189  state->pairs = (Pairs *) palloc(sizeof(Pairs) * state->plen);
190  state->pcur = 0;
191  state->ptr = state->begin;
192  state->word = NULL;
193 
194  while (1)
195  {
196  if (st == WKEY)
197  {
198  if (!get_val(state, false, &escaped))
199  return;
200  if (state->pcur >= state->plen)
201  {
202  state->plen *= 2;
203  state->pairs = (Pairs *) repalloc(state->pairs, sizeof(Pairs) * state->plen);
204  }
205  state->pairs[state->pcur].key = state->word;
206  state->pairs[state->pcur].keylen = hstoreCheckKeyLen(state->cur - state->word);
207  state->pairs[state->pcur].val = NULL;
208  state->word = NULL;
209  st = WEQ;
210  }
211  else if (st == WEQ)
212  {
213  if (*(state->ptr) == '=')
214  {
215  st = WGT;
216  }
217  else if (*(state->ptr) == '\0')
218  {
219  elog(ERROR, "Unexpected end of string");
220  }
221  else if (!isspace((unsigned char) *(state->ptr)))
222  {
223  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
224  }
225  }
226  else if (st == WGT)
227  {
228  if (*(state->ptr) == '>')
229  {
230  st = WVAL;
231  }
232  else if (*(state->ptr) == '\0')
233  {
234  elog(ERROR, "Unexpected end of string");
235  }
236  else
237  {
238  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
239  }
240  }
241  else if (st == WVAL)
242  {
243  if (!get_val(state, true, &escaped))
244  elog(ERROR, "Unexpected end of string");
245  state->pairs[state->pcur].val = state->word;
246  state->pairs[state->pcur].vallen = hstoreCheckValLen(state->cur - state->word);
247  state->pairs[state->pcur].isnull = false;
248  state->pairs[state->pcur].needfree = true;
249  if (state->cur - state->word == 4 && !escaped)
250  {
251  state->word[4] = '\0';
252  if (0 == pg_strcasecmp(state->word, "null"))
253  state->pairs[state->pcur].isnull = true;
254  }
255  state->word = NULL;
256  state->pcur++;
257  st = WDEL;
258  }
259  else if (st == WDEL)
260  {
261  if (*(state->ptr) == ',')
262  {
263  st = WKEY;
264  }
265  else if (*(state->ptr) == '\0')
266  {
267  return;
268  }
269  else if (!isspace((unsigned char) *(state->ptr)))
270  {
271  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
272  }
273  }
274  else
275  elog(ERROR, "Unknown state %d at line %d in file '%s'", st, __LINE__, __FILE__);
276 
277  state->ptr++;
278  }
279 }
char * word
Definition: hstore_io.c:34
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
#define WGT
Definition: hstore_io.c:178
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
size_t vallen
Definition: hstore.h:166
bool needfree
Definition: hstore.h:168
signed int int32
Definition: c.h:284
char * val
Definition: hstore.h:164
char * key
Definition: hstore.h:163
#define ERROR
Definition: elog.h:43
char * cur
Definition: hstore_io.c:33
#define WDEL
Definition: hstore_io.c:179
int pcur
Definition: hstore_io.c:38
#define WVAL
Definition: hstore_io.c:176
size_t keylen
Definition: hstore.h:165
bool isnull
Definition: hstore.h:167
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
Pairs * pairs
Definition: hstore_io.c:37
char * begin
Definition: hstore_io.c:31
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
char * ptr
Definition: hstore_io.c:32
void * palloc(Size size)
Definition: mcxt.c:848
#define WEQ
Definition: hstore_io.c:177
#define WKEY
Definition: hstore_io.c:175
int plen
Definition: hstore_io.c:39
#define elog
Definition: elog.h:219
static bool get_val(HSParser *state, bool ignoreeq, bool *escaped)
Definition: hstore_io.c:61
Definition: hstore.h:161

◆ PG_FUNCTION_INFO_V1() [1/13]

◆ PG_FUNCTION_INFO_V1() [2/13]

PG_FUNCTION_INFO_V1 ( hstore_recv  )

◆ PG_FUNCTION_INFO_V1() [3/13]

PG_FUNCTION_INFO_V1 ( hstore_from_text  )

◆ PG_FUNCTION_INFO_V1() [4/13]

PG_FUNCTION_INFO_V1 ( hstore_from_arrays  )

◆ PG_FUNCTION_INFO_V1() [5/13]

PG_FUNCTION_INFO_V1 ( hstore_from_array  )

◆ PG_FUNCTION_INFO_V1() [6/13]

PG_FUNCTION_INFO_V1 ( hstore_from_record  )

◆ PG_FUNCTION_INFO_V1() [7/13]

PG_FUNCTION_INFO_V1 ( hstore_populate_record  )

◆ PG_FUNCTION_INFO_V1() [8/13]

PG_FUNCTION_INFO_V1 ( hstore_out  )

◆ PG_FUNCTION_INFO_V1() [9/13]

PG_FUNCTION_INFO_V1 ( hstore_send  )

◆ PG_FUNCTION_INFO_V1() [10/13]

PG_FUNCTION_INFO_V1 ( hstore_to_json_loose  )

◆ PG_FUNCTION_INFO_V1() [11/13]

PG_FUNCTION_INFO_V1 ( hstore_to_json  )

◆ PG_FUNCTION_INFO_V1() [12/13]

PG_FUNCTION_INFO_V1 ( hstore_to_jsonb  )

◆ PG_FUNCTION_INFO_V1() [13/13]

PG_FUNCTION_INFO_V1 ( hstore_to_jsonb_loose  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 23 of file hstore_io.c.