Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nested having support #572

Merged
merged 2 commits into from Nov 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/query/builder.js
Expand Up @@ -260,6 +260,17 @@ QueryBuilder.prototype.whereWrapped = function(callback) {
return this;
};

// Helper for compiling any advanced `having` queries.
QueryBuilder.prototype.havingWrapped = function(callback) {
this._statements.push({
grouping: 'having',
type: 'whereWrapped',
value: callback,
bool: this._bool()
});
return this;
};

// Adds a `where exists` clause to the query.
QueryBuilder.prototype.whereExists = function(callback) {
this._statements.push({
Expand Down Expand Up @@ -459,6 +470,13 @@ QueryBuilder.prototype.andHaving = function(column, operator, value) {
if (column instanceof Raw && arguments.length === 1) {
return this._havingRaw(column);
}

// Check if the column is a function, in which case it's
// a having statement wrapped in parens.
if (_.isFunction(column)) {
return this.havingWrapped(column);
}

this._statements.push({
grouping: 'having',
type: 'havingBasic',
Expand Down
8 changes: 6 additions & 2 deletions lib/query/compiler.js
Expand Up @@ -190,8 +190,12 @@ QueryCompiler.prototype.having = function() {
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));
}else{
if(s.type === 'whereWrapped'){
sql.push(this.whereWrapped(s));
} else {
sql.push(str + this.formatter.checkRaw(s.value));
}
}
}
return sql.length > 1 ? sql.join(' ') : '';
Expand Down
1 change: 1 addition & 0 deletions lib/query/methods.js
Expand Up @@ -26,6 +26,7 @@ module.exports = [
'orWhere',
'whereRaw',
'whereWrapped',
'havingWrapped',
'orWhereRaw',
'whereExists',
'orWhereExists',
Expand Down
19 changes: 19 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -731,6 +731,25 @@ module.exports = function(qb, clientName, aliasName) {
default: 'select * from "users" having "email" > ?'
});
});

it("nested having", function() {
testsql(qb().select('*').from('users').having(function(){
this.where('email', '>', 1);
}), {
mysql: 'select * from `users` having (`email` > ?)',
default: 'select * from "users" having ("email" > ?)'
});
});

it("nested or havings", function() {
testsql(qb().select('*').from('users').having(function(){
this.where('email', '>', 10);
this.orWhere('email', '=', 7);
}), {
mysql: 'select * from `users` having (`email` > ? or `email` = ?)',
default: 'select * from "users" having ("email" > ? or "email" = ?)'
});
});

it("grouped having", function() {
testsql(qb().select('*').from('users').groupBy('email').having('email', '>', 1), {
Expand Down