node-oracledb/test/autoCommit4nestedExecutes.js

162 lines
5.2 KiB
JavaScript
Raw Normal View History

2022-04-19 08:06:36 +08:00
/* Copyright (c) 2015, 2022, Oracle and/or its affiliates. */
2015-12-21 19:50:55 +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.
2015-12-21 19:50:55 +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.
2015-12-21 19:50:55 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2015-12-21 19:50:55 +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.
2015-12-21 19:50:55 +08:00
* See the License for the specific language governing permissions and
* limitations under the License.
2016-03-24 14:09:53 +08:00
*
2015-12-21 19:50:55 +08:00
* NAME
* 63. autoCommit4nestedExecutes.js
*
* DESCRIPTION
2016-03-24 14:09:53 +08:00
* Nested executes where the 2nd execute fails used to cause an unexpected
* commit, even though the autoCommit:false setting is enabled at the
* execute() and/or oracledb level. This is github issue 269. It has
2015-12-21 19:50:55 +08:00
* been fixed in 1.4.
2016-03-24 14:09:53 +08:00
*
2015-12-21 19:50:55 +08:00
* https://github.com/oracle/node-oracledb/issues/269
2016-03-24 14:09:53 +08:00
*
2015-12-21 19:50:55 +08:00
*****************************************************************************/
2016-03-24 14:09:53 +08:00
'use strict';
2015-12-21 19:50:55 +08:00
2022-09-08 14:27:17 +08:00
const oracledb = require('oracledb');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
2015-12-21 19:50:55 +08:00
describe('63. autoCommit4nestedExecutes.js', function() {
2016-03-24 14:09:53 +08:00
2023-02-21 12:04:16 +08:00
const tableName = "nodb_issue269tab";
const procName = "issue269proc";
let connection;
2015-12-21 19:50:55 +08:00
2022-09-08 14:27:17 +08:00
before('prepare table and procedure', async function() {
2015-12-21 19:50:55 +08:00
2023-02-21 12:04:16 +08:00
const sqlCreateTab =
2016-03-24 14:09:53 +08:00
" BEGIN "
+ " DECLARE "
+ " e_table_missing EXCEPTION; "
+ " PRAGMA EXCEPTION_INIT(e_table_missing, -00942); "
2016-03-24 14:09:53 +08:00
+ " BEGIN "
+ " EXECUTE IMMEDIATE ('DROP TABLE " + tableName + " PURGE'); "
2015-12-21 19:50:55 +08:00
+ " EXCEPTION "
+ " WHEN e_table_missing "
2015-12-21 19:50:55 +08:00
+ " THEN NULL; "
+ " END; "
+ " EXECUTE IMMEDIATE (' "
+ " CREATE TABLE " + tableName + " ( "
+ " myts timestamp, p_iname VARCHAR2(40), "
2016-03-24 14:09:53 +08:00
+ " p_short_name VARCHAR2(40), p_comments VARCHAR2(40) "
+ " ) "
+ " '); "
+ " END; ";
2015-12-21 19:50:55 +08:00
2023-02-21 12:04:16 +08:00
const sqlCreateProc =
2015-12-21 19:50:55 +08:00
" CREATE OR REPLACE PROCEDURE " + procName + "(p_iname IN VARCHAR2, "
2016-03-24 14:09:53 +08:00
+ " p_short_name IN VARCHAR2, p_comments IN VARCHAR2, p_new_id OUT NUMBER, p_status OUT NUMBER, "
2015-12-21 19:50:55 +08:00
+ " p_description OUT VARCHAR2) "
+ " AS "
+ " BEGIN "
2016-03-24 14:09:53 +08:00
+ " p_description := p_iname || ' ' || p_short_name || ' ' || p_comments; "
2015-12-21 19:50:55 +08:00
+ " p_new_id := 1; "
+ " p_status := 2; "
+ " insert into " + tableName + " values (systimestamp, p_iname, p_short_name, p_comments); "
+ " END; ";
2016-03-24 14:09:53 +08:00
2022-09-08 14:27:17 +08:00
connection = await oracledb.getConnection(dbConfig);
await connection.execute(sqlCreateTab);
await connection.execute(sqlCreateProc);
2016-12-01 19:10:30 +08:00
}); // before
2015-12-21 19:50:55 +08:00
2022-09-08 14:27:17 +08:00
after('drop table and procedure', async function() {
await connection.execute("DROP PROCEDURE " + procName);
await connection.execute("DROP TABLE " + tableName + " PURGE");
2023-02-21 11:50:51 +08:00
await connection.close();
2016-12-01 19:10:30 +08:00
}); // after
2015-12-21 19:50:55 +08:00
2022-09-08 14:27:17 +08:00
it('63.1 nested execute() functions', async function() {
2016-03-24 14:09:53 +08:00
// sql will be the same for both execute calls
2023-02-21 12:04:16 +08:00
const procSql = "BEGIN " + procName + "(p_iname=>:p_iname, p_short_name=>:p_short_name, "
2015-12-21 19:50:55 +08:00
+ " p_comments=>:p_comments, p_new_id=>:p_new_id, p_status=>:p_status, "
2016-03-24 14:09:53 +08:00
+ " p_description=>:p_description); END;";
2023-02-21 12:04:16 +08:00
const pool = await oracledb.createPool(dbConfig);
const conn = await pool.getConnection();
2022-09-08 14:27:17 +08:00
await conn.execute(
procSql,
{
p_iname: "Test iname",
p_short_name: "TST",
p_comments: "Test comments",
p_new_id: {
type: oracledb.NUMBER,
dir: oracledb.BIND_OUT
},
p_status: {
type: oracledb.NUMBER,
dir: oracledb.BIND_OUT
},
p_description: {
type: oracledb.STRING,
dir: oracledb.BIND_OUT
}
2015-12-21 19:50:55 +08:00
},
2022-09-08 14:27:17 +08:00
{ autoCommit: false });
2023-02-21 12:04:16 +08:00
const binds = {
p_iname123: "Test iname", // specify wrong bind parameter name to cause an error
p_short_name: "TST",
p_comments: "Test comments",
p_new_id: {
type: oracledb.NUMBER,
dir: oracledb.BIND_OUT
},
p_status: {
type: oracledb.NUMBER,
dir: oracledb.BIND_OUT
},
p_description: {
type: oracledb.STRING,
dir: oracledb.BIND_OUT
}
};
const options = {
autoCommit: false
};
await assert.rejects(
async () => await conn.execute(procSql, binds, options),
2023-05-23 22:20:06 +08:00
/ORA-01036:|NJS-097:/
2023-02-21 12:04:16 +08:00
);
2015-12-21 19:50:55 +08:00
2022-09-08 14:27:17 +08:00
await conn.release();
await pool.terminate();
2023-02-21 12:04:16 +08:00
const result = await connection.execute(
2022-09-08 14:27:17 +08:00
"SELECT count(*) as amount FROM " + tableName,
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT });
assert.strictEqual(result.rows[0].AMOUNT, 0);
2023-02-21 12:04:16 +08:00
2022-09-08 14:27:17 +08:00
});
2023-02-21 12:04:16 +08:00
2016-12-01 19:10:30 +08:00
});