2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-08 13:09:03 +03:00
|
|
|
var assert = require('assert');
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
module.exports = function(Target) {
|
2014-04-15 11:43:47 -04:00
|
|
|
var _ = require('lodash');
|
2014-05-28 01:11:20 -04:00
|
|
|
var SqlString = require('./dialects/mysql/string');
|
2014-04-09 10:11:41 -04:00
|
|
|
|
2014-05-06 18:15:16 -04:00
|
|
|
Target.prototype.toQuery = function(tz) {
|
2014-04-15 11:43:47 -04:00
|
|
|
var data = this.toSQL(this._method);
|
2014-05-06 18:15:16 -04:00
|
|
|
if (this._errors && this._errors.length > 0) throw this._errors[0];
|
2014-04-15 11:43:47 -04:00
|
|
|
if (!_.isArray(data)) data = [data];
|
|
|
|
return _.map(data, function(statement) {
|
2014-05-06 18:15:16 -04:00
|
|
|
return this._formatQuery(statement.sql, statement.bindings, tz);
|
|
|
|
}, this).join(';\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
// Format the query as sql, prepping bindings as necessary.
|
|
|
|
Target.prototype._formatQuery = function(sql, bindings, tz) {
|
|
|
|
if (this.client && this.client.prepBindings) {
|
|
|
|
bindings = this.client.prepBindings(bindings, tz);
|
|
|
|
}
|
|
|
|
return SqlString.format(sql, bindings, true, tz);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Create a new instance of the `Runner`, passing in the current object.
|
2015-02-07 12:20:24 -05:00
|
|
|
Target.prototype.then = function(/* onFulfilled, onRejected */) {
|
2014-04-15 11:43:47 -04:00
|
|
|
var Runner = this.client.Runner;
|
2015-02-07 12:20:24 -05:00
|
|
|
var result = new Runner(this).run();
|
|
|
|
return result.then.apply(result, arguments);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Add additional "options" to the builder. Typically used for client specific
|
|
|
|
// items, like the `mysql` and `sqlite3` drivers.
|
|
|
|
Target.prototype.options = function(opts) {
|
|
|
|
this._options = this._options || [];
|
2014-06-14 16:26:01 -04:00
|
|
|
this._options.push(_.clone(opts) || {});
|
2014-04-15 11:43:47 -04:00
|
|
|
return this;
|
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Sets an explicit "connnection" we wish to use for this query.
|
|
|
|
Target.prototype.connection = function(connection) {
|
|
|
|
this._connection = connection;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set a debug flag for the current schema query stack.
|
2015-03-08 13:09:03 +03:00
|
|
|
Target.prototype.debug = function(enabled) {
|
|
|
|
assert(!arguments.length || typeof enabled === 'boolean', 'debug requires a boolean');
|
|
|
|
this._debug = arguments.length ? enabled : true;
|
2014-04-15 11:43:47 -04:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set the transaction object for this query.
|
|
|
|
Target.prototype.transacting = function(t) {
|
|
|
|
this._transacting = t;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initializes a stream.
|
|
|
|
Target.prototype.stream = function(options) {
|
|
|
|
var Runner = this.client.Runner;
|
|
|
|
return new Runner(this).stream(options);
|
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-06-03 14:21:31 -04:00
|
|
|
// Initialize a stream & pipe automatically.
|
|
|
|
Target.prototype.pipe = function(writable) {
|
|
|
|
var Runner = this.client.Runner;
|
|
|
|
return new Runner(this).pipe(writable);
|
|
|
|
};
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// Creates a method which "coerces" to a promise, by calling a
|
|
|
|
// "then" method on the current `Target`
|
2014-06-23 19:00:21 -04:00
|
|
|
_.each(['bind', 'catch', 'spread', 'otherwise', 'map', 'reduce', 'tap', 'thenReturn',
|
2014-04-15 11:43:47 -04:00
|
|
|
'return', 'yield', 'ensure', 'nodeify', 'exec'], function(method) {
|
|
|
|
Target.prototype[method] = function() {
|
|
|
|
var then = this.then();
|
|
|
|
return then[method].apply(then, arguments);
|
2014-04-09 10:11:41 -04:00
|
|
|
};
|
2014-04-15 11:43:47 -04:00
|
|
|
});
|
2014-04-09 10:11:41 -04:00
|
|
|
|
2015-02-07 12:20:24 -05:00
|
|
|
};
|