mirror of
https://github.com/knex/knex.git
synced 2025-12-27 15:08:47 +00:00
Fix #282, add raw support in groupBy / orderBy
Adds groupByRaw, orderByRaw as convenience methods for groupBy(raw) / orderBy(raw)
This commit is contained in:
parent
4edf47dfb4
commit
295f57d296
@ -22,9 +22,6 @@ function QueryBuilder() {
|
||||
}
|
||||
inherits(QueryBuilder, EventEmitter);
|
||||
|
||||
// Valid values for the `order by` clause generation.
|
||||
var orderBys = ['asc', 'desc'];
|
||||
|
||||
QueryBuilder.prototype.toString = function() {
|
||||
return this.toQuery();
|
||||
};
|
||||
@ -375,27 +372,51 @@ QueryBuilder.prototype.orWhereNotBetween = function(column, values) {
|
||||
};
|
||||
|
||||
// Adds a `group by` clause to the query.
|
||||
QueryBuilder.prototype.groupBy = function() {
|
||||
QueryBuilder.prototype.groupBy = function(item) {
|
||||
if (item instanceof Raw) {
|
||||
return this.groupByRaw.apply(this, arguments);
|
||||
}
|
||||
this._statements.push({
|
||||
grouping: 'group',
|
||||
type: 'groupByBasic',
|
||||
value: helpers.normalizeArr.apply(null, arguments)
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
// Adds a raw `group by` clause to the query.
|
||||
QueryBuilder.prototype.groupByRaw = function(sql, bindings) {
|
||||
var raw = (sql instanceof Raw ? sql : new Raw(sql, bindings));
|
||||
this._statements.push({
|
||||
grouping: 'group',
|
||||
type: 'groupByRaw',
|
||||
value: raw
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
// Adds a `order by` clause to the query.
|
||||
QueryBuilder.prototype.orderBy = function(column, direction) {
|
||||
if (!(direction instanceof Raw)) {
|
||||
if (!_.contains(orderBys, (direction || '').toLowerCase())) direction = 'asc';
|
||||
}
|
||||
this._statements.push({
|
||||
grouping: 'order',
|
||||
type: 'orderByBasic',
|
||||
value: column,
|
||||
direction: direction
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
// Add a raw `order by` clause to the query.
|
||||
QueryBuilder.prototype.orderByRaw = function(sql, bindings) {
|
||||
var raw = (sql instanceof Raw ? sql : new Raw(sql, bindings));
|
||||
this._statements.push({
|
||||
grouping: 'order',
|
||||
type: 'orderByRaw',
|
||||
value: raw
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
// Add a union statement to the query.
|
||||
QueryBuilder.prototype.union = function(callback, wrap) {
|
||||
if (arguments.length > 1) {
|
||||
|
||||
@ -265,10 +265,14 @@ QueryCompiler.prototype._groupsOrders = function(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);
|
||||
var str, item = items[i];
|
||||
if (item.value instanceof Raw) {
|
||||
str = this.formatter.checkRaw(item.value);
|
||||
} else {
|
||||
str = this.formatter.columnize(item.value);
|
||||
if (type === 'order') {
|
||||
str += ' ' + this.formatter.direction(item.direction);
|
||||
}
|
||||
}
|
||||
sql.push(str);
|
||||
}
|
||||
|
||||
@ -41,7 +41,9 @@ module.exports = [
|
||||
'orWhereBetween',
|
||||
'orWhereNotBetween',
|
||||
'groupBy',
|
||||
'groupByRaw',
|
||||
'orderBy',
|
||||
'orderByRaw',
|
||||
'union',
|
||||
'unionAll',
|
||||
'having',
|
||||
|
||||
@ -238,6 +238,18 @@ module.exports = function(pgclient, mysqlclient, sqlite3client) {
|
||||
expect(chain.sql).to.equal('select * from "users" order by "email" asc, "age" desc');
|
||||
});
|
||||
|
||||
it("raw group bys", function() {
|
||||
chain = sql().select('*').from('users').groupByRaw('id, email').toString();
|
||||
expect(chain).to.equal('select * from "users" group by id, email');
|
||||
});
|
||||
|
||||
it("raw order bys", function() {
|
||||
chain = sql().select('*').from('users').orderBy(raw('col NULLS LAST DESC')).toSQL();
|
||||
expect(chain.sql).to.equal('select * from "users" order by col NULLS LAST DESC');
|
||||
chain = sql().select('*').from('users').orderByRaw('col NULLS LAST DESC').toSQL();
|
||||
expect(chain.sql).to.equal('select * from "users" order by col NULLS LAST DESC');
|
||||
});
|
||||
|
||||
it("multiple order bys", function() {
|
||||
chain = sqlite3().select('*').from('users').orderBy('email').orderBy('age', 'desc').toSQL();
|
||||
expect(chain.sql).to.equal('select * from "users" order by "email" collate nocase asc, "age" collate nocase desc');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user