| 1 |
/* |
| 2 |
* contrib/hstore/hstore_io.c |
| 3 |
*/ |
| 4 |
#include "postgres.h" |
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
|
| 8 |
#include "access/htup_details.h" |
| 9 |
#include "catalog/pg_type.h" |
| 10 |
#include "common/jsonapi.h" |
| 11 |
#include "funcapi.h" |
| 12 |
#include "hstore.h" |
| 13 |
#include "lib/stringinfo.h" |
| 14 |
#include "libpq/pqformat.h" |
| 15 |
#include "nodes/miscnodes.h" |
| 16 |
#include "parser/scansup.h" |
| 17 |
#include "utils/builtins.h" |
| 18 |
#include "utils/json.h" |
| 19 |
#include "utils/jsonb.h" |
| 20 |
#include "utils/lsyscache.h" |
| 21 |
#include "utils/memutils.h" |
| 22 |
#include "utils/typcache.h" |
| 23 |
|
| 24 |
PG_MODULE_MAGIC_EXT( |
| 25 |
.name = "hstore", |
| 26 |
.version = PG_VERSION |
| 27 |
); |
| 28 |
|
| 29 |
/* old names for C functions */ |
| 30 |
HSTORE_POLLUTE(hstore_from_text, tconvert); |
| 31 |
|
| 32 |
|
| 33 |
typedef struct |
| 34 |
{ |
| 35 |
char *begin; |
| 36 |
char *ptr; |
| 37 |
char *cur; |
| 38 |
char *word; |
| 39 |
int wordlen; |
| 40 |
Node *escontext; |
| 41 |
|
| 42 |
Pairs *pairs; |
| 43 |
int pcur; |
| 44 |
int plen; |
| 45 |
} HSParser; |
| 46 |
|
| 47 |
static bool hstoreCheckKeyLength(size_t len, HSParser *state); |
| 48 |
static bool hstoreCheckValLength(size_t len, HSParser *state); |
| 49 |
|
| 50 |
|
| 51 |
#define RESIZEPRSBUF \ |
| 52 |
do { \ |
| 53 |
if ( state->cur - state->word + 1 >= state->wordlen ) \ |
| 54 |
{ \ |
| 55 |
int32 clen = state->cur - state->word; \ |
| 56 |
state->wordlen *= 2; \ |
| 57 |
state->word = (char*)repalloc( (void*)state->word, state->wordlen ); \ |
| 58 |
state->cur = state->word + clen; \ |
| 59 |
} \ |
| 60 |
} while (0) |
| 61 |
|
| 62 |
#define PRSSYNTAXERROR return prssyntaxerror(state) |
| 63 |
|
| 64 |
static bool |
| 65 |
prssyntaxerror(HSParser *state) |
| 66 |
{ |
| 67 |
errsave(state->escontext, |
| 68 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 69 |
errmsg("syntax error in hstore, near \"%.*s\" at position %d", |
| 70 |
pg_mblen_cstr(state->ptr), state->ptr, |
| 71 |
(int) (state->ptr - state->begin)))); |
| 72 |
/* In soft error situation, return false as convenience for caller */ |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
#define PRSEOF return prseof(state) |
| 77 |
|
| 78 |
static bool |
| 79 |
prseof(HSParser *state) |
| 80 |
{ |
| 81 |
errsave(state->escontext, |
| 82 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 83 |
errmsg("syntax error in hstore: unexpected end of string"))); |
| 84 |
/* In soft error situation, return false as convenience for caller */ |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
#define GV_WAITVAL 0 |
| 90 |
#define GV_INVAL 1 |
| 91 |
#define GV_INESCVAL 2 |
| 92 |
#define GV_WAITESCIN 3 |
| 93 |
#define GV_WAITESCESCIN 4 |
| 94 |
|
| 95 |
static bool |
| 96 |
get_val(HSParser *state, bool ignoreeq, bool *escaped) |
| 97 |
{ |
| 98 |
int st = GV_WAITVAL; |
| 99 |
|
| 100 |
state->wordlen = 32; |
| 101 |
state->cur = state->word = palloc(state->wordlen); |
| 102 |
*escaped = false; |
| 103 |
|
| 104 |
while (1) |
| 105 |
{ |
| 106 |
if (st == GV_WAITVAL) |
| 107 |
{ |
| 108 |
if (*(state->ptr) == '"') |
| 109 |
{ |
| 110 |
*escaped = true; |
| 111 |
st = GV_INESCVAL; |
| 112 |
} |
| 113 |
else if (*(state->ptr) == '\0') |
| 114 |
{ |
| 115 |
return false; |
| 116 |
} |
| 117 |
else if (*(state->ptr) == '=' && !ignoreeq) |
| 118 |
{ |
| 119 |
PRSSYNTAXERROR; |
| 120 |
} |
| 121 |
else if (*(state->ptr) == '\\') |
| 122 |
{ |
| 123 |
st = GV_WAITESCIN; |
| 124 |
} |
| 125 |
else if (!scanner_isspace((unsigned char) *(state->ptr))) |
| 126 |
{ |
| 127 |
*(state->cur) = *(state->ptr); |
| 128 |
state->cur++; |
| 129 |
st = GV_INVAL; |
| 130 |
} |
| 131 |
} |
| 132 |
else if (st == GV_INVAL) |
| 133 |
{ |
| 134 |
if (*(state->ptr) == '\\') |
| 135 |
{ |
| 136 |
st = GV_WAITESCIN; |
| 137 |
} |
| 138 |
else if (*(state->ptr) == '=' && !ignoreeq) |
| 139 |
{ |
| 140 |
state->ptr--; |
| 141 |
return true; |
| 142 |
} |
| 143 |
else if (*(state->ptr) == ',' && ignoreeq) |
| 144 |
{ |
| 145 |
state->ptr--; |
| 146 |
return true; |
| 147 |
} |
| 148 |
else if (scanner_isspace((unsigned char) *(state->ptr))) |
| 149 |
{ |
| 150 |
return true; |
| 151 |
} |
| 152 |
else if (*(state->ptr) == '\0') |
| 153 |
{ |
| 154 |
state->ptr--; |
| 155 |
return true; |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
RESIZEPRSBUF; |
| 160 |
*(state->cur) = *(state->ptr); |
| 161 |
state->cur++; |
| 162 |
} |
| 163 |
} |
| 164 |
else if (st == GV_INESCVAL) |
| 165 |
{ |
| 166 |
if (*(state->ptr) == '\\') |
| 167 |
{ |
| 168 |
st = GV_WAITESCESCIN; |
| 169 |
} |
| 170 |
else if (*(state->ptr) == '"') |
| 171 |
{ |
| 172 |
return true; |
| 173 |
} |
| 174 |
else if (*(state->ptr) == '\0') |
| 175 |
{ |
| 176 |
PRSEOF; |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
RESIZEPRSBUF; |
| 181 |
*(state->cur) = *(state->ptr); |
| 182 |
state->cur++; |
| 183 |
} |
| 184 |
} |
| 185 |
else if (st == GV_WAITESCIN) |
| 186 |
{ |
| 187 |
if (*(state->ptr) == '\0') |
| 188 |
PRSEOF; |
| 189 |
RESIZEPRSBUF; |
| 190 |
*(state->cur) = *(state->ptr); |
| 191 |
state->cur++; |
| 192 |
st = GV_INVAL; |
| 193 |
} |
| 194 |
else if (st == GV_WAITESCESCIN) |
| 195 |
{ |
| 196 |
if (*(state->ptr) == '\0') |
| 197 |
PRSEOF; |
| 198 |
RESIZEPRSBUF; |
| 199 |
*(state->cur) = *(state->ptr); |
| 200 |
state->cur++; |
| 201 |
st = GV_INESCVAL; |
| 202 |
} |
| 203 |
else |
| 204 |
elog(ERROR, "unrecognized get_val state: %d", st); |
| 205 |
|
| 206 |
state->ptr++; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
#define WKEY 0 |
| 211 |
#define WVAL 1 |
| 212 |
#define WEQ 2 |
| 213 |
#define WGT 3 |
| 214 |
#define WDEL 4 |
| 215 |
|
| 216 |
|
| 217 |
static bool |
| 218 |
parse_hstore(HSParser *state) |
| 219 |
{ |
| 220 |
int st = WKEY; |
| 221 |
bool escaped = false; |
| 222 |
|
| 223 |
state->plen = 16; |
| 224 |
state->pairs = (Pairs *) palloc(sizeof(Pairs) * state->plen); |
| 225 |
state->pcur = 0; |
| 226 |
state->ptr = state->begin; |
| 227 |
state->word = NULL; |
| 228 |
|
| 229 |
while (1) |
| 230 |
{ |
| 231 |
if (st == WKEY) |
| 232 |
{ |
| 233 |
if (!get_val(state, false, &escaped)) |
| 234 |
{ |
| 235 |
if (SOFT_ERROR_OCCURRED(state->escontext)) |
| 236 |
return false; |
| 237 |
return true; /* EOF, all okay */ |
| 238 |
} |
| 239 |
if (state->pcur >= state->plen) |
| 240 |
{ |
| 241 |
state->plen *= 2; |
| 242 |
state->pairs = (Pairs *) repalloc(state->pairs, sizeof(Pairs) * state->plen); |
| 243 |
} |
| 244 |
if (!hstoreCheckKeyLength(state->cur - state->word, state)) |
| 245 |
return false; |
| 246 |
state->pairs[state->pcur].key = state->word; |
| 247 |
state->pairs[state->pcur].keylen = state->cur - state->word; |
| 248 |
state->pairs[state->pcur].val = NULL; |
| 249 |
state->word = NULL; |
| 250 |
st = WEQ; |
| 251 |
} |
| 252 |
else if (st == WEQ) |
| 253 |
{ |
| 254 |
if (*(state->ptr) == '=') |
| 255 |
{ |
| 256 |
st = WGT; |
| 257 |
} |
| 258 |
else if (*(state->ptr) == '\0') |
| 259 |
{ |
| 260 |
PRSEOF; |
| 261 |
} |
| 262 |
else if (!scanner_isspace((unsigned char) *(state->ptr))) |
| 263 |
{ |
| 264 |
PRSSYNTAXERROR; |
| 265 |
} |
| 266 |
} |
| 267 |
else if (st == WGT) |
| 268 |
{ |
| 269 |
if (*(state->ptr) == '>') |
| 270 |
{ |
| 271 |
st = WVAL; |
| 272 |
} |
| 273 |
else if (*(state->ptr) == '\0') |
| 274 |
{ |
| 275 |
PRSEOF; |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
PRSSYNTAXERROR; |
| 280 |
} |
| 281 |
} |
| 282 |
else if (st == WVAL) |
| 283 |
{ |
| 284 |
if (!get_val(state, true, &escaped)) |
| 285 |
{ |
| 286 |
if (SOFT_ERROR_OCCURRED(state->escontext)) |
| 287 |
return false; |
| 288 |
PRSEOF; |
| 289 |
} |
| 290 |
if (!hstoreCheckValLength(state->cur - state->word, state)) |
| 291 |
return false; |
| 292 |
state->pairs[state->pcur].val = state->word; |
| 293 |
state->pairs[state->pcur].vallen = state->cur - state->word; |
| 294 |
state->pairs[state->pcur].isnull = false; |
| 295 |
state->pairs[state->pcur].needfree = true; |
| 296 |
if (state->cur - state->word == 4 && !escaped) |
| 297 |
{ |
| 298 |
state->word[4] = '\0'; |
| 299 |
if (pg_strcasecmp(state->word, "null") == 0) |
| 300 |
state->pairs[state->pcur].isnull = true; |
| 301 |
} |
| 302 |
state->word = NULL; |
| 303 |
state->pcur++; |
| 304 |
st = WDEL; |
| 305 |
} |
| 306 |
else if (st == WDEL) |
| 307 |
{ |
| 308 |
if (*(state->ptr) == ',') |
| 309 |
{ |
| 310 |
st = WKEY; |
| 311 |
} |
| 312 |
else if (*(state->ptr) == '\0') |
| 313 |
{ |
| 314 |
return true; |
| 315 |
} |
| 316 |
else if (!scanner_isspace((unsigned char) *(state->ptr))) |
| 317 |
{ |
| 318 |
PRSSYNTAXERROR; |
| 319 |
} |
| 320 |
} |
| 321 |
else |
| 322 |
elog(ERROR, "unrecognized parse_hstore state: %d", st); |
| 323 |
|
| 324 |
state->ptr++; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
static int |
| 329 |
comparePairs(const void *a, const void *b) |
| 330 |
{ |
| 331 |
const Pairs *pa = a; |
| 332 |
const Pairs *pb = b; |
| 333 |
|
| 334 |
if (pa->keylen == pb->keylen) |
| 335 |
{ |
| 336 |
int res = memcmp(pa->key, pb->key, pa->keylen); |
| 337 |
|
| 338 |
if (res) |
| 339 |
return res; |
| 340 |
|
| 341 |
/* guarantee that needfree will be later */ |
| 342 |
if (pb->needfree == pa->needfree) |
| 343 |
return 0; |
| 344 |
else if (pa->needfree) |
| 345 |
return 1; |
| 346 |
else |
| 347 |
return -1; |
| 348 |
} |
| 349 |
return (pa->keylen > pb->keylen) ? 1 : -1; |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
* this code still respects pairs.needfree, even though in general |
| 354 |
* it should never be called in a context where anything needs freeing. |
| 355 |
* we keep it because (a) those calls are in a rare code path anyway, |
| 356 |
* and (b) who knows whether they might be needed by some caller. |
| 357 |
*/ |
| 358 |
int |
| 359 |
hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen) |
| 360 |
{ |
| 361 |
Pairs *ptr, |
| 362 |
*res; |
| 363 |
|
| 364 |
*buflen = 0; |
| 365 |
if (l < 2) |
| 366 |
{ |
| 367 |
if (l == 1) |
| 368 |
*buflen = a->keylen + ((a->isnull) ? 0 : a->vallen); |
| 369 |
return l; |
| 370 |
} |
| 371 |
|
| 372 |
qsort(a, l, sizeof(Pairs), comparePairs); |
| 373 |
|
| 374 |
/* |
| 375 |
* We can't use qunique here because we have some clean-up code to run on |
| 376 |
* removed elements. |
| 377 |
*/ |
| 378 |
ptr = a + 1; |
| 379 |
res = a; |
| 380 |
while (ptr - a < l) |
| 381 |
{ |
| 382 |
if (ptr->keylen == res->keylen && |
| 383 |
memcmp(ptr->key, res->key, res->keylen) == 0) |
| 384 |
{ |
| 385 |
if (ptr->needfree) |
| 386 |
{ |
| 387 |
pfree(ptr->key); |
| 388 |
if (ptr->val != NULL) |
| 389 |
pfree(ptr->val); |
| 390 |
} |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
*buflen += res->keylen + ((res->isnull) ? 0 : res->vallen); |
| 395 |
res++; |
| 396 |
if (res != ptr) |
| 397 |
memcpy(res, ptr, sizeof(Pairs)); |
| 398 |
} |
| 399 |
|
| 400 |
ptr++; |
| 401 |
} |
| 402 |
|
| 403 |
*buflen += res->keylen + ((res->isnull) ? 0 : res->vallen); |
| 404 |
return res + 1 - a; |
| 405 |
} |
| 406 |
|
| 407 |
size_t |
| 408 |
hstoreCheckKeyLen(size_t len) |
| 409 |
{ |
| 410 |
if (len > HSTORE_MAX_KEY_LEN) |
| 411 |
ereport(ERROR, |
| 412 |
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), |
| 413 |
errmsg("string too long for hstore key"))); |
| 414 |
return len; |
| 415 |
} |
| 416 |
|
| 417 |
static bool |
| 418 |
hstoreCheckKeyLength(size_t len, HSParser *state) |
| 419 |
{ |
| 420 |
if (len > HSTORE_MAX_KEY_LEN) |
| 421 |
ereturn(state->escontext, false, |
| 422 |
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), |
| 423 |
errmsg("string too long for hstore key"))); |
| 424 |
return true; |
| 425 |
} |
| 426 |
|
| 427 |
size_t |
| 428 |
hstoreCheckValLen(size_t len) |
| 429 |
{ |
| 430 |
if (len > HSTORE_MAX_VALUE_LEN) |
| 431 |
ereport(ERROR, |
| 432 |
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), |
| 433 |
errmsg("string too long for hstore value"))); |
| 434 |
return len; |
| 435 |
} |
| 436 |
|
| 437 |
static bool |
| 438 |
hstoreCheckValLength(size_t len, HSParser *state) |
| 439 |
{ |
| 440 |
if (len > HSTORE_MAX_VALUE_LEN) |
| 441 |
ereturn(state->escontext, false, |
| 442 |
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), |
| 443 |
errmsg("string too long for hstore value"))); |
| 444 |
return true; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
HStore * |
| 449 |
hstorePairs(Pairs *pairs, int32 pcount, int32 buflen) |
| 450 |
{ |
| 451 |
HStore *out; |
| 452 |
HEntry *entry; |
| 453 |
char *ptr; |
| 454 |
char *buf; |
| 455 |
int32 len; |
| 456 |
int32 i; |
| 457 |
|
| 458 |
len = CALCDATASIZE(pcount, buflen); |
| 459 |
out = palloc(len); |
| 460 |
SET_VARSIZE(out, len); |
| 461 |
HS_SETCOUNT(out, pcount); |
| 462 |
|
| 463 |
if (pcount == 0) |
| 464 |
return out; |
| 465 |
|
| 466 |
entry = ARRPTR(out); |
| 467 |
buf = ptr = STRPTR(out); |
| 468 |
|
| 469 |
for (i = 0; i < pcount; i++) |
| 470 |
HS_ADDITEM(entry, buf, ptr, pairs[i]); |
| 471 |
|
| 472 |
HS_FINALIZE(out, pcount, buf, ptr); |
| 473 |
|
| 474 |
return out; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
PG_FUNCTION_INFO_V1(hstore_in); |
| 479 |
Datum |
| 480 |
hstore_in(PG_FUNCTION_ARGS) |
| 481 |
{ |
| 482 |
char *str = PG_GETARG_CSTRING(0); |
| 483 |
Node *escontext = fcinfo->context; |
| 484 |
HSParser state; |
| 485 |
int32 buflen; |
| 486 |
HStore *out; |
| 487 |
|
| 488 |
state.begin = str; |
| 489 |
state.escontext = escontext; |
| 490 |
|
| 491 |
if (!parse_hstore(&state)) |
| 492 |
PG_RETURN_NULL(); |
| 493 |
|
| 494 |
state.pcur = hstoreUniquePairs(state.pairs, state.pcur, &buflen); |
| 495 |
|
| 496 |
out = hstorePairs(state.pairs, state.pcur, buflen); |
| 497 |
|
| 498 |
PG_RETURN_POINTER(out); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
PG_FUNCTION_INFO_V1(hstore_recv); |
| 503 |
Datum |
| 504 |
hstore_recv(PG_FUNCTION_ARGS) |
| 505 |
{ |
| 506 |
int32 buflen; |
| 507 |
HStore *out; |
| 508 |
Pairs *pairs; |
| 509 |
int32 i; |
| 510 |
int32 pcount; |
| 511 |
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); |
| 512 |
|
| 513 |
pcount = pq_getmsgint(buf, 4); |
| 514 |
|
| 515 |
if (pcount == 0) |
| 516 |
{ |
| 517 |
out = hstorePairs(NULL, 0, 0); |
| 518 |
PG_RETURN_POINTER(out); |
| 519 |
} |
| 520 |
|
| 521 |
if (pcount < 0 || pcount > MaxAllocSize / sizeof(Pairs)) |
| 522 |
ereport(ERROR, |
| 523 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 524 |
errmsg("number of pairs (%d) exceeds the maximum allowed (%d)", |
| 525 |
pcount, (int) (MaxAllocSize / sizeof(Pairs))))); |
| 526 |
pairs = palloc(pcount * sizeof(Pairs)); |
| 527 |
|
| 528 |
for (i = 0; i < pcount; ++i) |
| 529 |
{ |
| 530 |
int rawlen = pq_getmsgint(buf, 4); |
| 531 |
int len; |
| 532 |
|
| 533 |
if (rawlen < 0) |
| 534 |
ereport(ERROR, |
| 535 |
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), |
| 536 |
errmsg("null value not allowed for hstore key"))); |
| 537 |
|
| 538 |
pairs[i].key = pq_getmsgtext(buf, rawlen, &len); |
| 539 |
pairs[i].keylen = hstoreCheckKeyLen(len); |
| 540 |
pairs[i].needfree = true; |
| 541 |
|
| 542 |
rawlen = pq_getmsgint(buf, 4); |
| 543 |
if (rawlen < 0) |
| 544 |
{ |
| 545 |
pairs[i].val = NULL; |
| 546 |
pairs[i].vallen = 0; |
| 547 |
pairs[i].isnull = true; |
| 548 |
} |
| 549 |
else |
| 550 |
{ |
| 551 |
pairs[i].val = pq_getmsgtext(buf, rawlen, &len); |
| 552 |
pairs[i].vallen = hstoreCheckValLen(len); |
| 553 |
pairs[i].isnull = false; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
pcount = hstoreUniquePairs(pairs, pcount, &buflen); |
| 558 |
|
| 559 |
out = hstorePairs(pairs, pcount, buflen); |
| 560 |
|
| 561 |
PG_RETURN_POINTER(out); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
PG_FUNCTION_INFO_V1(hstore_from_text); |
| 566 |
Datum |
| 567 |
hstore_from_text(PG_FUNCTION_ARGS) |
| 568 |
{ |
| 569 |
text *key; |
| 570 |
text *val = NULL; |
| 571 |
Pairs p; |
| 572 |
HStore *out; |
| 573 |
|
| 574 |
if (PG_ARGISNULL(0)) |
| 575 |
PG_RETURN_NULL(); |
| 576 |
|
| 577 |
p.needfree = false; |
| 578 |
key = PG_GETARG_TEXT_PP(0); |
| 579 |
p.key = VARDATA_ANY(key); |
| 580 |
p.keylen = hstoreCheckKeyLen(VARSIZE_ANY_EXHDR(key)); |
| 581 |
|
| 582 |
if (PG_ARGISNULL(1)) |
| 583 |
{ |
| 584 |
p.vallen = 0; |
| 585 |
p.isnull = true; |
| 586 |
} |
| 587 |
else |
| 588 |
{ |
| 589 |
val = PG_GETARG_TEXT_PP(1); |
| 590 |
p.val = VARDATA_ANY(val); |
| 591 |
p.vallen = hstoreCheckValLen(VARSIZE_ANY_EXHDR(val)); |
| 592 |
p.isnull = false; |
| 593 |
} |
| 594 |
|
| 595 |
out = hstorePairs(&p, 1, p.keylen + p.vallen); |
| 596 |
|
| 597 |
PG_RETURN_POINTER(out); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
PG_FUNCTION_INFO_V1(hstore_from_arrays); |
| 602 |
Datum |
| 603 |
hstore_from_arrays(PG_FUNCTION_ARGS) |
| 604 |
{ |
| 605 |
int32 buflen; |
| 606 |
HStore *out; |
| 607 |
Pairs *pairs; |
| 608 |
Datum *key_datums; |
| 609 |
bool *key_nulls; |
| 610 |
int key_count; |
| 611 |
Datum *value_datums; |
| 612 |
bool *value_nulls; |
| 613 |
int value_count; |
| 614 |
ArrayType *key_array; |
| 615 |
ArrayType *value_array; |
| 616 |
int i; |
| 617 |
|
| 618 |
if (PG_ARGISNULL(0)) |
| 619 |
PG_RETURN_NULL(); |
| 620 |
|
| 621 |
key_array = PG_GETARG_ARRAYTYPE_P(0); |
| 622 |
|
| 623 |
Assert(ARR_ELEMTYPE(key_array) == TEXTOID); |
| 624 |
|
| 625 |
/* |
| 626 |
* must check >1 rather than != 1 because empty arrays have 0 dimensions, |
| 627 |
* not 1 |
| 628 |
*/ |
| 629 |
|
| 630 |
if (ARR_NDIM(key_array) > 1) |
| 631 |
ereport(ERROR, |
| 632 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 633 |
errmsg("wrong number of array subscripts"))); |
| 634 |
|
| 635 |
deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count); |
| 636 |
|
| 637 |
/* see discussion in hstoreArrayToPairs() */ |
| 638 |
if (key_count > MaxAllocSize / sizeof(Pairs)) |
| 639 |
ereport(ERROR, |
| 640 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 641 |
errmsg("number of pairs (%d) exceeds the maximum allowed (%d)", |
| 642 |
key_count, (int) (MaxAllocSize / sizeof(Pairs))))); |
| 643 |
|
| 644 |
/* value_array might be NULL */ |
| 645 |
|
| 646 |
if (PG_ARGISNULL(1)) |
| 647 |
{ |
| 648 |
value_array = NULL; |
| 649 |
value_count = key_count; |
| 650 |
value_datums = NULL; |
| 651 |
value_nulls = NULL; |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
value_array = PG_GETARG_ARRAYTYPE_P(1); |
| 656 |
|
| 657 |
Assert(ARR_ELEMTYPE(value_array) == TEXTOID); |
| 658 |
|
| 659 |
if (ARR_NDIM(value_array) > 1) |
| 660 |
ereport(ERROR, |
| 661 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 662 |
errmsg("wrong number of array subscripts"))); |
| 663 |
|
| 664 |
if ((ARR_NDIM(key_array) > 0 || ARR_NDIM(value_array) > 0) && |
| 665 |
(ARR_NDIM(key_array) != ARR_NDIM(value_array) || |
| 666 |
ARR_DIMS(key_array)[0] != ARR_DIMS(value_array)[0] || |
| 667 |
ARR_LBOUND(key_array)[0] != ARR_LBOUND(value_array)[0])) |
| 668 |
ereport(ERROR, |
| 669 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 670 |
errmsg("arrays must have same bounds"))); |
| 671 |
|
| 672 |
deconstruct_array_builtin(value_array, TEXTOID, &value_datums, &value_nulls, &value_count); |
| 673 |
|
| 674 |
Assert(key_count == value_count); |
| 675 |
} |
| 676 |
|
| 677 |
pairs = palloc(key_count * sizeof(Pairs)); |
| 678 |
|
| 679 |
for (i = 0; i < key_count; ++i) |
| 680 |
{ |
| 681 |
if (key_nulls[i]) |
| 682 |
ereport(ERROR, |
| 683 |
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), |
| 684 |
errmsg("null value not allowed for hstore key"))); |
| 685 |
|
| 686 |
if (!value_nulls || value_nulls[i]) |
| 687 |
{ |
| 688 |
pairs[i].key = VARDATA(key_datums[i]); |
| 689 |
pairs[i].val = NULL; |
| 690 |
pairs[i].keylen = |
| 691 |
hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ); |
| 692 |
pairs[i].vallen = 4; |
| 693 |
pairs[i].isnull = true; |
| 694 |
pairs[i].needfree = false; |
| 695 |
} |
| 696 |
else |
| 697 |
{ |
| 698 |
pairs[i].key = VARDATA(key_datums[i]); |
| 699 |
pairs[i].val = VARDATA(value_datums[i]); |
| 700 |
pairs[i].keylen = |
| 701 |
hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ); |
| 702 |
pairs[i].vallen = |
| 703 |
hstoreCheckValLen(VARSIZE(value_datums[i]) - VARHDRSZ); |
| 704 |
pairs[i].isnull = false; |
| 705 |
pairs[i].needfree = false; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
key_count = hstoreUniquePairs(pairs, key_count, &buflen); |
| 710 |
|
| 711 |
out = hstorePairs(pairs, key_count, buflen); |
| 712 |
|
| 713 |
PG_RETURN_POINTER(out); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
PG_FUNCTION_INFO_V1(hstore_from_array); |
| 718 |
Datum |
| 719 |
hstore_from_array(PG_FUNCTION_ARGS) |
| 720 |
{ |
| 721 |
ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0); |
| 722 |
int ndims = ARR_NDIM(in_array); |
| 723 |
int count; |
| 724 |
int32 buflen; |
| 725 |
HStore *out; |
| 726 |
Pairs *pairs; |
| 727 |
Datum *in_datums; |
| 728 |
bool *in_nulls; |
| 729 |
int in_count; |
| 730 |
int i; |
| 731 |
|
| 732 |
Assert(ARR_ELEMTYPE(in_array) == TEXTOID); |
| 733 |
|
| 734 |
switch (ndims) |
| 735 |
{ |
| 736 |
case 0: |
| 737 |
out = hstorePairs(NULL, 0, 0); |
| 738 |
PG_RETURN_POINTER(out); |
| 739 |
|
| 740 |
case 1: |
| 741 |
if ((ARR_DIMS(in_array)[0]) % 2) |
| 742 |
ereport(ERROR, |
| 743 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 744 |
errmsg("array must have even number of elements"))); |
| 745 |
break; |
| 746 |
|
| 747 |
case 2: |
| 748 |
if ((ARR_DIMS(in_array)[1]) != 2) |
| 749 |
ereport(ERROR, |
| 750 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 751 |
errmsg("array must have two columns"))); |
| 752 |
break; |
| 753 |
|
| 754 |
default: |
| 755 |
ereport(ERROR, |
| 756 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 757 |
errmsg("wrong number of array subscripts"))); |
| 758 |
} |
| 759 |
|
| 760 |
deconstruct_array_builtin(in_array, TEXTOID, &in_datums, &in_nulls, &in_count); |
| 761 |
|
| 762 |
count = in_count / 2; |
| 763 |
|
| 764 |
/* see discussion in hstoreArrayToPairs() */ |
| 765 |
if (count > MaxAllocSize / sizeof(Pairs)) |
| 766 |
ereport(ERROR, |
| 767 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 768 |
errmsg("number of pairs (%d) exceeds the maximum allowed (%d)", |
| 769 |
count, (int) (MaxAllocSize / sizeof(Pairs))))); |
| 770 |
|
| 771 |
pairs = palloc(count * sizeof(Pairs)); |
| 772 |
|
| 773 |
for (i = 0; i < count; ++i) |
| 774 |
{ |
| 775 |
if (in_nulls[i * 2]) |
| 776 |
ereport(ERROR, |
| 777 |
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), |
| 778 |
errmsg("null value not allowed for hstore key"))); |
| 779 |
|
| 780 |
if (in_nulls[i * 2 + 1]) |
| 781 |
{ |
| 782 |
pairs[i].key = VARDATA(in_datums[i * 2]); |
| 783 |
pairs[i].val = NULL; |
| 784 |
pairs[i].keylen = |
| 785 |
hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ); |
| 786 |
pairs[i].vallen = 4; |
| 787 |
pairs[i].isnull = true; |
| 788 |
pairs[i].needfree = false; |
| 789 |
} |
| 790 |
else |
| 791 |
{ |
| 792 |
pairs[i].key = VARDATA(in_datums[i * 2]); |
| 793 |
pairs[i].val = VARDATA(in_datums[i * 2 + 1]); |
| 794 |
pairs[i].keylen = |
| 795 |
hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ); |
| 796 |
pairs[i].vallen = |
| 797 |
hstoreCheckValLen(VARSIZE(in_datums[i * 2 + 1]) - VARHDRSZ); |
| 798 |
pairs[i].isnull = false; |
| 799 |
pairs[i].needfree = false; |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
count = hstoreUniquePairs(pairs, count, &buflen); |
| 804 |
|
| 805 |
out = hstorePairs(pairs, count, buflen); |
| 806 |
|
| 807 |
PG_RETURN_POINTER(out); |
| 808 |
} |
| 809 |
|
| 810 |
/* most of hstore_from_record is shamelessly swiped from record_out */ |
| 811 |
|
| 812 |
/* |
| 813 |
* structure to cache metadata needed for record I/O |
| 814 |
*/ |
| 815 |
typedef struct ColumnIOData |
| 816 |
{ |
| 817 |
Oid column_type; |
| 818 |
Oid typiofunc; |
| 819 |
Oid typioparam; |
| 820 |
FmgrInfo proc; |
| 821 |
} ColumnIOData; |
| 822 |
|
| 823 |
typedef struct RecordIOData |
| 824 |
{ |
| 825 |
Oid record_type; |
| 826 |
int32 record_typmod; |
| 827 |
/* this field is used only if target type is domain over composite: */ |
| 828 |
void *domain_info; /* opaque cache for domain checks */ |
| 829 |
int ncolumns; |
| 830 |
ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER]; |
| 831 |
} RecordIOData; |
| 832 |
|
| 833 |
PG_FUNCTION_INFO_V1(hstore_from_record); |
| 834 |
Datum |
| 835 |
hstore_from_record(PG_FUNCTION_ARGS) |
| 836 |
{ |
| 837 |
HeapTupleHeader rec; |
| 838 |
int32 buflen; |
| 839 |
HStore *out; |
| 840 |
Pairs *pairs; |
| 841 |
Oid tupType; |
| 842 |
int32 tupTypmod; |
| 843 |
TupleDesc tupdesc; |
| 844 |
HeapTupleData tuple; |
| 845 |
RecordIOData *my_extra; |
| 846 |
int ncolumns; |
| 847 |
int i, |
| 848 |
j; |
| 849 |
Datum *values; |
| 850 |
bool *nulls; |
| 851 |
|
| 852 |
if (PG_ARGISNULL(0)) |
| 853 |
{ |
| 854 |
Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0); |
| 855 |
|
| 856 |
/* |
| 857 |
* We have no tuple to look at, so the only source of type info is the |
| 858 |
* argtype --- which might be domain over composite, but we don't care |
| 859 |
* here, since we have no need to be concerned about domain |
| 860 |
* constraints. The lookup_rowtype_tupdesc_domain call below will |
| 861 |
* error out if we don't have a known composite type oid here. |
| 862 |
*/ |
| 863 |
tupType = argtype; |
| 864 |
tupTypmod = -1; |
| 865 |
|
| 866 |
rec = NULL; |
| 867 |
} |
| 868 |
else |
| 869 |
{ |
| 870 |
rec = PG_GETARG_HEAPTUPLEHEADER(0); |
| 871 |
|
| 872 |
/* |
| 873 |
* Extract type info from the tuple itself -- this will work even for |
| 874 |
* anonymous record types. |
| 875 |
*/ |
| 876 |
tupType = HeapTupleHeaderGetTypeId(rec); |
| 877 |
tupTypmod = HeapTupleHeaderGetTypMod(rec); |
| 878 |
} |
| 879 |
|
| 880 |
tupdesc = lookup_rowtype_tupdesc_domain(tupType, tupTypmod, false); |
| 881 |
ncolumns = tupdesc->natts; |
| 882 |
|
| 883 |
/* |
| 884 |
* We arrange to look up the needed I/O info just once per series of |
| 885 |
* calls, assuming the record type doesn't change underneath us. |
| 886 |
*/ |
| 887 |
my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra; |
| 888 |
if (my_extra == NULL || |
| 889 |
my_extra->ncolumns != ncolumns) |
| 890 |
{ |
| 891 |
fcinfo->flinfo->fn_extra = |
| 892 |
MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, |
| 893 |
offsetof(RecordIOData, columns) + |
| 894 |
ncolumns * sizeof(ColumnIOData)); |
| 895 |
my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra; |
| 896 |
my_extra->record_type = InvalidOid; |
| 897 |
my_extra->record_typmod = 0; |
| 898 |
} |
| 899 |
|
| 900 |
if (my_extra->record_type != tupType || |
| 901 |
my_extra->record_typmod != tupTypmod) |
| 902 |
{ |
| 903 |
MemSet(my_extra, 0, |
| 904 |
offsetof(RecordIOData, columns) + |
| 905 |
ncolumns * sizeof(ColumnIOData)); |
| 906 |
my_extra->record_type = tupType; |
| 907 |
my_extra->record_typmod = tupTypmod; |
| 908 |
my_extra->ncolumns = ncolumns; |
| 909 |
} |
| 910 |
|
| 911 |
Assert(ncolumns <= MaxTupleAttributeNumber); /* thus, no overflow */ |
| 912 |
pairs = palloc(ncolumns * sizeof(Pairs)); |
| 913 |
|
| 914 |
if (rec) |
| 915 |
{ |
| 916 |
/* Build a temporary HeapTuple control structure */ |
| 917 |
tuple.t_len = HeapTupleHeaderGetDatumLength(rec); |
| 918 |
ItemPointerSetInvalid(&(tuple.t_self)); |
| 919 |
tuple.t_tableOid = InvalidOid; |
| 920 |
tuple.t_data = rec; |
| 921 |
|
| 922 |
values = (Datum *) palloc(ncolumns * sizeof(Datum)); |
| 923 |
nulls = (bool *) palloc(ncolumns * sizeof(bool)); |
| 924 |
|
| 925 |
/* Break down the tuple into fields */ |
| 926 |
heap_deform_tuple(&tuple, tupdesc, values, nulls); |
| 927 |
} |
| 928 |
else |
| 929 |
{ |
| 930 |
values = NULL; |
| 931 |
nulls = NULL; |
| 932 |
} |
| 933 |
|
| 934 |
for (i = 0, j = 0; i < ncolumns; ++i) |
| 935 |
{ |
| 936 |
ColumnIOData *column_info = &my_extra->columns[i]; |
| 937 |
Form_pg_attribute att = TupleDescAttr(tupdesc, i); |
| 938 |
Oid column_type = att->atttypid; |
| 939 |
char *value; |
| 940 |
|
| 941 |
/* Ignore dropped columns in datatype */ |
| 942 |
if (att->attisdropped) |
| 943 |
continue; |
| 944 |
|
| 945 |
pairs[j].key = NameStr(att->attname); |
| 946 |
pairs[j].keylen = hstoreCheckKeyLen(strlen(NameStr(att->attname))); |
| 947 |
|
| 948 |
if (!nulls || nulls[i]) |
| 949 |
{ |
| 950 |
pairs[j].val = NULL; |
| 951 |
pairs[j].vallen = 4; |
| 952 |
pairs[j].isnull = true; |
| 953 |
pairs[j].needfree = false; |
| 954 |
++j; |
| 955 |
continue; |
| 956 |
} |
| 957 |
|
| 958 |
/* |
| 959 |
* Convert the column value to text |
| 960 |
*/ |
| 961 |
if (column_info->column_type != column_type) |
| 962 |
{ |
| 963 |
bool typIsVarlena; |
| 964 |
|
| 965 |
getTypeOutputInfo(column_type, |
| 966 |
&column_info->typiofunc, |
| 967 |
&typIsVarlena); |
| 968 |
fmgr_info_cxt(column_info->typiofunc, &column_info->proc, |
| 969 |
fcinfo->flinfo->fn_mcxt); |
| 970 |
column_info->column_type = column_type; |
| 971 |
} |
| 972 |
|
| 973 |
value = OutputFunctionCall(&column_info->proc, values[i]); |
| 974 |
|
| 975 |
pairs[j].val = value; |
| 976 |
pairs[j].vallen = hstoreCheckValLen(strlen(value)); |
| 977 |
pairs[j].isnull = false; |
| 978 |
pairs[j].needfree = false; |
| 979 |
++j; |
| 980 |
} |
| 981 |
|
| 982 |
ncolumns = hstoreUniquePairs(pairs, j, &buflen); |
| 983 |
|
| 984 |
out = hstorePairs(pairs, ncolumns, buflen); |
| 985 |
|
| 986 |
ReleaseTupleDesc(tupdesc); |
| 987 |
|
| 988 |
PG_RETURN_POINTER(out); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
PG_FUNCTION_INFO_V1(hstore_populate_record); |
| 993 |
Datum |
| 994 |
hstore_populate_record(PG_FUNCTION_ARGS) |
| 995 |
{ |
| 996 |
Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0); |
| 997 |
HStore *hs; |
| 998 |
HEntry *entries; |
| 999 |
char *ptr; |
| 1000 |
HeapTupleHeader rec; |
| 1001 |
Oid tupType; |
| 1002 |
int32 tupTypmod; |
| 1003 |
TupleDesc tupdesc; |
| 1004 |
HeapTupleData tuple; |
| 1005 |
HeapTuple rettuple; |
| 1006 |
RecordIOData *my_extra; |
| 1007 |
int ncolumns; |
| 1008 |
int i; |
| 1009 |
Datum *values; |
| 1010 |
bool *nulls; |
| 1011 |
|
| 1012 |
if (!type_is_rowtype(argtype)) |
| 1013 |
ereport(ERROR, |
| 1014 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 1015 |
errmsg("first argument must be a rowtype"))); |
| 1016 |
|
| 1017 |
if (PG_ARGISNULL(0)) |
| 1018 |
{ |
| 1019 |
if (PG_ARGISNULL(1)) |
| 1020 |
PG_RETURN_NULL(); |
| 1021 |
|
| 1022 |
rec = NULL; |
| 1023 |
|
| 1024 |
/* |
| 1025 |
* We have no tuple to look at, so the only source of type info is the |
| 1026 |
* argtype. The lookup_rowtype_tupdesc_domain call below will error |
| 1027 |
* out if we don't have a known composite type oid here. |
| 1028 |
*/ |
| 1029 |
tupType = argtype; |
| 1030 |
tupTypmod = -1; |
| 1031 |
} |
| 1032 |
else |
| 1033 |
{ |
| 1034 |
rec = PG_GETARG_HEAPTUPLEHEADER(0); |
| 1035 |
|
| 1036 |
if (PG_ARGISNULL(1)) |
| 1037 |
PG_RETURN_POINTER(rec); |
| 1038 |
|
| 1039 |
/* |
| 1040 |
* Extract type info from the tuple itself -- this will work even for |
| 1041 |
* anonymous record types. |
| 1042 |
*/ |
| 1043 |
tupType = HeapTupleHeaderGetTypeId(rec); |
| 1044 |
tupTypmod = HeapTupleHeaderGetTypMod(rec); |
| 1045 |
} |
| 1046 |
|
| 1047 |
hs = PG_GETARG_HSTORE_P(1); |
| 1048 |
entries = ARRPTR(hs); |
| 1049 |
ptr = STRPTR(hs); |
| 1050 |
|
| 1051 |
/* |
| 1052 |
* if the input hstore is empty, we can only skip the rest if we were |
| 1053 |
* passed in a non-null record, since otherwise there may be issues with |
| 1054 |
* domain nulls. |
| 1055 |
*/ |
| 1056 |
|
| 1057 |
if (HS_COUNT(hs) == 0 && rec) |
| 1058 |
PG_RETURN_POINTER(rec); |
| 1059 |
|
| 1060 |
/* |
| 1061 |
* Lookup the input record's tupdesc. For the moment, we don't worry |
| 1062 |
* about whether it is a domain over composite. |
| 1063 |
*/ |
| 1064 |
tupdesc = lookup_rowtype_tupdesc_domain(tupType, tupTypmod, false); |
| 1065 |
ncolumns = tupdesc->natts; |
| 1066 |
|
| 1067 |
if (rec) |
| 1068 |
{ |
| 1069 |
/* Build a temporary HeapTuple control structure */ |
| 1070 |
tuple.t_len = HeapTupleHeaderGetDatumLength(rec); |
| 1071 |
ItemPointerSetInvalid(&(tuple.t_self)); |
| 1072 |
tuple.t_tableOid = InvalidOid; |
| 1073 |
tuple.t_data = rec; |
| 1074 |
} |
| 1075 |
|
| 1076 |
/* |
| 1077 |
* We arrange to look up the needed I/O info just once per series of |
| 1078 |
* calls, assuming the record type doesn't change underneath us. |
| 1079 |
*/ |
| 1080 |
my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra; |
| 1081 |
if (my_extra == NULL || |
| 1082 |
my_extra->ncolumns != ncolumns) |
| 1083 |
{ |
| 1084 |
fcinfo->flinfo->fn_extra = |
| 1085 |
MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, |
| 1086 |
offsetof(RecordIOData, columns) + |
| 1087 |
ncolumns * sizeof(ColumnIOData)); |
| 1088 |
my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra; |
| 1089 |
my_extra->record_type = InvalidOid; |
| 1090 |
my_extra->record_typmod = 0; |
| 1091 |
my_extra->domain_info = NULL; |
| 1092 |
} |
| 1093 |
|
| 1094 |
if (my_extra->record_type != tupType || |
| 1095 |
my_extra->record_typmod != tupTypmod) |
| 1096 |
{ |
| 1097 |
MemSet(my_extra, 0, |
| 1098 |
offsetof(RecordIOData, columns) + |
| 1099 |
ncolumns * sizeof(ColumnIOData)); |
| 1100 |
my_extra->record_type = tupType; |
| 1101 |
my_extra->record_typmod = tupTypmod; |
| 1102 |
my_extra->ncolumns = ncolumns; |
| 1103 |
} |
| 1104 |
|
| 1105 |
values = (Datum *) palloc(ncolumns * sizeof(Datum)); |
| 1106 |
nulls = (bool *) palloc(ncolumns * sizeof(bool)); |
| 1107 |
|
| 1108 |
if (rec) |
| 1109 |
{ |
| 1110 |
/* Break down the tuple into fields */ |
| 1111 |
heap_deform_tuple(&tuple, tupdesc, values, nulls); |
| 1112 |
} |
| 1113 |
else |
| 1114 |
{ |
| 1115 |
for (i = 0; i < ncolumns; ++i) |
| 1116 |
{ |
| 1117 |
values[i] = (Datum) 0; |
| 1118 |
nulls[i] = true; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
for (i = 0; i < ncolumns; ++i) |
| 1123 |
{ |
| 1124 |
ColumnIOData *column_info = &my_extra->columns[i]; |
| 1125 |
Form_pg_attribute att = TupleDescAttr(tupdesc, i); |
| 1126 |
Oid column_type = att->atttypid; |
| 1127 |
char *value; |
| 1128 |
int idx; |
| 1129 |
int vallen; |
| 1130 |
|
| 1131 |
/* Ignore dropped columns in datatype */ |
| 1132 |
if (att->attisdropped) |
| 1133 |
{ |
| 1134 |
nulls[i] = true; |
| 1135 |
continue; |
| 1136 |
} |
| 1137 |
|
| 1138 |
idx = hstoreFindKey(hs, 0, |
| 1139 |
NameStr(att->attname), |
| 1140 |
strlen(NameStr(att->attname))); |
| 1141 |
|
| 1142 |
/* |
| 1143 |
* we can't just skip here if the key wasn't found since we might have |
| 1144 |
* a domain to deal with. If we were passed in a non-null record |
| 1145 |
* datum, we assume that the existing values are valid (if they're |
| 1146 |
* not, then it's not our fault), but if we were passed in a null, |
| 1147 |
* then every field which we don't populate needs to be run through |
| 1148 |
* the input function just in case it's a domain type. |
| 1149 |
*/ |
| 1150 |
if (idx < 0 && rec) |
| 1151 |
continue; |
| 1152 |
|
| 1153 |
/* |
| 1154 |
* Prepare to convert the column value from text |
| 1155 |
*/ |
| 1156 |
if (column_info->column_type != column_type) |
| 1157 |
{ |
| 1158 |
getTypeInputInfo(column_type, |
| 1159 |
&column_info->typiofunc, |
| 1160 |
&column_info->typioparam); |
| 1161 |
fmgr_info_cxt(column_info->typiofunc, &column_info->proc, |
| 1162 |
fcinfo->flinfo->fn_mcxt); |
| 1163 |
column_info->column_type = column_type; |
| 1164 |
} |
| 1165 |
|
| 1166 |
if (idx < 0 || HSTORE_VALISNULL(entries, idx)) |
| 1167 |
{ |
| 1168 |
/* |
| 1169 |
* need InputFunctionCall to happen even for nulls, so that domain |
| 1170 |
* checks are done |
| 1171 |
*/ |
| 1172 |
values[i] = InputFunctionCall(&column_info->proc, NULL, |
| 1173 |
column_info->typioparam, |
| 1174 |
att->atttypmod); |
| 1175 |
nulls[i] = true; |
| 1176 |
} |
| 1177 |
else |
| 1178 |
{ |
| 1179 |
vallen = HSTORE_VALLEN(entries, idx); |
| 1180 |
value = palloc(1 + vallen); |
| 1181 |
memcpy(value, HSTORE_VAL(entries, ptr, idx), vallen); |
| 1182 |
value[vallen] = 0; |
| 1183 |
|
| 1184 |
values[i] = InputFunctionCall(&column_info->proc, value, |
| 1185 |
column_info->typioparam, |
| 1186 |
att->atttypmod); |
| 1187 |
nulls[i] = false; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
rettuple = heap_form_tuple(tupdesc, values, nulls); |
| 1192 |
|
| 1193 |
/* |
| 1194 |
* If the target type is domain over composite, all we know at this point |
| 1195 |
* is that we've made a valid value of the base composite type. Must |
| 1196 |
* check domain constraints before deciding we're done. |
| 1197 |
*/ |
| 1198 |
if (argtype != tupdesc->tdtypeid) |
| 1199 |
domain_check(HeapTupleGetDatum(rettuple), false, |
| 1200 |
argtype, |
| 1201 |
&my_extra->domain_info, |
| 1202 |
fcinfo->flinfo->fn_mcxt); |
| 1203 |
|
| 1204 |
ReleaseTupleDesc(tupdesc); |
| 1205 |
|
| 1206 |
PG_RETURN_DATUM(HeapTupleGetDatum(rettuple)); |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
static char * |
| 1211 |
cpw(char *dst, char *src, int len) |
| 1212 |
{ |
| 1213 |
char *ptr = src; |
| 1214 |
|
| 1215 |
while (ptr - src < len) |
| 1216 |
{ |
| 1217 |
if (*ptr == '"' || *ptr == '\\') |
| 1218 |
*dst++ = '\\'; |
| 1219 |
*dst++ = *ptr++; |
| 1220 |
} |
| 1221 |
return dst; |
| 1222 |
} |
| 1223 |
|
| 1224 |
PG_FUNCTION_INFO_V1(hstore_out); |
| 1225 |
Datum |
| 1226 |
hstore_out(PG_FUNCTION_ARGS) |
| 1227 |
{ |
| 1228 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 1229 |
int buflen, |
| 1230 |
i; |
| 1231 |
int count = HS_COUNT(in); |
| 1232 |
char *out, |
| 1233 |
*ptr; |
| 1234 |
char *base = STRPTR(in); |
| 1235 |
HEntry *entries = ARRPTR(in); |
| 1236 |
|
| 1237 |
if (count == 0) |
| 1238 |
PG_RETURN_CSTRING(pstrdup("")); |
| 1239 |
|
| 1240 |
buflen = 0; |
| 1241 |
|
| 1242 |
/* |
| 1243 |
* this loop overestimates due to pessimistic assumptions about escaping, |
| 1244 |
* so very large hstore values can't be output. this could be fixed, but |
| 1245 |
* many other data types probably have the same issue. This replaced code |
| 1246 |
* that used the original varlena size for calculations, which was wrong |
| 1247 |
* in some subtle ways. |
| 1248 |
*/ |
| 1249 |
|
| 1250 |
for (i = 0; i < count; i++) |
| 1251 |
{ |
| 1252 |
/* include "" and => and comma-space */ |
| 1253 |
buflen += 6 + 2 * HSTORE_KEYLEN(entries, i); |
| 1254 |
/* include "" only if nonnull */ |
| 1255 |
buflen += 2 + (HSTORE_VALISNULL(entries, i) |
| 1256 |
? 2 |
| 1257 |
: 2 * HSTORE_VALLEN(entries, i)); |
| 1258 |
} |
| 1259 |
|
| 1260 |
out = ptr = palloc(buflen); |
| 1261 |
|
| 1262 |
for (i = 0; i < count; i++) |
| 1263 |
{ |
| 1264 |
*ptr++ = '"'; |
| 1265 |
ptr = cpw(ptr, HSTORE_KEY(entries, base, i), HSTORE_KEYLEN(entries, i)); |
| 1266 |
*ptr++ = '"'; |
| 1267 |
*ptr++ = '='; |
| 1268 |
*ptr++ = '>'; |
| 1269 |
if (HSTORE_VALISNULL(entries, i)) |
| 1270 |
{ |
| 1271 |
*ptr++ = 'N'; |
| 1272 |
*ptr++ = 'U'; |
| 1273 |
*ptr++ = 'L'; |
| 1274 |
*ptr++ = 'L'; |
| 1275 |
} |
| 1276 |
else |
| 1277 |
{ |
| 1278 |
*ptr++ = '"'; |
| 1279 |
ptr = cpw(ptr, HSTORE_VAL(entries, base, i), HSTORE_VALLEN(entries, i)); |
| 1280 |
*ptr++ = '"'; |
| 1281 |
} |
| 1282 |
|
| 1283 |
if (i + 1 != count) |
| 1284 |
{ |
| 1285 |
*ptr++ = ','; |
| 1286 |
*ptr++ = ' '; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
*ptr = '\0'; |
| 1290 |
|
| 1291 |
PG_RETURN_CSTRING(out); |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
PG_FUNCTION_INFO_V1(hstore_send); |
| 1296 |
Datum |
| 1297 |
hstore_send(PG_FUNCTION_ARGS) |
| 1298 |
{ |
| 1299 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 1300 |
int i; |
| 1301 |
int count = HS_COUNT(in); |
| 1302 |
char *base = STRPTR(in); |
| 1303 |
HEntry *entries = ARRPTR(in); |
| 1304 |
StringInfoData buf; |
| 1305 |
|
| 1306 |
pq_begintypsend(&buf); |
| 1307 |
|
| 1308 |
pq_sendint32(&buf, count); |
| 1309 |
|
| 1310 |
for (i = 0; i < count; i++) |
| 1311 |
{ |
| 1312 |
int32 keylen = HSTORE_KEYLEN(entries, i); |
| 1313 |
|
| 1314 |
pq_sendint32(&buf, keylen); |
| 1315 |
pq_sendtext(&buf, HSTORE_KEY(entries, base, i), keylen); |
| 1316 |
if (HSTORE_VALISNULL(entries, i)) |
| 1317 |
{ |
| 1318 |
pq_sendint32(&buf, -1); |
| 1319 |
} |
| 1320 |
else |
| 1321 |
{ |
| 1322 |
int32 vallen = HSTORE_VALLEN(entries, i); |
| 1323 |
|
| 1324 |
pq_sendint32(&buf, vallen); |
| 1325 |
pq_sendtext(&buf, HSTORE_VAL(entries, base, i), vallen); |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
/* |
| 1334 |
* hstore_to_json_loose |
| 1335 |
* |
| 1336 |
* This is a heuristic conversion to json which treats |
| 1337 |
* 't' and 'f' as booleans and strings that look like numbers as numbers, |
| 1338 |
* as long as they don't start with a leading zero followed by another digit |
| 1339 |
* (think zip codes or phone numbers starting with 0). |
| 1340 |
*/ |
| 1341 |
PG_FUNCTION_INFO_V1(hstore_to_json_loose); |
| 1342 |
Datum |
| 1343 |
hstore_to_json_loose(PG_FUNCTION_ARGS) |
| 1344 |
{ |
| 1345 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 1346 |
int i; |
| 1347 |
int count = HS_COUNT(in); |
| 1348 |
char *base = STRPTR(in); |
| 1349 |
HEntry *entries = ARRPTR(in); |
| 1350 |
StringInfoData dst; |
| 1351 |
|
| 1352 |
if (count == 0) |
| 1353 |
PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2)); |
| 1354 |
|
| 1355 |
initStringInfo(&dst); |
| 1356 |
|
| 1357 |
appendStringInfoChar(&dst, '{'); |
| 1358 |
|
| 1359 |
for (i = 0; i < count; i++) |
| 1360 |
{ |
| 1361 |
escape_json_with_len(&dst, |
| 1362 |
HSTORE_KEY(entries, base, i), |
| 1363 |
HSTORE_KEYLEN(entries, i)); |
| 1364 |
appendStringInfoString(&dst, ": "); |
| 1365 |
if (HSTORE_VALISNULL(entries, i)) |
| 1366 |
appendStringInfoString(&dst, "null"); |
| 1367 |
/* guess that values of 't' or 'f' are booleans */ |
| 1368 |
else if (HSTORE_VALLEN(entries, i) == 1 && |
| 1369 |
*(HSTORE_VAL(entries, base, i)) == 't') |
| 1370 |
appendStringInfoString(&dst, "true"); |
| 1371 |
else if (HSTORE_VALLEN(entries, i) == 1 && |
| 1372 |
*(HSTORE_VAL(entries, base, i)) == 'f') |
| 1373 |
appendStringInfoString(&dst, "false"); |
| 1374 |
else |
| 1375 |
{ |
| 1376 |
char *str = HSTORE_VAL(entries, base, i); |
| 1377 |
int len = HSTORE_VALLEN(entries, i); |
| 1378 |
|
| 1379 |
if (IsValidJsonNumber(str, len)) |
| 1380 |
appendBinaryStringInfo(&dst, str, len); |
| 1381 |
else |
| 1382 |
escape_json_with_len(&dst, str, len); |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (i + 1 != count) |
| 1386 |
appendStringInfoString(&dst, ", "); |
| 1387 |
} |
| 1388 |
appendStringInfoChar(&dst, '}'); |
| 1389 |
|
| 1390 |
PG_RETURN_TEXT_P(cstring_to_text_with_len(dst.data, dst.len)); |
| 1391 |
} |
| 1392 |
|
| 1393 |
PG_FUNCTION_INFO_V1(hstore_to_json); |
| 1394 |
Datum |
| 1395 |
hstore_to_json(PG_FUNCTION_ARGS) |
| 1396 |
{ |
| 1397 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 1398 |
int i; |
| 1399 |
int count = HS_COUNT(in); |
| 1400 |
char *base = STRPTR(in); |
| 1401 |
HEntry *entries = ARRPTR(in); |
| 1402 |
StringInfoData dst; |
| 1403 |
|
| 1404 |
if (count == 0) |
| 1405 |
PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2)); |
| 1406 |
|
| 1407 |
initStringInfo(&dst); |
| 1408 |
|
| 1409 |
appendStringInfoChar(&dst, '{'); |
| 1410 |
|
| 1411 |
for (i = 0; i < count; i++) |
| 1412 |
{ |
| 1413 |
escape_json_with_len(&dst, |
| 1414 |
HSTORE_KEY(entries, base, i), |
| 1415 |
HSTORE_KEYLEN(entries, i)); |
| 1416 |
appendStringInfoString(&dst, ": "); |
| 1417 |
if (HSTORE_VALISNULL(entries, i)) |
| 1418 |
appendStringInfoString(&dst, "null"); |
| 1419 |
else |
| 1420 |
{ |
| 1421 |
escape_json_with_len(&dst, |
| 1422 |
HSTORE_VAL(entries, base, i), |
| 1423 |
HSTORE_VALLEN(entries, i)); |
| 1424 |
} |
| 1425 |
|
| 1426 |
if (i + 1 != count) |
| 1427 |
appendStringInfoString(&dst, ", "); |
| 1428 |
} |
| 1429 |
appendStringInfoChar(&dst, '}'); |
| 1430 |
|
| 1431 |
PG_RETURN_TEXT_P(cstring_to_text_with_len(dst.data, dst.len)); |
| 1432 |
} |
| 1433 |
|
| 1434 |
PG_FUNCTION_INFO_V1(hstore_to_jsonb); |
| 1435 |
Datum |
| 1436 |
hstore_to_jsonb(PG_FUNCTION_ARGS) |
| 1437 |
{ |
| 1438 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 1439 |
int i; |
| 1440 |
int count = HS_COUNT(in); |
| 1441 |
char *base = STRPTR(in); |
| 1442 |
HEntry *entries = ARRPTR(in); |
| 1443 |
JsonbParseState *state = NULL; |
| 1444 |
JsonbValue *res; |
| 1445 |
|
| 1446 |
(void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL); |
| 1447 |
|
| 1448 |
for (i = 0; i < count; i++) |
| 1449 |
{ |
| 1450 |
JsonbValue key, |
| 1451 |
val; |
| 1452 |
|
| 1453 |
key.type = jbvString; |
| 1454 |
key.val.string.len = HSTORE_KEYLEN(entries, i); |
| 1455 |
key.val.string.val = HSTORE_KEY(entries, base, i); |
| 1456 |
|
| 1457 |
(void) pushJsonbValue(&state, WJB_KEY, &key); |
| 1458 |
|
| 1459 |
if (HSTORE_VALISNULL(entries, i)) |
| 1460 |
{ |
| 1461 |
val.type = jbvNull; |
| 1462 |
} |
| 1463 |
else |
| 1464 |
{ |
| 1465 |
val.type = jbvString; |
| 1466 |
val.val.string.len = HSTORE_VALLEN(entries, i); |
| 1467 |
val.val.string.val = HSTORE_VAL(entries, base, i); |
| 1468 |
} |
| 1469 |
(void) pushJsonbValue(&state, WJB_VALUE, &val); |
| 1470 |
} |
| 1471 |
|
| 1472 |
res = pushJsonbValue(&state, WJB_END_OBJECT, NULL); |
| 1473 |
|
| 1474 |
PG_RETURN_POINTER(JsonbValueToJsonb(res)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
PG_FUNCTION_INFO_V1(hstore_to_jsonb_loose); |
| 1478 |
Datum |
| 1479 |
hstore_to_jsonb_loose(PG_FUNCTION_ARGS) |
| 1480 |
{ |
| 1481 |
HStore *in = PG_GETARG_HSTORE_P(0); |
| 1482 |
int i; |
| 1483 |
int count = HS_COUNT(in); |
| 1484 |
char *base = STRPTR(in); |
| 1485 |
HEntry *entries = ARRPTR(in); |
| 1486 |
JsonbParseState *state = NULL; |
| 1487 |
JsonbValue *res; |
| 1488 |
StringInfoData tmp; |
| 1489 |
|
| 1490 |
initStringInfo(&tmp); |
| 1491 |
|
| 1492 |
(void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL); |
| 1493 |
|
| 1494 |
for (i = 0; i < count; i++) |
| 1495 |
{ |
| 1496 |
JsonbValue key, |
| 1497 |
val; |
| 1498 |
|
| 1499 |
key.type = jbvString; |
| 1500 |
key.val.string.len = HSTORE_KEYLEN(entries, i); |
| 1501 |
key.val.string.val = HSTORE_KEY(entries, base, i); |
| 1502 |
|
| 1503 |
(void) pushJsonbValue(&state, WJB_KEY, &key); |
| 1504 |
|
| 1505 |
if (HSTORE_VALISNULL(entries, i)) |
| 1506 |
{ |
| 1507 |
val.type = jbvNull; |
| 1508 |
} |
| 1509 |
/* guess that values of 't' or 'f' are booleans */ |
| 1510 |
else if (HSTORE_VALLEN(entries, i) == 1 && |
| 1511 |
*(HSTORE_VAL(entries, base, i)) == 't') |
| 1512 |
{ |
| 1513 |
val.type = jbvBool; |
| 1514 |
val.val.boolean = true; |
| 1515 |
} |
| 1516 |
else if (HSTORE_VALLEN(entries, i) == 1 && |
| 1517 |
*(HSTORE_VAL(entries, base, i)) == 'f') |
| 1518 |
{ |
| 1519 |
val.type = jbvBool; |
| 1520 |
val.val.boolean = false; |
| 1521 |
} |
| 1522 |
else |
| 1523 |
{ |
| 1524 |
resetStringInfo(&tmp); |
| 1525 |
appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i), |
| 1526 |
HSTORE_VALLEN(entries, i)); |
| 1527 |
if (IsValidJsonNumber(tmp.data, tmp.len)) |
| 1528 |
{ |
| 1529 |
Datum numd; |
| 1530 |
|
| 1531 |
val.type = jbvNumeric; |
| 1532 |
numd = DirectFunctionCall3(numeric_in, |
| 1533 |
CStringGetDatum(tmp.data), |
| 1534 |
ObjectIdGetDatum(InvalidOid), |
| 1535 |
Int32GetDatum(-1)); |
| 1536 |
val.val.numeric = DatumGetNumeric(numd); |
| 1537 |
} |
| 1538 |
else |
| 1539 |
{ |
| 1540 |
val.type = jbvString; |
| 1541 |
val.val.string.len = HSTORE_VALLEN(entries, i); |
| 1542 |
val.val.string.val = HSTORE_VAL(entries, base, i); |
| 1543 |
} |
| 1544 |
} |
| 1545 |
(void) pushJsonbValue(&state, WJB_VALUE, &val); |
| 1546 |
} |
| 1547 |
|
| 1548 |
res = pushJsonbValue(&state, WJB_END_OBJECT, NULL); |
| 1549 |
|
| 1550 |
PG_RETURN_POINTER(JsonbValueToJsonb(res)); |
| 1551 |
} |