2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Oracle Client
|
|
|
|
// -------
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign, map, flatten, values } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import Formatter from './formatter';
|
|
|
|
import Client from '../../client';
|
2016-08-09 17:23:07 -04:00
|
|
|
import Promise from 'bluebird';
|
2016-05-17 01:01:34 +10:00
|
|
|
import * as helpers from '../../helpers';
|
|
|
|
import SqlString from '../../query/string';
|
|
|
|
|
|
|
|
import Transaction from './transaction';
|
|
|
|
import QueryCompiler from './query/compiler';
|
|
|
|
import SchemaCompiler from './schema/compiler';
|
|
|
|
import ColumnBuilder from './schema/columnbuilder';
|
|
|
|
import ColumnCompiler from './schema/columncompiler';
|
|
|
|
import TableCompiler from './schema/tablecompiler';
|
|
|
|
import OracleQueryStream from './stream';
|
|
|
|
import { ReturningHelper } from './utils';
|
2016-03-02 17:07:05 +01: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) {
|
|
|
|
Client.call(this, config)
|
|
|
|
}
|
|
|
|
inherits(Client_Oracle, Client)
|
|
|
|
|
|
|
|
assign(Client_Oracle.prototype, {
|
|
|
|
|
|
|
|
dialect: 'oracle',
|
|
|
|
|
|
|
|
driverName: 'oracle',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_driver() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return require('oracle')
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
Transaction,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
Formatter,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
QueryCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
SchemaCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ColumnBuilder,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ColumnCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
TableCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
prepBindings(bindings) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return map(bindings, (value) => {
|
|
|
|
// returning helper uses always ROWID as string
|
|
|
|
if (value instanceof ReturningHelper && this.driver) {
|
|
|
|
return new this.driver.OutParam(this.driver.OCCISTRING)
|
|
|
|
}
|
|
|
|
else if (typeof value === 'boolean') {
|
|
|
|
return value ? 1 : 0
|
|
|
|
}
|
|
|
|
else if (Buffer.isBuffer(value)) {
|
|
|
|
return SqlString.bufferToString(value)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2016-05-17 01:01:34 +10:00
|
|
|
acquireRawConnection() {
|
|
|
|
const client = this
|
2016-03-02 17:07:05 +01:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
client.driver.connect(client.connectionSettings,
|
|
|
|
function(err, connection) {
|
|
|
|
if (err) return rejecter(err)
|
|
|
|
Promise.promisifyAll(connection)
|
|
|
|
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.
|
2016-05-17 01:01:34 +10:00
|
|
|
destroyRawConnection(connection, cb) {
|
2016-03-02 17:07:05 +01:00
|
|
|
connection.close()
|
|
|
|
cb()
|
|
|
|
},
|
|
|
|
|
|
|
|
// Return the database for the Oracle client.
|
2016-05-17 01:01:34 +10:00
|
|
|
database() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return this.connectionSettings.database
|
|
|
|
},
|
|
|
|
|
|
|
|
// Position the bindings for the query.
|
2016-05-17 01:01:34 +10:00
|
|
|
positionBindings(sql) {
|
|
|
|
let questionCount = 0
|
2016-03-02 17:07:05 +01:00
|
|
|
return sql.replace(/\?/g, function() {
|
|
|
|
questionCount += 1
|
2016-05-17 01:01:34 +10:00
|
|
|
return `:${questionCount}`
|
2016-03-02 17:07:05 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_stream(connection, obj, stream, options) {
|
2016-03-02 17:07:05 +01:00
|
|
|
obj.sql = this.positionBindings(obj.sql);
|
|
|
|
return new Promise(function (resolver, rejecter) {
|
|
|
|
stream.on('error', rejecter);
|
|
|
|
stream.on('end', resolver);
|
2016-05-17 01:01:34 +10:00
|
|
|
const queryStream = new OracleQueryStream(connection, obj.sql, obj.bindings, options);
|
2016-03-02 17:07:05 +01:00
|
|
|
queryStream.pipe(stream)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Runs the query on the specified connection, providing the bindings
|
|
|
|
// and any other necessary prep work.
|
2016-05-17 01:01:34 +10:00
|
|
|
_query(connection, obj) {
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// convert ? params into positional bindings (:1)
|
|
|
|
obj.sql = this.positionBindings(obj.sql);
|
|
|
|
|
|
|
|
if (!obj.sql) throw new Error('The query is empty');
|
|
|
|
|
|
|
|
return connection.executeAsync(obj.sql, obj.bindings).then(function(response) {
|
|
|
|
if (!obj.returning) return response
|
2016-05-17 01:01:34 +10:00
|
|
|
const rowIds = obj.outParams.map((v, i) => response[`returnParam${i ? i : ''}`]);
|
2016-03-02 17:07:05 +01:00
|
|
|
return connection.executeAsync(obj.returningSql, rowIds)
|
|
|
|
}).then(function(response) {
|
|
|
|
obj.response = response;
|
2016-06-20 17:03:52 +02:00
|
|
|
obj.rowsAffected = response.updateCount;
|
|
|
|
return obj;
|
2016-03-02 17:07:05 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
// Process the response as returned from the query.
|
2016-05-17 01:01:34 +10:00
|
|
|
processResponse(obj, runner) {
|
|
|
|
let { response } = obj;
|
2016-05-18 19:59:24 +10:00
|
|
|
const { method } = obj;
|
2016-03-02 17:07:05 +01:00
|
|
|
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 = map(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));
|
|
|
|
}
|
2016-06-20 17:03:52 +02:00
|
|
|
return obj.rowsAffected;
|
2016-03-02 17:07:05 +01:00
|
|
|
default:
|
|
|
|
return response;
|
|
|
|
}
|
2016-03-19 19:14:46 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ping(resource, callback) {
|
2016-06-10 17:35:35 -04:00
|
|
|
resource.execute('SELECT 1 FROM DUAL', [], callback);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default Client_Oracle
|