2016-03-02 17:07:05 +01:00
|
|
|
// Oracle Client
|
|
|
|
// -------
|
2018-07-09 08:10:34 -04: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 Client from '../../client';
|
2016-08-09 17:23:07 -04:00
|
|
|
import Promise from 'bluebird';
|
2018-07-09 08:10:34 -04:00
|
|
|
import { bufferToString } from '../../query/string';
|
2016-09-13 08:15:58 -04:00
|
|
|
import Formatter from './formatter';
|
2016-05-17 01:01:34 +10:00
|
|
|
|
|
|
|
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 { 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.
|
2016-09-12 18:26:49 -04:00
|
|
|
export default function Client_Oracle(config) {
|
2018-07-09 08:10:34 -04:00
|
|
|
Client.call(this, config);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
inherits(Client_Oracle, Client);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
assign(Client_Oracle.prototype, {
|
|
|
|
dialect: 'oracle',
|
|
|
|
|
|
|
|
driverName: 'oracle',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_driver() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return require('oracle');
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-09-12 18:45:35 -04:00
|
|
|
transaction() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Transaction(this, ...arguments);
|
2016-09-12 18:45:35 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-12 18:26:49 -04:00
|
|
|
formatter() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Formatter(this, ...arguments);
|
2016-09-12 18:26:49 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
queryCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new QueryCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
schemaCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new SchemaCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
columnBuilder() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new ColumnBuilder(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
columnCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new ColumnCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
tableCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new TableCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
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) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new this.driver.OutParam(this.driver.OCCISTRING);
|
|
|
|
} else if (typeof value === 'boolean') {
|
|
|
|
return value ? 1 : 0;
|
|
|
|
} else if (Buffer.isBuffer(value)) {
|
|
|
|
return bufferToString(value);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return value;
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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() {
|
2016-09-13 18:12:23 -04:00
|
|
|
return new Promise((resolver, rejecter) => {
|
2019-03-05 18:46:58 +01:00
|
|
|
this.driver.connect(this.connectionSettings, (err, connection) => {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
Promise.promisifyAll(connection);
|
|
|
|
if (this.connectionSettings.prefetchRowCount) {
|
|
|
|
connection.setPrefetchRowCount(
|
|
|
|
this.connectionSettings.prefetchRowCount
|
|
|
|
);
|
2016-09-13 18:12:23 -04:00
|
|
|
}
|
2019-03-05 18:46:58 +01:00
|
|
|
resolver(connection);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
2016-09-13 18:12:23 -04:00
|
|
|
destroyRawConnection(connection) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return Promise.fromCallback(connection.close.bind(connection));
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Return the database for the Oracle client.
|
2016-05-17 01:01:34 +10:00
|
|
|
database() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this.connectionSettings.database;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Position the bindings for the query.
|
2016-05-17 01:01:34 +10:00
|
|
|
positionBindings(sql) {
|
2018-07-09 08:10:34 -04:00
|
|
|
let questionCount = 0;
|
2016-03-02 17:07:05 +01:00
|
|
|
return sql.replace(/\?/g, function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
questionCount += 1;
|
|
|
|
return `:${questionCount}`;
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_stream(connection, obj, stream, options) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2016-09-13 18:12:23 -04:00
|
|
|
stream.on('error', (err) => {
|
|
|
|
if (isConnectionError(err)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
connection.__knex__disposed = err;
|
2016-09-13 18:12:23 -04:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
rejecter(err);
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
stream.on('end', resolver);
|
2018-07-09 08:10:34 -04:00
|
|
|
const queryStream = connection.queryStream(
|
|
|
|
obj.sql,
|
|
|
|
obj.bindings,
|
|
|
|
options
|
|
|
|
);
|
2016-10-14 17:00:39 +02:00
|
|
|
queryStream.pipe(stream);
|
2018-06-27 16:42:50 -03:00
|
|
|
queryStream.on('error', function(error) {
|
|
|
|
rejecter(error);
|
|
|
|
stream.emit('error', error);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
if (!obj.sql) throw new Error('The query is empty');
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
return connection
|
|
|
|
.executeAsync(obj.sql, obj.bindings)
|
|
|
|
.then(function(response) {
|
|
|
|
if (!obj.returning) return response;
|
|
|
|
const rowIds = obj.outParams.map(
|
|
|
|
(v, i) => response[`returnParam${i ? i : ''}`]
|
|
|
|
);
|
|
|
|
return connection.executeAsync(obj.returningSql, rowIds);
|
|
|
|
})
|
|
|
|
.then(function(response) {
|
|
|
|
obj.response = response;
|
|
|
|
obj.rowsAffected = response.updateCount;
|
|
|
|
return obj;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (isConnectionError(err)) {
|
|
|
|
connection.__knex__disposed = err;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
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':
|
|
|
|
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;
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
});
|
2016-09-13 18:12:23 -04:00
|
|
|
|
|
|
|
// If the error is any of these, we'll assume we need to
|
|
|
|
// mark the connection as failed
|
|
|
|
const connectionErrors = [
|
2018-07-09 08:10:34 -04:00
|
|
|
'ORA-12514',
|
|
|
|
'NJS-040',
|
|
|
|
'NJS-024',
|
|
|
|
'NJS-003',
|
|
|
|
'NJS-024',
|
|
|
|
];
|
2016-09-13 18:12:23 -04:00
|
|
|
|
|
|
|
function isConnectionError(err) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return connectionErrors.some((prefix) => err.message.indexOf(prefix) === 0);
|
2016-09-13 18:12:23 -04:00
|
|
|
}
|