node-oracledb/test/clobPlsqlString.js

207 lines
6.3 KiB
JavaScript
Raw Normal View History

2022-04-19 08:06:36 +08:00
/* Copyright (c) 2015, 2022, 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
var oracledb = require('oracledb');
var async = require('async');
var should = require('should');
2016-01-25 19:04:10 +08:00
var stream = require('stream');
var dbConfig = require('./dbconfig.js');
2015-11-15 05:46:31 +08:00
var assist = require('./dataTypeAssist.js');
describe('60. clobPlsqlString.js', function() {
var connection = null;
var tableName = "nodb_myclobs";
2016-03-24 14:09:53 +08:00
2015-11-15 05:46:31 +08:00
before('get one connection, prepare table', function(done) {
async.series([
function(callback) {
oracledb.getConnection(
2023-02-21 12:04:16 +08:00
dbConfig,
2015-11-15 05:46:31 +08:00
function(err, conn) {
2016-01-25 19:04:10 +08:00
should.not.exist(err);
connection = conn;
callback();
2015-11-15 05:46:31 +08:00
}
);
},
function(callback) {
assist.createTable(connection, tableName, callback);
}
], done);
2016-12-01 19:10:30 +08:00
}); // before
2015-11-15 05:46:31 +08:00
after('release connection', function(done) {
async.series([
function(callback) {
connection.execute(
"DROP TABLE nodb_myclobs purge",
2015-11-15 05:46:31 +08:00
function(err) {
should.not.exist(err);
callback();
}
);
},
function(callback) {
2021-04-01 12:48:37 +08:00
connection.release(function(err) {
2015-11-15 05:46:31 +08:00
should.not.exist(err);
callback();
});
}
], done);
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() {
before('insert data', function(done) {
connection.execute(
"INSERT INTO nodb_myclobs (num, content) VALUES (1, 'abcdefghijklmnopqrstuvwxyz')",
2016-01-25 19:04:10 +08:00
function(err) {
should.not.exist(err);
done();
}
);
2016-12-01 19:10:30 +08:00
}); // before
2016-03-24 14:09:53 +08:00
2016-01-25 19:04:10 +08:00
it('60.1.1 PL/SQL OUT CLOB parameters can also be bound as STRING', function(done) {
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}
},
function(err, result) {
should.not.exist(err);
(result.outBinds.cbv).should.be.a.String();
2016-01-25 19:04:10 +08:00
(result.outBinds.cbv).should.eql('abcdefghijklmnopqrstuvwxyz');
done();
}
);
2016-12-01 19:10:30 +08:00
}); // 60.1.1
2015-11-15 05:46:31 +08:00
2016-01-25 19:04:10 +08:00
it('60.1.2 The returned length is limited to the maximum size', function(done) {
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, maxSize: 5 }
},
function(err, result) {
should.exist(err);
(err.message).should.startWith('ORA-06502'); // PL/SQL: numeric or value error
2016-12-01 19:10:30 +08:00
should.not.exist(result);
2016-01-25 19:04:10 +08:00
done();
}
);
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() {
var dataLength = 1000000;
var rawData = assist.createCharString(dataLength);
it('60.2.1 maxSize option does not take effect when bind out type is clob', function(done) {
async.series([
function doInsert(callback) {
connection.execute(
"INSERT INTO " + tableName + " VALUES (2, EMPTY_CLOB()) RETURNING content INTO :lobbv",
{ lobbv: {type: oracledb.CLOB, dir: oracledb.BIND_OUT} },
2016-03-24 14:09:53 +08:00
{ autoCommit: false },
2016-01-25 19:04:10 +08:00
function(err, result) {
should.not.exist(err);
2016-03-24 14:09:53 +08:00
2016-01-25 19:04:10 +08:00
var lob = result.outBinds.lobbv[0];
lob.on('error', function(err) {
should.not.exist(err);
return callback(err);
});
2016-03-24 14:09:53 +08:00
2016-01-25 19:04:10 +08:00
var inStream = new stream.Readable();
inStream._read = function noop() {};
inStream.push(rawData);
inStream.push(null);
2016-03-24 14:09:53 +08:00
inStream.on('error', function(err) {
2016-01-25 19:04:10 +08:00
should.not.exist(err);
return callback(err);
});
inStream.on('end', function() {
connection.commit(function(err) {
should.not.exist(err);
callback();
});
});
inStream.pipe(lob);
}
);
},
function doQuery(callback) {
connection.execute(
"BEGIN SELECT content INTO :bv FROM " + tableName + " WHERE num = 2; END;",
{ bv: {dir: oracledb.BIND_OUT, type: oracledb.CLOB} },
{ maxRows: 500 },
function(err, result) {
should.not.exist(err);
var content = '';
var lob = result.outBinds.bv;
lob.setEncoding('utf8');
lob.on('data', function(chunk) {
content += chunk;
});
lob.on('end', function() {
2016-03-24 14:09:53 +08:00
(content.length).should.be.exactly(dataLength);
2016-01-25 19:04:10 +08:00
(content).should.eql(rawData);
callback();
});
lob.on('error', function(err) {
should.not.exist(err);
});
}
);
}
], done);
2016-12-01 19:10:30 +08:00
});
}); // 60.2
2016-03-24 14:09:53 +08:00
2016-12-01 19:10:30 +08:00
});