2016-05-17 01:01:34 +10:00
|
|
|
import QueryBuilder from './query/builder';
|
|
|
|
import Raw from './raw';
|
2016-03-02 16:52:32 +01:00
|
|
|
|
2016-09-12 18:26:49 -04:00
|
|
|
import { transform } from 'lodash'
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
// Valid values for the `order by` clause generation.
|
2016-05-18 19:59:24 +10:00
|
|
|
const orderBys = ['asc', 'desc'];
|
2016-05-17 01:01:34 +10:00
|
|
|
|
|
|
|
// Turn this into a lookup map
|
|
|
|
const operators = transform([
|
|
|
|
'=', '<', '>', '<=', '>=', '<>', '!=', 'like',
|
2017-08-24 04:09:57 -04:00
|
|
|
'not like', 'between', 'ilike', 'not ilike', '&', '|', '^', '<<', '>>',
|
2016-05-17 01:01:34 +10:00
|
|
|
'rlike', 'regexp', 'not regexp', '~', '~*', '!~', '!~*',
|
|
|
|
'#', '&&', '@>', '<@', '||'
|
|
|
|
], (result, key) => {
|
|
|
|
result[key] = true
|
|
|
|
}, {});
|
|
|
|
|
2016-09-12 18:26:49 -04:00
|
|
|
export default class Formatter {
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-09-12 18:26:49 -04:00
|
|
|
constructor(client) {
|
|
|
|
this.client = client
|
|
|
|
this.bindings = []
|
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Accepts a string or array of columns to wrap as appropriate.
|
2016-05-06 13:28:50 +10:00
|
|
|
columnize(target) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const columns = typeof target === 'string' ? [target] : target
|
|
|
|
let str = '', i = -1;
|
2015-05-09 13:58:18 -04:00
|
|
|
while (++i < columns.length) {
|
|
|
|
if (i > 0) str += ', '
|
|
|
|
str += this.wrap(columns[i])
|
|
|
|
}
|
|
|
|
return str
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Turns a list of values into a list of ?'s, joining them with commas unless
|
|
|
|
// a "joining" value is specified (e.g. ' and ')
|
2016-05-06 13:28:50 +10:00
|
|
|
parameterize(values, notSetValue) {
|
2015-05-09 13:58:18 -04:00
|
|
|
if (typeof values === 'function') return this.parameter(values);
|
2016-05-18 19:59:24 +10:00
|
|
|
values = Array.isArray(values) ? values : [values];
|
2016-05-17 01:01:34 +10:00
|
|
|
let str = '', i = -1;
|
2015-05-09 13:58:18 -04:00
|
|
|
while (++i < values.length) {
|
|
|
|
if (i > 0) str += ', '
|
|
|
|
str += this.parameter(values[i] === undefined ? notSetValue : values[i])
|
|
|
|
}
|
|
|
|
return str;
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Checks whether a value is a function... if it is, we compile it
|
|
|
|
// otherwise we check whether it's a raw
|
2016-05-06 13:28:50 +10:00
|
|
|
parameter(value) {
|
2015-05-09 13:58:18 -04:00
|
|
|
if (typeof value === 'function') {
|
|
|
|
return this.outputQuery(this.compileCallback(value), true);
|
|
|
|
}
|
|
|
|
return this.unwrapRaw(value, true) || '?';
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-06 13:28:50 +10:00
|
|
|
unwrapRaw(value, isParameter) {
|
2016-05-17 01:01:34 +10:00
|
|
|
let query;
|
2015-05-09 13:58:18 -04:00
|
|
|
if (value instanceof QueryBuilder) {
|
|
|
|
query = this.client.queryCompiler(value).toSQL()
|
|
|
|
if (query.bindings) {
|
|
|
|
this.bindings = this.bindings.concat(query.bindings);
|
|
|
|
}
|
|
|
|
return this.outputQuery(query, isParameter);
|
|
|
|
}
|
|
|
|
if (value instanceof Raw) {
|
2015-11-13 20:35:17 +02:00
|
|
|
value.client = this.client;
|
2015-05-09 13:58:18 -04:00
|
|
|
query = value.toSQL()
|
|
|
|
if (query.bindings) {
|
|
|
|
this.bindings = this.bindings.concat(query.bindings);
|
|
|
|
}
|
|
|
|
return query.sql
|
|
|
|
}
|
|
|
|
if (isParameter) {
|
|
|
|
this.bindings.push(value);
|
|
|
|
}
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-06 13:28:50 +10:00
|
|
|
rawOrFn(value, method) {
|
2015-05-09 13:58:18 -04:00
|
|
|
if (typeof value === 'function') {
|
|
|
|
return this.outputQuery(this.compileCallback(value, method));
|
|
|
|
}
|
|
|
|
return this.unwrapRaw(value) || '';
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Puts the appropriate wrapper around a value depending on the database
|
|
|
|
// engine, unless it's a knex.raw value, in which case it's left alone.
|
2016-05-06 13:28:50 +10:00
|
|
|
wrap(value) {
|
2015-05-09 13:58:18 -04:00
|
|
|
if (typeof value === 'function') {
|
|
|
|
return this.outputQuery(this.compileCallback(value), true);
|
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
const raw = this.unwrapRaw(value);
|
2015-05-09 13:58:18 -04:00
|
|
|
if (raw) return raw;
|
|
|
|
if (typeof value === 'number') return value;
|
|
|
|
return this._wrapString(value + '');
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-06 13:28:50 +10:00
|
|
|
wrapAsIdentifier(value) {
|
2016-02-02 14:46:18 -03:00
|
|
|
return this.client.wrapIdentifier((value || '').trim());
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2016-02-02 14:46:18 -03:00
|
|
|
|
2016-05-06 13:28:50 +10:00
|
|
|
alias(first, second) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return first + ' as ' + second;
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// The operator method takes a value and returns something or other.
|
2016-05-06 13:28:50 +10:00
|
|
|
operator(value) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const raw = this.unwrapRaw(value);
|
2015-05-09 13:58:18 -04:00
|
|
|
if (raw) return raw;
|
|
|
|
if (operators[(value || '').toLowerCase()] !== true) {
|
2016-05-17 01:01:34 +10:00
|
|
|
throw new TypeError(`The operator "${value}" is not permitted`);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
return value;
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Specify the direction of the ordering.
|
2016-05-06 13:28:50 +10:00
|
|
|
direction(value) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const raw = this.unwrapRaw(value);
|
2015-05-09 13:58:18 -04:00
|
|
|
if (raw) return raw;
|
|
|
|
return orderBys.indexOf((value || '').toLowerCase()) !== -1 ? value : 'asc';
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Compiles a callback using the query builder.
|
2016-05-06 13:28:50 +10:00
|
|
|
compileCallback(callback, method) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const { client } = this;
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Build the callback
|
2016-05-18 19:59:24 +10:00
|
|
|
const builder = client.queryBuilder();
|
2015-05-09 13:58:18 -04:00
|
|
|
callback.call(builder, builder);
|
|
|
|
|
|
|
|
// Compile the callback, using the current formatter (to track all bindings).
|
2016-05-17 01:01:34 +10:00
|
|
|
const compiler = client.queryCompiler(builder);
|
2015-05-09 13:58:18 -04:00
|
|
|
compiler.formatter = this;
|
|
|
|
|
|
|
|
// Return the compiled & parameterized sql.
|
|
|
|
return compiler.toSQL(method || 'select');
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Ensures the query is aliased if necessary.
|
2016-05-06 13:28:50 +10:00
|
|
|
outputQuery(compiled, isParameter) {
|
2016-05-17 01:01:34 +10:00
|
|
|
let sql = compiled.sql || '';
|
2015-05-09 13:58:18 -04:00
|
|
|
if (sql) {
|
2016-11-18 13:43:39 +02:00
|
|
|
if (
|
|
|
|
(compiled.method === 'select' || compiled.method === 'first') &&
|
|
|
|
(isParameter || compiled.as)
|
|
|
|
) {
|
2016-05-17 01:01:34 +10:00
|
|
|
sql = `(${sql})`;
|
2015-05-09 13:58:18 -04:00
|
|
|
if (compiled.as) return this.alias(sql, this.wrap(compiled.as))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sql;
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Coerce to string to prevent strange errors when it's not a string.
|
2016-05-06 13:28:50 +10:00
|
|
|
_wrapString(value) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const asIndex = value.toLowerCase().indexOf(' as ');
|
2015-05-09 13:58:18 -04:00
|
|
|
if (asIndex !== -1) {
|
2016-05-18 19:59:24 +10:00
|
|
|
const first = value.slice(0, asIndex)
|
2016-05-17 01:01:34 +10:00
|
|
|
const second = value.slice(asIndex + 4)
|
2016-02-02 14:46:18 -03:00
|
|
|
return this.alias(this.wrap(first), this.wrapAsIdentifier(second))
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
const wrapped = [];
|
|
|
|
let i = -1;
|
|
|
|
const segments = value.split('.');
|
2015-05-09 13:58:18 -04:00
|
|
|
while (++i < segments.length) {
|
|
|
|
value = segments[i];
|
|
|
|
if (i === 0 && segments.length > 1) {
|
|
|
|
wrapped.push(this.wrap((value || '').trim()));
|
|
|
|
} else {
|
|
|
|
wrapped.push(this.client.wrapIdentifier((value || '').trim()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrapped.join('.');
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:26:49 -04:00
|
|
|
}
|