knex/lib/dialects/oracle/index.js

136 lines
3.7 KiB
JavaScript
Raw Normal View History

'use strict';
2014-08-11 12:25:39 +02:00
// Oracle Client
// -------
var inherits = require('inherits');
var Client = require('../../client');
var Promise = require('../../promise');
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) {
Client.call(this, config);
2014-08-11 12:25:39 +02:00
}
inherits(Client_Oracle, Client);
// The "dialect", for reference elsewhere.
Client_Oracle.prototype.dialect = 'oracle';
// Lazy-load the oracle dependency, since we might just be
// using the client to generate SQL strings.
Client_Oracle.prototype.initDriver = function() {
2014-11-26 17:45:11 -06:00
Client_Oracle.prototype.driver = Client_Oracle.prototype.driver || require(
'oracle');
2014-08-11 12:25:39 +02:00
};
// Attach a `Formatter` constructor to the client object.
Client_Oracle.prototype.initFormatter = function() {
require('./formatter')(this);
};
// Attaches the `Raw` constructor to the client object.
Client_Oracle.prototype.initRaw = function() {
require('./raw')(this);
};
// Attaches the `FunctionHelper` constructor to the client object.
Client_Oracle.prototype.initFunctionHelper = function() {
require('./functionhelper')(this);
};
// Attaches the `Transaction` constructor to the client object.
Client_Oracle.prototype.initTransaction = function() {
require('./transaction')(this);
};
// Attaches `QueryBuilder` and `QueryCompiler` constructors
// to the client object.
Client_Oracle.prototype.initQuery = function() {
require('./query')(this);
};
// Initialize the query "runner"
Client_Oracle.prototype.initRunner = function() {
require('./runner')(this);
};
// Lazy-load the schema dependencies; we may not need to use them.
Client_Oracle.prototype.initSchema = function() {
require('./schema')(this);
};
// Lazy-load the migration dependency
Client_Oracle.prototype.initMigrator = function() {
require('./migrator')(this);
};
2014-08-13 22:02:18 +02:00
// Lazy-load the seeding dependency
Client_Oracle.prototype.initSeeder = function() {
require('./seeder')(this);
};
2014-08-11 12:25:39 +02:00
// Get a raw connection, called by the `pool` whenever a new
// connection needs to be added to the pool.
Client_Oracle.prototype.acquireRawConnection = function() {
var self = this;
return new Promise(function(resolver, rejecter) {
2014-11-26 17:45:11 -06:00
self.driver.connect(self.connectionSettings,
function(err,
connection) {
if (err) return rejecter(err);
if (self.connectionSettings.prefetchRowCount) {
connection.setPrefetchRowCount(self.connectionSettings.prefetchRowCount);
}
resolver(connection);
});
2014-08-11 12:25:39 +02:00
});
};
// Used to explicitly close a connection, called internally by the pool
// when a connection times out or the pool is shutdown.
Client_Oracle.prototype.destroyRawConnection = function(connection, cb) {
2014-08-11 12:25:39 +02:00
connection.close();
cb()
2014-08-11 12:25:39 +02:00
};
// Return the database for the Oracle client.
Client_Oracle.prototype.database = function() {
return this.connectionSettings.database;
};
// Position the bindings for the query.
Client_Oracle.prototype.positionBindings = function(sql) {
var questionCount = 0;
return sql.replace(/\?/g, function() {
questionCount += 1;
return ':' + questionCount;
});
};
var ReturningHelper = require('./utils').ReturningHelper;
2014-11-26 17:45:11 -06:00
Client_Oracle.prototype.preprocessBindings = function(bindings) {
2014-08-11 12:25:39 +02:00
if (!bindings) {
return bindings;
}
var driver = this.driver;
2014-11-26 17:45:11 -06:00
return bindings.map(function(binding) {
if (binding instanceof ReturningHelper && driver) {
// returning helper uses always ROWID as string
return new driver.OutParam(driver.OCCISTRING);
2014-08-11 12:25:39 +02:00
}
if (typeof binding === 'boolean') {
return binding ? 1 : 0;
}
return binding;
});
};
module.exports = Client_Oracle;