2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-04-16 10:40:55 -04:00
|
|
|
// Runner
|
|
|
|
// -------
|
|
|
|
module.exports = function(client) {
|
|
|
|
|
|
|
|
var Promise = require('../../promise');
|
2014-05-05 21:53:09 -04:00
|
|
|
|
|
|
|
// Require the SQLite3 Runner.
|
|
|
|
require('../sqlite3/runner')(client);
|
|
|
|
var Runner_SQLite3 = client.Runner;
|
2014-04-16 10:40:55 -04:00
|
|
|
|
|
|
|
var inherits = require('inherits');
|
2014-06-09 12:57:10 -04:00
|
|
|
var _ = require('lodash');
|
2014-08-14 15:32:26 -04:00
|
|
|
|
2014-04-16 10:40:55 -04:00
|
|
|
// Inherit from the `Runner` constructor's prototype,
|
|
|
|
// so we can add the correct `then` method.
|
|
|
|
function Runner_WebSQL() {
|
2014-05-05 21:53:09 -04:00
|
|
|
Runner_SQLite3.apply(this, arguments);
|
2014-04-16 10:40:55 -04:00
|
|
|
}
|
|
|
|
inherits(Runner_WebSQL, Runner_SQLite3);
|
|
|
|
|
|
|
|
// Runs the query on the specified connection, providing the bindings and any other necessary prep work.
|
|
|
|
Runner_WebSQL.prototype._query = Promise.method(function(obj) {
|
|
|
|
if (this.isDebugging()) this.debug(obj);
|
|
|
|
var connection = this.connection;
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
if (!connection) {
|
|
|
|
return rejecter(new Error('No connection provided.'));
|
|
|
|
}
|
2014-05-05 21:53:09 -04:00
|
|
|
connection.executeSql(obj.sql, obj.bindings, function(trx, response) {
|
2014-04-16 10:40:55 -04:00
|
|
|
obj.response = response;
|
|
|
|
return resolver(obj);
|
2014-05-05 21:53:09 -04:00
|
|
|
}, function(trx, err) {
|
|
|
|
console.error(err);
|
|
|
|
rejecter(err);
|
2014-04-16 10:40:55 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-14 15:32:26 -04:00
|
|
|
|
|
|
|
// Null out the transaction statements so they aren't run.
|
|
|
|
Runner_WebSQL.prototype._beginTransaction = null;
|
|
|
|
Runner_WebSQL.prototype._commitTransaction = null;
|
|
|
|
Runner_WebSQL.prototype._rollbackTransaction = null;
|
|
|
|
|
2014-04-16 10:40:55 -04:00
|
|
|
// Ensures the response is returned in the same format as other clients.
|
|
|
|
Runner_WebSQL.prototype.processResponse = function(obj) {
|
|
|
|
var resp = obj.response;
|
2014-07-11 08:13:36 -04:00
|
|
|
if (obj.output) return obj.output.call(this, resp);
|
2014-06-04 16:24:29 -04:00
|
|
|
switch (obj.method) {
|
|
|
|
case 'pluck':
|
|
|
|
case 'first':
|
|
|
|
case 'select':
|
|
|
|
var results = [];
|
|
|
|
for (var i = 0, l = resp.rows.length; i < l; i++) {
|
|
|
|
results[i] = _.clone(resp.rows.item(i));
|
|
|
|
}
|
|
|
|
if (obj.method === 'pluck') results = _.pluck(results, obj.pluck);
|
|
|
|
return obj.method === 'first' ? results[0] : results;
|
|
|
|
case 'insert':
|
|
|
|
return [resp.insertId];
|
|
|
|
case 'delete':
|
|
|
|
case 'update':
|
2014-06-18 12:39:58 -04:00
|
|
|
case 'counter':
|
2014-06-04 16:24:29 -04:00
|
|
|
return resp.rowsAffected;
|
|
|
|
default:
|
|
|
|
return resp;
|
2014-04-16 10:40:55 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assign the newly extended `Runner` constructor to the client object.
|
2014-05-05 21:53:09 -04:00
|
|
|
client.Runner = Runner_WebSQL;
|
2014-04-16 10:40:55 -04:00
|
|
|
|
|
|
|
};
|