2014-04-15 11:43:47 -04:00
|
|
|
// MySQL Runner
|
|
|
|
// ------
|
2014-04-08 16:25:57 -04:00
|
|
|
module.exports = function(client) {
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
var _ = require('lodash');
|
2014-04-08 16:25:57 -04:00
|
|
|
var inherits = require('inherits');
|
|
|
|
|
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
|
|
|
// Inherit from the `Runner` constructor's prototype,
|
|
|
|
// so we can add the correct `then` method.
|
|
|
|
function Runner_MySQL() {
|
|
|
|
this.client = client;
|
|
|
|
Runner.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(Runner_MySQL, Runner);
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Grab a connection, run the query via the MySQL streaming interface,
|
|
|
|
// and pass that through to the stream we've sent back to the client.
|
2014-04-09 10:11:41 -04:00
|
|
|
Runner_MySQL.prototype._stream = function(stream, options) {
|
|
|
|
return Promise.bind(this)
|
|
|
|
.then(this.ensureConnection)
|
|
|
|
.then(function() {
|
|
|
|
var sql = this.builder.toSQL();
|
|
|
|
var err = new Error('The stream may only be used with a single query statement.');
|
|
|
|
if (_.isArray(sql)) {
|
|
|
|
stream.emit('error', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return sql;
|
|
|
|
}).then(function(sql) {
|
|
|
|
var runner = this;
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
stream.on('error', rejecter);
|
|
|
|
stream.on('end', resolver);
|
|
|
|
runner.connection.query(sql.sql, sql.bindings).stream(options).pipe(stream);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(this.cleanupConnection);
|
|
|
|
};
|
|
|
|
|
2014-04-08 16:25:57 -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_MySQL.prototype._query = Promise.method(function(obj) {
|
|
|
|
var sql = obj.sql;
|
2014-04-16 01:23:50 -04:00
|
|
|
if (this.isDebugging()) this.debug(obj);
|
|
|
|
if (obj.options) sql = _.extend({sql: sql}, obj.options);
|
|
|
|
var connection = this.connection;
|
|
|
|
if (!sql) throw new Error('The query is empty');
|
2014-04-15 11:43:47 -04:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2014-04-16 01:23:50 -04:00
|
|
|
connection.query(sql, obj.bindings, function(err, rows, fields) {
|
2014-04-15 11:43:47 -04:00
|
|
|
if (err) return rejecter(err);
|
2014-04-16 01:23:50 -04:00
|
|
|
obj.response = [rows, fields];
|
|
|
|
resolver(obj);
|
2014-04-15 11:43:47 -04:00
|
|
|
});
|
|
|
|
});
|
2014-04-08 16:25:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Process the response as returned from the query.
|
2014-04-09 10:11:41 -04:00
|
|
|
Runner_MySQL.prototype.processResponse = function(obj) {
|
|
|
|
var response = obj.response;
|
2014-04-16 01:23:50 -04:00
|
|
|
var method = obj.method;
|
2014-04-09 10:11:41 -04:00
|
|
|
var rows = response[0];
|
|
|
|
var fields = response[1];
|
|
|
|
var resp;
|
2014-04-16 01:23:50 -04:00
|
|
|
if (obj.output) {
|
|
|
|
return obj.output.call(this, rows, fields);
|
2014-04-09 10:11:41 -04:00
|
|
|
} else if (method === 'select') {
|
|
|
|
resp = utils.skim(rows);
|
|
|
|
} else if (method === 'insert') {
|
|
|
|
resp = [rows.insertId];
|
2014-04-16 02:59:27 -04:00
|
|
|
} else if (method === 'del' || method === 'update') {
|
2014-04-09 10:11:41 -04:00
|
|
|
resp = rows.affectedRows;
|
|
|
|
} else {
|
|
|
|
resp = response;
|
|
|
|
}
|
|
|
|
return resp;
|
2014-04-08 16:25:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Assign the newly extended `Runner` constructor to the client object.
|
|
|
|
client.Runner = Runner_MySQL;
|
|
|
|
|
|
|
|
};
|