knex/lib/raw.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

2013-09-13 16:58:38 -04:00
// Raw
// -------
var _ = require('lodash');
2013-12-27 14:44:21 -05:00
var SqlString = require('./sqlstring');
2013-12-27 14:44:21 -05:00
var Raw = module.exports = function(sql, bindings) {
this.sql = sql;
this.bindings = bindings;
this.flags = {};
};
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-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-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-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-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-12-27 14:44:21 -05:00
// Coerce the `Raw`.
require('./coerceable')(Raw);