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

squashed problems with .having() #107

Merged
merged 1 commit into from Nov 20, 2013
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
6 changes: 4 additions & 2 deletions clients/base/grammar.js
Expand Up @@ -199,12 +199,14 @@ define(function(require, exports) {

// Compiles the `having` statements.
compileHavings: function(qb, havings) {
return 'having ' + havings.map(function(having) {
if (!havings.length) return;
var h = 'having ' + havings.map(function(having) {
if (having.type === 'Raw') {
return having.bool + ' ' + having.sql;
}
return having.bool + ' ' + this.wrap(having.column) + ' ' + having.operator + ' ' + this.parameter(having['value']);
}, this).replace(/and |or /, '');
}, this)
return h.replace(/and |or /, '');
},

// Compiles the `order by` statements.
Expand Down
13 changes: 8 additions & 5 deletions lib/builder.js
Expand Up @@ -79,7 +79,7 @@ define(function(require, exports) {
var items = [
'aggregate', 'type', 'joins', 'wheres', 'orders',
'columns', 'values', 'bindings', 'grammar',
'transaction', 'unions', 'flags'
'transaction', 'unions', 'flags', 'havings'
];
for (var i = 0, l = items.length; i < l; i++) {
var k = items[i];
Expand All @@ -93,6 +93,7 @@ define(function(require, exports) {
this.joins = [];
this.wheres = [];
this.orders = [];
this.havings = [];
this.columns = [];
this.values = [];
this.bindings = [];
Expand Down Expand Up @@ -322,7 +323,7 @@ define(function(require, exports) {
// Adds a `having` clause to the query.
having: function(column, operator, value, bool) {
if (column instanceof Raw) {
return this.havingRaw(column.value, bool);
return this.havingRaw(column.sql, column.bindings, bool);
}
this.havings.push({column: column, operator: (operator || ''), value: (value || ''), bool: bool || 'and'});
this.bindings.push(value);
Expand All @@ -335,14 +336,16 @@ define(function(require, exports) {
},

// Adds a raw `having` clause to the query.
havingRaw: function(sql, bool) {
havingRaw: function(sql, bindings, bool) {
bindings = _.isArray(bindings) ? bindings : (bindings ? [bindings] : []);
this.havings.push({type: 'Raw', sql: sql, bool: bool || 'and'});
push.apply(this.bindings, bindings);
return this;
},

// Adds a raw `or having` clause to the query.
orHavingRaw: function(sql) {
return this.havingRaw(sql, 'or');
orHavingRaw: function(sql, bindings) {
return this.havingRaw(sql, bindings, 'or');
},

offset: function(value) {
Expand Down
2 changes: 2 additions & 0 deletions lib/common.js
Expand Up @@ -11,6 +11,8 @@ define(function(require, exports) {
var _ = require('underscore');
var Helpers = require('./helpers').Helpers;
var SqlString = require('./sqlstring').SqlString;

var Promise = require('./promise').Promise;

var push = [].push;

Expand Down
17 changes: 16 additions & 1 deletion test/unit/builder.js
Expand Up @@ -338,7 +338,22 @@ describe('Builder', function () {
});

describe('having', function() {


it('should add a having clause to the query', function() {
builder.having('columnName', '=', 3);
expect(builder.havings).to.have.length(1);
expect(builder.bindings).to.have.length(1);
expect(builder.bindings[0]).to.equal(3);
expect(builder.toString()).to.equal("select * having `columnName` = 3");
});

it('should allow a raw instance as the first argument, which will add a havingRaw clause', function() {
builder.having(new Raw({}).query('id > ?', 2));
expect(builder.havings).to.have.length(1);
expect(builder.bindings).to.have.length(1);
expect(builder.bindings[0]).to.equal(2);
});

});

describe('offset / limit', function() {
Expand Down