*** pgsql/src/interfaces/ecpg/ecpglib/data.c 2009/10/01 18:03:54 1.45 --- pgsql/src/interfaces/ecpg/ecpglib/data.c 2009/11/27 13:32:17 1.46 *************** *** 1,4 **** ! /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/data.c,v 1.44 2009/08/07 16:47:53 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" --- 1,4 ---- ! /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/data.c,v 1.45 2009/10/01 18:03:54 meskes Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" *************** ecpg_get_data(const PGresult *results, i *** 62,67 **** --- 62,78 ---- ecpg_log("ecpg_get_data on line %d: RESULT: %s offset: %ld; array: %s\n", lineno, pval ? (binary ? "BINARY" : pval) : "EMPTY", log_offset, isarray ? "yes" : "no"); + /* pval is a pointer to the value */ + if (!pval) + { + /* + * This should never happen because we already checked that we + * found at least one tuple, but let's play it safe. + */ + ecpg_raise(lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL); + return (false); + } + /* We will have to decode the value */ /* *************** ecpg_get_data(const PGresult *results, i *** 122,132 **** if (value_for_indicator == -1) return (true); - /* pval is a pointer to the value */ /* let's check if it really is an array if it should be one */ if (isarray == ECPG_ARRAY_ARRAY) { ! if (!pval || *pval != '{') { ecpg_raise(lineno, ECPG_DATA_NOT_ARRAY, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL); --- 133,142 ---- if (value_for_indicator == -1) return (true); /* let's check if it really is an array if it should be one */ if (isarray == ECPG_ARRAY_ARRAY) { ! if (*pval != '{') { ecpg_raise(lineno, ECPG_DATA_NOT_ARRAY, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL); *************** ecpg_get_data(const PGresult *results, i *** 151,197 **** { if (binary) { ! if (pval) { ! if (varcharsize == 0 || varcharsize * offset >= size) ! memcpy((char *) ((long) var + offset * act_tuple), ! pval, size); ! else ! { ! memcpy((char *) ((long) var + offset * act_tuple), ! pval, varcharsize * offset); ! if (varcharsize * offset < size) { ! /* truncation */ ! switch (ind_type) ! { ! case ECPGt_short: ! case ECPGt_unsigned_short: ! *((short *) (ind + ind_offset * act_tuple)) = size; ! break; ! case ECPGt_int: ! case ECPGt_unsigned_int: ! *((int *) (ind + ind_offset * act_tuple)) = size; ! break; ! case ECPGt_long: ! case ECPGt_unsigned_long: ! *((long *) (ind + ind_offset * act_tuple)) = size; ! break; #ifdef HAVE_LONG_LONG_INT_64 ! case ECPGt_long_long: ! case ECPGt_unsigned_long_long: ! *((long long int *) (ind + ind_offset * act_tuple)) = size; ! break; #endif /* HAVE_LONG_LONG_INT_64 */ ! default: ! break; ! } ! sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W'; } } - pval += size; } } else { --- 161,204 ---- { if (binary) { ! if (varcharsize == 0 || varcharsize * offset >= size) ! memcpy((char *) ((long) var + offset * act_tuple), ! pval, size); ! else { ! memcpy((char *) ((long) var + offset * act_tuple), ! pval, varcharsize * offset); ! if (varcharsize * offset < size) ! { ! /* truncation */ ! switch (ind_type) { ! case ECPGt_short: ! case ECPGt_unsigned_short: ! *((short *) (ind + ind_offset * act_tuple)) = size; ! break; ! case ECPGt_int: ! case ECPGt_unsigned_int: ! *((int *) (ind + ind_offset * act_tuple)) = size; ! break; ! case ECPGt_long: ! case ECPGt_unsigned_long: ! *((long *) (ind + ind_offset * act_tuple)) = size; ! break; #ifdef HAVE_LONG_LONG_INT_64 ! case ECPGt_long_long: ! case ECPGt_unsigned_long_long: ! *((long long int *) (ind + ind_offset * act_tuple)) = size; ! break; #endif /* HAVE_LONG_LONG_INT_64 */ ! default: ! break; } + sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W'; } } + pval += size; } else { *************** ecpg_get_data(const PGresult *results, i *** 209,227 **** case ECPGt_short: case ECPGt_int: case ECPGt_long: ! if (pval) { ! res = strtol(pval, &scan_length, 10); ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_INT_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! pval = scan_length; } ! else ! res = 0L; switch (type) { --- 216,229 ---- case ECPGt_short: case ECPGt_int: case ECPGt_long: ! res = strtol(pval, &scan_length, 10); ! if (garbage_left(isarray, scan_length, compat)) { ! ecpg_raise(lineno, ECPG_INT_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } ! pval = scan_length; switch (type) { *************** ecpg_get_data(const PGresult *results, i *** 243,261 **** case ECPGt_unsigned_short: case ECPGt_unsigned_int: case ECPGt_unsigned_long: ! if (pval) { ! ures = strtoul(pval, &scan_length, 10); ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_UINT_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! pval = scan_length; } ! else ! ures = 0L; switch (type) { --- 245,258 ---- case ECPGt_unsigned_short: case ECPGt_unsigned_int: case ECPGt_unsigned_long: ! ures = strtoul(pval, &scan_length, 10); ! if (garbage_left(isarray, scan_length, compat)) { ! ecpg_raise(lineno, ECPG_UINT_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } ! pval = scan_length; switch (type) { *************** ecpg_get_data(const PGresult *results, i *** 277,312 **** #ifdef HAVE_LONG_LONG_INT_64 #ifdef HAVE_STRTOLL case ECPGt_long_long: ! if (pval) { ! *((long long int *) (var + offset * act_tuple)) = strtoll(pval, &scan_length, 10); ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_INT_FORMAT, ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! pval = scan_length; } ! else ! *((long long int *) (var + offset * act_tuple)) = (long long) 0; break; #endif /* HAVE_STRTOLL */ #ifdef HAVE_STRTOULL case ECPGt_unsigned_long_long: ! if (pval) { ! *((unsigned long long int *) (var + offset * act_tuple)) = strtoull(pval, &scan_length, 10); ! if ((isarray && *scan_length != ',' && *scan_length != '}') ! || (!isarray && !(INFORMIX_MODE(compat) && *scan_length == '.') && *scan_length != '\0' && *scan_length != ' ')) /* Garbage left */ ! { ! ecpg_raise(lineno, ECPG_UINT_FORMAT, ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! pval = scan_length; } ! else ! *((unsigned long long int *) (var + offset * act_tuple)) = (long long) 0; break; #endif /* HAVE_STRTOULL */ --- 274,299 ---- #ifdef HAVE_LONG_LONG_INT_64 #ifdef HAVE_STRTOLL case ECPGt_long_long: ! *((long long int *) (var + offset * act_tuple)) = strtoll(pval, &scan_length, 10); ! if (garbage_left(isarray, scan_length, compat)) { ! ecpg_raise(lineno, ECPG_INT_FORMAT, ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } ! pval = scan_length; break; #endif /* HAVE_STRTOLL */ #ifdef HAVE_STRTOULL case ECPGt_unsigned_long_long: ! *((unsigned long long int *) (var + offset * act_tuple)) = strtoull(pval, &scan_length, 10); ! if ((isarray && *scan_length != ',' && *scan_length != '}') ! || (!isarray && !(INFORMIX_MODE(compat) && *scan_length == '.') && *scan_length != '\0' && *scan_length != ' ')) /* Garbage left */ { ! ecpg_raise(lineno, ECPG_UINT_FORMAT, ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } ! pval = scan_length; break; #endif /* HAVE_STRTOULL */ *************** ecpg_get_data(const PGresult *results, i *** 314,339 **** case ECPGt_float: case ECPGt_double: ! if (pval) ! { ! if (isarray && *pval == '"') ! dres = strtod(pval + 1, &scan_length); ! else ! dres = strtod(pval, &scan_length); ! if (isarray && *scan_length == '"') ! scan_length++; ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_FLOAT_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! pval = scan_length; } ! else ! dres = 0.0; switch (type) { --- 301,321 ---- case ECPGt_float: case ECPGt_double: ! if (isarray && *pval == '"') ! dres = strtod(pval + 1, &scan_length); ! else ! dres = strtod(pval, &scan_length); ! if (isarray && *scan_length == '"') ! scan_length++; ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_FLOAT_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } ! pval = scan_length; switch (type) { *************** ecpg_get_data(const PGresult *results, i *** 350,386 **** break; case ECPGt_bool: ! if (pval) { ! if (pval[0] == 'f' && pval[1] == '\0') ! { ! if (offset == sizeof(char)) ! *((char *) (var + offset * act_tuple)) = false; ! else if (offset == sizeof(int)) ! *((int *) (var + offset * act_tuple)) = false; ! else ! ecpg_raise(lineno, ECPG_CONVERT_BOOL, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, ! NULL); ! break; ! } ! else if (pval[0] == 't' && pval[1] == '\0') ! { ! if (offset == sizeof(char)) ! *((char *) (var + offset * act_tuple)) = true; ! else if (offset == sizeof(int)) ! *((int *) (var + offset * act_tuple)) = true; ! else ! ecpg_raise(lineno, ECPG_CONVERT_BOOL, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, ! NULL); ! break; ! } ! else if (pval[0] == '\0' && PQgetisnull(results, act_tuple, act_field)) ! { ! /* NULL is valid */ ! break; ! } } ecpg_raise(lineno, ECPG_CONVERT_BOOL, --- 332,365 ---- break; case ECPGt_bool: ! if (pval[0] == 'f' && pval[1] == '\0') { ! if (offset == sizeof(char)) ! *((char *) (var + offset * act_tuple)) = false; ! else if (offset == sizeof(int)) ! *((int *) (var + offset * act_tuple)) = false; ! else ! ecpg_raise(lineno, ECPG_CONVERT_BOOL, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, ! NULL); ! break; ! } ! else if (pval[0] == 't' && pval[1] == '\0') ! { ! if (offset == sizeof(char)) ! *((char *) (var + offset * act_tuple)) = true; ! else if (offset == sizeof(int)) ! *((int *) (var + offset * act_tuple)) = true; ! else ! ecpg_raise(lineno, ECPG_CONVERT_BOOL, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, ! NULL); ! break; ! } ! else if (pval[0] == '\0' && PQgetisnull(results, act_tuple, act_field)) ! { ! /* NULL is valid */ ! break; } ecpg_raise(lineno, ECPG_CONVERT_BOOL, *************** ecpg_get_data(const PGresult *results, i *** 391,397 **** case ECPGt_char: case ECPGt_unsigned_char: case ECPGt_string: - if (pval) { char *str = (char *) ((long) var + offset * act_tuple); if (varcharsize == 0 || varcharsize > size) --- 370,375 ---- *************** ecpg_get_data(const PGresult *results, i *** 446,452 **** break; case ECPGt_varchar: - if (pval) { struct ECPGgeneric_varchar *variable = (struct ECPGgeneric_varchar *) ((long) var + offset * act_tuple); --- 424,429 ---- *************** ecpg_get_data(const PGresult *results, i *** 495,553 **** case ECPGt_decimal: case ECPGt_numeric: ! if (pval) ! { ! if (isarray && *pval == '"') ! nres = PGTYPESnumeric_from_asc(pval + 1, &scan_length); ! else ! nres = PGTYPESnumeric_from_asc(pval, &scan_length); ! /* did we get an error? */ ! if (nres == NULL) ! { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! if (INFORMIX_MODE(compat)) ! { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! nres = PGTYPESnumeric_new(); ! if (nres) ! ECPGset_noind_null(ECPGt_numeric, nres); ! else ! { ! ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ! ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); ! return (false); ! } ! } else { ! ecpg_raise(lineno, ECPG_NUMERIC_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); return (false); } } else { ! if (isarray && *scan_length == '"') ! scan_length++; ! ! if (garbage_left(isarray, scan_length, compat)) ! { ! free(nres); ! ecpg_raise(lineno, ECPG_NUMERIC_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } - pval = scan_length; } else ! nres = PGTYPESnumeric_from_asc("0.0", &scan_length); if (type == ECPGt_numeric) PGTYPESnumeric_copy(nres, (numeric *) (var + offset * act_tuple)); --- 472,525 ---- case ECPGt_decimal: case ECPGt_numeric: ! if (isarray && *pval == '"') ! nres = PGTYPESnumeric_from_asc(pval + 1, &scan_length); ! else ! nres = PGTYPESnumeric_from_asc(pval, &scan_length); ! /* did we get an error? */ ! if (nres == NULL) ! { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! if (INFORMIX_MODE(compat)) ! { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! nres = PGTYPESnumeric_new(); ! if (nres) ! ECPGset_noind_null(ECPGt_numeric, nres); else { ! ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ! ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); return (false); } } else { ! ecpg_raise(lineno, ECPG_NUMERIC_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } } else ! { ! if (isarray && *scan_length == '"') ! scan_length++; ! ! if (garbage_left(isarray, scan_length, compat)) ! { ! free(nres); ! ecpg_raise(lineno, ECPG_NUMERIC_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! } ! pval = scan_length; if (type == ECPGt_numeric) PGTYPESnumeric_copy(nres, (numeric *) (var + offset * act_tuple)); *************** ecpg_get_data(const PGresult *results, i *** 558,708 **** break; case ECPGt_interval: ! if (pval) ! { ! if (isarray && *pval == '"') ! ires = PGTYPESinterval_from_asc(pval + 1, &scan_length); ! else ! ires = PGTYPESinterval_from_asc(pval, &scan_length); ! ! /* did we get an error? */ ! if (ires == NULL) ! { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! if (INFORMIX_MODE(compat)) ! { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! ires = (interval *) ecpg_alloc(sizeof(interval), lineno); ! if (!ires) ! return (false); ! ECPGset_noind_null(ECPGt_interval, ires); ! } ! else ! { ! ecpg_raise(lineno, ECPG_INTERVAL_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); return (false); ! } } else { ! if (isarray && *scan_length == '"') ! scan_length++; ! ! if (garbage_left(isarray, scan_length, compat)) ! { ! free(ires); ! ecpg_raise(lineno, ECPG_INTERVAL_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } - pval = scan_length; } else ! ires = PGTYPESinterval_from_asc("0 seconds", NULL); PGTYPESinterval_copy(ires, (interval *) (var + offset * act_tuple)); free(ires); break; case ECPGt_date: ! if (pval) { ! if (isarray && *pval == '"') ! ddres = PGTYPESdate_from_asc(pval + 1, &scan_length); ! else ! ddres = PGTYPESdate_from_asc(pval, &scan_length); ! /* did we get an error? */ ! if (errno != 0) { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! ! if (INFORMIX_MODE(compat)) ! { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! ECPGset_noind_null(ECPGt_date, &ddres); ! } ! else ! { ! ecpg_raise(lineno, ECPG_DATE_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } else { ! if (isarray && *scan_length == '"') ! scan_length++; ! ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_DATE_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } ! *((date *) (var + offset * act_tuple)) = ddres; ! pval = scan_length; } break; case ECPGt_timestamp: ! if (pval) { ! if (isarray && *pval == '"') ! tres = PGTYPEStimestamp_from_asc(pval + 1, &scan_length); ! else ! tres = PGTYPEStimestamp_from_asc(pval, &scan_length); ! /* did we get an error? */ ! if (errno != 0) { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! ! if (INFORMIX_MODE(compat)) ! { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! ECPGset_noind_null(ECPGt_timestamp, &tres); ! } ! else ! { ! ecpg_raise(lineno, ECPG_TIMESTAMP_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } else { ! if (isarray && *scan_length == '"') ! scan_length++; ! ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_TIMESTAMP_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } ! *((timestamp *) (var + offset * act_tuple)) = tres; ! pval = scan_length; } break; default: --- 530,670 ---- break; case ECPGt_interval: ! if (isarray && *pval == '"') ! ires = PGTYPESinterval_from_asc(pval + 1, &scan_length); ! else ! ires = PGTYPESinterval_from_asc(pval, &scan_length); ! /* did we get an error? */ ! if (ires == NULL) ! { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! if (INFORMIX_MODE(compat)) ! { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! ires = (interval *) ecpg_alloc(sizeof(interval), lineno); ! if (!ires) return (false); ! ! ECPGset_noind_null(ECPGt_interval, ires); } else { ! ecpg_raise(lineno, ECPG_INTERVAL_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } } else ! { ! if (isarray && *scan_length == '"') ! scan_length++; ! ! if (garbage_left(isarray, scan_length, compat)) ! { ! free(ires); ! ecpg_raise(lineno, ECPG_INTERVAL_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } ! } ! pval = scan_length; PGTYPESinterval_copy(ires, (interval *) (var + offset * act_tuple)); free(ires); break; + case ECPGt_date: ! if (isarray && *pval == '"') ! ddres = PGTYPESdate_from_asc(pval + 1, &scan_length); ! else ! ddres = PGTYPESdate_from_asc(pval, &scan_length); ! ! /* did we get an error? */ ! if (errno != 0) { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! if (INFORMIX_MODE(compat)) { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! ECPGset_noind_null(ECPGt_date, &ddres); } else { ! ecpg_raise(lineno, ECPG_DATE_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } + } + else + { + if (isarray && *scan_length == '"') + scan_length++; ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_DATE_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } + + *((date *) (var + offset * act_tuple)) = ddres; + pval = scan_length; break; case ECPGt_timestamp: ! if (isarray && *pval == '"') ! tres = PGTYPEStimestamp_from_asc(pval + 1, &scan_length); ! else ! tres = PGTYPEStimestamp_from_asc(pval, &scan_length); ! ! /* did we get an error? */ ! if (errno != 0) { ! ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n", ! lineno, pval ? pval : "", errno); ! if (INFORMIX_MODE(compat)) { ! /* ! * Informix wants its own NULL value here ! * instead of an error ! */ ! ECPGset_noind_null(ECPGt_timestamp, &tres); } else { ! ecpg_raise(lineno, ECPG_TIMESTAMP_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); } + } + else + { + if (isarray && *scan_length == '"') + scan_length++; ! if (garbage_left(isarray, scan_length, compat)) ! { ! ecpg_raise(lineno, ECPG_TIMESTAMP_FORMAT, ! ECPG_SQLSTATE_DATATYPE_MISMATCH, pval); ! return (false); ! } } + + *((timestamp *) (var + offset * act_tuple)) = tres; + pval = scan_length; break; default: