Skip to content

Commit 48ccc7a

Browse files
committed
Add BigInt support for binding values
1 parent a32b2ab commit 48ccc7a

File tree

7 files changed

+135
-6
lines changed

7 files changed

+135
-6
lines changed

doc/src/release_notes.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ Common Changes
2323
#) Added support for an Oracle Database 23c JSON feature improving JSON
2424
storage usage.
2525

26-
#) Updated error thrown during pool reconfiguration while poolMax is 0 from
27-
`ORA-24413` to `NJS-007`.
26+
#) Updated error thrown during pool reconfiguration from `ORA-24413` to
27+
`NJS-007` when poolMax is 0.
2828

29-
#) Added new condition check during pool reconfiguration for `poolMin > poolMax`
30-
and error `NJS-092` will be thrown.
29+
#) Throw `NJS-092`` error if `poolMin` > `poolMax` during pool
30+
reconfiguration.
31+
32+
#) Added support for binding BigInt values. BigInts like ``123n`` can
33+
be passed to ``connection.execute()``, ``connection.executeMany()``.
34+
See `PR #1572 <https://github.com/oracle/node-oracledb/pull/1572>`__
35+
(Slawomir Osoba).
3136

3237
Thin Mode Changes
3338
++++++++++++++++++

lib/connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class Connection extends EventEmitter {
200200
typeof value === 'number' ||
201201
typeof value === 'string' ||
202202
typeof value === 'boolean' ||
203+
typeof value === 'bigint' ||
203204
Array.isArray(value) ||
204205
value instanceof Float32Array ||
205206
value instanceof Float64Array ||

lib/transformer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function transformValueIn(info, value, options) {
172172
return value;
173173

174174
// handle numbers
175-
} else if (typeof value === 'number') {
175+
} else if (typeof value === 'number' || typeof value === 'bigint') {
176176
checkType(info, options,
177177
types.DB_TYPE_NUMBER,
178178
types.DB_TYPE_BINARY_INTEGER,

src/njsVariable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ bool njsVariable_setScalarValue(njsVariable *var, uint32_t pos, napi_env env,
823823
}
824824

825825
// handle binding numbers
826-
if (valueType == napi_number) {
826+
if (valueType == napi_number || valueType == napi_bigint) {
827827
if (var->varTypeNum == DPI_ORACLE_TYPE_NUMBER) {
828828
NJS_CHECK_NAPI(env, napi_coerce_to_string(env, value, &numStr));
829829
return njsVariable_setFromString(var, pos, env, numStr, baton);

test/bigInt.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* Copyright 2024, Oracle and/or its affiliates. */
2+
3+
/******************************************************************************
4+
*
5+
* This software is dual-licensed to you under the Universal Permissive License
6+
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
7+
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
8+
* either license.
9+
*
10+
* If you elect to accept the software under the Apache License, Version 2.0,
11+
* the following applies:
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* https://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*
25+
* NAME
26+
* 300. bigInt.js
27+
*
28+
* DESCRIPTION
29+
* Test cases related to use of bigInts for binding and fetching.
30+
*
31+
*****************************************************************************/
32+
'use strict';
33+
34+
const oracledb = require('oracledb');
35+
const dbConfig = require('./dbconfig.js');
36+
const assert = require('assert');
37+
38+
describe('300. bigInt.js', function() {
39+
let connection;
40+
const createTable = `CREATE TABLE nodb_bigint (id NUMBER)`;
41+
const dropTable = `DROP TABLE nodb_bigint PURGE`;
42+
43+
before('get connection and create table', async function() {
44+
connection = await oracledb.getConnection(dbConfig);
45+
await connection.execute(createTable);
46+
});
47+
48+
after('drop table and release connection', async function() {
49+
await connection.execute(dropTable);
50+
await connection.close();
51+
});
52+
53+
describe('300.1 can bind bigInts', function() {
54+
55+
it('300.1.1 with execute', async function() {
56+
const sql = 'INSERT INTO nodb_bigint VALUES(:1)';
57+
const bind = [123n];
58+
await assert.doesNotReject(connection.execute(sql, bind));
59+
}); //300.1.1
60+
61+
it('300.1.2 with executeMany', async function() {
62+
const sql = 'INSERT INTO nodb_bigint VALUES(:a)';
63+
const binds = [
64+
{ a: 1234n },
65+
{ a: 98765432123456n },
66+
{ a: -1234n },
67+
{ a: BigInt(Number.MAX_SAFE_INTEGER) + 1n }
68+
];
69+
const options = {
70+
bindDefs: {
71+
a: { type: oracledb.NUMBER }
72+
}
73+
};
74+
await assert.doesNotReject(connection.executeMany(sql, binds, options));
75+
}); //300.1.2
76+
77+
}); //300.1
78+
79+
describe('300.2 fetch values', function() {
80+
const sql = 'SELECT id FROM nodb_bigint WHERE id = :1';
81+
const bind = [ 123n ];
82+
83+
afterEach('Reset oracledb.fetchTypeHandler property', function() {
84+
oracledb.fetchTypeHandler = undefined;
85+
});
86+
87+
it('300.2.1 fetches the value as number(default)', async function() {
88+
const result = await connection.execute(sql, bind);
89+
assert.deepStrictEqual(result.rows[0][0], 123);
90+
}); //300.2.1
91+
92+
it('300.2.2 use fetchTypeHandler to get BigInt value', async function() {
93+
const myFetchTypeHandler = function() {
94+
return {converter: (val) => BigInt(val)};
95+
};
96+
97+
oracledb.fetchTypeHandler = myFetchTypeHandler;
98+
99+
const result = await connection.execute(sql, bind);
100+
assert.deepStrictEqual(result.rows[0][0], 123n);
101+
}); //300.2.2
102+
103+
it('300.2.3 fetches values greater than Number.MAX_SAFE_INTEGER correctly', async function() {
104+
const bindval = [ BigInt(Number.MAX_SAFE_INTEGER) + 1n ];
105+
106+
const result = await connection.execute(sql, bindval);
107+
assert.deepStrictEqual(result.rows[0][0], Number.MAX_SAFE_INTEGER + 1);
108+
109+
}); //300.2.3
110+
111+
}); //300.2
112+
113+
}); //300

test/list.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,3 +5812,12 @@ oracledb.OUT_FORMAT_OBJECT and resultSet = true
58125812

58135813
299. invalidNumber.js
58145814
299.1 throws error for invalid numbers
5815+
5816+
300. bigInt.js
5817+
300.1 can bind bigInts
5818+
300.1.1 with execute
5819+
300.1.2 with executeMany
5820+
300.2 fetch values
5821+
300.2.1 fetches the value as number(default)
5822+
300.2.2 use fetchTypeHandler to get BigInt value
5823+
300.2.3 fetches values greater than Number.MAX_SAFE_INTEGER correctly

test/opts/.mocharc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,4 @@ spec:
281281
- test/dataTypeVector3.js
282282
- test/jsonDualityViews7.js
283283
- test/invalidNumber.js
284+
- test/bigInt.js

0 commit comments

Comments
 (0)