2019-03-13 08:02:49 +08:00
|
|
|
// Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-13 08:02:49 +08:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const QueryStream = require('./querystream.js');
|
|
|
|
const nodbUtil = require('./util.js');
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2018-02-06 10:14:19 +08:00
|
|
|
// fetchRowsToReturn is used to materialize the rows for an execute call using
|
|
|
|
// the resultSet returned from the C layer.
|
2019-05-07 12:43:22 +08:00
|
|
|
function fetchRowsToReturn(oracledb, executeOpts, resultSet, cb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
let rowsFetched = [];
|
|
|
|
let fetchArraySize;
|
|
|
|
let maxRows;
|
2018-02-06 10:14:19 +08:00
|
|
|
|
|
|
|
fetchArraySize = executeOpts.fetchArraySize;
|
|
|
|
if (fetchArraySize === undefined) {
|
|
|
|
fetchArraySize = oracledb.fetchArraySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxRows = executeOpts.maxRows;
|
|
|
|
if (maxRows === undefined) {
|
|
|
|
maxRows = oracledb.maxRows;
|
|
|
|
}
|
|
|
|
|
2019-03-18 11:01:52 +08:00
|
|
|
const fetchRowsCb = function(err, rows) {
|
2018-02-06 10:14:19 +08:00
|
|
|
|
|
|
|
if (err) {
|
2019-05-07 12:43:22 +08:00
|
|
|
cb(err);
|
2018-02-06 10:14:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rows) {
|
|
|
|
rowsFetched = rowsFetched.concat(rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowsFetched.length == maxRows || rows.length < fetchArraySize) {
|
2019-05-07 12:43:22 +08:00
|
|
|
cb(null, rowsFetched);
|
2018-02-06 10:14:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-07 12:43:22 +08:00
|
|
|
resultSet.getRows(fetchArraySize, fetchRowsCb);
|
2018-02-06 10:14:19 +08:00
|
|
|
};
|
|
|
|
|
2019-05-07 12:43:22 +08:00
|
|
|
resultSet.getRows(fetchArraySize, fetchRowsCb);
|
2018-02-06 10:14:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
// This execute function is used to override the execute method of the Connection
|
|
|
|
// class, which is defined in the C layer. The override allows us to do things
|
|
|
|
// like extend out the resultSet instance prior to passing it to the caller.
|
2018-02-06 10:14:19 +08:00
|
|
|
function execute(sql, a2, a3, a4) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
|
|
|
let binds = [];
|
|
|
|
let executeOpts = {};
|
|
|
|
let executeCb;
|
|
|
|
let custExecuteCb;
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length > 1 && arguments.length < 5, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
|
2016-05-16 07:53:23 +08:00
|
|
|
|
|
|
|
switch (arguments.length) {
|
|
|
|
case 2:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
|
2018-02-06 10:14:19 +08:00
|
|
|
executeCb = a2;
|
2016-05-16 07:53:23 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-005', 2);
|
|
|
|
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
|
2018-02-06 10:14:19 +08:00
|
|
|
binds = a2;
|
|
|
|
executeCb = a3;
|
2016-05-16 07:53:23 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-005', 2);
|
|
|
|
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-005', 3);
|
|
|
|
nodbUtil.assert(typeof a4 === 'function', 'NJS-005', 4);
|
2018-02-06 10:14:19 +08:00
|
|
|
binds = a2;
|
|
|
|
executeOpts = a3;
|
|
|
|
executeCb = a4;
|
2016-05-16 07:53:23 +08:00
|
|
|
break;
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
custExecuteCb = function(err, result) {
|
2019-03-18 11:01:52 +08:00
|
|
|
let outBindsKeys;
|
|
|
|
let outBindsIdx;
|
2016-05-16 07:53:23 +08:00
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
if (err) {
|
|
|
|
executeCb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
// Need to extend resultsets which may come from either the query results
|
2019-05-07 12:43:22 +08:00
|
|
|
// or outBinds or implicit results
|
2016-02-29 10:48:48 +08:00
|
|
|
if (result.resultSet) {
|
2019-03-13 08:02:49 +08:00
|
|
|
result.resultSet._setup(executeOpts);
|
2018-02-06 10:14:19 +08:00
|
|
|
if (executeOpts.resultSet) {
|
|
|
|
executeCb(null, result);
|
|
|
|
} else {
|
2019-05-07 12:43:22 +08:00
|
|
|
fetchRowsToReturn(self._oracledb, executeOpts, result.resultSet,
|
|
|
|
function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
executeCb(err);
|
|
|
|
} else {
|
|
|
|
result.rows = rows;
|
|
|
|
delete result.resultSet;
|
|
|
|
executeCb(null, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2018-02-06 10:14:19 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (result.outBinds) {
|
|
|
|
outBindsKeys = Object.keys(result.outBinds);
|
2016-05-16 07:53:23 +08:00
|
|
|
|
2018-02-06 10:14:19 +08:00
|
|
|
for (outBindsIdx = 0; outBindsIdx < outBindsKeys.length; outBindsIdx += 1) {
|
|
|
|
if (result.outBinds[outBindsKeys[outBindsIdx]] instanceof self._oracledb.ResultSet) {
|
2019-03-13 08:02:49 +08:00
|
|
|
result.outBinds[outBindsKeys[outBindsIdx]]._setup(executeOpts);
|
2018-02-06 10:14:19 +08:00
|
|
|
}
|
2016-05-16 07:53:23 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 12:43:22 +08:00
|
|
|
if (result.implicitResults && !executeOpts.resultSet) {
|
|
|
|
function processImplicitResult(ix) {
|
|
|
|
const resultSet = result.implicitResults[ix];
|
|
|
|
if (!resultSet) {
|
|
|
|
executeCb(null, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetchRowsToReturn(self._oracledb, executeOpts, resultSet,
|
|
|
|
function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
executeCb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result.implicitResults[ix] = rows;
|
|
|
|
processImplicitResult(ix + 1);
|
|
|
|
}
|
|
|
|
);
|
2019-06-03 10:13:47 +08:00
|
|
|
}
|
2019-05-07 12:43:22 +08:00
|
|
|
processImplicitResult(0);
|
|
|
|
} else {
|
|
|
|
if (result.implicitResults) {
|
|
|
|
for (let i = 0; i < result.implicitResults.length; i++) {
|
|
|
|
result.implicitResults[i]._setup(executeOpts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
executeCb(null, result);
|
|
|
|
}
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-02-06 10:14:19 +08:00
|
|
|
self._execute.call(self, sql, binds, executeOpts, custExecuteCb);
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
2018-03-27 14:05:27 +08:00
|
|
|
// This executeMany function is used to override the executeMany method of
|
|
|
|
// the Connection class, which is defined in the C layer.
|
2018-06-27 14:38:48 +08:00
|
|
|
function executeMany(sql, bindsOrNumIters, a3, a4) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
|
|
|
let options = {};
|
|
|
|
let executeCb;
|
2018-03-27 14:05:27 +08:00
|
|
|
|
|
|
|
nodbUtil.assert(arguments.length > 2 && arguments.length < 5, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
|
2018-08-22 11:26:42 +08:00
|
|
|
if (typeof bindsOrNumIters === 'number') {
|
|
|
|
nodbUtil.assert(Number.isInteger(bindsOrNumIters) && bindsOrNumIters > 0,
|
2018-09-04 06:07:07 +08:00
|
|
|
'NJS-005', 2);
|
2018-08-22 11:26:42 +08:00
|
|
|
} else {
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(Array.isArray(bindsOrNumIters), 'NJS-005', 2);
|
2018-08-22 11:26:42 +08:00
|
|
|
}
|
2018-03-27 14:05:27 +08:00
|
|
|
|
|
|
|
switch (arguments.length) {
|
|
|
|
case 3:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
|
2018-03-27 14:05:27 +08:00
|
|
|
executeCb = a3;
|
|
|
|
break;
|
|
|
|
case 4:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-005', 3);
|
|
|
|
nodbUtil.assert(typeof a4 === 'function', 'NJS-005', 4);
|
2018-03-27 14:05:27 +08:00
|
|
|
options = a3;
|
|
|
|
executeCb = a4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:38:48 +08:00
|
|
|
self._executeMany.call(self, sql, bindsOrNumIters, options, executeCb);
|
2018-03-27 14:05:27 +08:00
|
|
|
}
|
|
|
|
|
2018-03-27 14:02:47 +08:00
|
|
|
// This getStatementInfo function is just a place holder to allow for easier extension later.
|
|
|
|
function getStatementInfo(sql, getStatementInfoCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2018-03-27 14:02:47 +08:00
|
|
|
|
|
|
|
nodbUtil.assert(arguments.length === 2, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof getStatementInfoCb === 'function', 'NJS-005', 1);
|
2018-03-27 14:02:47 +08:00
|
|
|
|
|
|
|
self._getStatementInfo.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
// This commit function is just a place holder to allow for easier extension later.
|
2016-05-16 07:53:23 +08:00
|
|
|
function commit(commitCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof commitCb === 'function', 'NJS-005', 1);
|
2016-05-16 07:53:23 +08:00
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
self._commit.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2017-06-14 09:58:58 +08:00
|
|
|
// This createLob function is just a place holder to allow for easier extension later.
|
|
|
|
function createLob(type, createLobCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2017-06-14 09:58:58 +08:00
|
|
|
|
|
|
|
nodbUtil.assert(arguments.length === 2, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof createLobCb === 'function', 'NJS-005', 2);
|
2017-06-14 09:58:58 +08:00
|
|
|
|
|
|
|
self._createLob.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
// This rollback function is just a place holder to allow for easier extension later.
|
2016-05-16 07:53:23 +08:00
|
|
|
function rollback(rollbackCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof rollbackCb === 'function', 'NJS-005', 1);
|
2016-05-16 07:53:23 +08:00
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
self._rollback.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:41:12 +08:00
|
|
|
// This close function is used to override the close method of the Connection
|
2017-06-14 09:58:58 +08:00
|
|
|
// class, which is defined in the C layer.
|
2019-01-04 05:55:45 +08:00
|
|
|
function close(a1, a2) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
|
|
|
let options = {};
|
|
|
|
let closeCb;
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2019-01-04 05:55:45 +08:00
|
|
|
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
|
|
|
|
|
|
|
|
switch (arguments.length) {
|
|
|
|
case 1:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof a1 === 'function', 'NJS-005', 1);
|
2019-01-04 05:55:45 +08:00
|
|
|
closeCb = a1;
|
|
|
|
break;
|
|
|
|
case 2:
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(nodbUtil.isObject(a1), 'NJS-005', 1);
|
|
|
|
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
|
2019-01-04 05:55:45 +08:00
|
|
|
options = a1;
|
|
|
|
closeCb = a2;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2019-01-04 05:55:45 +08:00
|
|
|
self._close(options, function(err) {
|
2017-06-14 09:58:58 +08:00
|
|
|
if (!err) {
|
|
|
|
self.emit('_after_close');
|
2016-05-16 07:53:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-27 13:41:12 +08:00
|
|
|
closeCb(err);
|
2016-05-16 07:53:23 +08:00
|
|
|
});
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
2018-03-27 13:41:12 +08:00
|
|
|
// This break function is just a place holder to allow for easier extension later.
|
2016-02-29 10:48:48 +08:00
|
|
|
// It's attached to the module as break is a reserved word.
|
2016-05-16 07:53:23 +08:00
|
|
|
module.break = function(breakCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof breakCb === 'function', 'NJS-005', 1);
|
2016-05-16 07:53:23 +08:00
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
self._break.apply(self, arguments);
|
|
|
|
};
|
|
|
|
|
2018-03-27 13:17:16 +08:00
|
|
|
// This changePassword function is just a place holder to allow for easier extension later.
|
|
|
|
function changePassword(user, password, newPassword, changePasswordCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2018-03-27 13:17:16 +08:00
|
|
|
|
|
|
|
nodbUtil.assert(arguments.length === 4, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof user === 'string', 'NJS-005', 1);
|
|
|
|
nodbUtil.assert(typeof password === 'string', 'NJS-005', 2);
|
|
|
|
nodbUtil.assert(typeof newPassword === 'string', 'NJS-005', 3);
|
|
|
|
nodbUtil.assert(typeof changePasswordCb === 'function', 'NJS-005', 4);
|
2018-03-27 13:17:16 +08:00
|
|
|
|
|
|
|
self._changePassword.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:46:22 +08:00
|
|
|
// This ping function is just a place holder to allow for easier extension later.
|
|
|
|
function ping(pingCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2018-03-27 13:46:22 +08:00
|
|
|
|
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof pingCb === 'function', 'NJS-005', 1);
|
2018-03-27 13:46:22 +08:00
|
|
|
|
|
|
|
self._ping.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2018-05-30 09:18:42 +08:00
|
|
|
// create a subscription which can be used to get notifications of database
|
|
|
|
// changes
|
2018-06-05 16:26:30 +08:00
|
|
|
function subscribe(name, options, subscribeCb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2018-05-30 09:18:42 +08:00
|
|
|
|
2018-06-05 16:26:30 +08:00
|
|
|
nodbUtil.assert(arguments.length == 3, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
|
|
|
|
nodbUtil.assert(nodbUtil.isObject(options), 'NJS-005', 2);
|
|
|
|
nodbUtil.assert(typeof subscribeCb === 'function', 'NJS-005', 3);
|
2018-05-30 09:18:42 +08:00
|
|
|
self._subscribe.call(self, name, options, subscribeCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroy a subscription which was earlier created using subscribe()
|
|
|
|
function unsubscribe(name, cb) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
2018-05-30 09:18:42 +08:00
|
|
|
|
|
|
|
nodbUtil.assert(arguments.length == 2, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
|
|
|
|
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
|
2018-05-30 09:18:42 +08:00
|
|
|
|
|
|
|
self._unsubscribe.call(self, name, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-13 08:02:49 +08:00
|
|
|
// define class
|
|
|
|
class Connection extends EventEmitter {
|
|
|
|
|
|
|
|
// extend class with promisified functions
|
|
|
|
_extend(oracledb) {
|
|
|
|
this._oracledb = oracledb;
|
|
|
|
this.break = nodbUtil.promisify(oracledb, module.break);
|
|
|
|
this.changePassword = nodbUtil.promisify(oracledb, changePassword);
|
|
|
|
this.close = nodbUtil.promisify(oracledb, close);
|
|
|
|
this.commit = nodbUtil.promisify(oracledb, commit);
|
|
|
|
this.createLob = nodbUtil.promisify(oracledb, createLob);
|
|
|
|
this.execute = nodbUtil.promisify(oracledb, execute);
|
|
|
|
this.executeMany = nodbUtil.promisify(oracledb, executeMany);
|
|
|
|
this.getStatementInfo = nodbUtil.promisify(oracledb, getStatementInfo);
|
|
|
|
this.ping = nodbUtil.promisify(oracledb, ping);
|
|
|
|
this.release = nodbUtil.promisify(oracledb, close);
|
|
|
|
this.rollback = nodbUtil.promisify(oracledb, rollback);
|
|
|
|
this.subscribe = nodbUtil.promisify(oracledb, subscribe);
|
|
|
|
this.unsubscribe = nodbUtil.promisify(oracledb, unsubscribe);
|
|
|
|
}
|
2018-05-30 09:18:42 +08:00
|
|
|
|
2019-03-13 08:02:49 +08:00
|
|
|
// To obtain a SodaDatabase object (high-level SODA object associated with
|
|
|
|
// current connection)
|
|
|
|
getSodaDatabase() {
|
|
|
|
nodbUtil.assert(arguments.length === 0, 'NJS-009');
|
|
|
|
return this._getSodaDatabase();
|
|
|
|
}
|
2018-09-26 16:31:14 +08:00
|
|
|
|
2019-05-03 07:13:32 +08:00
|
|
|
// Returns an AQ queue
|
|
|
|
queue(name) {
|
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
|
|
|
|
const queue = this._queue(name);
|
|
|
|
queue.name = name;
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:02:49 +08:00
|
|
|
// The queryStream function is similar to execute except that it immediately
|
|
|
|
// returns a QueryStream.
|
|
|
|
queryStream(sql, binding, options) {
|
2019-03-18 11:01:52 +08:00
|
|
|
const self = this;
|
|
|
|
let stream;
|
2018-09-26 16:31:14 +08:00
|
|
|
|
2019-03-13 08:02:49 +08:00
|
|
|
nodbUtil.assert(arguments.length > 0 && arguments.length < 4, 'NJS-009');
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
|
2019-03-13 08:02:49 +08:00
|
|
|
|
|
|
|
if (binding) {
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(nodbUtil.isObjectOrArray(binding), 'NJS-005', 2);
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
2019-03-13 08:02:49 +08:00
|
|
|
|
|
|
|
if (options) {
|
2019-03-18 10:58:21 +08:00
|
|
|
nodbUtil.assert(nodbUtil.isObject(options), 'NJS-005', 3);
|
2019-03-13 08:02:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
binding = binding || [];
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
options.resultSet = true;
|
|
|
|
|
|
|
|
stream = new QueryStream(null);
|
|
|
|
|
|
|
|
// calling execute() via nextTick to ensure that handlers are registered
|
|
|
|
// prior to the events being emitted
|
|
|
|
process.nextTick(function() {
|
|
|
|
self._execute(sql, binding, options, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
stream._open(err, null);
|
2019-05-07 12:43:22 +08:00
|
|
|
} else if (result.resultSet) {
|
2019-03-13 08:02:49 +08:00
|
|
|
result.resultSet._setup(options);
|
|
|
|
stream._open(null, result.resultSet);
|
2019-05-07 12:43:22 +08:00
|
|
|
} else {
|
|
|
|
stream._open(new Error(nodbUtil.getErrorMessage('NJS-019')));
|
2019-03-13 08:02:49 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
2019-03-13 08:02:49 +08:00
|
|
|
|
|
|
|
// module.exports.extend = extend;
|
|
|
|
module.exports = Connection;
|