2023-05-03 18:32:09 +08:00
|
|
|
/* Copyright (c) 2021, 2023, Oracle and/or its affiliates. */
|
2021-04-01 12:48:37 +08:00
|
|
|
|
2019-06-23 06:36:03 +08:00
|
|
|
/******************************************************************************
|
2019-06-28 08:23:14 +08:00
|
|
|
*
|
2023-02-21 09:43:43 +08:00
|
|
|
* This software is dual-licensed to you under the Universal Permissive License
|
|
|
|
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
|
|
|
|
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
|
|
|
|
* either license.
|
2019-06-23 06:36:03 +08:00
|
|
|
*
|
2023-02-21 09:43:43 +08:00
|
|
|
* If you elect to accept the software under the Apache License, Version 2.0,
|
|
|
|
* the following applies:
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
2019-06-23 06:36:03 +08:00
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
2023-02-21 09:43:43 +08:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2019-06-23 06:36:03 +08:00
|
|
|
*
|
2023-02-21 09:43:43 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
2019-06-23 06:36:03 +08:00
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* NAME
|
2021-10-17 14:18:44 +08:00
|
|
|
* 197. dbObjectsNestedTable.js
|
2019-06-23 06:36:03 +08:00
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* Testing Oracle Database Nested Table Object Collection type(s)
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2019-06-28 08:23:14 +08:00
|
|
|
'use strict';
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2021-10-11 10:33:01 +08:00
|
|
|
const oracledb = require ('oracledb');
|
|
|
|
const assert = require ('assert');
|
|
|
|
const dbConfig = require ('./dbconfig.js');
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-05-24 00:01:55 +08:00
|
|
|
describe('197. dbObjectsNestedTable.js', () => {
|
2019-06-23 06:36:03 +08:00
|
|
|
let connection = null;
|
|
|
|
|
|
|
|
before (async () => {
|
2023-02-21 11:20:36 +08:00
|
|
|
connection = await oracledb.getConnection (dbConfig);
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let sql = `CREATE TYPE nodb_test197_typ IS TABLE OF VARCHAR2(30)`;
|
|
|
|
await connection.execute (sql);
|
|
|
|
await connection.commit ();
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `CREATE TABLE nodb_test197_TAB (
|
|
|
|
ID NUMBER, DEPT_NAMES nodb_test197_typ)
|
|
|
|
NESTED TABLE dept_names store as dnames_nt`;
|
|
|
|
await connection.execute (sql);
|
|
|
|
await connection.commit ();
|
2019-06-23 06:36:03 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
after (async () => {
|
2023-02-21 11:20:36 +08:00
|
|
|
await connection.execute (`drop table nodb_test197_tab purge`);
|
|
|
|
await connection.execute (`drop type nodb_test197_typ`);
|
|
|
|
await connection.commit ();
|
|
|
|
await connection.close ();
|
2019-06-23 06:36:03 +08:00
|
|
|
});
|
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
it('197.1 Insert into table with Nested-table + getValues',
|
2019-06-28 08:23:14 +08:00
|
|
|
async () => {
|
2023-02-21 11:20:36 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19701;
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append ("Shipping");
|
|
|
|
obj.append ("Finance");
|
|
|
|
obj.append ("Sales") ;
|
|
|
|
await connection.execute (sql, { id: id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
|
|
|
|
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB`;
|
|
|
|
const result = await connection.execute (sql);
|
|
|
|
obj = result.rows[0][1];
|
|
|
|
|
|
|
|
const arr = obj.getValues ();
|
|
|
|
assert.equal (arr.length, 3);
|
|
|
|
|
|
|
|
assert.strictEqual (arr[0], "Shipping");
|
|
|
|
assert.strictEqual (arr[1], "Finance");
|
|
|
|
assert.strictEqual (arr[2], "Sales");
|
|
|
|
}
|
|
|
|
); // 197.1
|
2019-06-23 06:36:03 +08:00
|
|
|
|
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
it ('197.2 Insert into table with Nested-table + getKeys',
|
2019-06-28 08:23:14 +08:00
|
|
|
async () => {
|
2023-02-21 11:20:36 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES (:id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19702;
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append ("Shipping");
|
|
|
|
obj.append ("Finance");
|
|
|
|
obj.append ("Sales") ;
|
|
|
|
await connection.execute (sql, { id: id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
2019-07-11 12:49:31 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB`;
|
|
|
|
const result = await connection.execute (sql);
|
|
|
|
obj = result.rows[0][1];
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
const arr = obj.getKeys ();
|
|
|
|
assert.equal (arr.length, 3);
|
|
|
|
assert.strictEqual (arr[0], 0);
|
|
|
|
assert.strictEqual (arr[1], 1);
|
|
|
|
assert.strictEqual (arr[2], 2);
|
|
|
|
}
|
|
|
|
); // 197.2
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
it ('197.3 Insert into table with Nested-table + first * next',
|
2019-06-28 08:23:14 +08:00
|
|
|
async () => {
|
2023-02-21 11:20:36 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19703;
|
2019-07-11 12:49:31 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append ("Shipping");
|
|
|
|
obj.append ("Finance");
|
|
|
|
obj.append ("Sales") ;
|
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB`;
|
|
|
|
const result = await connection.execute (sql);
|
|
|
|
obj = result.rows[0][1];
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let index = obj.getFirstIndex ();
|
|
|
|
while (index != null) {
|
|
|
|
const v = obj.getElement (index);
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
assert.strictEqual (v, "Shipping");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
assert.strictEqual (v, "Finance");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
assert.strictEqual (v, "Sales");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index = obj.getNextIndex (index);
|
2019-07-11 12:49:31 +08:00
|
|
|
}
|
2023-02-21 11:20:36 +08:00
|
|
|
}
|
|
|
|
); //197.3
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
it('197.4 Insert into table with Nested-table + last + prev',
|
2019-06-28 08:23:14 +08:00
|
|
|
async () => {
|
2023-02-21 11:20:36 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19704;
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append ("Shipping");
|
|
|
|
obj.append ("Finance");
|
|
|
|
obj.append ("Sales") ;
|
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB`;
|
|
|
|
const result = await connection.execute (sql);
|
|
|
|
obj = result.rows[0][1];
|
|
|
|
|
|
|
|
let index = obj.getLastIndex ();
|
|
|
|
while (index != null) {
|
|
|
|
let v = obj.getElement (index);
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
assert.strictEqual (v, "Shipping");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
assert.strictEqual (v, "Finance");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
assert.strictEqual (v, "Sales");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index = obj.getPrevIndex (index);
|
2019-07-11 12:49:31 +08:00
|
|
|
}
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
}
|
|
|
|
); //197.4
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
it('197.5 Insert into table with Nested-table + getElement',
|
|
|
|
async () => {
|
2019-07-11 12:49:31 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
2021-10-11 10:33:01 +08:00
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
2023-02-21 11:20:36 +08:00
|
|
|
const id = 19705;
|
2019-06-28 08:23:14 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append ("Shipping");
|
|
|
|
obj.append ("Finance");
|
|
|
|
obj.append ("Sales") ;
|
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
2019-06-28 08:23:14 +08:00
|
|
|
await connection.commit ();
|
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB`;
|
|
|
|
const result = await connection.execute (sql);
|
2019-06-28 08:23:14 +08:00
|
|
|
obj = result.rows[0][1];
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
// randomly use getElement ()
|
|
|
|
let v = obj.getElement (1);
|
|
|
|
assert.strictEqual (v, "Finance");
|
|
|
|
v = obj.getElement (0);
|
|
|
|
assert.strictEqual (v, "Shipping");
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
}
|
|
|
|
); //197.5
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
it('197.6 Insert into table with Nested-table + hasElement',
|
|
|
|
async () => {
|
2019-07-11 12:49:31 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
2021-10-11 10:33:01 +08:00
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
2023-02-21 11:20:36 +08:00
|
|
|
const id = 19706;
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append ("Shipping");
|
|
|
|
obj.append ("Finance");
|
|
|
|
obj.append ("Sales") ;
|
2021-04-01 12:48:37 +08:00
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
2019-07-11 12:49:31 +08:00
|
|
|
await connection.commit ();
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB`;
|
|
|
|
const result = await connection.execute (sql);
|
2019-07-11 12:49:31 +08:00
|
|
|
obj = result.rows[0][1];
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
assert.strictEqual(obj.hasElement(0), true);
|
|
|
|
assert.strictEqual(obj.hasElement(1), true);
|
|
|
|
assert.strictEqual(obj.hasElement(2), true);
|
|
|
|
assert.strictEqual(obj.hasElement(3), false);
|
2019-07-11 12:49:31 +08:00
|
|
|
}
|
2023-02-21 11:20:36 +08:00
|
|
|
); //197.6
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
it('197.7 Insert into table with Nested-table NULL Value for object',
|
|
|
|
async () => {
|
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19707;
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
await connection.execute (sql, { id : id,
|
|
|
|
v : { type : objClass, val : null } });
|
|
|
|
await connection.commit ();
|
|
|
|
|
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :ID`;
|
|
|
|
const result = await connection.execute (sql, { ID : 19707});
|
|
|
|
const obj = result.rows[0][1];
|
|
|
|
|
|
|
|
assert.strictEqual (obj, null);
|
|
|
|
}
|
|
|
|
); // 197.7
|
|
|
|
|
|
|
|
it('197.8 Insert into table with Nested-table NULL Value for object',
|
|
|
|
async () => {
|
2019-07-11 12:49:31 +08:00
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
2021-10-11 10:33:01 +08:00
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
2023-02-21 11:20:36 +08:00
|
|
|
const id = 19708;
|
|
|
|
|
|
|
|
let obj = new objClass ();
|
|
|
|
obj.append (null);
|
|
|
|
obj.append (null);
|
|
|
|
obj.append (null) ;
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
2019-07-11 12:49:31 +08:00
|
|
|
await connection.commit ();
|
2019-06-23 06:36:03 +08:00
|
|
|
|
2023-02-21 11:20:36 +08:00
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :ID`;
|
|
|
|
const result = await connection.execute (sql, { ID : 19708 });
|
2019-07-11 12:49:31 +08:00
|
|
|
obj = result.rows[0][1];
|
2023-02-21 11:20:36 +08:00
|
|
|
const arr = obj.getValues();
|
2023-05-03 18:32:09 +08:00
|
|
|
assert.deepStrictEqual(arr, [ null, null, null ]);
|
2023-02-21 11:20:36 +08:00
|
|
|
|
|
|
|
assert.strictEqual (obj.getElement(0), null);
|
2019-07-11 12:49:31 +08:00
|
|
|
}
|
2023-02-21 11:20:36 +08:00
|
|
|
); // 197.8
|
|
|
|
|
|
|
|
it('197.9 Insert into table and use deleteElement()', async () => {
|
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19709;
|
|
|
|
|
|
|
|
let obj = new objClass ([ "One", "Two", "Three", "four" ]);
|
|
|
|
obj.deleteElement (2); // delete the 3rd element "Three"
|
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
|
|
|
|
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :id`;
|
|
|
|
const result = await connection.execute (sql, { id : id });
|
|
|
|
obj = result.rows[0][1];
|
|
|
|
const arr = obj.getValues ();
|
2023-05-03 18:32:09 +08:00
|
|
|
assert.deepStrictEqual(arr, [ 'One', 'Two', 'four' ]);
|
2023-02-21 11:20:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('197.10 Insert into table and use setElement()', async () => {
|
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19710;
|
|
|
|
|
|
|
|
let obj = new objClass ([ "One", "Two", "Three", "Four" ]);
|
|
|
|
obj.setElement (2, "3");
|
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
|
|
|
|
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :id`;
|
|
|
|
const result = await connection.execute (sql, { id : id });
|
|
|
|
obj = result.rows[0][1];
|
|
|
|
const arr = obj.getValues ();
|
2023-05-03 18:32:09 +08:00
|
|
|
assert.deepStrictEqual(arr, [ 'One', 'Two', '3', 'Four' ]);
|
2023-02-21 11:20:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('197.11 Insert into table and use trim()', async () => {
|
|
|
|
let sql = `INSERT INTO NODB_TEST197_TAB VALUES ( :id, :v )`;
|
|
|
|
const objClass = await connection.getDbObjectClass ("NODB_TEST197_TYP");
|
|
|
|
const id = 19711;
|
|
|
|
|
|
|
|
let obj = new objClass ([ "One", "Two", "Three", "Four" ]);
|
|
|
|
obj.trim (2);
|
|
|
|
await connection.execute (sql, { id : id, v : { val : obj } });
|
|
|
|
await connection.commit ();
|
|
|
|
|
|
|
|
sql = `SELECT * FROM NODB_TEST197_TAB WHERE ID = :id`;
|
|
|
|
const result = await connection.execute (sql, { id : id });
|
|
|
|
obj = result.rows[0][1];
|
|
|
|
const arr = obj.getValues ();
|
2023-05-03 18:32:09 +08:00
|
|
|
assert.deepStrictEqual(arr, [ 'One', 'Two' ]);
|
2019-06-23 06:36:03 +08:00
|
|
|
});
|
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
});
|