2014-04-08 16:25:57 -04:00
|
|
|
// SQLite3
|
|
|
|
// -------
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
var inherits = require('inherits');
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
var Client = require('../../client');
|
|
|
|
var Promise = require('../../promise');
|
2014-04-08 16:25:57 -04:00
|
|
|
|
|
|
|
function Client_SQLite3(config) {
|
2014-04-15 11:43:47 -04:00
|
|
|
Client.apply(this, arguments);
|
2014-04-08 16:25:57 -04:00
|
|
|
if (config.debug) this.isDebugging = true;
|
|
|
|
if (config.connection) {
|
|
|
|
this.initDriver();
|
2014-04-16 01:23:50 -04:00
|
|
|
this.initRunner();
|
2014-04-08 16:25:57 -04:00
|
|
|
this.connectionSettings = config.connection;
|
2014-04-16 01:23:50 -04:00
|
|
|
this.initPool();
|
|
|
|
this.pool = new this.Pool(config.pool);
|
2014-04-08 16:25:57 -04:00
|
|
|
}
|
|
|
|
// Todo: Plugins here possibly??
|
|
|
|
}
|
2014-04-15 11:43:47 -04:00
|
|
|
inherits(Client_SQLite3, Client);
|
2014-04-08 16:25:57 -04:00
|
|
|
|
|
|
|
// Lazy load the sqlite3 module, since we might just be using
|
|
|
|
// the client to generate SQL strings.
|
|
|
|
var sqlite3;
|
|
|
|
|
|
|
|
Client_SQLite3.prototype.dialect = 'sqlite3',
|
|
|
|
|
|
|
|
Client_SQLite3.prototype.initTransaction = function() {
|
|
|
|
require('./transaction')(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Client_SQLite3.prototype.initFormatter = function() {
|
|
|
|
require('./formatter')(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Lazy-load the sqlite3 dependency.
|
|
|
|
Client_SQLite3.prototype.initDriver = function() {
|
|
|
|
sqlite3 = sqlite3 || require('sqlite3');
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize the raw connection on the client.
|
|
|
|
Client_SQLite3.prototype.initRaw = function() {
|
|
|
|
require('./raw')(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Always initialize with the "Query" and "QueryCompiler"
|
|
|
|
// objects, each of which is unique to this client (and thus)
|
|
|
|
// can be altered without messing up anything for anyone else.
|
|
|
|
Client_SQLite3.prototype.initQuery = function() {
|
|
|
|
require('./query')(this);
|
|
|
|
};
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Initializes a new pool instance for the current client.
|
|
|
|
Client_SQLite3.prototype.initPool = function() {
|
|
|
|
require('./pool')(this);
|
|
|
|
};
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
// Initialize the query "runner"
|
|
|
|
Client_SQLite3.prototype.initRunner = function() {
|
|
|
|
require('./runner')(this);
|
|
|
|
};
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Lazy-load the schema dependencies.
|
|
|
|
Client_SQLite3.prototype.initSchema = function() {
|
|
|
|
require('./schema')(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Lazy-load the migration dependency
|
|
|
|
Client_SQLite3.prototype.initMigrator = function() {
|
|
|
|
require('./migrator')(this);
|
|
|
|
};
|
|
|
|
|
2014-04-09 10:11:41 -04:00
|
|
|
// Get a raw connection from the database, returning a promise with the connection object.
|
2014-04-08 16:25:57 -04:00
|
|
|
Client_SQLite3.prototype.acquireRawConnection = function() {
|
|
|
|
var driver = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var db = new sqlite3.Database(driver.connectionSettings.filename, function(err) {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(db);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
|
|
|
Client_SQLite3.prototype.destroyRawConnection = Promise.method(function(connection) {
|
|
|
|
connection.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Client_SQLite3;
|