node-oracledb/test/longDMLBind.js

262 lines
7.9 KiB
JavaScript
Raw Normal View History

2018-02-09 16:51:13 +08:00
/* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. */
2017-06-14 09:25:16 +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.
*
* The node-oracledb test suite uses 'mocha', 'should' and 'async'.
* See LICENSE.md for relevant licenses.
*
* NAME
* 125. longDMLBind.js
*
* DESCRIPTION
* Test LONG type DML support.
* Long column restrictions: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements001.htm#SQLRF00201
*
*****************************************************************************/
'use strict';
var oracledb = require('oracledb');
var should = require('should');
var async = require('async');
var dbConfig = require('./dbconfig.js');
var random = require('./random.js');
var sql = require('./sql.js');
describe('125. longDMLBind.js', function() {
2017-06-14 09:25:16 +08:00
var connection = null;
var tableName = "nodb_long";
var insertID = 0;
var table_create = "BEGIN \n" +
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
" BEGIN \n" +
" EXECUTE IMMEDIATE('DROP TABLE " + tableName + " PURGE'); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
" CREATE TABLE " + tableName + " ( \n" +
" id NUMBER, \n" +
" content LONG \n" +
" ) \n" +
" '); \n" +
"END; ";
var table_drop = "DROP TABLE " + tableName + " PURGE";
before(function(done) {
async.series([
function(cb) {
oracledb.getConnection(dbConfig, function(err, conn) {
should.not.exist(err);
connection = conn;
cb();
});
},
function(cb) {
sql.executeSql(connection, table_create, {}, {}, cb);
}
], done);
}); // before
after(function(done) {
async.series([
function(cb) {
sql.executeSql(connection, table_drop, {}, {}, cb);
},
function(cb) {
connection.release(function(err) {
should.not.exist(err);
cb();
});
}
], done);
}); // after
beforeEach(function(done) {
insertID++;
done();
});
describe('125.1 INSERT and SELECT', function() {
it('125.1.1 works with data size 64K - 1', function(done) {
var insertedStr = random.getRandomLengthString(65535);
2017-06-17 07:40:09 +08:00
test1(insertedStr, 65535, done);
2017-06-14 09:25:16 +08:00
});
it('125.1.2 works with data size 64K', function(done) {
var insertedStr = random.getRandomLengthString(65536);
2017-06-17 07:40:09 +08:00
test1(insertedStr, 65536, done);
2017-06-14 09:25:16 +08:00
});
it('125.1.3 works with data size 64K + 1', function(done) {
var insertedStr = random.getRandomLengthString(65537);
2017-06-17 07:40:09 +08:00
test1(insertedStr, 65537, done);
2017-06-14 09:25:16 +08:00
});
it('125.1.4 works with data size 1MB + 1', function(done) {
var size = 1 * 1024 * 1024 + 1;
var insertedStr = random.getRandomLengthString(size);
2017-06-17 07:40:09 +08:00
test1(insertedStr, size, done);
2017-06-14 09:25:16 +08:00
});
}); // 125.1
describe('125.2 UPDATE', function() {
it('125.2.1 works with data size 64K - 1', function(done) {
var insertedStr = random.getRandomLengthString(100);
var updateStr = random.getRandomLengthString(65535);
2017-06-17 07:40:09 +08:00
test2(insertedStr, updateStr, 65535, done);
2017-06-14 09:25:16 +08:00
});
it('125.2.2 works with data size 64K', function(done) {
var insertedStr = random.getRandomLengthString(200);
var updateStr = random.getRandomLengthString(65536);
2017-06-17 07:40:09 +08:00
test2(insertedStr, updateStr, 65536, done);
2017-06-14 09:25:16 +08:00
});
it('125.2.3 works with data size 64K + 1', function(done) {
var insertedStr = random.getRandomLengthString(10);
var updateStr = random.getRandomLengthString(65537);
2017-06-17 07:40:09 +08:00
test2(insertedStr, updateStr, 65537, done);
2017-06-14 09:25:16 +08:00
});
it('125.2.4 works with data size 1MB + 1', function(done) {
var size = 1 * 1024 * 1024 + 1;
var insertedStr = random.getRandomLengthString(65536);
var updateStr = random.getRandomLengthString(size);
2017-06-17 07:40:09 +08:00
test2(insertedStr, updateStr, size, done);
2017-06-14 09:25:16 +08:00
});
}); // 125.3
describe('125.3 RETURNING INTO', function() {
it('125.3.1 do not support in returning into', function(done) {
var insertedStr = random.getRandomLengthString(100);
var updateStr = random.getRandomLengthString(65535);
test3(insertedStr, updateStr, done);
});
}); // 125.3
2017-06-17 07:40:09 +08:00
var test1 = function(content, maxsize, callback) {
2017-06-14 09:25:16 +08:00
async.series([
function(cb) {
2017-06-17 07:40:09 +08:00
insert(content, maxsize, cb);
2017-06-14 09:25:16 +08:00
},
function(cb) {
fetch(content, cb);
}
], callback);
};
2017-06-17 07:40:09 +08:00
var test2 = function(insertedStr, updateStr, maxsize, callback) {
2017-06-14 09:25:16 +08:00
async.series([
function(cb) {
2017-06-17 07:40:09 +08:00
insert(insertedStr, insertedStr.length, cb);
2017-06-14 09:25:16 +08:00
},
function(cb) {
2017-06-17 07:40:09 +08:00
update(updateStr, maxsize, cb);
2017-06-14 09:25:16 +08:00
},
function(cb) {
fetch(updateStr, cb);
}
], callback);
};
var test3 = function(insertedStr, updateStr, callback) {
async.series([
function(cb) {
2017-06-17 07:40:09 +08:00
insert(insertedStr, insertedStr.length, cb);
2017-06-14 09:25:16 +08:00
},
function(cb) {
returning(updateStr, cb);
}
], callback);
};
2017-06-17 07:40:09 +08:00
var insert = function(content, maxsize, callback) {
2017-06-14 09:25:16 +08:00
var sql = "insert into " + tableName + " (id, content) values (:i, :c)";
var bindVar = {
i: { val: insertID, dir: oracledb.BIND_IN, type: oracledb.NUMBER },
2017-06-17 07:40:09 +08:00
c: { val: content, dir: oracledb.BIND_IN, type: oracledb.STRING, maxSize: maxsize }
2017-06-14 09:25:16 +08:00
};
connection.execute(
sql,
bindVar,
function(err, result) {
should.not.exist(err);
(result.rowsAffected).should.be.exactly(1);
callback();
}
);
};
2017-06-17 07:40:09 +08:00
var update = function(content, maxsize, callback) {
2017-06-14 09:25:16 +08:00
var sql = "update " + tableName + " set content = :c where id = :i";
var bindVar = {
i: { val: insertID, dir: oracledb.BIND_IN, type: oracledb.NUMBER },
2017-06-17 07:40:09 +08:00
c: { val: content, dir: oracledb.BIND_IN, type: oracledb.STRING, maxSize: maxsize }
2017-06-14 09:25:16 +08:00
};
connection.execute(
sql,
bindVar,
function(err, result) {
should.not.exist(err);
(result.rowsAffected).should.be.exactly(1);
callback();
}
);
};
var returning = function(content, callback) {
var sql = "update " + tableName + " set content = :c1 where id = :i returning content into :c2";
var bindVar = {
i: { val: insertID, dir: oracledb.BIND_IN, type: oracledb.NUMBER },
c1: { val: content, dir: oracledb.BIND_IN, type: oracledb.STRING },
c2: { dir: oracledb.BIND_OUT, type: oracledb.STRING }
};
connection.execute(
sql,
bindVar,
function(err) {
should.exist(err);
2017-06-17 07:40:09 +08:00
// ORA-22816: unsupported feature with RETURNING clause
(err.message).should.startWith("ORA-22816:");
2017-06-14 09:25:16 +08:00
callback();
}
);
};
var fetch = function(expected, callback) {
var sql = "select content from " + tableName + " where id = " + insertID;
connection.execute(
sql,
function(err, result) {
should.not.exist(err);
should.strictEqual(result.rows[0][0], expected);
callback();
}
);
};
});