2022-04-19 08:06:36 +08:00
|
|
|
/* Copyright (c) 2020, 2022, Oracle and/or its affiliates. */
|
2020-03-21 05:14:52 +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.
|
2020-03-21 05:14:52 +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.
|
2020-03-21 05:14:52 +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
|
2020-03-21 05:14:52 +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.
|
2020-03-21 05:14:52 +08:00
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* NAME
|
|
|
|
* 232. nestedCursor01.js
|
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* Nested Cursor.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const oracledb = require('oracledb');
|
2023-02-21 10:53:17 +08:00
|
|
|
const assert = require('assert');
|
2023-02-21 12:04:16 +08:00
|
|
|
const dbConfig = require('./dbconfig.js');
|
2020-03-21 05:14:52 +08:00
|
|
|
const testsUtil = require('./testsUtil.js');
|
|
|
|
|
|
|
|
describe('232. nestedCursor01.js', () => {
|
|
|
|
|
|
|
|
let conn;
|
|
|
|
const peopleTab = 'nodb_nc01_people';
|
|
|
|
const addrTab = 'nodb_nc01_addr';
|
|
|
|
|
|
|
|
before(async () => {
|
2023-02-21 12:04:16 +08:00
|
|
|
conn = await oracledb.getConnection(dbConfig);
|
2023-02-21 10:53:17 +08:00
|
|
|
let sql =
|
|
|
|
`create table ${peopleTab} (
|
|
|
|
id number,
|
|
|
|
name varchar2(20),
|
|
|
|
addresskey number
|
|
|
|
)`;
|
|
|
|
let plsql = testsUtil.sqlCreateTable(peopleTab, sql);
|
|
|
|
await conn.execute(plsql);
|
2020-03-21 05:14:52 +08:00
|
|
|
|
2023-02-21 10:53:17 +08:00
|
|
|
sql =
|
|
|
|
`create table ${addrTab} (
|
|
|
|
addresskey number,
|
|
|
|
country varchar2(20)
|
|
|
|
)`;
|
|
|
|
plsql = testsUtil.sqlCreateTable(addrTab, sql);
|
|
|
|
await conn.execute(plsql);
|
2020-03-21 05:14:52 +08:00
|
|
|
}); // before()
|
|
|
|
|
|
|
|
after(async () => {
|
2023-02-21 10:53:17 +08:00
|
|
|
let sql = `drop table ${peopleTab} purge`;
|
|
|
|
await conn.execute(sql);
|
2020-03-21 05:14:52 +08:00
|
|
|
|
2023-02-21 10:53:17 +08:00
|
|
|
sql = `drop table ${addrTab} purge`;
|
|
|
|
await conn.execute(sql);
|
2020-03-21 05:14:52 +08:00
|
|
|
|
2023-02-21 10:53:17 +08:00
|
|
|
await conn.close();
|
2020-03-21 05:14:52 +08:00
|
|
|
}); // after
|
|
|
|
|
|
|
|
it('232.1 example/selectnestedcursor.js', async () => {
|
|
|
|
|
2023-02-21 10:53:17 +08:00
|
|
|
const binds1 = [
|
|
|
|
[101, "Alice", 201 ],
|
|
|
|
[102, "Bruce", 203 ],
|
|
|
|
[103, "Christine", 203 ],
|
|
|
|
[104, "David", 201 ],
|
|
|
|
[105, "Erica", 203 ],
|
|
|
|
[106, "Frank", 202 ]
|
|
|
|
];
|
|
|
|
const opt1 = {
|
|
|
|
autoCommit: true,
|
|
|
|
bindDefs: [
|
|
|
|
{ type: oracledb.NUMBER },
|
|
|
|
{ type: oracledb.STRING, maxSize: 20 },
|
|
|
|
{ type: oracledb.NUMBER },
|
|
|
|
]
|
|
|
|
};
|
|
|
|
const sql1 = `INSERT INTO ${peopleTab} VALUES (:1, :2, :3)`;
|
|
|
|
const result1 = await conn.executeMany(sql1, binds1, opt1);
|
|
|
|
assert.strictEqual(result1.rowsAffected, binds1.length);
|
2020-03-21 05:14:52 +08:00
|
|
|
|
2023-02-21 10:53:17 +08:00
|
|
|
const binds2 = [
|
|
|
|
[201, "Austria" ],
|
|
|
|
[202, "Brazil" ],
|
|
|
|
[203, "Chile" ]
|
|
|
|
];
|
|
|
|
const opt2 = {
|
|
|
|
autoCommit: true,
|
|
|
|
bindDefs: [
|
|
|
|
{ type: oracledb.NUMBER },
|
|
|
|
{ type: oracledb.STRING, maxSize: 20 }
|
|
|
|
]
|
|
|
|
};
|
|
|
|
const sql2 = `INSERT INTO ${addrTab} VALUES (:1, :2)`;
|
|
|
|
const result2 = await conn.executeMany(sql2, binds2, opt2);
|
|
|
|
assert.strictEqual(result2.rowsAffected, binds2.length);
|
2020-03-21 05:14:52 +08:00
|
|
|
|
2023-02-21 10:53:17 +08:00
|
|
|
// For each country, show who lives there
|
|
|
|
const sql3 = `
|
|
|
|
SELECT country,
|
|
|
|
CURSOR(SELECT name
|
|
|
|
FROM ${peopleTab} p
|
|
|
|
WHERE p.addresskey = a.addresskey
|
|
|
|
ORDER by name
|
|
|
|
) as NC
|
|
|
|
FROM ${addrTab} a
|
|
|
|
ORDER BY country
|
|
|
|
`;
|
|
|
|
const binds3 = {};
|
|
|
|
const opt3 = {
|
|
|
|
outFormat: oracledb.OUT_FORMAT_OBJECT
|
|
|
|
};
|
|
|
|
const result3 = await conn.execute(sql3, binds3, opt3);
|
|
|
|
assert.strictEqual(result3.metaData[0].name, 'COUNTRY');
|
|
|
|
assert.strictEqual(result3.metaData[1].name, 'NC');
|
|
|
|
assert.strictEqual(result3.metaData[1].metaData[0].name, 'NAME');
|
|
|
|
assert.strictEqual(result3.rows[0].COUNTRY, 'Austria');
|
|
|
|
assert.strictEqual(result3.rows[0].NC[0].NAME, 'Alice');
|
|
|
|
assert.strictEqual(result3.rows[0].NC[1].NAME, 'David');
|
|
|
|
assert.strictEqual(result3.rows[1].COUNTRY, 'Brazil');
|
|
|
|
assert.strictEqual(result3.rows[1].NC[0].NAME, 'Frank');
|
|
|
|
assert.strictEqual(result3.rows[2].COUNTRY, 'Chile');
|
|
|
|
assert.strictEqual(result3.rows[2].NC[0].NAME, 'Bruce');
|
|
|
|
assert.strictEqual(result3.rows[2].NC[1].NAME, 'Christine');
|
|
|
|
assert.strictEqual(result3.rows[2].NC[2].NAME, 'Erica');
|
2020-03-21 05:14:52 +08:00
|
|
|
|
|
|
|
}); // 232.1
|
2021-04-01 12:48:37 +08:00
|
|
|
});
|