knex/lib/query/compiler.js

373 lines
12 KiB
JavaScript
Raw Normal View History

// Query Compiler
// -------
var _ = require('lodash');
var helpers = require('../helpers');
var Raw = require('../raw');
// The "QueryCompiler" takes all of the query statements which have been
// gathered in the "QueryBuilder" and turns them into a properly formatted / bound
// query string.
2014-04-27 19:36:40 -04:00
function QueryCompiler(queryBuilder) {
2014-04-15 13:10:32 -04:00
this.method = queryBuilder._method || 'select';
this.options = queryBuilder._options;
this.single = queryBuilder._single;
this.transacting = queryBuilder._transacting;
this.grouped = _.groupBy(queryBuilder._statements, 'grouping');
this.tableName = this.single.table ? this.formatter.wrap(this.single.table) : '';
2014-04-27 19:36:40 -04:00
}
// Collapse the builder into a single object
2014-04-09 10:11:41 -04:00
QueryCompiler.prototype.toSQL = function(method) {
2014-04-15 13:10:32 -04:00
var val = this[method]();
var defaults = {
2014-04-09 10:11:41 -04:00
method: method,
options: this.options && this.options.length > 0 ?
_.extend.apply(_, this.options) : void 0,
bindings: this.formatter.bindings
2013-12-27 14:44:21 -05:00
};
2014-04-15 13:10:32 -04:00
if (_.isString(val)) {
val = {sql: val};
}
return _.extend(defaults, val);
2014-04-09 10:11:41 -04:00
};
2013-12-27 14:44:21 -05:00
var components = [
'columns', 'join', 'where', 'union', 'group',
'having', 'order', 'limit', 'offset', 'lock'
];
2013-12-27 14:44:21 -05:00
// Compiles the `select` statement, or nested sub-selects
// by calling each of the component compilers, trimming out
// the empties, and returning a generated query string.
QueryCompiler.prototype.select = function() {
var statements = [];
for (var i = 0, l = components.length; i < l; i++) {
var component = components[i];
statements.push(this[component](this));
}
return _.compact(statements).join(' ');
2014-04-09 10:11:41 -04:00
};
QueryCompiler.prototype.first = QueryCompiler.prototype.select;
2014-06-04 16:24:29 -04:00
QueryCompiler.prototype.pluck = function() {
return {
sql: this.select(),
pluck: this.single.pluck
};
};
// Compiles an "insert" query, allowing for multiple
// inserts using a single query statement.
QueryCompiler.prototype.insert = function() {
2014-04-16 01:23:50 -04:00
var sql = 'insert into ' + this.tableName + ' ';
if (_.isEmpty(this.single.insert)) {
sql += this._emptyInsertValue;
} else {
var insertData = this._prepInsert(this.single.insert);
if (_.isString(insertData)) {
sql += insertData;
} else {
sql += '(' + this.formatter.columnize(insertData.columns) + ') values (' +
_.map(insertData.values, this.formatter.parameterize, this.formatter).join('), (') + ')';
}
}
return sql;
};
// Compiles the "update" query.
QueryCompiler.prototype.update = function() {
obj = helpers.sortObject(obj);
var vals = [];
for (var i = 0; i < obj.length; i++) {
var value = obj[i];
vals.push(this.formatter.wrap(value[0]) + ' = ' + this.formatter.parameter(value[1]));
}
if (!_.isEmpty(ret)) this.returning(ret);
return {
grouping: 'update',
columns: vals.join(', ')
};
2014-04-09 10:11:41 -04:00
};
// Compiles the columns in the query, specifying if an item was distinct.
QueryCompiler.prototype.columns = function() {
var distinct = false;
if (this.onlyUnions()) return '';
var columns = this.grouped.columns || [];
var sql = [];
if (columns) {
for (var i = 0, l = columns.length; i < l; i++) {
var stmt = columns[i];
if (stmt.distinct) distinct = true;
if (stmt.type === 'aggregate') {
sql.push(this.aggregate(stmt));
} else if (stmt.value && stmt.value.length > 0) {
sql.push(this.formatter.columnize(stmt.value));
2013-12-27 14:44:21 -05:00
}
}
}
2014-04-16 01:23:50 -04:00
if (sql.length === 0) sql.push('*');
return 'select ' + (distinct ? 'distinct ' : '') +
(sql.join(', ') || '*') + (this.tableName ? ' from ' + this.tableName : '');
2014-04-09 10:11:41 -04:00
};
QueryCompiler.prototype.aggregate = function(stmt) {
var val = stmt.value;
var splitOn = val.toLowerCase().indexOf(' as ');
// Allows us to speciy an alias for the aggregate types.
if (splitOn !== -1) {
var col = val.slice(0, splitOn);
var alias = val.slice(splitOn + 4);
return stmt.method + '(' + this.formatter.wrap(col) + ') as ' + this.formatter.wrap(alias);
}
return stmt.method + '(' + this.formatter.wrap(val) + ')';
2014-04-09 10:11:41 -04:00
};
// Compiles all each of the `join` clauses on the query,
// including any nested join queries.
QueryCompiler.prototype.join = function() {
var joins = this.grouped.join;
if (!joins) return '';
var sql = [];
for (var i = 0, l = joins.length; i < l; i++) {
var stmt = joins[i];
var str = stmt.joinType + ' join ' + this.formatter.wrap(stmt.table);
for (var i2 = 0, l2 = stmt.clauses.length; i2 < l2; i2++) {
var clause = stmt.clauses[i2];
if (i2 > 0) {
str += ' ' + clause[1] + ' ';
} else {
str += ' on ';
2013-12-27 14:44:21 -05:00
}
str += this.formatter.wrap(clause[2]) + ' ' + this.formatter.operator(clause[3]) +
' ' + this.formatter.wrap(clause[4]);
2013-12-27 14:44:21 -05:00
}
sql.push(str);
}
return sql.length > 0 ? sql.join(' ') : '';
2014-04-09 10:11:41 -04:00
};
// Compiles all `where` statements on the query.
QueryCompiler.prototype.where = function() {
var wheres = this.grouped.where;
if (!wheres) return;
var sql = [];
sql[0] = 'where';
for (var i = 0, l = wheres.length; i < l; i++) {
var stmt = wheres[i];
if (i !== 0) sql.push(stmt.bool);
sql.push(this[stmt.type](stmt));
}
return sql.length > 1 ? sql.join(' ') : '';
2014-04-09 10:11:41 -04:00
};
QueryCompiler.prototype.group = function() {
return this._groupsOrders('group');
2014-04-09 10:11:41 -04:00
};
QueryCompiler.prototype.order = function() {
return this._groupsOrders('order');
2014-04-09 10:11:41 -04:00
};
// Compiles the `having` statements.
QueryCompiler.prototype.having = function() {
var havings = this.grouped.having;
if (!havings) return '';
var sql = ['having'];
for (var i = 0, l = havings.length; i < l; i++) {
var str = '', s = havings[i];
if (i !== 0) str = s.bool + ' ';
if (s.type === 'havingBasic') {
sql.push(str + this.formatter.columnize(s.column) + ' ' +
this.formatter.operator(s.operator) + ' ' + this.formatter.parameter(s.value));
} else {
sql.push(str + this.formatter.checkRaw(s.value));
}
}
return sql.length > 1 ? sql.join(' ') : '';
2014-04-09 10:11:41 -04:00
};
// Compile the "union" queries attached to the main query.
QueryCompiler.prototype.union = function() {
var onlyUnions = this.onlyUnions();
var unions = this.grouped.union;
if (!unions) return '';
var sql = '';
for (var i = 0, l = unions.length; i < l; i++) {
var union = unions[i];
if (i > 0) sql += ' ';
if (i > 0 || !onlyUnions) sql += union.clause + ' ';
sql += this.formatter.rawOrFn(union.value, union.wrap);
}
return sql;
2014-04-09 10:11:41 -04:00
};
2013-12-27 14:44:21 -05:00
// If we haven't specified any columns or a `tableName`, we're assuming this
// is only being used for unions.
QueryCompiler.prototype.onlyUnions = function() {
return (!this.grouped.columns && this.grouped.union && !this.tableName);
2014-04-09 10:11:41 -04:00
};
QueryCompiler.prototype.limit = function() {
2014-04-15 13:10:32 -04:00
if (this.single.limit == void 0) return '';
return 'limit ' + this.formatter.parameter(this.single.limit);
2014-04-09 10:11:41 -04:00
};
QueryCompiler.prototype.offset = function() {
2014-04-15 13:10:32 -04:00
if (this.single.offset == void 0) return '';
return 'offset ' + this.formatter.parameter(this.single.offset);
2014-04-09 10:11:41 -04:00
};
// Compiles a `delete` query.
2014-04-16 02:59:27 -04:00
QueryCompiler.prototype.del = function() {
var wheres = this.where();
2014-04-15 13:10:32 -04:00
return 'delete from ' + this.tableName +
(wheres ? ' ' + wheres : '');
2014-04-09 10:11:41 -04:00
};
// Compiles a `truncate` query.
QueryCompiler.prototype.truncate = function() {
return 'truncate ' + this.tableName;
2014-04-09 10:11:41 -04:00
};
2014-04-15 13:10:32 -04:00
// Compiles the "locks".
QueryCompiler.prototype.lock = function() {
2014-04-16 10:41:16 -04:00
if (this.single.lock) {
if (!this.transacting) {
helpers.warn('You are attempting to perform a "lock" command outside of a transaction.');
} else {
return this[this.single.lock]();
}
2014-04-15 13:10:32 -04:00
}
2014-04-09 10:11:41 -04:00
};
2014-04-16 01:23:50 -04:00
// Compile the "counter".
QueryCompiler.prototype.counter = function() {
var counter = this.single.counter;
var toUpdate = {};
toUpdate[counter.column] = new Raw(this.formatter.wrap(counter.column) +
' ' + (counter.symbol || '+') +
' ' + counter.amount);
this.single.update = toUpdate;
return this.update();
};
// Compiles the `order by` statements.
QueryCompiler.prototype._groupsOrders = function(type) {
var items = this.grouped[type];
if (!items) return '';
var sql = [];
for (var i = 0, l = items.length; i < l; i++) {
var item = items[i];
var str = this.formatter.columnize(item.value);
if (type === 'order') {
str += ' ' + this.formatter.direction(item.direction);
}
sql.push(str);
}
return sql.length > 0 ? type + ' by ' + sql.join(', ') : '';
};
// Where Clause
// ------
QueryCompiler.prototype.whereIn = function(statement) {
2014-04-16 02:50:19 -04:00
if (_.isArray(statement.column)) return this.multiWhereIn(statement);
return this.formatter.wrap(statement.column) + ' ' + this._not(statement, 'in ') +
this.wrap(this.formatter.parameterize(statement.value));
};
2014-04-16 02:50:19 -04:00
QueryCompiler.prototype.multiWhereIn = function(statement) {
return '(' + _.map(statement.column, this.formatter.wrap, this.formatter) + ') ' +
this._not(statement, 'in ') + '((' +
_.map(statement.value, this.formatter.parameterize, this.formatter).join('),(') + '))';
};
QueryCompiler.prototype.whereNull = function(statement) {
return this.formatter.wrap(statement.column) + ' is ' + this._not(statement, 'null');
};
// Compiles a basic "where" clause.
QueryCompiler.prototype.whereBasic = function(statement) {
return this.formatter.wrap(statement.column) + ' ' +
this.formatter.operator(statement.operator) + ' ' +
this.formatter.parameter(statement.value);
};
QueryCompiler.prototype.whereExists = function(statement) {
return this._not(statement, 'exists') + ' (' + this.formatter.compileCallback(statement.value) + ')';
};
QueryCompiler.prototype.whereWrapped = function(statement) {
return '(' + this.formatter.compileCallback(statement.value, 'where').slice(6) + ')';
};
QueryCompiler.prototype.whereBetween = function(statement) {
return this.formatter.wrap(statement.column) + ' ' + this._not(statement, 'between') + ' ' +
2014-04-16 01:23:50 -04:00
_.map(statement.value, this.formatter.parameter, this.formatter).join(' and ');
};
// Compiles a "whereRaw" query.
QueryCompiler.prototype.whereRaw = function(statement) {
return this.formatter.checkRaw(statement.value);
};
QueryCompiler.prototype.wrap = function(str) {
if (str.charAt(0) !== '(') return '(' + str + ')';
return str;
};
// Determines whether to add a "not" prefix to the where clause.
QueryCompiler.prototype._not = function(statement, str) {
2014-04-15 13:10:32 -04:00
if (statement.not) return 'not ' + str;
return str;
};
// "Preps" the insert.
QueryCompiler.prototype._prepInsert = function(data) {
2014-04-15 13:10:32 -04:00
var isRaw = this.formatter.rawOrFn(data);
if (isRaw) return isRaw;
var values = [];
var columns, colList;
if (!_.isArray(data)) data = data ? [data] : [];
for (var i = 0, l = data.length; i<l; i++) {
var sorted = helpers.sortObject(data[i]);
columns = _.pluck(sorted, 0);
colList = colList || columns;
if (!_.isEqual(columns, colList)) {
return this._prepInsert(this._normalizeInsert(data));
}
values.push(_.pluck(sorted, 1));
}
return {
columns: columns,
values: values
2014-04-09 10:11:41 -04:00
};
};
2013-12-27 14:44:21 -05:00
// If we are running an insert with variable object keys, we need to normalize
// for the missing keys, presumably setting the values to undefined.
QueryCompiler.prototype._normalizeInsert = function(data) {
if (!_.isArray(data)) return _.clone(data);
var defaultObj = _.reduce(_.union.apply(_, _.map(data, function(val) {
return _.keys(val);
})), function(memo, key) {
memo[key] = void 0;
return memo;
}, {});
return _.map(data, function(row) { return _.defaults(row, defaultObj); });
};
// "Preps" the update.
QueryCompiler.prototype._prepUpdate = function(data) {
var vals = [];
var sorted = helpers.sortObject(data);
2014-04-16 01:23:50 -04:00
for (var i = 0, l = sorted.length; i < l; i++) {
vals.push(this.formatter.wrap(sorted[i][0]) + ' = ' + this.formatter.parameter(sorted[i][1]));
2014-04-15 13:10:32 -04:00
}
return vals;
};
2013-12-27 14:44:21 -05:00
2014-04-16 03:49:25 -04:00
module.exports = QueryCompiler;