2013-09-13 16:58:38 -04:00
|
|
|
// Raw
|
|
|
|
// -------
|
2013-11-27 16:51:01 -05:00
|
|
|
var _ = require('lodash');
|
2013-12-27 14:44:21 -05:00
|
|
|
var SqlString = require('./sqlstring');
|
2014-02-21 17:16:11 -05:00
|
|
|
var FluentChain = require('fluent-chain');
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
function Raw(sql, bindings) {
|
2014-02-22 17:06:33 -05:00
|
|
|
if (sql.toSql) {
|
2014-02-21 17:16:11 -05:00
|
|
|
var output = sql.toSql();
|
|
|
|
this.sql = output.sql;
|
|
|
|
this.bindings = output.bindings;
|
|
|
|
} else {
|
|
|
|
this.sql = sql;
|
|
|
|
this.bindings = bindings;
|
|
|
|
this.flags = {};
|
|
|
|
}
|
2014-03-14 16:56:55 -04:00
|
|
|
}
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
// Set to know to output the original output from the query builder.
|
2014-03-14 16:56:55 -04:00
|
|
|
Raw.prototype = {
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
constructor: Raw,
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
_method: 'raw',
|
2014-02-21 17:16:11 -05:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
// Set the transacting flag on the raw query, if this query is
|
|
|
|
// run independently and needs to be on the transaction.
|
|
|
|
transacting: function(t) {
|
|
|
|
this.flags.transacting = t;
|
|
|
|
return this;
|
|
|
|
},
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
// Wraps the current sql with `before` and `after`.
|
|
|
|
wrap: function(before, after) {
|
|
|
|
this.sql = before + this.sql + after;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the raw sql for the query.
|
|
|
|
toSql: function() {
|
|
|
|
return {
|
|
|
|
sql: this.sql,
|
|
|
|
bindings: this.bindings
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Turn the raw query into a string.
|
|
|
|
toQuery: function() {
|
|
|
|
return SqlString.format(this.sql, this.bindings) + ';';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Assumes the "__client" property has been injected internally,
|
|
|
|
// as is the case when called with `knex.raw`.
|
|
|
|
then: function(onFulfilled, onRejected) {
|
|
|
|
return this.__client.runThen(this).spread(onFulfilled, onRejected);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Calls `toString` on the Knex object.
|
|
|
|
toString: function() {
|
|
|
|
return '[object Knex$raw]';
|
|
|
|
}
|
2013-09-03 22:25:54 -04:00
|
|
|
|
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.
|
|
|
|
require('./coerceable')(Raw);
|
|
|
|
|
|
|
|
module.exports = Raw;
|