Skip to content

Commit

Permalink
added support for the 'having' clause
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlacot committed Jul 11, 2012
1 parent def164c commit 8848416
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions joli.js
Expand Up @@ -417,6 +417,7 @@ var joliCreator = function() {
this.data = {
as: null,
from: null,
having: null,
join: [],
limit: null,
operation: null,
Expand Down Expand Up @@ -522,6 +523,10 @@ var joliCreator = function() {
query += ' group by ' + this.data.groupBy.join(', ');
}

if (this.data.having) {
query += ' having ' + this.data.having;
}

if (this.data.order.length > 0) {
query += ' order by ' + this.data.order.join(', ');
}
Expand All @@ -540,6 +545,30 @@ var joliCreator = function() {
this.data.groupBy = group;
return this;
},
having: function(expression, value) {
if (null !== this.data.where) {
this.data.having += ' and ';
} else {
this.data.having = '';
}

// handle replacing multiple values
if ('array' === joli.getType(value)) {
var i = 0;

// replace question marks one at a time from the array
while (expression.indexOf('?') !== -1 && value[i] !== undefined) {
expression = expression.replace(/\?/i, '"' + value[i] + '"');
i++;
}

this.data.having += expression;
} else {
this.data.having += expression.replace(/\?/gi, '"' + value + '"');
}

return this;
},
hydrate: function(rows, hydratationMode) {
var result = [];

Expand Down

0 comments on commit 8848416

Please sign in to comment.