Skip to content

Commit

Permalink
Merge branch 'master' into migrations
Browse files Browse the repository at this point in the history
* master:
  Allowing Knex.Raw statements in where clauses, #15
  shouldn't need bindings on raw queries
  • Loading branch information
tgriesser committed Jun 8, 2013
2 parents 5d7a560 + d8ebb38 commit a1fdaee
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
45 changes: 26 additions & 19 deletions knex.js
Expand Up @@ -285,11 +285,11 @@
// Compiles the `having` statements.
compileHavings: function(qb, havings) {
return 'having ' + havings.map(function(having) {
if (having.type === 'raw') {
if (having.type === 'Raw') {
return having.bool + ' ' + having.sql;
}
return '' + this.wrap(having.column) + ' ' + having.operator + ' ' + this.parameter(having['value']);
}, this).join('and ').replace(/and /, '');
return having.bool + ' ' + this.wrap(having.column) + ' ' + having.operator + ' ' + this.parameter(having['value']);
}, this).replace(/and |or /, '');
},

// Compiles the `order by` statements.
Expand Down Expand Up @@ -531,6 +531,9 @@
if (_.isFunction(column)) {
return this._whereNested(column, bool);
}
if (column instanceof Raw) {
return this.whereRaw(column.value, bool);
}
if (_.isObject(column)) {
for (var key in column) {
value = column[key];
Expand Down Expand Up @@ -567,17 +570,14 @@
},

// Adds a raw `where` clause to the query.
whereRaw: function(sql, bindings, bool) {
bindings || (bindings = []);
bool || (bool = 'and');
this.wheres.push({type: 'Raw', sql:sql, bool:bool});
push.apply(this.bindings, bindings);
whereRaw: function(sql, bool) {
this.wheres.push({type: 'Raw', sql: sql, bool: bool || 'and'});
return this;
},

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

// Adds a `where exists` clause to the query.
Expand Down Expand Up @@ -701,24 +701,31 @@
},

// Adds a `having` clause to the query.
having: function(column, operator, value) {
this.havings.push({column: column, operator: (operator || ''), value: (value || '')});
having: function(column, operator, value, bool) {
if (column instanceof Raw) {
return this.havingRaw(column.value, bool);
}
this.havings.push({column: column, operator: (operator || ''), value: (value || ''), bool: bool || 'and'});
this.bindings.push(value);
return this;
},

havingRaw: function(sql, bindings) {
this.havings.push({type: 'Raw', sql: sql, bool: 'and'});
this.bindings.push(bindings);
return this;
// Adds an `or having` clause to the query.
orHaving: function(column, operator, value) {
return this.having(column, operator, value, 'or');
},

orHavingRaw: function(sql, bindings) {
this.havings.push({type: 'Raw', sql: sql, bool: 'or'});
this.bindings.push(bindings);
// Adds a raw `having` clause to the query.
havingRaw: function(sql, bool) {
this.havings.push({type: 'Raw', sql: sql, bool: bool || 'and'});
return this;
},

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

// ----------------------------------------------------------------------

offset: function(value) {
Expand Down
6 changes: 5 additions & 1 deletion test/lib/selects.js
Expand Up @@ -110,7 +110,11 @@ module.exports = function(Knex, dbName, resolver) {
});

it("supports the <> operator", function(ok) {
Knex('accounts').where('id', '<>', 2).select('email', 'logins').then(resolver(ok), ok);
Knex('accounts').where('id', '<>', 2).select('email', 'logins').then(resolver(ok), ok);
});

it("Allows for Knex.Raw passed to the `where` clause", function(ok) {
Knex('accounts').where(Knex.Raw('id = 2')).select('email', 'logins').then(resolver(ok), ok);
});

});
Expand Down
28 changes: 28 additions & 0 deletions test/shared/output.js
Expand Up @@ -490,6 +490,20 @@ module.exports = {
bindings: [2]
}
},
'selects.22': {
mysql: {
sql: ['select `email`, `logins` from `accounts` where id = 2'],
bindings: []
},
postgres: {
sql: ['select "email", "logins" from "accounts" where id = 2'],
bindings: []
},
sqlite3: {
sql: ['select "email", "logins" from "accounts" where id = 2'],
bindings: []
}
},
'aggregate.1': {
mysql: {
sql: ['select sum(`logins`) as aggregate from `accounts`'],
Expand Down Expand Up @@ -2020,6 +2034,20 @@ module.exports = {
logins: 2
}]
},
'selects.22': {
mysql: [{
email: 'test2@example.com',
logins: 1
}],
postgres: [{
email: 'test2@example.com',
logins: 1
}],
sqlite3: [{
email: 'test2@example.com',
logins: 1
}]
},
'aggregate.1': {
mysql: [{
aggregate: 10
Expand Down

0 comments on commit a1fdaee

Please sign in to comment.