2016-02-29 10:48:48 +08:00
|
|
|
/* Copyright (c) 2016, 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-05-16 07:53:23 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
var resultset = require('./resultset.js');
|
2016-05-16 07:53:23 +08:00
|
|
|
var QueryStream = require('./querystream.js');
|
|
|
|
var nodbUtil = require('./util.js');
|
|
|
|
var executePromisified;
|
|
|
|
var commitPromisified;
|
|
|
|
var rollbackPromisified;
|
|
|
|
var releasePromisified;
|
|
|
|
var breakPromisified;
|
2016-03-24 18:18:16 +08:00
|
|
|
|
|
|
|
// The queryStream function is similar to execute except that it immediately
|
2016-05-16 07:53:23 +08:00
|
|
|
// returns a QueryStream.
|
2016-03-24 18:18:16 +08:00
|
|
|
function queryStream(sql, binding, options) {
|
|
|
|
var self = this;
|
|
|
|
var stream;
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length > 0 && arguments.length < 4, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof sql === 'string', 'NJS-006', 1);
|
|
|
|
|
|
|
|
if (binding) {
|
|
|
|
nodbUtil.assert(nodbUtil.isObjectOrArray(binding), 'NJS-006', 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
nodbUtil.assert(nodbUtil.isObject(options), 'NJS-006', 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
binding = binding || [];
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
options.resultSet = true;
|
|
|
|
|
|
|
|
stream = new QueryStream(null, self._oracledb);
|
|
|
|
|
|
|
|
self._execute(sql, binding, options, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
stream._open(err, null);
|
|
|
|
} else {
|
|
|
|
resultset.extend(result.resultSet, self._oracledb);
|
|
|
|
stream._open(null, result.resultSet);
|
|
|
|
}
|
|
|
|
});
|
2016-03-24 18:18:16 +08:00
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
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.
|
|
|
|
function execute(a1, a2, a3, a4) {
|
|
|
|
var self = this;
|
|
|
|
var executeCb;
|
|
|
|
var custExecuteCb;
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length > 1 && arguments.length < 5, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof a1 === 'string', 'NJS-006', 1);
|
|
|
|
|
|
|
|
switch (arguments.length) {
|
|
|
|
case 2:
|
|
|
|
nodbUtil.assert(typeof a2 === 'function', 'NJS-006', 2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-006', 2);
|
|
|
|
nodbUtil.assert(typeof a3 === 'function', 'NJS-006', 3);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-006', 2);
|
|
|
|
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-006', 3);
|
|
|
|
nodbUtil.assert(typeof a4 === 'function', 'NJS-006', 4);
|
|
|
|
break;
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
custExecuteCb = function(err, result) {
|
2016-05-16 07:53:23 +08:00
|
|
|
var outBindsKeys;
|
|
|
|
var outBindsIdx;
|
|
|
|
|
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
|
|
|
|
// or outBinds.
|
2016-02-29 10:48:48 +08:00
|
|
|
if (result.resultSet) {
|
2016-05-16 07:53:23 +08:00
|
|
|
resultset.extend(result.resultSet, self._oracledb);
|
|
|
|
} else if (result.outBinds) {
|
|
|
|
outBindsKeys = Object.keys(result.outBinds);
|
|
|
|
|
|
|
|
for (outBindsIdx = 0; outBindsIdx < outBindsKeys.length; outBindsIdx += 1) {
|
|
|
|
if (result.outBinds[outBindsKeys[outBindsIdx]] instanceof self._oracledb.ResultSet) {
|
|
|
|
resultset.extend(result.outBinds[outBindsKeys[outBindsIdx]], self._oracledb);
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
executeCb(null, result);
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (arguments.length) {
|
|
|
|
case 4:
|
|
|
|
executeCb = a4;
|
|
|
|
self._execute.call(self, a1, a2, a3, custExecuteCb);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
executeCb = a3;
|
|
|
|
self._execute.call(self, a1, a2, custExecuteCb);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
executeCb = a2;
|
|
|
|
self._execute.call(self, a1, custExecuteCb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
executePromisified = nodbUtil.promisify(execute);
|
|
|
|
|
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) {
|
2016-02-29 10:48:48 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof commitCb === 'function', 'NJS-006', 1);
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
self._commit.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
commitPromisified = nodbUtil.promisify(commit);
|
|
|
|
|
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) {
|
2016-02-29 10:48:48 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof rollbackCb === 'function', 'NJS-006', 1);
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
self._rollback.apply(self, arguments);
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
rollbackPromisified = nodbUtil.promisify(rollback);
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
// This release function is used to override the release method of the Connection
|
|
|
|
// class, which is defined in the C layer. Currently the main difference is that
|
|
|
|
// connections obtained from a pool need to invoke the pool's _onConnectionRelease
|
|
|
|
// method so the pool can dequeue the next connection request.
|
|
|
|
function release(releaseCb) {
|
|
|
|
var self = this;
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof releaseCb === 'function', 'NJS-006', 1);
|
2016-02-29 10:48:48 +08:00
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
self._release(function(err) {
|
|
|
|
releaseCb(err);
|
|
|
|
|
|
|
|
// pool will only exist for connections obtained from a pool.
|
|
|
|
if (self._pool && self._pool.queueRequests !== false) {
|
2016-02-29 10:48:48 +08:00
|
|
|
self._pool._onConnectionRelease();
|
2016-05-16 07:53:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
releasePromisified = nodbUtil.promisify(release);
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
// This release function is just a place holder to allow for easier extension later.
|
|
|
|
// It's attached to the module as break is a reserved word.
|
2016-05-16 07:53:23 +08:00
|
|
|
module.break = function(breakCb) {
|
2016-02-29 10:48:48 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
nodbUtil.assert(arguments.length === 1, 'NJS-009');
|
|
|
|
nodbUtil.assert(typeof breakCb === 'function', 'NJS-006', 1);
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
self._break.apply(self, arguments);
|
|
|
|
};
|
|
|
|
|
2016-05-16 07:53:23 +08:00
|
|
|
breakPromisified = nodbUtil.promisify(module.break);
|
|
|
|
|
2016-02-29 10:48:48 +08:00
|
|
|
// The extend method is used to extend the Connection instance from the C layer with
|
|
|
|
// custom properties and method overrides. References to the original methods are
|
|
|
|
// maintained so they can be invoked by the overriding method at the right time.
|
2016-03-24 18:18:16 +08:00
|
|
|
function extend(conn, oracledb, pool) {
|
2016-02-29 10:48:48 +08:00
|
|
|
// Using Object.defineProperties to add properties to the Connection instance with
|
|
|
|
// special properties, such as enumerable but not writable.
|
|
|
|
Object.defineProperties(
|
|
|
|
conn,
|
|
|
|
{
|
2016-03-24 18:18:16 +08:00
|
|
|
_oracledb: { // storing a reference to the base instance to avoid circular references with require
|
|
|
|
value: oracledb
|
|
|
|
},
|
2016-02-29 10:48:48 +08:00
|
|
|
_pool: {
|
|
|
|
value: pool
|
|
|
|
},
|
|
|
|
_execute: {
|
|
|
|
value: conn.execute
|
|
|
|
},
|
2016-03-24 18:18:16 +08:00
|
|
|
queryStream: {
|
|
|
|
value: queryStream,
|
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
|
|
|
},
|
2016-02-29 10:48:48 +08:00
|
|
|
execute: {
|
2016-05-16 07:53:23 +08:00
|
|
|
value: executePromisified,
|
2016-03-01 05:38:52 +08:00
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
2016-02-29 10:48:48 +08:00
|
|
|
},
|
|
|
|
_commit: {
|
|
|
|
value: conn.commit
|
|
|
|
},
|
|
|
|
commit: {
|
2016-05-16 07:53:23 +08:00
|
|
|
value: commitPromisified,
|
2016-03-01 05:38:52 +08:00
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
2016-02-29 10:48:48 +08:00
|
|
|
},
|
|
|
|
_rollback: {
|
|
|
|
value: conn.rollback
|
|
|
|
},
|
|
|
|
rollback: {
|
2016-05-16 07:53:23 +08:00
|
|
|
value: rollbackPromisified,
|
2016-03-01 05:38:52 +08:00
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
2016-02-29 10:48:48 +08:00
|
|
|
},
|
|
|
|
_release: {
|
|
|
|
value: conn.release
|
|
|
|
},
|
|
|
|
release: {
|
2016-05-16 07:53:23 +08:00
|
|
|
value: releasePromisified,
|
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
|
|
|
},
|
|
|
|
close: { // alias for release
|
|
|
|
value: releasePromisified,
|
2016-03-01 05:38:52 +08:00
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
2016-02-29 10:48:48 +08:00
|
|
|
},
|
|
|
|
_break: {
|
|
|
|
value: conn.break
|
|
|
|
},
|
|
|
|
break: {
|
2016-05-16 07:53:23 +08:00
|
|
|
value: breakPromisified,
|
2016-03-01 05:38:52 +08:00
|
|
|
enumerable: true,
|
|
|
|
writable: true
|
2016-02-29 10:48:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.extend = extend;
|