IBM / nodejs-idb-connector

A JavaScript (Node.js) library for communicating with Db2 for IBM i, with support for queries, procedures, and much more. Uses traditional callback-style syntax

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enable returning `SQL_DOUBLE` type as a JS Number

abmusse opened this issue · comments

Describe the solution you'd like
Enable returning SQL_DOUBLE type as a JS Number instead of a JS String if dbstmt.asNumber(true) is set.

Contiuation of #64

Currently only SQL_INTEGER, SQL_SMALLINT, SQL_DECIMAL, and SQL_NUMERIC types will be returned as JS Number when dbstmt.asNumber(true) is set.

Additional context

image

Source

According to the docs:

Floating-point
A single-precision floating-point number is a 32-bit approximate representation 
of a real number. The range of magnitude is approximately 
1.17549436 × 10-38 to 3.40282356 × 1038.

A double-precision floating-point number is a IEEE 64-bit approximate representation 
of a real number. The range of magnitude is approximately
 2.2250738585072014 × 10^-308 to 1.7976931348623158 × 10^308.

Single-precision floating-point is generally accurate to 7 digits of precision.
Double-precision floating-point is generally accurate to 15 digits of precision.

JavaScript Number has the following min and max values:

Number.MAX_VALUE = 1.7976931348623157e+308

Number.MIN_VALUE = 5e-324

Seems like the JS Number Class will be able to hold the SQL_DOUBLE type.

We can probably just add a case for SQL_DOUBLE here

@dmabupt @kadler @markdirish @ThePrez Do you think this would work?

resolved with commit 1b4d8d1

idb-connector v1.2.5 is released to fix this issue.

Nice!

I think we should also add test cases selecting and fetching Min and Max values for DOUBLE, FLOAT, REAL types to confirm the behavior. Similar to how we have done here.

Note that precision and scale returned for REAL and DOUBLE columns is not the same as for DECIMAL/NUMERIC columns. It does not contain the number of digits, but instead precision will be either 4 (REAL) or 8 (DOUBLE), which is the size in bytes that it takes up.

Code like this is not correct

case SQL_FLOAT:
case SQL_DOUBLE:
{
maxColLen = dbColumn[col].colPrecise + dbColumn[col].colScale + 3;
bindingRowInC[col] = (SQLCHAR *)calloc(maxColLen, sizeof(SQLCHAR));

case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
if (asNumber == true && dbColumn[col].colPrecise <= 15)

Hello,
@kadler commit bcb1962 fixed it.
@abmusse the above commit added the test cases.