2022-04-19 08:06:36 +08:00
|
|
|
/* Copyright (c) 2019, 2022, Oracle and/or its affiliates. */
|
2019-07-02 20:30:04 +08:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* You may not use the identified files except in compliance with the Apache
|
|
|
|
* License, Version 2.0 (the "License.")
|
|
|
|
*
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* NAME
|
|
|
|
* 211. dbObject12.js
|
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* examples/plsqlrecord.js
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const oracledb = require('oracledb');
|
2021-10-11 10:33:01 +08:00
|
|
|
const assert = require('assert');
|
2019-07-02 20:30:04 +08:00
|
|
|
const dbconfig = require('./dbconfig.js');
|
2019-11-25 10:13:17 +08:00
|
|
|
const testsUtil = require('./testsUtil.js');
|
2019-07-02 20:30:04 +08:00
|
|
|
|
2019-11-25 10:13:17 +08:00
|
|
|
describe('211. dbObject12.js', function() {
|
2019-07-02 20:30:04 +08:00
|
|
|
|
2019-11-25 10:13:17 +08:00
|
|
|
let isRunnable = false;
|
2019-07-02 20:30:04 +08:00
|
|
|
let conn;
|
|
|
|
|
|
|
|
const PKG = 'NODB_REC_PKG';
|
|
|
|
const TYPE = 'NODB_REC_TYP';
|
|
|
|
|
2019-11-25 10:13:17 +08:00
|
|
|
before(async function() {
|
|
|
|
isRunnable = await testsUtil.checkPrerequisites();
|
2021-04-01 12:48:37 +08:00
|
|
|
if (!isRunnable) {
|
2019-11-25 10:13:17 +08:00
|
|
|
this.skip();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
conn = await oracledb.getConnection(dbconfig);
|
2019-07-02 20:30:04 +08:00
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
let plsql = `
|
2019-11-25 10:13:17 +08:00
|
|
|
CREATE OR REPLACE PACKAGE ${PKG} AS
|
|
|
|
TYPE ${TYPE} IS RECORD (name VARCHAR2(40), pos NUMBER);
|
|
|
|
PROCEDURE myproc (p_in IN ${TYPE}, p_out OUT ${TYPE});
|
|
|
|
END ${PKG};
|
|
|
|
`;
|
|
|
|
await conn.execute(plsql);
|
2019-07-02 20:30:04 +08:00
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
plsql = `
|
2019-11-25 10:13:17 +08:00
|
|
|
CREATE OR REPLACE PACKAGE BODY ${PKG} AS
|
|
|
|
PROCEDURE myproc (p_in IN ${TYPE}, p_out OUT ${TYPE}) AS
|
|
|
|
BEGIN
|
|
|
|
p_out := p_in;
|
|
|
|
p_out.pos := p_out.pos * 2;
|
|
|
|
END;
|
|
|
|
END ${PKG};
|
|
|
|
`;
|
|
|
|
await conn.execute(plsql);
|
2019-07-02 20:30:04 +08:00
|
|
|
|
2021-04-01 12:48:37 +08:00
|
|
|
} catch (err) {
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.fail(err);
|
2019-11-25 10:13:17 +08:00
|
|
|
}
|
2019-07-02 20:30:04 +08:00
|
|
|
}
|
2019-11-25 10:13:17 +08:00
|
|
|
|
2019-07-02 20:30:04 +08:00
|
|
|
}); // before()
|
|
|
|
|
2019-11-25 10:13:17 +08:00
|
|
|
after(async function() {
|
2021-04-01 12:48:37 +08:00
|
|
|
if (!isRunnable) {
|
2019-11-25 10:13:17 +08:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
let sql = `DROP PACKAGE ${PKG}`;
|
|
|
|
await conn.execute(sql);
|
2019-07-02 20:30:04 +08:00
|
|
|
|
2019-11-25 10:13:17 +08:00
|
|
|
await conn.close();
|
2021-04-01 12:48:37 +08:00
|
|
|
} catch (err) {
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.fail(err);
|
2019-11-25 10:13:17 +08:00
|
|
|
}
|
2019-07-02 20:30:04 +08:00
|
|
|
}
|
2019-11-25 10:13:17 +08:00
|
|
|
|
2019-07-02 20:30:04 +08:00
|
|
|
}); // after()
|
|
|
|
|
|
|
|
it('211.1 examples/plsqlrecord.js', async () => {
|
|
|
|
try {
|
|
|
|
const CALL = `CALL ${PKG}.myproc(:inbv, :outbv)`;
|
|
|
|
const RecTypeClass = await conn.getDbObjectClass(`${PKG}.${TYPE}`);
|
|
|
|
|
|
|
|
// Using the constructor to create an object
|
2021-04-01 12:48:37 +08:00
|
|
|
const obj1 = new RecTypeClass({ NAME: 'Ship', POS: 12 });
|
2019-07-02 20:30:04 +08:00
|
|
|
let binds = {
|
|
|
|
inbv: obj1,
|
|
|
|
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }
|
|
|
|
};
|
|
|
|
const result1 = await conn.execute(CALL, binds);
|
|
|
|
let out = result1.outBinds.outbv;
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.strictEqual(out.NAME, obj1.NAME);
|
|
|
|
assert.strictEqual(out.POS, (obj1.POS * 2));
|
2019-07-02 20:30:04 +08:00
|
|
|
|
|
|
|
// Binding the record values directly'
|
|
|
|
const obj2 = { NAME: 'Plane', POS: 34 };
|
|
|
|
binds = {
|
|
|
|
inbv: { type: RecTypeClass, val: obj2 },
|
|
|
|
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }
|
|
|
|
};
|
|
|
|
const result2 = await conn.execute(CALL, binds);
|
|
|
|
out = result2.outBinds.outbv;
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.strictEqual(out.NAME, obj2.NAME);
|
|
|
|
assert.strictEqual(out.POS, (obj2.POS * 2));
|
2019-07-02 20:30:04 +08:00
|
|
|
|
|
|
|
// Using the type name
|
|
|
|
const obj3 = { NAME: 'Car', POS: 56 };
|
|
|
|
binds = {
|
|
|
|
inbv: { type: `${PKG}.${TYPE}`, val: obj3 },
|
|
|
|
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }
|
|
|
|
};
|
|
|
|
const result3 = await conn.execute(CALL, binds);
|
|
|
|
out = result3.outBinds.outbv;
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.strictEqual(out.NAME, obj3.NAME);
|
|
|
|
assert.strictEqual(out.POS, (obj3.POS * 2));
|
2019-07-02 20:30:04 +08:00
|
|
|
|
|
|
|
// Batch exeuction with executeMany()
|
|
|
|
const obj4 = [
|
|
|
|
{ NAME: 'Train', POS: 78 },
|
|
|
|
{ NAME: 'Bike', POS: 83 }
|
|
|
|
];
|
|
|
|
binds = [
|
|
|
|
{ inbv: obj4[0] },
|
|
|
|
{ inbv: obj4[1] }
|
|
|
|
];
|
2021-10-11 10:33:01 +08:00
|
|
|
const opts = {
|
2019-07-02 20:30:04 +08:00
|
|
|
bindDefs: {
|
|
|
|
inbv: { type: RecTypeClass },
|
|
|
|
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const result4 = await conn.executeMany(CALL, binds, opts);
|
|
|
|
for (let i = 0, out = result4.outBinds; i < binds.length; i++) {
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.strictEqual(out[i].outbv.NAME, obj4[i].NAME);
|
|
|
|
assert.strictEqual(out[i].outbv.POS, (obj4[i].POS * 2));
|
2019-07-02 20:30:04 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-10-11 10:33:01 +08:00
|
|
|
assert.fail(err);
|
2019-07-02 20:30:04 +08:00
|
|
|
}
|
|
|
|
}); // 211.1
|
2021-04-01 12:48:37 +08:00
|
|
|
});
|