knex/lib/dialects/sqlite3/runner.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-04-15 13:10:32 -04:00
// Runner
// -------
module.exports = function(client) {
2014-04-16 10:41:16 -04:00
2014-06-04 16:24:29 -04:00
var _ = require('lodash');
2014-04-16 01:23:50 -04:00
var Promise = require('../../promise');
var Runner = require('../../runner');
2014-04-16 10:41:16 -04:00
var helpers = require('../../helpers');
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-09 10:11:41 -04:00
// Runs the query on the specified connection, providing the bindings and any other necessary prep work.
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);
var callMethod;
switch (method) {
case 'insert':
case 'update':
case 'counter':
case 'del':
callMethod = 'run';
break;
default:
callMethod = '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) {
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-09 10:11:41 -04:00
// Sounds like .each doesn't work great for
2014-05-05 19:48:12 -04:00
Runner_SQLite3.prototype._stream = Promise.method(function(sql, stream, options) {
var runner = this;
return new Promise(function(resolver, rejecter) {
stream.on('error', rejecter);
stream.on('end', resolver);
return runner.query(sql).map(function(row) {
2014-05-05 19:48:12 -04:00
stream.write(row);
}).catch(function() {
stream.emit('error');
}).then(function() {
2014-05-05 19:48:12 -04:00
stream.end();
});
});
});
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);
2014-06-04 16:24:29 -04:00
switch (obj.method) {
case 'select':
case 'pluck':
case 'first':
response = helpers.skim(response);
if (obj.method === 'pluck') response = _.pluck(response, obj.pluck);
return obj.method === 'first' ? response[0] : response;
case 'insert':
return [ctx.lastID];
case 'del':
case 'update':
case 'counter':
2014-06-04 16:24:29 -04:00
return ctx.changes;
default:
return response;
2014-04-09 10:11:41 -04:00
}
};
// Assign the newly extended `Runner` constructor to the client object.
client.Runner = Runner_SQLite3;
};