| 1 |
/* src/interfaces/ecpg/ecpglib/data.c */ |
| 2 |
|
| 3 |
#define POSTGRES_ECPG_INTERNAL |
| 4 |
#include "postgres_fe.h" |
| 5 |
|
| 6 |
#include <math.h> |
| 7 |
|
| 8 |
#include "ecpgerrno.h" |
| 9 |
#include "ecpglib.h" |
| 10 |
#include "ecpglib_extern.h" |
| 11 |
#include "ecpgtype.h" |
| 12 |
#include "pgtypes_date.h" |
| 13 |
#include "pgtypes_interval.h" |
| 14 |
#include "pgtypes_numeric.h" |
| 15 |
#include "pgtypes_timestamp.h" |
| 16 |
#include "sqlca.h" |
| 17 |
|
| 18 |
/* returns true if character c is a delimiter for the given array type */ |
| 19 |
static bool |
| 20 |
array_delimiter(enum ARRAY_TYPE isarray, char c) |
| 21 |
{ |
| 22 |
if (isarray == ECPG_ARRAY_ARRAY && c == ',') |
| 23 |
return true; |
| 24 |
|
| 25 |
if (isarray == ECPG_ARRAY_VECTOR && c == ' ') |
| 26 |
return true; |
| 27 |
|
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
/* returns true if character c marks the boundary for the given array type */ |
| 32 |
static bool |
| 33 |
array_boundary(enum ARRAY_TYPE isarray, char c) |
| 34 |
{ |
| 35 |
if (isarray == ECPG_ARRAY_ARRAY && c == '}') |
| 36 |
return true; |
| 37 |
|
| 38 |
if (isarray == ECPG_ARRAY_VECTOR && c == '\0') |
| 39 |
return true; |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
/* returns true if some garbage is found at the end of the scanned string */ |
| 45 |
static bool |
| 46 |
garbage_left(enum ARRAY_TYPE isarray, char **scan_length, enum COMPAT_MODE compat) |
| 47 |
{ |
| 48 |
/* |
| 49 |
* INFORMIX allows for selecting a numeric into an int, the result is |
| 50 |
* truncated |
| 51 |
*/ |
| 52 |
if (isarray == ECPG_ARRAY_NONE) |
| 53 |
{ |
| 54 |
if (INFORMIX_MODE(compat) && **scan_length == '.') |
| 55 |
{ |
| 56 |
/* skip invalid characters */ |
| 57 |
do |
| 58 |
{ |
| 59 |
(*scan_length)++; |
| 60 |
} while (isdigit((unsigned char) **scan_length)); |
| 61 |
} |
| 62 |
|
| 63 |
if (**scan_length != ' ' && **scan_length != '\0') |
| 64 |
return true; |
| 65 |
} |
| 66 |
else if (ECPG_IS_ARRAY(isarray) && !array_delimiter(isarray, **scan_length) && !array_boundary(isarray, **scan_length)) |
| 67 |
return true; |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/* stolen code from src/backend/utils/adt/float.c */ |
| 73 |
#if defined(WIN32) && !defined(NAN) |
| 74 |
static const uint32 nan[2] = {0xffffffff, 0x7fffffff}; |
| 75 |
|
| 76 |
#define NAN (*(const double *) nan) |
| 77 |
#endif |
| 78 |
|
| 79 |
static double |
| 80 |
get_float8_infinity(void) |
| 81 |
{ |
| 82 |
#ifdef INFINITY |
| 83 |
return (double) INFINITY; |
| 84 |
#else |
| 85 |
return (double) (HUGE_VAL * HUGE_VAL); |
| 86 |
#endif |
| 87 |
} |
| 88 |
|
| 89 |
static double |
| 90 |
get_float8_nan(void) |
| 91 |
{ |
| 92 |
/* (double) NAN doesn't work on some NetBSD/MIPS releases */ |
| 93 |
#if defined(NAN) && !(defined(__NetBSD__) && defined(__mips__)) |
| 94 |
return (double) NAN; |
| 95 |
#else |
| 96 |
return (double) (0.0 / 0.0); |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
static bool |
| 101 |
check_special_value(char *ptr, double *retval, char **endptr) |
| 102 |
{ |
| 103 |
if (pg_strncasecmp(ptr, "NaN", 3) == 0) |
| 104 |
{ |
| 105 |
*retval = get_float8_nan(); |
| 106 |
*endptr = ptr + 3; |
| 107 |
return true; |
| 108 |
} |
| 109 |
else if (pg_strncasecmp(ptr, "Infinity", 8) == 0) |
| 110 |
{ |
| 111 |
*retval = get_float8_infinity(); |
| 112 |
*endptr = ptr + 8; |
| 113 |
return true; |
| 114 |
} |
| 115 |
else if (pg_strncasecmp(ptr, "-Infinity", 9) == 0) |
| 116 |
{ |
| 117 |
*retval = -get_float8_infinity(); |
| 118 |
*endptr = ptr + 9; |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/* imported from src/backend/utils/adt/encode.c */ |
| 126 |
|
| 127 |
unsigned |
| 128 |
ecpg_hex_enc_len(unsigned srclen) |
| 129 |
{ |
| 130 |
return srclen << 1; |
| 131 |
} |
| 132 |
|
| 133 |
unsigned |
| 134 |
ecpg_hex_dec_len(unsigned srclen) |
| 135 |
{ |
| 136 |
return srclen >> 1; |
| 137 |
} |
| 138 |
|
| 139 |
static inline char |
| 140 |
get_hex(char c) |
| 141 |
{ |
| 142 |
static const int8 hexlookup[128] = { |
| 143 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 144 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 145 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 146 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 147 |
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 148 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 149 |
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 150 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 151 |
}; |
| 152 |
int res = -1; |
| 153 |
|
| 154 |
if (c > 0 && c < 127) |
| 155 |
res = hexlookup[(unsigned char) c]; |
| 156 |
|
| 157 |
return (char) res; |
| 158 |
} |
| 159 |
|
| 160 |
static unsigned |
| 161 |
hex_decode(const char *src, unsigned len, char *dst) |
| 162 |
{ |
| 163 |
const char *s, |
| 164 |
*srcend; |
| 165 |
char v1, |
| 166 |
v2, |
| 167 |
*p; |
| 168 |
|
| 169 |
srcend = src + len; |
| 170 |
s = src; |
| 171 |
p = dst; |
| 172 |
while (s < srcend) |
| 173 |
{ |
| 174 |
if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r') |
| 175 |
{ |
| 176 |
s++; |
| 177 |
continue; |
| 178 |
} |
| 179 |
v1 = get_hex(*s++) << 4; |
| 180 |
if (s >= srcend) |
| 181 |
return -1; |
| 182 |
|
| 183 |
v2 = get_hex(*s++); |
| 184 |
*p++ = v1 | v2; |
| 185 |
} |
| 186 |
|
| 187 |
return p - dst; |
| 188 |
} |
| 189 |
|
| 190 |
unsigned |
| 191 |
ecpg_hex_encode(const char *src, unsigned len, char *dst) |
| 192 |
{ |
| 193 |
static const char hextbl[] = "0123456789abcdef"; |
| 194 |
const char *end = src + len; |
| 195 |
|
| 196 |
while (src < end) |
| 197 |
{ |
| 198 |
*dst++ = hextbl[(*src >> 4) & 0xF]; |
| 199 |
*dst++ = hextbl[*src & 0xF]; |
| 200 |
src++; |
| 201 |
} |
| 202 |
return len * 2; |
| 203 |
} |
| 204 |
|
| 205 |
bool |
| 206 |
ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno, |
| 207 |
enum ECPGttype type, enum ECPGttype ind_type, |
| 208 |
char *var, char *ind, long varcharsize, long offset, |
| 209 |
long ind_offset, enum ARRAY_TYPE isarray, enum COMPAT_MODE compat, bool force_indicator) |
| 210 |
{ |
| 211 |
struct sqlca_t *sqlca = ECPGget_sqlca(); |
| 212 |
char *pval = (char *) PQgetvalue(results, act_tuple, act_field); |
| 213 |
int binary = PQfformat(results, act_field); |
| 214 |
int size = PQgetlength(results, act_tuple, act_field); |
| 215 |
int value_for_indicator = 0; |
| 216 |
long log_offset; |
| 217 |
|
| 218 |
if (sqlca == NULL) |
| 219 |
{ |
| 220 |
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, |
| 221 |
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* If we are running in a regression test, do not log the offset variable, |
| 227 |
* it depends on the machine's alignment. |
| 228 |
*/ |
| 229 |
if (ecpg_internal_regression_mode) |
| 230 |
log_offset = -1; |
| 231 |
else |
| 232 |
log_offset = offset; |
| 233 |
|
| 234 |
ecpg_log("ecpg_get_data on line %d: RESULT: %s offset: %ld; array: %s\n", lineno, pval ? (binary ? "BINARY" : pval) : "EMPTY", log_offset, ECPG_IS_ARRAY(isarray) ? "yes" : "no"); |
| 235 |
|
| 236 |
/* pval is a pointer to the value */ |
| 237 |
if (!pval) |
| 238 |
{ |
| 239 |
/* |
| 240 |
* This should never happen because we already checked that we found |
| 241 |
* at least one tuple, but let's play it safe. |
| 242 |
*/ |
| 243 |
ecpg_raise(lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL); |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
/* We will have to decode the value */ |
| 248 |
|
| 249 |
/* |
| 250 |
* check for null value and set indicator accordingly, i.e. -1 if NULL and |
| 251 |
* 0 if not |
| 252 |
*/ |
| 253 |
if (PQgetisnull(results, act_tuple, act_field)) |
| 254 |
value_for_indicator = -1; |
| 255 |
|
| 256 |
switch (ind_type) |
| 257 |
{ |
| 258 |
case ECPGt_short: |
| 259 |
case ECPGt_unsigned_short: |
| 260 |
*((short *) (ind + ind_offset * act_tuple)) = value_for_indicator; |
| 261 |
break; |
| 262 |
case ECPGt_int: |
| 263 |
case ECPGt_unsigned_int: |
| 264 |
*((int *) (ind + ind_offset * act_tuple)) = value_for_indicator; |
| 265 |
break; |
| 266 |
case ECPGt_long: |
| 267 |
case ECPGt_unsigned_long: |
| 268 |
*((long *) (ind + ind_offset * act_tuple)) = value_for_indicator; |
| 269 |
break; |
| 270 |
case ECPGt_long_long: |
| 271 |
case ECPGt_unsigned_long_long: |
| 272 |
*((long long int *) (ind + ind_offset * act_tuple)) = value_for_indicator; |
| 273 |
break; |
| 274 |
case ECPGt_NO_INDICATOR: |
| 275 |
if (value_for_indicator == -1) |
| 276 |
{ |
| 277 |
if (force_indicator == false) |
| 278 |
{ |
| 279 |
/* |
| 280 |
* Informix has an additional way to specify NULLs note |
| 281 |
* that this uses special values to denote NULL |
| 282 |
*/ |
| 283 |
ECPGset_noind_null(type, var + offset * act_tuple); |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
ecpg_raise(lineno, ECPG_MISSING_INDICATOR, |
| 288 |
ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER, |
| 289 |
NULL); |
| 290 |
return false; |
| 291 |
} |
| 292 |
} |
| 293 |
break; |
| 294 |
default: |
| 295 |
ecpg_raise(lineno, ECPG_UNSUPPORTED, |
| 296 |
ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, |
| 297 |
ecpg_type_name(ind_type)); |
| 298 |
return false; |
| 299 |
break; |
| 300 |
} |
| 301 |
|
| 302 |
if (value_for_indicator == -1) |
| 303 |
return true; |
| 304 |
|
| 305 |
/* let's check if it really is an array if it should be one */ |
| 306 |
if (isarray == ECPG_ARRAY_ARRAY) |
| 307 |
{ |
| 308 |
if (*pval != '{') |
| 309 |
{ |
| 310 |
ecpg_raise(lineno, ECPG_DATA_NOT_ARRAY, |
| 311 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL); |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
switch (type) |
| 316 |
{ |
| 317 |
case ECPGt_char: |
| 318 |
case ECPGt_unsigned_char: |
| 319 |
case ECPGt_varchar: |
| 320 |
case ECPGt_string: |
| 321 |
break; |
| 322 |
|
| 323 |
default: |
| 324 |
pval++; |
| 325 |
break; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
do |
| 330 |
{ |
| 331 |
if (binary) |
| 332 |
{ |
| 333 |
if (varcharsize == 0 || varcharsize * offset >= size) |
| 334 |
memcpy(var + offset * act_tuple, pval, size); |
| 335 |
else |
| 336 |
{ |
| 337 |
memcpy(var + offset * act_tuple, pval, varcharsize * offset); |
| 338 |
|
| 339 |
if (varcharsize * offset < size) |
| 340 |
{ |
| 341 |
/* truncation */ |
| 342 |
switch (ind_type) |
| 343 |
{ |
| 344 |
case ECPGt_short: |
| 345 |
case ECPGt_unsigned_short: |
| 346 |
*((short *) (ind + ind_offset * act_tuple)) = size; |
| 347 |
break; |
| 348 |
case ECPGt_int: |
| 349 |
case ECPGt_unsigned_int: |
| 350 |
*((int *) (ind + ind_offset * act_tuple)) = size; |
| 351 |
break; |
| 352 |
case ECPGt_long: |
| 353 |
case ECPGt_unsigned_long: |
| 354 |
*((long *) (ind + ind_offset * act_tuple)) = size; |
| 355 |
break; |
| 356 |
case ECPGt_long_long: |
| 357 |
case ECPGt_unsigned_long_long: |
| 358 |
*((long long int *) (ind + ind_offset * act_tuple)) = size; |
| 359 |
break; |
| 360 |
default: |
| 361 |
break; |
| 362 |
} |
| 363 |
sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W'; |
| 364 |
} |
| 365 |
} |
| 366 |
pval += size; |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
switch (type) |
| 371 |
{ |
| 372 |
long res; |
| 373 |
unsigned long ures; |
| 374 |
double dres; |
| 375 |
char *scan_length; |
| 376 |
numeric *nres; |
| 377 |
date ddres; |
| 378 |
timestamp tres; |
| 379 |
interval *ires; |
| 380 |
char *endptr, |
| 381 |
endchar; |
| 382 |
|
| 383 |
case ECPGt_short: |
| 384 |
case ECPGt_int: |
| 385 |
case ECPGt_long: |
| 386 |
res = strtol(pval, &scan_length, 10); |
| 387 |
if (garbage_left(isarray, &scan_length, compat)) |
| 388 |
{ |
| 389 |
ecpg_raise(lineno, ECPG_INT_FORMAT, |
| 390 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 391 |
return false; |
| 392 |
} |
| 393 |
pval = scan_length; |
| 394 |
|
| 395 |
switch (type) |
| 396 |
{ |
| 397 |
case ECPGt_short: |
| 398 |
*((short *) (var + offset * act_tuple)) = (short) res; |
| 399 |
break; |
| 400 |
case ECPGt_int: |
| 401 |
*((int *) (var + offset * act_tuple)) = (int) res; |
| 402 |
break; |
| 403 |
case ECPGt_long: |
| 404 |
*((long *) (var + offset * act_tuple)) = (long) res; |
| 405 |
break; |
| 406 |
default: |
| 407 |
/* Cannot happen */ |
| 408 |
break; |
| 409 |
} |
| 410 |
break; |
| 411 |
|
| 412 |
case ECPGt_unsigned_short: |
| 413 |
case ECPGt_unsigned_int: |
| 414 |
case ECPGt_unsigned_long: |
| 415 |
ures = strtoul(pval, &scan_length, 10); |
| 416 |
if (garbage_left(isarray, &scan_length, compat)) |
| 417 |
{ |
| 418 |
ecpg_raise(lineno, ECPG_UINT_FORMAT, |
| 419 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 420 |
return false; |
| 421 |
} |
| 422 |
pval = scan_length; |
| 423 |
|
| 424 |
switch (type) |
| 425 |
{ |
| 426 |
case ECPGt_unsigned_short: |
| 427 |
*((unsigned short *) (var + offset * act_tuple)) = (unsigned short) ures; |
| 428 |
break; |
| 429 |
case ECPGt_unsigned_int: |
| 430 |
*((unsigned int *) (var + offset * act_tuple)) = (unsigned int) ures; |
| 431 |
break; |
| 432 |
case ECPGt_unsigned_long: |
| 433 |
*((unsigned long *) (var + offset * act_tuple)) = (unsigned long) ures; |
| 434 |
break; |
| 435 |
default: |
| 436 |
/* Cannot happen */ |
| 437 |
break; |
| 438 |
} |
| 439 |
break; |
| 440 |
|
| 441 |
case ECPGt_long_long: |
| 442 |
*((long long int *) (var + offset * act_tuple)) = strtoll(pval, &scan_length, 10); |
| 443 |
if (garbage_left(isarray, &scan_length, compat)) |
| 444 |
{ |
| 445 |
ecpg_raise(lineno, ECPG_INT_FORMAT, ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 446 |
return false; |
| 447 |
} |
| 448 |
pval = scan_length; |
| 449 |
|
| 450 |
break; |
| 451 |
|
| 452 |
case ECPGt_unsigned_long_long: |
| 453 |
*((unsigned long long int *) (var + offset * act_tuple)) = strtoull(pval, &scan_length, 10); |
| 454 |
if (garbage_left(isarray, &scan_length, compat)) |
| 455 |
{ |
| 456 |
ecpg_raise(lineno, ECPG_UINT_FORMAT, ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 457 |
return false; |
| 458 |
} |
| 459 |
pval = scan_length; |
| 460 |
|
| 461 |
break; |
| 462 |
|
| 463 |
case ECPGt_float: |
| 464 |
case ECPGt_double: |
| 465 |
if (isarray && *pval == '"') |
| 466 |
pval++; |
| 467 |
|
| 468 |
if (!check_special_value(pval, &dres, &scan_length)) |
| 469 |
dres = strtod(pval, &scan_length); |
| 470 |
|
| 471 |
if (isarray && *scan_length == '"') |
| 472 |
scan_length++; |
| 473 |
|
| 474 |
/* no special INFORMIX treatment for floats */ |
| 475 |
if (garbage_left(isarray, &scan_length, ECPG_COMPAT_PGSQL)) |
| 476 |
{ |
| 477 |
ecpg_raise(lineno, ECPG_FLOAT_FORMAT, |
| 478 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 479 |
return false; |
| 480 |
} |
| 481 |
pval = scan_length; |
| 482 |
|
| 483 |
switch (type) |
| 484 |
{ |
| 485 |
case ECPGt_float: |
| 486 |
*((float *) (var + offset * act_tuple)) = dres; |
| 487 |
break; |
| 488 |
case ECPGt_double: |
| 489 |
*((double *) (var + offset * act_tuple)) = dres; |
| 490 |
break; |
| 491 |
default: |
| 492 |
/* Cannot happen */ |
| 493 |
break; |
| 494 |
} |
| 495 |
break; |
| 496 |
|
| 497 |
case ECPGt_bool: |
| 498 |
if (pval[0] == 'f' && pval[1] == '\0') |
| 499 |
{ |
| 500 |
*((bool *) (var + offset * act_tuple)) = false; |
| 501 |
pval++; |
| 502 |
break; |
| 503 |
} |
| 504 |
else if (pval[0] == 't' && pval[1] == '\0') |
| 505 |
{ |
| 506 |
*((bool *) (var + offset * act_tuple)) = true; |
| 507 |
pval++; |
| 508 |
break; |
| 509 |
} |
| 510 |
else if (pval[0] == '\0' && PQgetisnull(results, act_tuple, act_field)) |
| 511 |
{ |
| 512 |
/* NULL is valid */ |
| 513 |
break; |
| 514 |
} |
| 515 |
|
| 516 |
ecpg_raise(lineno, ECPG_CONVERT_BOOL, |
| 517 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 518 |
return false; |
| 519 |
break; |
| 520 |
|
| 521 |
case ECPGt_bytea: |
| 522 |
{ |
| 523 |
struct ECPGgeneric_bytea *variable = |
| 524 |
(struct ECPGgeneric_bytea *) (var + offset * act_tuple); |
| 525 |
long dst_size, |
| 526 |
src_size, |
| 527 |
dec_size; |
| 528 |
|
| 529 |
if (size < 2 || pval[0] != '\\' || pval[1] != 'x') |
| 530 |
{ |
| 531 |
ecpg_raise(lineno, ECPG_BYTEA_FORMAT, |
| 532 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 533 |
return false; |
| 534 |
} |
| 535 |
|
| 536 |
dst_size = ecpg_hex_enc_len(varcharsize); |
| 537 |
src_size = size - 2; /* exclude backslash + 'x' */ |
| 538 |
dec_size = src_size < dst_size ? src_size : dst_size; |
| 539 |
variable->len = hex_decode(pval + 2, dec_size, variable->arr); |
| 540 |
|
| 541 |
if (dst_size < src_size) |
| 542 |
{ |
| 543 |
long rcv_size = ecpg_hex_dec_len(size - 2); |
| 544 |
|
| 545 |
/* truncation */ |
| 546 |
switch (ind_type) |
| 547 |
{ |
| 548 |
case ECPGt_short: |
| 549 |
case ECPGt_unsigned_short: |
| 550 |
*((short *) (ind + ind_offset * act_tuple)) = rcv_size; |
| 551 |
break; |
| 552 |
case ECPGt_int: |
| 553 |
case ECPGt_unsigned_int: |
| 554 |
*((int *) (ind + ind_offset * act_tuple)) = rcv_size; |
| 555 |
break; |
| 556 |
case ECPGt_long: |
| 557 |
case ECPGt_unsigned_long: |
| 558 |
*((long *) (ind + ind_offset * act_tuple)) = rcv_size; |
| 559 |
break; |
| 560 |
case ECPGt_long_long: |
| 561 |
case ECPGt_unsigned_long_long: |
| 562 |
*((long long int *) (ind + ind_offset * act_tuple)) = rcv_size; |
| 563 |
break; |
| 564 |
default: |
| 565 |
break; |
| 566 |
} |
| 567 |
sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W'; |
| 568 |
} |
| 569 |
|
| 570 |
pval += size; |
| 571 |
} |
| 572 |
break; |
| 573 |
|
| 574 |
case ECPGt_char: |
| 575 |
case ECPGt_unsigned_char: |
| 576 |
case ECPGt_string: |
| 577 |
{ |
| 578 |
char *str = (char *) (var + offset * act_tuple); |
| 579 |
|
| 580 |
/* |
| 581 |
* If varcharsize is unknown and the offset is that of |
| 582 |
* char *, then this variable represents the array of |
| 583 |
* character pointers. So, use extra indirection. |
| 584 |
*/ |
| 585 |
if (varcharsize == 0 && offset == sizeof(char *)) |
| 586 |
str = *(char **) str; |
| 587 |
|
| 588 |
if (varcharsize > size) |
| 589 |
{ |
| 590 |
/* |
| 591 |
* compatibility mode, blank pad and null |
| 592 |
* terminate char array |
| 593 |
*/ |
| 594 |
if (ORACLE_MODE(compat) && (type == ECPGt_char || type == ECPGt_unsigned_char)) |
| 595 |
{ |
| 596 |
memset(str, ' ', varcharsize); |
| 597 |
memcpy(str, pval, size); |
| 598 |
str[varcharsize - 1] = '\0'; |
| 599 |
|
| 600 |
/* |
| 601 |
* compatibility mode empty string gets -1 |
| 602 |
* indicator but no warning |
| 603 |
*/ |
| 604 |
if (size == 0) |
| 605 |
{ |
| 606 |
/* truncation */ |
| 607 |
switch (ind_type) |
| 608 |
{ |
| 609 |
case ECPGt_short: |
| 610 |
case ECPGt_unsigned_short: |
| 611 |
*((short *) (ind + ind_offset * act_tuple)) = -1; |
| 612 |
break; |
| 613 |
case ECPGt_int: |
| 614 |
case ECPGt_unsigned_int: |
| 615 |
*((int *) (ind + ind_offset * act_tuple)) = -1; |
| 616 |
break; |
| 617 |
case ECPGt_long: |
| 618 |
case ECPGt_unsigned_long: |
| 619 |
*((long *) (ind + ind_offset * act_tuple)) = -1; |
| 620 |
break; |
| 621 |
case ECPGt_long_long: |
| 622 |
case ECPGt_unsigned_long_long: |
| 623 |
*((long long int *) (ind + ind_offset * act_tuple)) = -1; |
| 624 |
break; |
| 625 |
default: |
| 626 |
break; |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
strncpy(str, pval, size + 1); |
| 633 |
} |
| 634 |
/* do the rtrim() */ |
| 635 |
if (type == ECPGt_string) |
| 636 |
{ |
| 637 |
char *last = str + size; |
| 638 |
|
| 639 |
while (last > str && (*last == ' ' || *last == '\0')) |
| 640 |
{ |
| 641 |
*last = '\0'; |
| 642 |
last--; |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
else |
| 647 |
{ |
| 648 |
int charsize = varcharsize; |
| 649 |
|
| 650 |
/* |
| 651 |
* assume that the caller provided storage exactly |
| 652 |
* fit when varcharsize is zero. |
| 653 |
*/ |
| 654 |
if (varcharsize == 0) |
| 655 |
charsize = size + 1; |
| 656 |
|
| 657 |
strncpy(str, pval, charsize); |
| 658 |
|
| 659 |
/* compatibility mode, null terminate char array */ |
| 660 |
if (ORACLE_MODE(compat) && (charsize - 1) < size) |
| 661 |
{ |
| 662 |
if (type == ECPGt_char || type == ECPGt_unsigned_char) |
| 663 |
str[charsize - 1] = '\0'; |
| 664 |
} |
| 665 |
|
| 666 |
if (charsize < size || (ORACLE_MODE(compat) && (charsize - 1) < size)) |
| 667 |
{ |
| 668 |
/* truncation */ |
| 669 |
switch (ind_type) |
| 670 |
{ |
| 671 |
case ECPGt_short: |
| 672 |
case ECPGt_unsigned_short: |
| 673 |
*((short *) (ind + ind_offset * act_tuple)) = size; |
| 674 |
break; |
| 675 |
case ECPGt_int: |
| 676 |
case ECPGt_unsigned_int: |
| 677 |
*((int *) (ind + ind_offset * act_tuple)) = size; |
| 678 |
break; |
| 679 |
case ECPGt_long: |
| 680 |
case ECPGt_unsigned_long: |
| 681 |
*((long *) (ind + ind_offset * act_tuple)) = size; |
| 682 |
break; |
| 683 |
case ECPGt_long_long: |
| 684 |
case ECPGt_unsigned_long_long: |
| 685 |
*((long long int *) (ind + ind_offset * act_tuple)) = size; |
| 686 |
break; |
| 687 |
default: |
| 688 |
break; |
| 689 |
} |
| 690 |
sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W'; |
| 691 |
} |
| 692 |
} |
| 693 |
pval += size; |
| 694 |
} |
| 695 |
break; |
| 696 |
|
| 697 |
case ECPGt_varchar: |
| 698 |
{ |
| 699 |
struct ECPGgeneric_varchar *variable = |
| 700 |
(struct ECPGgeneric_varchar *) (var + offset * act_tuple); |
| 701 |
|
| 702 |
variable->len = size; |
| 703 |
if (varcharsize == 0) |
| 704 |
strncpy(variable->arr, pval, variable->len); |
| 705 |
else |
| 706 |
{ |
| 707 |
strncpy(variable->arr, pval, varcharsize); |
| 708 |
|
| 709 |
if (variable->len > varcharsize) |
| 710 |
{ |
| 711 |
/* truncation */ |
| 712 |
switch (ind_type) |
| 713 |
{ |
| 714 |
case ECPGt_short: |
| 715 |
case ECPGt_unsigned_short: |
| 716 |
*((short *) (ind + ind_offset * act_tuple)) = variable->len; |
| 717 |
break; |
| 718 |
case ECPGt_int: |
| 719 |
case ECPGt_unsigned_int: |
| 720 |
*((int *) (ind + ind_offset * act_tuple)) = variable->len; |
| 721 |
break; |
| 722 |
case ECPGt_long: |
| 723 |
case ECPGt_unsigned_long: |
| 724 |
*((long *) (ind + ind_offset * act_tuple)) = variable->len; |
| 725 |
break; |
| 726 |
case ECPGt_long_long: |
| 727 |
case ECPGt_unsigned_long_long: |
| 728 |
*((long long int *) (ind + ind_offset * act_tuple)) = variable->len; |
| 729 |
break; |
| 730 |
default: |
| 731 |
break; |
| 732 |
} |
| 733 |
sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W'; |
| 734 |
|
| 735 |
variable->len = varcharsize; |
| 736 |
} |
| 737 |
} |
| 738 |
pval += size; |
| 739 |
} |
| 740 |
break; |
| 741 |
|
| 742 |
case ECPGt_decimal: |
| 743 |
case ECPGt_numeric: |
| 744 |
for (endptr = pval; *endptr && *endptr != ',' && *endptr != '}'; endptr++); |
| 745 |
endchar = *endptr; |
| 746 |
*endptr = '\0'; |
| 747 |
nres = PGTYPESnumeric_from_asc(pval, &scan_length); |
| 748 |
*endptr = endchar; |
| 749 |
|
| 750 |
/* did we get an error? */ |
| 751 |
if (nres == NULL) |
| 752 |
{ |
| 753 |
ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", |
| 754 |
lineno, pval, errno); |
| 755 |
|
| 756 |
if (INFORMIX_MODE(compat)) |
| 757 |
{ |
| 758 |
/* |
| 759 |
* Informix wants its own NULL value here instead |
| 760 |
* of an error |
| 761 |
*/ |
| 762 |
nres = PGTYPESnumeric_new(); |
| 763 |
if (nres) |
| 764 |
ECPGset_noind_null(ECPGt_numeric, nres); |
| 765 |
else |
| 766 |
{ |
| 767 |
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, |
| 768 |
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); |
| 769 |
return false; |
| 770 |
} |
| 771 |
} |
| 772 |
else |
| 773 |
{ |
| 774 |
ecpg_raise(lineno, ECPG_NUMERIC_FORMAT, |
| 775 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 776 |
return false; |
| 777 |
} |
| 778 |
} |
| 779 |
else |
| 780 |
{ |
| 781 |
if (!isarray && garbage_left(isarray, &scan_length, compat)) |
| 782 |
{ |
| 783 |
free(nres); |
| 784 |
ecpg_raise(lineno, ECPG_NUMERIC_FORMAT, |
| 785 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 786 |
return false; |
| 787 |
} |
| 788 |
} |
| 789 |
pval = scan_length; |
| 790 |
|
| 791 |
if (type == ECPGt_numeric) |
| 792 |
PGTYPESnumeric_copy(nres, (numeric *) (var + offset * act_tuple)); |
| 793 |
else |
| 794 |
PGTYPESnumeric_to_decimal(nres, (decimal *) (var + offset * act_tuple)); |
| 795 |
|
| 796 |
PGTYPESnumeric_free(nres); |
| 797 |
break; |
| 798 |
|
| 799 |
case ECPGt_interval: |
| 800 |
if (*pval == '"') |
| 801 |
pval++; |
| 802 |
|
| 803 |
for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++); |
| 804 |
endchar = *endptr; |
| 805 |
*endptr = '\0'; |
| 806 |
ires = PGTYPESinterval_from_asc(pval, &scan_length); |
| 807 |
*endptr = endchar; |
| 808 |
|
| 809 |
/* did we get an error? */ |
| 810 |
if (ires == NULL) |
| 811 |
{ |
| 812 |
ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", |
| 813 |
lineno, pval, errno); |
| 814 |
|
| 815 |
if (INFORMIX_MODE(compat)) |
| 816 |
{ |
| 817 |
/* |
| 818 |
* Informix wants its own NULL value here instead |
| 819 |
* of an error |
| 820 |
*/ |
| 821 |
ires = (interval *) ecpg_alloc(sizeof(interval), lineno); |
| 822 |
if (!ires) |
| 823 |
return false; |
| 824 |
|
| 825 |
ECPGset_noind_null(ECPGt_interval, ires); |
| 826 |
} |
| 827 |
else |
| 828 |
{ |
| 829 |
ecpg_raise(lineno, ECPG_INTERVAL_FORMAT, |
| 830 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 831 |
return false; |
| 832 |
} |
| 833 |
} |
| 834 |
else |
| 835 |
{ |
| 836 |
if (*scan_length == '"') |
| 837 |
scan_length++; |
| 838 |
|
| 839 |
if (!isarray && garbage_left(isarray, &scan_length, compat)) |
| 840 |
{ |
| 841 |
free(ires); |
| 842 |
ecpg_raise(lineno, ECPG_INTERVAL_FORMAT, |
| 843 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 844 |
return false; |
| 845 |
} |
| 846 |
} |
| 847 |
pval = scan_length; |
| 848 |
|
| 849 |
PGTYPESinterval_copy(ires, (interval *) (var + offset * act_tuple)); |
| 850 |
free(ires); |
| 851 |
break; |
| 852 |
|
| 853 |
case ECPGt_date: |
| 854 |
if (*pval == '"') |
| 855 |
pval++; |
| 856 |
|
| 857 |
for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++); |
| 858 |
endchar = *endptr; |
| 859 |
*endptr = '\0'; |
| 860 |
ddres = PGTYPESdate_from_asc(pval, &scan_length); |
| 861 |
*endptr = endchar; |
| 862 |
|
| 863 |
/* did we get an error? */ |
| 864 |
if (errno != 0) |
| 865 |
{ |
| 866 |
ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", |
| 867 |
lineno, pval, errno); |
| 868 |
|
| 869 |
if (INFORMIX_MODE(compat)) |
| 870 |
{ |
| 871 |
/* |
| 872 |
* Informix wants its own NULL value here instead |
| 873 |
* of an error |
| 874 |
*/ |
| 875 |
ECPGset_noind_null(ECPGt_date, &ddres); |
| 876 |
} |
| 877 |
else |
| 878 |
{ |
| 879 |
ecpg_raise(lineno, ECPG_DATE_FORMAT, |
| 880 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 881 |
return false; |
| 882 |
} |
| 883 |
} |
| 884 |
else |
| 885 |
{ |
| 886 |
if (*scan_length == '"') |
| 887 |
scan_length++; |
| 888 |
|
| 889 |
if (!isarray && garbage_left(isarray, &scan_length, compat)) |
| 890 |
{ |
| 891 |
ecpg_raise(lineno, ECPG_DATE_FORMAT, |
| 892 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 893 |
return false; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
*((date *) (var + offset * act_tuple)) = ddres; |
| 898 |
pval = scan_length; |
| 899 |
break; |
| 900 |
|
| 901 |
case ECPGt_timestamp: |
| 902 |
if (*pval == '"') |
| 903 |
pval++; |
| 904 |
|
| 905 |
for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++); |
| 906 |
endchar = *endptr; |
| 907 |
*endptr = '\0'; |
| 908 |
tres = PGTYPEStimestamp_from_asc(pval, &scan_length); |
| 909 |
*endptr = endchar; |
| 910 |
|
| 911 |
/* did we get an error? */ |
| 912 |
if (errno != 0) |
| 913 |
{ |
| 914 |
ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", |
| 915 |
lineno, pval, errno); |
| 916 |
|
| 917 |
if (INFORMIX_MODE(compat)) |
| 918 |
{ |
| 919 |
/* |
| 920 |
* Informix wants its own NULL value here instead |
| 921 |
* of an error |
| 922 |
*/ |
| 923 |
ECPGset_noind_null(ECPGt_timestamp, &tres); |
| 924 |
} |
| 925 |
else |
| 926 |
{ |
| 927 |
ecpg_raise(lineno, ECPG_TIMESTAMP_FORMAT, |
| 928 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 929 |
return false; |
| 930 |
} |
| 931 |
} |
| 932 |
else |
| 933 |
{ |
| 934 |
if (*scan_length == '"') |
| 935 |
scan_length++; |
| 936 |
|
| 937 |
if (!isarray && garbage_left(isarray, &scan_length, compat)) |
| 938 |
{ |
| 939 |
ecpg_raise(lineno, ECPG_TIMESTAMP_FORMAT, |
| 940 |
ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); |
| 941 |
return false; |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
*((timestamp *) (var + offset * act_tuple)) = tres; |
| 946 |
pval = scan_length; |
| 947 |
break; |
| 948 |
|
| 949 |
default: |
| 950 |
ecpg_raise(lineno, ECPG_UNSUPPORTED, |
| 951 |
ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, |
| 952 |
ecpg_type_name(type)); |
| 953 |
return false; |
| 954 |
break; |
| 955 |
} |
| 956 |
if (ECPG_IS_ARRAY(isarray)) |
| 957 |
{ |
| 958 |
bool string = false; |
| 959 |
|
| 960 |
/* set array to next entry */ |
| 961 |
++act_tuple; |
| 962 |
|
| 963 |
/* set pval to the next entry */ |
| 964 |
|
| 965 |
/* |
| 966 |
* *pval != '\0' should not be needed, but is used as a safety |
| 967 |
* guard |
| 968 |
*/ |
| 969 |
for (; *pval != '\0' && (string || (!array_delimiter(isarray, *pval) && !array_boundary(isarray, *pval))); ++pval) |
| 970 |
if (*pval == '"') |
| 971 |
string = string ? false : true; |
| 972 |
|
| 973 |
if (array_delimiter(isarray, *pval)) |
| 974 |
++pval; |
| 975 |
} |
| 976 |
} |
| 977 |
} while (*pval != '\0' && !array_boundary(isarray, *pval)); |
| 978 |
|
| 979 |
return true; |
| 980 |
} |