2014-04-15 13:10:32 -04:00
|
|
|
// Runner
|
|
|
|
// -------
|
2014-04-08 16:25:57 -04:00
|
|
|
module.exports = function(client) {
|
2014-04-16 01:23:50 -04:00
|
|
|
var Promise = require('../../promise');
|
|
|
|
var Runner = require('../../runner');
|
|
|
|
var utils = require('../../utils');
|
2014-04-08 16:25:57 -04:00
|
|
|
var inherits = require('inherits');
|
|
|
|
|
|
|
|
// Inherit from the `Runner` constructor's prototype,
|
|
|
|
// so we can add the correct `then` method.
|
|
|
|
function Runner_SQLite3() {
|
|
|
|
this.client = client;
|
|
|
|
Runner.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(Runner_SQLite3, Runner);
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
Runner_SQLite3.prototype._beginTransaction = 'begin transaction;';
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-09 10:11:41 -04:00
|
|
|
// Runs the query on the specified connection, providing the bindings and any other necessary prep work.
|
2014-04-15 11:43:47 -04:00
|
|
|
Runner_SQLite3.prototype._query = Promise.method(function(obj) {
|
2014-04-09 10:11:41 -04:00
|
|
|
var method = obj.method;
|
2014-04-16 01:23:50 -04:00
|
|
|
if (this.isDebugging()) this.debug(obj);
|
2014-04-16 02:59:27 -04:00
|
|
|
var callMethod = (method === 'insert' || method === 'update' || method === 'del') ? 'run' : 'all';
|
2014-04-16 01:23:50 -04:00
|
|
|
var connection = this.connection;
|
2014-04-09 10:11:41 -04:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2014-04-15 11:43:47 -04:00
|
|
|
if (!connection || !connection[callMethod]) {
|
|
|
|
return rejecter(new Error('Error calling ' + callMethod + ' on connection.'));
|
|
|
|
}
|
2014-04-09 10:11:41 -04:00
|
|
|
connection[callMethod](obj.sql, obj.bindings, function(err, response) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
obj.response = response;
|
|
|
|
|
|
|
|
// We need the context here, as it contains
|
|
|
|
// the "this.lastID" or "this.changes"
|
|
|
|
obj.context = this;
|
|
|
|
return resolver(obj);
|
|
|
|
});
|
|
|
|
});
|
2014-04-15 11:43:47 -04:00
|
|
|
});
|
2014-04-09 10:11:41 -04:00
|
|
|
|
|
|
|
// Ensures the response is returned in the same format as other clients.
|
|
|
|
Runner_SQLite3.prototype.processResponse = function(obj) {
|
|
|
|
var ctx = obj.context;
|
|
|
|
var response = obj.response;
|
2014-04-16 01:23:50 -04:00
|
|
|
if (obj.output) return obj.output.call(this, response);
|
|
|
|
if (obj.method === 'select') {
|
|
|
|
response = utils.skim(response);
|
|
|
|
} else if (obj.method === 'insert') {
|
2014-04-09 10:11:41 -04:00
|
|
|
response = [ctx.lastID];
|
2014-04-16 02:59:27 -04:00
|
|
|
} else if (obj.method === 'del' || obj.method === 'update') {
|
2014-04-09 10:11:41 -04:00
|
|
|
response = ctx.changes;
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Assign the newly extended `Runner` constructor to the client object.
|
|
|
|
client.Runner = Runner_SQLite3;
|
|
|
|
|
|
|
|
};
|