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');
|
2014-04-16 04:29:20 -04:00
|
|
|
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_PG() {
|
|
|
|
this.client = client;
|
|
|
|
Runner.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(Runner_PG, Runner);
|
|
|
|
|
2014-04-09 10:11:41 -04:00
|
|
|
var PGQueryStream;
|
2014-05-05 19:48:12 -04:00
|
|
|
Runner_PG.prototype._stream = Promise.method(function(sql, stream, options) {
|
2014-04-09 10:11:41 -04:00
|
|
|
PGQueryStream = PGQueryStream || require('pg-query-stream');
|
2014-05-05 19:48:12 -04:00
|
|
|
var runner = this;
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
stream.on('error', rejecter);
|
|
|
|
stream.on('end', resolver);
|
|
|
|
runner.connection.query(new PGQueryStream(sql.sql, sql.bindings, options)).pipe(stream);
|
|
|
|
});
|
|
|
|
});
|
2014-04-09 10:11:41 -04:00
|
|
|
|
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_PG.prototype._query = Promise.method(function(obj) {
|
2014-04-09 10:11:41 -04:00
|
|
|
var connection = this.connection;
|
2014-04-16 04:29:20 -04:00
|
|
|
var sql = obj.sql = utils.pgBindings(obj.sql);
|
2014-04-16 01:23:50 -04:00
|
|
|
if (this.isDebugging()) this.debug(obj);
|
|
|
|
if (obj.options) sql = _.extend({text: sql}, obj.options);
|
2014-04-09 10:11:41 -04:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2014-04-16 01:23:50 -04:00
|
|
|
connection.query(sql, obj.bindings, function(err, response) {
|
2014-04-09 10:11:41 -04:00
|
|
|
if (err) return rejecter(err);
|
2014-04-16 01:23:50 -04:00
|
|
|
obj.response = response;
|
|
|
|
resolver(obj);
|
2014-04-09 10:11:41 -04:00
|
|
|
});
|
|
|
|
});
|
2014-04-08 16:25:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Ensures the response is returned in the same format as other clients.
|
2014-04-09 10:11:41 -04:00
|
|
|
Runner_PG.prototype.processResponse = function(obj) {
|
2014-04-16 01:23:50 -04:00
|
|
|
var resp = obj.response;
|
2014-06-04 15:55:41 -04:00
|
|
|
if (obj.output) return obj.output.call(this, resp);
|
|
|
|
if (obj.method === 'raw') return resp;
|
2014-04-16 01:23:50 -04:00
|
|
|
var returning = obj.returning;
|
|
|
|
if (resp.command === 'SELECT') return resp.rows;
|
2014-04-16 02:59:27 -04:00
|
|
|
if (returning) {
|
2014-04-16 01:23:50 -04:00
|
|
|
var returns = [];
|
|
|
|
for (var i = 0, l = resp.rows.length; i < l; i++) {
|
|
|
|
var row = resp.rows[i];
|
|
|
|
if (returning === '*' || _.isArray(returning)) {
|
|
|
|
returns[i] = row;
|
|
|
|
} else {
|
|
|
|
returns[i] = row[returning];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returns;
|
|
|
|
}
|
|
|
|
if (resp.command === 'UPDATE' || resp.command === 'DELETE') {
|
|
|
|
return resp.rowCount;
|
|
|
|
}
|
|
|
|
return resp;
|
2014-04-08 16:25:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Assign the newly extended `Runner` constructor to the client object.
|
|
|
|
client.Runner = Runner_PG;
|
|
|
|
|
|
|
|
};
|