node-oracledb/test/clobPlsqlString.js

132 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-02-21 14:08:24 +08:00
/* Copyright (c) 2015, 2023, Oracle and/or its affiliates. */
2015-11-15 05:46:31 +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-11-15 05:46:31 +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-11-15 05:46:31 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2015-11-15 05:46:31 +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-11-15 05:46:31 +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-11-15 05:46:31 +08:00
* NAME
* 60. clobPlsqlString.js
*
* DESCRIPTION
*
* PL/SQL OUT CLOB parameters can also be bound as `STRING`
* The returned length is limited to the maximum size of maxSize option.
2016-03-24 14:09:53 +08:00
*
* When the types of bind out variables are not STRING or BUFFER,
2016-01-25 19:04:10 +08:00
* maxSize option will not take effect.
2016-03-24 14:09:53 +08:00
*
2015-11-15 05:46:31 +08:00
*****************************************************************************/
'use strict';
2015-11-15 05:46:31 +08:00
2023-02-21 14:08:24 +08:00
const oracledb = require('oracledb');
2023-03-14 14:33:48 +08:00
const assert = require('assert');
2023-02-21 14:08:24 +08:00
const stream = require('stream');
const dbConfig = require('./dbconfig.js');
const assist = require('./dataTypeAssist.js');
2015-11-15 05:46:31 +08:00
describe('60. clobPlsqlString.js', function() {
2023-02-21 14:08:24 +08:00
let connection = null;
2023-03-14 14:33:48 +08:00
const tableName = `nodb_myclobs`;
before('get one connection, prepare table', async function() {
connection = await oracledb.getConnection(dbConfig);
await connection.execute(assist.sqlCreateTable(tableName));
2016-12-01 19:10:30 +08:00
}); // before
2015-11-15 05:46:31 +08:00
2023-03-14 14:33:48 +08:00
after('release connection', async function() {
await connection.execute(`DROP TABLE nodb_myclobs purge`);
await connection.close();
2016-12-01 19:10:30 +08:00
}); // after
2015-11-15 05:46:31 +08:00
2016-01-25 19:04:10 +08:00
describe('60.1 BIND OUT as STRING', function() {
2023-03-14 14:33:48 +08:00
before('insert data', async function() {
await connection.execute(`INSERT INTO nodb_myclobs (num, content) VALUES (1, 'abcdefghijklmnopqrstuvwxyz')`);
2016-12-01 19:10:30 +08:00
}); // before
2016-03-24 14:09:53 +08:00
2023-03-14 14:33:48 +08:00
it('60.1.1 PL/SQL OUT CLOB parameters can also be bound as STRING', async function() {
let result = await connection.execute(`BEGIN SELECT content INTO :cbv FROM nodb_myclobs WHERE num = :id; END;`,
2016-01-25 19:04:10 +08:00
{
id: 1,
cbv: { type: oracledb.STRING, dir: oracledb.BIND_OUT}
2023-03-14 14:33:48 +08:00
});
assert.strictEqual(typeof result.outBinds.cbv, "string");
assert.strictEqual(result.outBinds.cbv, 'abcdefghijklmnopqrstuvwxyz');
2016-12-01 19:10:30 +08:00
}); // 60.1.1
2015-11-15 05:46:31 +08:00
2023-03-14 14:33:48 +08:00
it('60.1.2 The returned length is limited to the maximum size', async function() {
await assert.rejects(
async () => {
await connection.execute(`BEGIN SELECT content INTO :cbv FROM nodb_myclobs WHERE num = :id; END;`,
{
id: 1,
cbv: { type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 5 }});
2016-01-25 19:04:10 +08:00
},
2023-03-14 14:33:48 +08:00
/ORA-06502:/ // PL/SQL: numeric or value error
2016-01-25 19:04:10 +08:00
);
2016-12-01 19:10:30 +08:00
}); // 60.1.2
}); // 60.1
2016-01-25 19:04:10 +08:00
describe('60.2 BIND OUT as CLOB', function() {
2023-03-14 14:33:48 +08:00
const dataLength = 1000000;
const rawData = assist.createCharString(dataLength);
it('60.2.1 maxSize option does not take effect when bind out type is clob', async function() {
let result = await connection.execute(
`INSERT INTO ` + tableName + ` VALUES (2, EMPTY_CLOB()) RETURNING content INTO :lobbv`,
{ lobbv: {type: oracledb.CLOB, dir: oracledb.BIND_OUT} },
{ autoCommit: false });
const lob = result.outBinds.lobbv[0];
await new Promise((resolve, reject) => {
lob.on("error", reject);
let inStream = new stream.Readable();
inStream._read = function noop() {};
inStream.push(rawData);
inStream.push(null);
inStream.on('error', reject);
inStream.on('end', resolve);
inStream.pipe(lob);
});
await connection.commit();
result = await connection.execute(
"BEGIN SELECT content INTO :bv FROM " + tableName + " WHERE num = 2; END;",
{ bv: {dir: oracledb.BIND_OUT, type: oracledb.CLOB} },
{ maxRows: 500 }
);
2016-03-24 14:09:53 +08:00
2023-03-14 14:33:48 +08:00
const content = await result.outBinds.bv.getData();
assert.strictEqual(content.length, dataLength);
assert.strictEqual(content, rawData);
}); // 60.2
});
2016-12-01 19:10:30 +08:00
});