2013-09-13 16:58:38 -04:00
|
|
|
// Raw
|
|
|
|
// -------
|
2013-12-27 14:44:21 -05:00
|
|
|
var SqlString = require('./sqlstring');
|
2014-04-08 16:25:57 -04:00
|
|
|
var shared = require('./shared');
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
function Raw(sql, bindings) {
|
2014-04-08 16:25:57 -04:00
|
|
|
this.sql = sql;
|
|
|
|
this.bindings = bindings;
|
|
|
|
this._debug = void 0;
|
|
|
|
this._transacting = void 0;
|
2014-03-14 16:56:55 -04:00
|
|
|
}
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Set to know the original source of the request for
|
|
|
|
// post-processing.
|
|
|
|
Raw.prototype._method = 'raw',
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Set the transacting flag on the raw query, if this query is
|
|
|
|
// run independently and needs to be on the transaction.
|
|
|
|
Raw.prototype.transacting = shared.transacting;
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
Raw.prototype.debug = shared.debug;
|
2014-02-21 17:16:11 -05:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Wraps the current sql with `before` and `after`.
|
|
|
|
Raw.prototype.wrap = function(before, after) {
|
|
|
|
this.sql = before + this.sql + after;
|
|
|
|
return this;
|
|
|
|
};
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Turn the raw query into a string.
|
|
|
|
Raw.prototype.toQuery = function() {
|
|
|
|
return SqlString.format(this.sql, this.bindings) + ';';
|
|
|
|
};
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Calls `toString` on the Knex object.
|
|
|
|
// return '[object Knex$raw]';
|
|
|
|
Raw.prototype.toString = function() {
|
|
|
|
return this.toQuery();
|
|
|
|
};
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Returns the raw sql for the query.
|
|
|
|
Raw.prototype.toSql = function() {
|
|
|
|
return {
|
|
|
|
sql: this.sql,
|
|
|
|
bindings: this.bindings
|
|
|
|
};
|
|
|
|
};
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Call `runThen` on the client, processing the query and calling the `onFulfilled`
|
|
|
|
// and `onRejected` as defined
|
|
|
|
Raw.prototype.then = function(onFulfilled, onRejected) {
|
|
|
|
return new this.client.Runner(this).then(onFulfilled, onRejected);
|
|
|
|
};
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Call `spread` rather than `then`.
|
|
|
|
Raw.prototype.spread = function(onFulfilled, onRejected) {
|
|
|
|
return new this.client.Runner(this).spread(onFulfilled, onRejected);
|
2013-12-27 14:44:21 -05:00
|
|
|
};
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
// Allow the `Raw` object to be utilized with full access to the relevant
|
|
|
|
// promise API.
|
2014-04-08 16:25:57 -04:00
|
|
|
require('./interface')(Raw);
|
2014-03-14 16:56:55 -04:00
|
|
|
|
|
|
|
module.exports = Raw;
|