2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2013-09-13 16:58:38 -04:00
|
|
|
// Raw
|
|
|
|
// -------
|
2015-04-19 16:31:52 -04:00
|
|
|
var _ = require('lodash')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
var assign = require('lodash/object/assign');
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
function Raw(client) {
|
|
|
|
this.sql = ''
|
|
|
|
this.bindings = []
|
|
|
|
this.client = client
|
2014-03-14 16:56:55 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(Raw, EventEmitter)
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
assign(Raw.prototype, {
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
set: function(sql, bindings) {
|
|
|
|
if (sql && sql.toSQL) {
|
|
|
|
var output = sql.toSQL()
|
|
|
|
sql = output.sql
|
|
|
|
bindings = output.bindings
|
2014-08-14 18:02:13 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
this.sql = sql + ''
|
|
|
|
this.bindings = ([]).concat(bindings === undefined ? [] : bindings)
|
|
|
|
this.interpolateBindings()
|
|
|
|
this._debug = void 0
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
// Wraps the current sql with `before` and `after`.
|
|
|
|
wrap: function(before, after) {
|
|
|
|
this.sql = before + this.sql + after
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
// Calls `toString` on the Knex object.
|
|
|
|
toString: function() {
|
|
|
|
return this.toQuery()
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ensure all Raw / builder bindings are mixed-in to the ? placeholders
|
|
|
|
// as appropriate.
|
|
|
|
interpolateBindings: function() {
|
|
|
|
var replacements = []
|
|
|
|
this.bindings = _.reduce(this.bindings, function(accum, param, index) {
|
|
|
|
var innerBindings = [param]
|
|
|
|
if (param && param.toSQL) {
|
|
|
|
var result = this.splicer(param, index)
|
|
|
|
innerBindings = result.bindings
|
|
|
|
replacements.push(result.replacer)
|
|
|
|
}
|
|
|
|
return accum.concat(innerBindings)
|
|
|
|
}, [], this)
|
2014-08-14 18:02:13 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// we run this in reverse order, because ? concats earlier in the
|
|
|
|
// query string will disrupt indices for later ones
|
|
|
|
this.sql = _.reduce(replacements.reverse(), function(accum, fn) {
|
|
|
|
return fn(accum)
|
|
|
|
}, this.sql.split('?')).join('?')
|
|
|
|
},
|
2014-08-14 18:02:13 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Returns a replacer function that splices into the i'th
|
|
|
|
// ? in the sql string the inner raw's sql,
|
|
|
|
// and the bindings associated with it
|
|
|
|
splicer: function(raw, i) {
|
|
|
|
var obj = raw.toSQL()
|
2014-07-17 17:19:17 +10:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// the replacer function assumes that the sql has been
|
|
|
|
// already sql.split('?') and will be arr.join('?')
|
|
|
|
var replacer = function(arr) {
|
|
|
|
arr[i] = arr[i] + obj.sql + arr[i + 1]
|
|
|
|
arr.splice(i + 1, 1)
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
replacer: replacer,
|
|
|
|
bindings: obj.bindings
|
|
|
|
}
|
|
|
|
},
|
2014-07-17 17:19:17 +10:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Returns the raw sql for the query.
|
|
|
|
toSQL: function() {
|
|
|
|
return {
|
|
|
|
sql: this.sql,
|
|
|
|
method: 'raw',
|
|
|
|
bindings: this.bindings
|
|
|
|
}
|
|
|
|
}
|
2014-07-17 17:19:17 +10:00
|
|
|
|
2015-04-19 16:31:52 -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.
|
2015-04-19 16:31:52 -04:00
|
|
|
require('./interface')(Raw)
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
module.exports = Raw
|