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