2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-11 12:25:39 +02:00
|
|
|
// Oracle Client
|
|
|
|
// -------
|
2015-04-22 10:34:14 -04:00
|
|
|
var _ = require('lodash')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var Client = require('../../client')
|
|
|
|
var Promise = require('../../promise')
|
|
|
|
var ReturningHelper = require('./utils').ReturningHelper
|
|
|
|
var Formatter = require('./formatter')
|
|
|
|
var assign = require('lodash/object/assign')
|
|
|
|
|
|
|
|
var QueryCompiler = require('./query/compiler')
|
|
|
|
var SchemaCompiler = require('./schema/compiler')
|
|
|
|
var ColumnBuilder = require('./schema/columnbuilder')
|
|
|
|
var ColumnCompiler = require('./schema/columncompiler')
|
|
|
|
var TableCompiler = require('./schema/tablecompiler')
|
|
|
|
var OracleQueryStream = require('./stream')
|
2014-08-11 12:25:39 +02:00
|
|
|
|
|
|
|
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
|
|
|
// objects, which extend the base 'lib/query/builder' and
|
|
|
|
// 'lib/query/compiler', respectively.
|
|
|
|
function Client_Oracle(config) {
|
2015-04-19 16:31:52 -04:00
|
|
|
Client.call(this, config)
|
2014-08-11 12:25:39 +02:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(Client_Oracle, Client)
|
|
|
|
|
|
|
|
assign(Client_Oracle.prototype, {
|
|
|
|
|
|
|
|
dialect: 'oracle',
|
|
|
|
|
|
|
|
driverName: 'oracle',
|
|
|
|
|
|
|
|
Formatter: Formatter,
|
|
|
|
|
|
|
|
QueryCompiler: QueryCompiler,
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
SchemaCompiler: SchemaCompiler,
|
|
|
|
|
|
|
|
ColumnBuilder: ColumnBuilder,
|
|
|
|
|
|
|
|
ColumnCompiler: ColumnCompiler,
|
|
|
|
|
|
|
|
TableCompiler: TableCompiler,
|
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
|
|
|
acquireRawConnection: function() {
|
|
|
|
var client = this
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
client.driver.connect(client.connectionSettings,
|
|
|
|
function(err, connection) {
|
|
|
|
if (err) return rejecter(err)
|
|
|
|
if (client.connectionSettings.prefetchRowCount) {
|
|
|
|
connection.setPrefetchRowCount(client.connectionSettings.prefetchRowCount)
|
|
|
|
}
|
|
|
|
resolver(connection)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
|
|
|
destroyRawConnection: function(connection, cb) {
|
|
|
|
connection.close()
|
|
|
|
cb()
|
|
|
|
},
|
|
|
|
|
|
|
|
// Return the database for the Oracle client.
|
|
|
|
database: function() {
|
|
|
|
return this.connectionSettings.database
|
|
|
|
},
|
|
|
|
|
|
|
|
// Position the bindings for the query.
|
|
|
|
positionBindings: function(sql) {
|
|
|
|
var questionCount = 0
|
|
|
|
return sql.replace(/\?/g, function() {
|
|
|
|
questionCount += 1
|
|
|
|
return ':' + questionCount
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
_stream: function(connection, obj, stream, options) {
|
|
|
|
obj.sql = this.positionBindings(obj.sql);
|
|
|
|
return new Promise(function (resolver, rejecter) {
|
|
|
|
stream.on('error', rejecter);
|
|
|
|
stream.on('end', resolver);
|
|
|
|
var queryStream = new OracleQueryStream(connection, obj.sql, obj.bindings, options);
|
|
|
|
queryStream.pipe(stream)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Runs the query on the specified connection, providing the bindings
|
|
|
|
// and any other necessary prep work.
|
|
|
|
_query: function(connection, obj) {
|
|
|
|
|
|
|
|
// convert ? params into positional bindings (:1)
|
|
|
|
obj.sql = this.client.positionBindings(obj.sql);
|
|
|
|
|
|
|
|
obj.bindings = obj.bindings || [];
|
|
|
|
|
|
|
|
if (!obj.sql) throw new Error('The query is empty');
|
|
|
|
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
connection.execute(obj.sql, obj.bindings, function(err, response) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
|
|
|
|
if (obj.returning) {
|
|
|
|
var rowIds = obj.outParams.map(function (v, i) {
|
|
|
|
return response['returnParam' + (i ? i : '')];
|
|
|
|
});
|
|
|
|
|
|
|
|
return connection.execute(obj.returningSql, rowIds, function (err, subres) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
obj.response = subres;
|
|
|
|
resolver(obj);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
obj.response = response;
|
|
|
|
resolver(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Process the response as returned from the query.
|
|
|
|
processResponse: function(obj, runner) {
|
|
|
|
var response = obj.response;
|
|
|
|
var method = obj.method;
|
|
|
|
if (obj.output) return obj.output.call(runner, response);
|
|
|
|
switch (method) {
|
|
|
|
case 'select':
|
|
|
|
case 'pluck':
|
|
|
|
case 'first':
|
|
|
|
response = helpers.skim(response);
|
|
|
|
if (obj.method === 'pluck') response = _.pluck(response, obj.pluck);
|
|
|
|
return obj.method === 'first' ? response[0] : response;
|
|
|
|
case 'insert':
|
|
|
|
case 'del':
|
|
|
|
case 'update':
|
|
|
|
case 'counter':
|
|
|
|
if (obj.returning) {
|
|
|
|
if (obj.returning.length > 1 || obj.returning[0] === '*') {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
// return an array with values if only one returning value was specified
|
|
|
|
return _.flatten(_.map(response, _.values));
|
|
|
|
}
|
|
|
|
return response.updateCount;
|
|
|
|
default:
|
|
|
|
return response;
|
2014-08-11 12:25:39 +02:00
|
|
|
}
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
2014-08-11 12:25:39 +02:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
})
|
2014-08-11 12:25:39 +02:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
Client_Oracle.__proto__ = Client
|
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
module.exports = Client_Oracle
|