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
MIT License
38 stars 23 forks source link

Enable returning `SQL_DOUBLE` type as a JS Number #104

Closed abmusse closed 4 years ago

abmusse commented 4 years ago

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?

dmabupt commented 4 years ago

resolved with commit 1b4d8d1

dmabupt commented 4 years ago

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

abmusse commented 4 years ago

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.

kadler commented 4 years ago

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 https://github.com/IBM/nodejs-idb-connector/blob/1b4d8d19b0230e553f542c1b2ea74c0ce5e52231/src/db2ia/dbstmt.cc#L2099-L2103 https://github.com/IBM/nodejs-idb-connector/blob/1b4d8d19b0230e553f542c1b2ea74c0ce5e52231/src/db2ia/dbstmt.cc#L2269-L2272

dmabupt commented 4 years ago

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