Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WuKYw-0003AJ-3Z for pgsql-odbc@arkaria.postgresql.org; Tue, 10 Jun 2014 11:49:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WuKYv-0003z8-3y for pgsql-odbc@arkaria.postgresql.org; Tue, 10 Jun 2014 11:49:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WuKYu-0003yv-4o for pgsql-odbc@postgresql.org; Tue, 10 Jun 2014 11:49:56 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1WuKYl-0000Ti-JP for pgsql-odbc@postgresql.org; Tue, 10 Jun 2014 11:49:53 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 886DA98084; Tue, 10 Jun 2014 04:49:42 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 8207E90900; Tue, 10 Jun 2014 04:49:42 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 8BC33C0059; Tue, 10 Jun 2014 04:49:54 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA; Tue, 10 Jun 2014 04:49:52 -0700 (PDT) Message-ID: <5396F0CF.4040107@vmware.com> Date: Tue, 10 Jun 2014 14:49:35 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.5.0 MIME-Version: 1.0 To: Walter Couto , Hiroshi Inoue , "pgsql-odbc@postgresql.org" Subject: Re: Problem retrieving a numeric(38,0) value as SQL_NUMERIC_STRUCT if value needs to use all 16 SQLCHAR elements of the val array References: <7FA4F9E5512F214C801E596ECD8790E066A79839@ETNAMAIL01.embarcadero.com>, <538D44EC.10201@tpf.co.jp> <538E9137.1000308@tpf.co.jp> <7FA4F9E5512F214C801E596ECD8790E066A7FCE4@ETNAMAIL01.embarcadero.com> In-Reply-To: <7FA4F9E5512F214C801E596ECD8790E066A7FCE4@ETNAMAIL01.embarcadero.com> Content-Type: multipart/mixed; boundary="------------090303060105010209040202" X-Pg-Spam-Score: -7.6 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------090303060105010209040202 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Ugh, the decimal->binary conversion algorithm is really ugly even before this patch, and the patch doesn't make it any prettier. It took me quite a while to understand how it works: it repeatedly divides the base-10 representation by two, and outputs the reminder bit. There are much better and faster ways to convert from decimal to binary, and they're not really any more complicated either. The binary->decimal conversion in ResolveNumericParam is even more ugly. To be honest, I still don't understand the algorithm behind it, but it looks complicated and slow. There's a fast-path for small numbers that fit in an UInt4, which doesn't make it any simpler. Aside from ugly and slow, the binary->decimal conversion is also buggy: 1.The following input creates a core dump with "Floating point exception": precision=3, scale=50, val=0x02 0x03 2. Building the final output string is broken if scale is large, e.g. this produces garbage output: precision=25, scale=80 ERROR: invalid input syntax for type numeric: "p.000`000H0000000000000000000000aT000000000000000000000000000000000000000000000770"; 3. The param_string buffer allocated for the result of the binary->decimal conversion is too small. The buffer is 128 bytes, but a numeric can have a scale as large as 127. That won't fit, as the sign and decimal dot use two more bytes. I propose the attached patch to fix all of the above, rewriting the binary->decimal and decimal->binary conversions altogether. It fixes all of the known issues, the ones listed above and the one Walter reported. It also adds a regression test for all of them. Can anyone come up with some more special cases that this patched or the old conversion might handle incorrectly, for adding to the regression suite? One more thing: The precision works in a funny way, if you pass a "val" with more bits than fit in precision. I filled the val-bytes with 0x9F 0x86 0x01, which corresponds to 99999 in decimal. I passed that as a param with different precisions: sign 1 prec 1 scale 0 val 9F8601: 159 sign 1 prec 2 scale 0 val 9F8601: 159 sign 1 prec 3 scale 0 val 9F8601: 34463 sign 1 prec 4 scale 0 val 9F8601: 34463 sign 1 prec 5 scale 0 val 9F8601: 99999 sign 1 prec 6 scale 0 val 9F8601: 99999 The conversion algorithm seems to assume that there are no extra bits in the val-array that don't fit within the precision. Perhaps that's a reasonable assumption, but I think it would be more robust to ignore any extra bits if they are there, rather than sometimes take some of them into account. (Or, always parse all the bits, ignoring the precision.) I did that in the attached patch. However, according to this article I found from Microsoft: https://support.microsoft.com/kb/222831 "The precision and scale fields of the numeric structure are never used for input from an application, only for output from the driver to the application." So we probably should be ignoring them completely, and get the precision and scale from elsewhere. PS. ResolveNumericParam always returns TRUE. That's fortunate, because the caller would incorrectly fall through the switch-case if it didn't.. When the code was added, it fell through to the "default" case, but the interval-cases were later added in between, breaking it. But it's not a live bug, since ResolveNumericParam always returns TRUE. Fixed that too in the attached patch. - Heikki --------------090303060105010209040202 Content-Type: text/x-diff; name="fix-numeric-conversions-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-numeric-conversions-1.patch" commit fd0aaec61a199fffea75579f7aea821f8094374c Author: Heikki Linnakangas Date: Tue Jun 10 13:35:05 2014 +0300 Rewrite the conversion functions between strings and SQL_NUMERIC_STRUCT. Also adds a test case for the conversions. diff --git a/convert.c b/convert.c index 8871216..ae18250 100644 --- a/convert.c +++ b/convert.c @@ -215,6 +215,8 @@ static unsigned ODBCINT64 ATOI64U(const char *val) #endif /* WIN32 */ #endif /* ODBCINT64 */ +static void parse_to_numeric_struct(const char *wv, SQL_NUMERIC_STRUCT *ns, BOOL *overflow); + /* * TIMESTAMP <-----> SIMPLE_TIME * precision support since 7.2. @@ -1595,80 +1597,21 @@ inolog("2stime fr=%d\n", std_time.fr); break; #if (ODBCVER >= 0x0300) - case SQL_C_NUMERIC: - { - SQL_NUMERIC_STRUCT *ns; - int i, nlen, bit, hval, tv, dig, sta, olen; - char calv[SQL_MAX_NUMERIC_LEN * 3]; - const char *wv; - BOOL dot_exist; - - len = sizeof(SQL_NUMERIC_STRUCT); - if (bind_size > 0) - ns = (SQL_NUMERIC_STRUCT *) rgbValueBindRow; - else - ns = (SQL_NUMERIC_STRUCT *) rgbValue + bind_row; - for (wv = neut_str; *wv && isspace((unsigned char) *wv); wv++) - ; - ns->sign = 1; - if (*wv == '-') - { - ns->sign = 0; - wv++; - } - else if (*wv == '+') - wv++; - while (*wv == '0') wv++; - ns->precision = 0; - ns->scale = 0; - for (nlen = 0, dot_exist = FALSE;; wv++) - { - if (*wv == '.') - { - if (dot_exist) - break; - dot_exist = TRUE; - } - else if (*wv == '\0' || !isdigit((unsigned char) *wv)) - break; - else - { - if (dot_exist) - ns->scale++; - ns->precision++; - calv[nlen++] = *wv; - } - } - memset(ns->val, 0, sizeof(ns->val)); - for (hval = 0, bit = 1L, sta = 0, olen = 0; sta < nlen;) - { - for (dig = 0, i = sta; i < nlen; i++) + case SQL_C_NUMERIC: { - tv = dig * 10 + calv[i] - '0'; - dig = tv % 2; - calv[i] = tv / 2 + '0'; - if (i == sta && tv < 2) - sta++; - } - if (dig > 0) - hval |= bit; - bit <<= 1; - if (bit >= (1L << 8)) - { - ns->val[olen++] = hval; - hval = 0; - bit = 1L; - if (olen >= SQL_MAX_NUMERIC_LEN - 1) - { - ns->scale = sta - ns->precision; - break; - } + SQL_NUMERIC_STRUCT *ns; + BOOL overflowed; + + if (bind_size > 0) + ns = (SQL_NUMERIC_STRUCT *) rgbValueBindRow; + else + ns = (SQL_NUMERIC_STRUCT *) rgbValue + bind_row; + + parse_to_numeric_struct(neut_str, ns, &overflowed); + if (overflowed) + result = COPY_RESULT_TRUNCATED; } - } - if (hval && olen < SQL_MAX_NUMERIC_LEN - 1) - ns->val[olen++] = hval; - } - break; + break; #endif /* ODBCVER */ case SQL_C_SSHORT: @@ -3829,136 +3772,201 @@ cleanup: } #if (ODBCVER >= 0x0300) -static BOOL + +/* + * With SQL_MAX_NUMERIC_LEN = 16, the highest representable number is + * 2^128 - 1, which fits in 39 digits. + */ +#define MAX_NUMERIC_DIGITS 39 + +/* + * Convert a SQL_NUMERIC_STRUCT into string representation. + */ +static void ResolveNumericParam(const SQL_NUMERIC_STRUCT *ns, char *chrform) { - static const int prec[] = {1, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39}; - Int4 i, j, k, ival, vlen, len, newlen; - UCHAR calv[40]; + Int4 i, vlen, len, newlen; const UCHAR *val = (const UCHAR *) ns->val; - BOOL next_figure; + UCHAR vals[SQL_MAX_NUMERIC_LEN]; + int lastnonzero; + UCHAR calv[MAX_NUMERIC_DIGITS]; + int precision; inolog("C_NUMERIC [prec=%d scale=%d]", ns->precision, ns->scale); + if (0 == ns->precision) { strcpy(chrform, "0"); - return TRUE; - } - else if (ns->precision < prec[sizeof(Int4)]) - { - for (i = 0, ival = 0; i < sizeof(Int4) && prec[i] <= ns->precision; i++) - { -inolog("(%d)", val[i]); - ival += (val[i] << (8 * i)); /* ns->val is little endian */ - } -inolog(" ival=%d,%d", ival, (val[3] << 24) | (val[2] << 16) | (val[1] << 8) | val[0]); - if (0 == ns->scale) - { - if (0 == ns->sign) - ival *= -1; - sprintf(chrform, "%d", ival); - } - else if (ns->scale > 0) - { - Int4 i, div, o1val, o2val; - - for (i = 0, div = 1; i < ns->scale; i++) - div *= 10; - o1val = ival / div; - o2val = ival % div; - if (0 == ns->sign) - sprintf(chrform, "-%d.%0*d", o1val, ns->scale, o2val); - else - sprintf(chrform, "%d.%0*d", o1val, ns->scale, o2val); - } -inolog(" convval=%s\n", chrform); - return TRUE; + return; } - for (i = 0; i < SQL_MAX_NUMERIC_LEN && prec[i] <= ns->precision; i++) - ; - vlen = i; + precision = ns->precision; + if (precision > MAX_NUMERIC_DIGITS) + precision = MAX_NUMERIC_DIGITS; + + /* + * The representation in SQL_NUMERIC_STRUCT is 16 bytes with most + * significant byte first. Make a working copy. + */ + memcpy(vals, val, SQL_MAX_NUMERIC_LEN); + + vlen = SQL_MAX_NUMERIC_LEN; len = 0; - memset(calv, 0, sizeof(calv)); -inolog(" len1=%d", vlen); - for (i = vlen - 1; i >= 0; i--) + do { - for (j = len - 1; j >= 0; j--) - { - if (!calv[j]) - continue; - ival = (((Int4)calv[j]) << 8); - calv[j] = (ival % 10); - ival /= 10; - calv[j + 1] += (ival % 10); - ival /= 10; - calv[j + 2] += (ival % 10); - ival /= 10; - calv[j + 3] += ival; - for (k = j;; k++) - { - next_figure = FALSE; - if (calv[k] > 0) - { - if (k >= len) - len = k + 1; - while (calv[k] > 9) - { - calv[k + 1]++; - calv[k] -= 10; - next_figure = TRUE; - } - } - if (k >= j + 3 && !next_figure) - break; - } - } - ival = val[i]; - if (!ival) - continue; - calv[0] += (ival % 10); - ival /= 10; - calv[1] += (ival % 10); - ival /= 10; - calv[2] += ival; - for (j = 0;; j++) + UInt2 d, r; + + /* + * Divide the number by 10, and output the reminder as the next digit. + * + * Begin from the most-significant byte (last in the array), and at + * each step, carry the remainder to the prev byte. + */ + r = 0; + lastnonzero = -1; + for (i = vlen - 1; i >= 0; i--) { - next_figure = FALSE; - if (calv[j] > 0) - { - if (j >= len) - len = j + 1; - while (calv[j] > 9) - { - calv[j + 1]++; - calv[j] -= 10; - next_figure = TRUE; - } - } - if (j >= 2 && !next_figure) - break; + UInt2 v; + + v = ((UInt2) vals[i]) + (r << 8); + d = v / 10; r = v % 10; + vals[i] = (UCHAR) d; + + if (d != 0 && lastnonzero == -1) + lastnonzero = i; } - } + + /* output the remainder */ + calv[len++] = r; + + vlen = lastnonzero + 1; + } while(lastnonzero >= 0 && len < precision); + + /* + * calv now contains the digits in reverse order, i.e. least significant + * digit is at calv[0] + */ + inolog(" len2=%d", len); + + /* build the final output string. */ newlen = 0; if (0 == ns->sign) chrform[newlen++] = '-'; - if (i = len - 1, i < ns->scale) + + i = len - 1; + if (i < ns->scale) i = ns->scale; for (; i >= ns->scale; i--) - chrform[newlen++] = calv[i] + '0'; + { + if (i >= len) + chrform[newlen++] = '0'; + else + chrform[newlen++] = calv[i] + '0'; + } if (ns->scale > 0) { chrform[newlen++] = '.'; for (; i >= 0; i--) - chrform[newlen++] = calv[i] + '0'; + { + if (i >= len) + chrform[newlen++] = '0'; + else + chrform[newlen++] = calv[i] + '0'; + } } if (0 == len) chrform[newlen++] = '0'; chrform[newlen] = '\0'; inolog(" convval(2) len=%d %s\n", newlen, chrform); - return TRUE; } + +/* + * Convert a string representation of a numeric into SQL_NUMERIC_STRUCT. + */ +static void +parse_to_numeric_struct(const char *wv, SQL_NUMERIC_STRUCT *ns, BOOL *overflow) +{ + int i, nlen, dig; + char calv[SQL_MAX_NUMERIC_LEN * 3]; + BOOL dot_exist; + + *overflow = FALSE; + + /* skip leading space */ + while (*wv && isspace((unsigned char) *wv)) + wv++; + + /* sign */ + ns->sign = 1; + if (*wv == '-') + { + ns->sign = 0; + wv++; + } + else if (*wv == '+') + wv++; + + /* skip leading zeros */ + while (*wv == '0') + wv++; + + /* read the digits into calv */ + ns->precision = 0; + ns->scale = 0; + for (nlen = 0, dot_exist = FALSE;; wv++) + { + if (*wv == '.') + { + if (dot_exist) + break; + dot_exist = TRUE; + } + else if (*wv == '\0' || !isdigit((unsigned char) *wv)) + break; + else + { + if (nlen >= sizeof(calv)) + { + if (dot_exist) + break; + else + { + ns->scale--; + *overflow = TRUE; + continue; + } + } + if (dot_exist) + ns->scale++; + calv[nlen++] = *wv; + } + } + ns->precision = nlen; + + /* Convert the decimal digits to binary */ + memset(ns->val, 0, sizeof(ns->val)); + for (dig = 0; dig < nlen; dig++) + { + UInt4 carry; + + /* multiply the current value by 10, and add the next digit */ + carry = calv[dig] - '0'; + for (i = 0; i < sizeof(ns->val); i++) + { + UInt4 t; + + t = ((UInt4) ns->val[i]) * 10 + carry; + ns->val[i] = (unsigned char) (t & 0xFF); + carry = (t >> 8); + } + + if (carry != 0) + *overflow = TRUE; + } +} + + #endif /* ODBCVER */ /* @@ -3975,7 +3983,7 @@ ResolveOneParam(QueryBuild *qb, QueryParse *qp, BOOL *isnull) PutDataInfo *pdata = qb->pdata; int param_number; - char param_string[128], + char param_string[150], tmp[256]; char cbuf[PG_NUMERIC_MAX_PRECISION * 2]; /* seems big enough to handle the data in this function */ OID param_pgtype; @@ -4403,8 +4411,10 @@ mylog("C_WCHAR=%s(%d)\n", buffer, used); } #if (ODBCVER >= 0x0300) case SQL_C_NUMERIC: - if (ResolveNumericParam((SQL_NUMERIC_STRUCT *) buffer, param_string)) - break; + { + ResolveNumericParam((SQL_NUMERIC_STRUCT *) buffer, param_string); + break; + } case SQL_C_INTERVAL_YEAR: ivsign = ivstruct->interval_sign ? "-" : ""; sprintf(param_string, "%s%d years", ivsign, ivstruct->intval.year_month.year); diff --git a/test/Makefile.in b/test/Makefile.in index 4bb5149..8bc3419 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -2,7 +2,7 @@ TESTS = connect stmthandles select commands multistmt getresult prepare \ params notice arraybinding insertreturning dataatexecution \ boolsaschar cvtnulldate alter quotes cursors positioned-update \ catalogfunctions bindcol lfconversion cte deprecated error-rollback \ - diagnostic + diagnostic numeric TESTBINS = $(patsubst %,src/%-test, $(TESTS)) TESTSQLS = $(patsubst %,sql/%.sql, $(TESTS)) diff --git a/test/expected/numeric.out b/test/expected/numeric.out new file mode 100644 index 0000000..e01c6d2 --- /dev/null +++ b/test/expected/numeric.out @@ -0,0 +1,61 @@ +\! ./src/numeric-test +connected +Testing SQL_NUMERIC_STRUCT params... + +sign 1 prec 5 scale 3 val 7C62: + 25.212 +sign 1 prec 41 scale 0 val 78563412 78563412 78563412 78563412: + 24197857161011715162171839636988778104 +sign 1 prec 38 scale 0 val 4EF338DE509049C4133302F0F6B04909: + 12345678901234567890123456789012345678 +sign 1 prec 50 scale 0 val FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF: + 340282366920938463463374607431768211455 +sign 1 prec 1 scale 0 val 00: + 0 +sign 0 prec 1 scale 0 val 00: + 0 +sign 1 prec 3 scale 2 val 0203: + 7.70 +sign 1 prec 1 scale 0 val 3930: + 1 +sign 1 prec 2 scale 0 val 3930: + 12 +sign 1 prec 3 scale 0 val 3930: + 123 +sign 1 prec 4 scale 0 val 3930: + 1234 +sign 1 prec 5 scale 0 val 3930: + 12345 +sign 1 prec 6 scale 0 val 3930: + 12345 +sign 1 prec 3 scale 50 val 0203: + 0.00000000000000000000000000000000000000000000000770 +sign 1 prec 25 scale 80 val 0203: + 0.00000000000000000000000000000000000000000000000000000000000000000000000000000770 +sign 0 prec 25 scale 127 val FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF: + -0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003402823669209384634633746 +sign 1 prec 3 scale 50 val 0203: + 0.00000000000000000000000000000000000000000000000770 +Testing SQL_NUMERIC_STRUCT results... + +25.212: + sign 1 prec 5 scale 3 val 7C620000000000000000000000000000 +24197857161011715162171839636988778104: + sign 1 prec 38 scale 0 val 78563412785634127856341278563412 +12345678901234567890123456789012345678: + sign 1 prec 38 scale 0 val 4EF338DE509049C4133302F0F6B04909 +340282366920938463463374607431768211455: + sign 1 prec 39 scale 0 val FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF +340282366920938463463374607431768211456: + sign 1 prec 39 scale 0 val 00000000000000000000000000000000 +340282366920938463463374607431768211457: + sign 1 prec 39 scale 0 val 01000000000000000000000000000000 +-0: + sign 1 prec 0 scale 0 val 00000000000000000000000000000000 +0: + sign 1 prec 0 scale 0 val 00000000000000000000000000000000 +-7.70: + sign 0 prec 3 scale 2 val 02030000000000000000000000000000 +999999999999: + sign 1 prec 12 scale 0 val FF0FA5D4E80000000000000000000000 +disconnecting diff --git a/test/src/numeric-test.c b/test/src/numeric-test.c new file mode 100644 index 0000000..3f34bdc --- /dev/null +++ b/test/src/numeric-test.c @@ -0,0 +1,223 @@ +/* + * Test cases for dealing with SQL_NUMERIC_STRUCT + */ +#include +#include + +#include "common.h" + +static unsigned char +hex_to_int(char c) +{ + char result; + + if (c >= '0' && c <= '9') + result = c - '0'; + else if (c >= 'a' && c <= 'f') + result = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + result = c - 'A' + 10; + else + { + fprintf(stderr, "invalid hex-encoded numeric value\n"); + exit(1); + } + return (unsigned char) result; +} + +static void +build_numeric_struct(SQL_NUMERIC_STRUCT *numericparam, + unsigned char sign, char *hexstr, + unsigned char precision, unsigned char scale) +{ + int len; + + /* parse the hex-encoded value */ + memset(numericparam, 0, sizeof(SQL_NUMERIC_STRUCT)); + + numericparam->sign = sign; + numericparam->precision = precision; + numericparam->scale = scale; + + len = 0; + while (*hexstr) + { + if (*hexstr == ' ') + { + hexstr++; + continue; + } + if (len >= SQL_MAX_NUMERIC_LEN) + { + fprintf(stderr, "hex-encoded numeric value too long\n"); + exit(1); + } + numericparam->val[len] = + hex_to_int(*hexstr) << 4 | hex_to_int(*(hexstr + 1)); + hexstr += 2; + len++; + } +} + +static void +test_numeric_param(HSTMT hstmt, unsigned char sign, char *hexval, + unsigned char precision, unsigned char scale) +{ + SQL_NUMERIC_STRUCT numericparam; + SQLLEN cbParam1; + SQLRETURN rc; + char buf[200]; + + build_numeric_struct(&numericparam, sign, hexval, precision, scale); + + cbParam1 = sizeof(numericparam); + rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, + SQL_C_NUMERIC, /* value type */ + SQL_NUMERIC, /* param type */ + 0, /* column size (ignored for SQL_INTERVAL_SECOND) */ + 0, /* dec digits */ + &numericparam, /* param value ptr */ + sizeof(numericparam), /* buffer len (ignored for SQL_C_INTERVAL_SECOND) */ + &cbParam1 /* StrLen_or_IndPtr (ignored for SQL_C_INTERVAL_SECOND) */); + CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt); + + /* Execute */ + rc = SQLExecute(hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("SQLExecute failed", SQL_HANDLE_STMT, hstmt); + } + else + { + /* print result */ + rc = SQLFetch(hstmt); + CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt); + + rc = SQLGetData(hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), NULL); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + printf("sign %u prec %u scale %d val %s:\n %s\n", + sign, precision, scale, hexval, buf); + } + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); +} + +static void +test_numeric_result(HSTMT hstmt, char *numstr) +{ + char sql[100]; + SQL_NUMERIC_STRUCT numericres; + SQLRETURN rc; + + /* + * assume 'numstr' param to be well-formed (we're testing how the + * results come out, not the input handling) + */ + snprintf(sql, sizeof(sql), "SELECT '%s'::numeric", numstr); + rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + + rc = SQLFetch(hstmt); + CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt); + + rc = SQLGetData(hstmt, 1, SQL_C_NUMERIC, &numericres, sizeof(numericres), NULL); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + printf("%s:\n sign %u prec %u scale %d val %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n", + numstr, numericres.sign, numericres.precision, numericres.scale, + numericres.val[0], numericres.val[1], + numericres.val[2], numericres.val[3], + numericres.val[4], numericres.val[5], + numericres.val[6], numericres.val[7], + numericres.val[8], numericres.val[9], + numericres.val[10], numericres.val[11], + numericres.val[12], numericres.val[13], + numericres.val[14], numericres.val[15]); + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); +} + +int main(int argc, char **argv) +{ + SQLRETURN rc; + HSTMT hstmt = SQL_NULL_HSTMT; + + test_connect(); + + rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + /**** Test binding SQL_NUMERIC_STRUCT params (SQL_C_NUMERIC) ****/ + + printf("Testing SQL_NUMERIC_STRUCT params...\n\n"); + + rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT ?::numeric", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); + + /* 25.212 (per Microsoft KB 22831) */ + test_numeric_param(hstmt, 1, "7C62", 5, 3); + + /* 24197857161011715162171839636988778104 */ + test_numeric_param(hstmt, 1, "78563412 78563412 78563412 78563412", 41, 0); + + /* 12345678901234567890123456789012345678 */ + test_numeric_param(hstmt, 1, "4EF338DE509049C4133302F0F6B04909", 38, 0); + + /* highest possible non-scaled: 340282366920938463463374607431768211455 */ + test_numeric_param(hstmt, 1, "FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF", 50, 0); + + /* positive and negative zero */ + test_numeric_param(hstmt, 1, "00", 1, 0); + test_numeric_param(hstmt, 0, "00", 1, 0); + + /* -7.70 */ + test_numeric_param(hstmt, 1, "0203", 3, 2); + + /* 0.12345, with 1-6 digit precision: */ + test_numeric_param(hstmt, 1, "3930", 1, 5); + test_numeric_param(hstmt, 1, "3930", 2, 5); + test_numeric_param(hstmt, 1, "3930", 3, 5); + test_numeric_param(hstmt, 1, "3930", 4, 5); + test_numeric_param(hstmt, 1, "3930", 5, 5); + test_numeric_param(hstmt, 1, "3930", 6, 5); + + /* large scale with small value */ + test_numeric_param(hstmt, 1, "0203", 3, 50); + + /* medium-sized scale and precision */ + test_numeric_param(hstmt, 1, "0203", 25, 80); + + /* max length output; negative with max scale and decimal dot */ + test_numeric_param(hstmt, 0, "FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF", 25, 127); + + /* large scale with small value */ + test_numeric_param(hstmt, 1, "0203", 3, 50); + + + /**** Test fetching SQL_NUMERIC_STRUCT results ****/ + printf("Testing SQL_NUMERIC_STRUCT results...\n\n"); + + test_numeric_result(hstmt, "25.212"); + test_numeric_result(hstmt, "24197857161011715162171839636988778104"); + test_numeric_result(hstmt, "12345678901234567890123456789012345678"); + /* highest number */ + test_numeric_result(hstmt, "340282366920938463463374607431768211455"); + /* overflow */ + test_numeric_result(hstmt, "340282366920938463463374607431768211456"); + test_numeric_result(hstmt, "340282366920938463463374607431768211457"); + + test_numeric_result(hstmt, "-0"); + test_numeric_result(hstmt, "0"); + test_numeric_result(hstmt, "-7.70"); + test_numeric_result(hstmt, "999999999999"); + + /* Clean up */ + test_disconnect(); + + return 0; +} --------------090303060105010209040202 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------090303060105010209040202--