knex/lib/dialects/sqlite3/runner.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

module.exports = function(client) {
var Runner = require('../../runner');
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);
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.dialectQuery = Promise.method(function(obj) {
if (!this.connection) throw new Error('No database connection exists for the query');
var method = obj.method;
var callMethod = (method === 'insert' || method === 'update' || method === 'delete') ? 'run' : 'all';
return new Promise(function(resolver, rejecter) {
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);
});
});
}),
// 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;
if (obj.output) return obj.output.call(runner, response);
if (method === 'select') {
response = helpers.skim(response);
} else if (method === 'insert') {
response = [ctx.lastID];
} else if (method === 'delete' || method === 'update') {
response = ctx.changes;
}
return response;
};
// Assign the newly extended `Runner` constructor to the client object.
client.Runner = Runner_SQLite3;
};