|
| 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 |
0 commit comments