2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Raw
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
2016-05-28 19:45:00 +02:00
|
|
|
import * as helpers from './helpers';
|
2016-05-17 01:01:34 +10:00
|
|
|
import { EventEmitter } from 'events';
|
2016-09-16 16:04:11 -04:00
|
|
|
import debug from 'debug'
|
2016-03-02 16:52:32 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign, reduce, isPlainObject, isObject, isUndefined, isNumber } from 'lodash'
|
2016-09-12 18:01:47 -04:00
|
|
|
import Formatter from './formatter'
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-11-22 21:58:21 +13:00
|
|
|
import uuid from 'uuid';
|
2016-06-17 09:19:20 -07:00
|
|
|
|
2016-09-16 16:04:11 -04:00
|
|
|
const debugBindings = debug('knex:bindings')
|
|
|
|
|
2016-09-12 18:01:47 -04:00
|
|
|
const fakeClient = {
|
|
|
|
formatter() {
|
|
|
|
return new Formatter(fakeClient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Raw(client = fakeClient) {
|
2016-05-18 19:59:24 +10:00
|
|
|
this.client = client
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-18 19:59:24 +10:00
|
|
|
this.sql = ''
|
2015-05-09 13:58:18 -04:00
|
|
|
this.bindings = []
|
|
|
|
|
|
|
|
// Todo: Deprecate
|
|
|
|
this._wrappedBefore = undefined
|
2016-05-18 19:59:24 +10:00
|
|
|
this._wrappedAfter = undefined
|
|
|
|
this._debug = client && client.config && client.config.debug
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
inherits(Raw, EventEmitter)
|
|
|
|
|
|
|
|
assign(Raw.prototype, {
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
set(sql, bindings) {
|
2016-05-18 19:59:24 +10:00
|
|
|
this.sql = sql
|
2016-06-14 19:50:51 +10:00
|
|
|
this.bindings = (
|
|
|
|
(isObject(bindings) && !bindings.toSQL) ||
|
|
|
|
isUndefined(bindings)
|
|
|
|
) ? bindings : [bindings]
|
2016-01-30 14:52:07 +01:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
2016-05-26 11:06:33 -07:00
|
|
|
timeout(ms, {cancel} = {}) {
|
2016-09-12 18:01:47 -04:00
|
|
|
if (isNumber(ms) && ms > 0) {
|
2016-03-08 13:07:50 +01:00
|
|
|
this._timeout = ms;
|
2016-05-26 11:06:33 -07:00
|
|
|
if (cancel) {
|
2016-05-27 14:47:12 -07:00
|
|
|
this.client.assertCanCancelQuery();
|
|
|
|
this._cancelOnTimeout = true;
|
2016-05-26 11:06:33 -07:00
|
|
|
}
|
2016-03-08 13:07:50 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// Wraps the current sql with `before` and `after`.
|
2016-05-17 01:01:34 +10:00
|
|
|
wrap(before, after) {
|
2015-05-09 13:58:18 -04:00
|
|
|
this._wrappedBefore = before
|
2016-05-18 19:59:24 +10:00
|
|
|
this._wrappedAfter = after
|
2015-05-09 13:58:18 -04:00
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
// Calls `toString` on the Knex object.
|
2016-05-17 01:01:34 +10:00
|
|
|
toString() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return this.toQuery()
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the raw sql for the query.
|
2016-05-17 01:01:34 +10:00
|
|
|
toSQL(method, tz) {
|
2016-09-12 18:01:47 -04:00
|
|
|
let obj
|
|
|
|
const formatter = this.client.formatter()
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (Array.isArray(this.bindings)) {
|
2016-09-12 18:01:47 -04:00
|
|
|
obj = replaceRawArrBindings(this, formatter)
|
2015-11-06 16:17:50 -07:00
|
|
|
} else if (this.bindings && isPlainObject(this.bindings)) {
|
2016-09-12 18:01:47 -04:00
|
|
|
obj = replaceKeyBindings(this, formatter)
|
2015-05-09 13:58:18 -04:00
|
|
|
} else {
|
2016-09-12 18:01:47 -04:00
|
|
|
obj = {
|
2015-05-09 13:58:18 -04:00
|
|
|
method: 'raw',
|
|
|
|
sql: this.sql,
|
2016-09-12 18:01:47 -04:00
|
|
|
bindings: isUndefined(this.bindings) ? [] : [this.bindings]
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (this._wrappedBefore) {
|
2016-09-12 18:01:47 -04:00
|
|
|
obj.sql = this._wrappedBefore + obj.sql
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
if (this._wrappedAfter) {
|
2016-09-12 18:01:47 -04:00
|
|
|
obj.sql = obj.sql + this._wrappedAfter
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
|
|
|
|
obj.options = reduce(this._options, assign, {})
|
|
|
|
|
|
|
|
if (this._timeout) {
|
|
|
|
obj.timeout = this._timeout;
|
2016-05-26 11:06:33 -07:00
|
|
|
if (this._cancelOnTimeout) {
|
2016-09-12 18:01:47 -04:00
|
|
|
obj.cancelOnTimeout = this._cancelOnTimeout;
|
2016-05-26 11:06:33 -07:00
|
|
|
}
|
2016-03-08 13:07:50 +01:00
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
|
|
|
|
obj.bindings = obj.bindings || [];
|
|
|
|
if (helpers.containsUndefined(obj.bindings)) {
|
2016-09-16 16:04:11 -04:00
|
|
|
debugBindings(obj.bindings)
|
2016-09-12 18:01:47 -04:00
|
|
|
throw new Error(
|
|
|
|
`Undefined binding(s) detected when compiling RAW query: ` +
|
|
|
|
obj.sql
|
|
|
|
);
|
2016-03-15 21:33:39 +01:00
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
|
|
|
|
obj.__knexQueryUid = uuid.v4();
|
|
|
|
|
|
|
|
return obj
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-09-12 18:01:47 -04:00
|
|
|
function replaceRawArrBindings(raw, formatter) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const expectedBindings = raw.bindings.length
|
2016-05-18 19:59:24 +10:00
|
|
|
const values = raw.bindings
|
|
|
|
let index = 0;
|
2015-11-13 21:01:01 +02:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql = raw.sql.replace(/\\?\?\??/g, function(match) {
|
2015-11-13 21:01:01 +02:00
|
|
|
if (match === '\\?') {
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const value = values[index++]
|
2016-03-08 08:41:13 +01:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (match === '??') {
|
2016-09-12 18:01:47 -04:00
|
|
|
return formatter.columnize(value)
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
return formatter.parameter(value)
|
2015-05-09 13:58:18 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if (expectedBindings !== index) {
|
2016-05-17 01:01:34 +10:00
|
|
|
throw new Error(`Expected ${expectedBindings} bindings, saw ${index}`)
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
method: 'raw',
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
2016-09-12 18:01:47 -04:00
|
|
|
bindings: formatter.bindings
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:01:47 -04:00
|
|
|
function replaceKeyBindings(raw, formatter) {
|
2016-05-18 19:59:24 +10:00
|
|
|
const values = raw.bindings
|
2016-09-12 18:01:47 -04:00
|
|
|
|
|
|
|
let { sql } = raw
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-07-19 11:42:49 -06:00
|
|
|
const regex = /\\?(:\w+:?)/g
|
|
|
|
sql = raw.sql.replace(regex, function(full, part) {
|
|
|
|
if (full !== part) {
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const key = full.trim();
|
|
|
|
const isIdentifier = key[key.length - 1] === ':'
|
|
|
|
const value = isIdentifier ? values[key.slice(1, -1)] : values[key.slice(1)]
|
2016-09-12 18:01:47 -04:00
|
|
|
|
2016-03-07 16:31:39 +01:00
|
|
|
if (value === undefined) {
|
2016-09-12 18:01:47 -04:00
|
|
|
formatter.bindings.push(value);
|
2016-03-07 16:31:39 +01:00
|
|
|
return full;
|
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (isIdentifier) {
|
2016-09-12 18:01:47 -04:00
|
|
|
return full.replace(key, formatter.columnize(value))
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2016-09-12 18:01:47 -04:00
|
|
|
return full.replace(key, formatter.parameter(value))
|
2015-05-09 13:58:18 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
method: 'raw',
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
2016-09-12 18:01:47 -04:00
|
|
|
bindings: formatter.bindings
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the `Raw` object to be utilized with full access to the relevant
|
|
|
|
// promise API.
|
|
|
|
require('./interface')(Raw)
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default Raw
|