knex/lib/raw.js

69 lines
1.6 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');
2014-02-21 17:16:11 -05:00
var FluentChain = require('fluent-chain');
function Raw(sql, bindings) {
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 = {};
}
}
2013-12-27 14:44:21 -05:00
// Set to know to output the original output from the query builder.
Raw.prototype = {
constructor: Raw,
_method: 'raw',
2014-02-21 17:16:11 -05: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;
},
// 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-12-27 14:44:21 -05:00
};
// Allow the `Raw` object to be utilized with full access to the relevant
// promise API.
require('./coerceable')(Raw);
module.exports = Raw;