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
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
var Raw = module.exports = function(sql, bindings) {
|
2014-02-21 17:16:11 -05:00
|
|
|
if (sql instanceof FluentChain) {
|
|
|
|
var output = sql.toSql();
|
|
|
|
this.sql = output.sql;
|
|
|
|
this.bindings = output.bindings;
|
|
|
|
} else {
|
|
|
|
this.sql = sql;
|
|
|
|
this.bindings = bindings;
|
|
|
|
this.flags = {};
|
|
|
|
}
|
2013-11-27 16:51:01 -05: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.
|
|
|
|
Raw.prototype._method = 'raw';
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2013-12-27 14:44:21 -05: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 = function(t) {
|
|
|
|
this.flags.transacting = t;
|
|
|
|
return this;
|
|
|
|
};
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2014-02-21 17:16:11 -05:00
|
|
|
Raw.prototype.wrap = function(before, after) {
|
|
|
|
this.sql = before + this.sql + after;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
// Returns the raw sql for the query.
|
|
|
|
Raw.prototype.toSql = function() {
|
|
|
|
return {
|
|
|
|
sql: this.sql,
|
|
|
|
bindings: this.bindings
|
|
|
|
};
|
|
|
|
};
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
// Turn the raw query into a string.
|
|
|
|
Raw.prototype.toString = function() {
|
|
|
|
return SqlString.format(this.sql, this.bindings) + ';';
|
|
|
|
};
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
// Assumes the "__client" property has been injected internally.
|
|
|
|
Raw.prototype.then = function(onFulfilled, onRejected) {
|
|
|
|
return this.__client.runThen(this).spread(onFulfilled, onRejected);
|
|
|
|
};
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
// Coerce the `Raw`.
|
|
|
|
require('./coerceable')(Raw);
|