knex/lib/formatter.js

125 lines
3.6 KiB
JavaScript
Raw Normal View History

const Raw = require('./raw');
const {
columnize: columnize_,
wrap: wrap_,
unwrapRaw: unwrapRaw_,
operator: operator_,
outputQuery: outputQuery_,
rawOrFn: rawOrFn_,
} = require('./formatter/wrappingFormatter');
const {
compileCallback: compileCallback_,
} = require('./formatter/formatterUtils');
2015-05-09 13:58:18 -04:00
// Valid values for the `order by` clause generation.
const orderBys = ['asc', 'desc'];
class Formatter {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
constructor(client, builder) {
this.client = client;
this.builder = builder;
this.bindings = [];
2016-09-12 18:26:49 -04:00
}
2015-05-09 13:58:18 -04:00
// Accepts a string or array of columns to wrap as appropriate.
columnize(target) {
return columnize_(target, this.builder, this.client, this);
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 ')
parameterize(values, notSetValue) {
2015-05-09 13:58:18 -04:00
if (typeof values === 'function') return this.parameter(values);
values = Array.isArray(values) ? values : [values];
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]);
2015-05-09 13:58:18 -04:00
}
return str;
2016-09-12 18:26:49 -04:00
}
2015-05-09 13:58:18 -04:00
// Formats `values` into a parenthesized list of parameters for a `VALUES`
// clause.
//
// [1, 2] -> '(?, ?)'
// [[1, 2], [3, 4]] -> '((?, ?), (?, ?))'
// knex('table') -> '(select * from "table")'
// knex.raw('select ?', 1) -> '(select ?)'
//
values(values) {
if (Array.isArray(values)) {
if (Array.isArray(values[0])) {
return `(${values
.map((value) => `(${this.parameterize(value)})`)
.join(', ')})`;
}
return `(${this.parameterize(values)})`;
}
if (values instanceof Raw) {
return `(${this.parameter(values)})`;
}
return this.parameter(values);
}
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
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
unwrapRaw(value, isParameter) {
return unwrapRaw_(value, isParameter, this.builder, this.client, this);
2016-09-12 18:26:49 -04:00
}
2015-05-09 13:58:18 -04:00
/**
* Creates SQL for a parameter, which might be passed to where() or .with() or
* pretty much anywhere in API.
*
* @param value Callback (for where or complete builder), Raw or QueryBuilder
* @param method Optional at least 'select' or 'update' are valid
*/
rawOrFn(value, method) {
return rawOrFn_(value, method, this.builder, this.client, this);
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.
wrap(value, isParameter) {
return wrap_(value, isParameter, this.builder, this.client, this);
2016-09-12 18:26:49 -04: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
operator(value) {
return operator_(value, this.builder, this.client, this);
2016-09-12 18:26:49 -04:00
}
2015-05-09 13:58:18 -04:00
// Specify the direction of the ordering.
direction(value) {
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.
compileCallback(callback, method) {
return compileCallback_(callback, method, this.client, this);
2016-09-12 18:26:49 -04:00
}
2015-05-09 13:58:18 -04:00
// Ensures the query is aliased if necessary.
outputQuery(compiled, isParameter) {
return outputQuery_(compiled, isParameter, this.builder, this.client, this);
}
2016-09-12 18:26:49 -04:00
}
module.exports = Formatter;