Skip to content

Commit

Permalink
Fix breakage on empty query via .getAssn of a hasMany/through relatio…
Browse files Browse the repository at this point in the history
…nship
  • Loading branch information
Ben Ng committed May 4, 2014
1 parent 727deed commit 6f8517d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/adapters/transformers/sql.js
Expand Up @@ -54,26 +54,44 @@ var sql = utils.mixin(new BaseTransformer(), new (function () {
return sql;
};

/*
* Note: please return null on empty operations.
* returning empty strings leads to weirdness like
* `WHERE ()` appearing in queries, which breaks
* postgresql (and possibly others)
*/
this.transformOperation = function (op) {
var self = this
, ops = [];
if (op.isEmpty()) {
return '';
return null;
}
else {
if (op.type == 'not') {
return 'NOT (' + self.transformOperation(op.operand()) + ')';
}
else {
op.forEach(function (o) {
var temp;
if (o instanceof operation.OperationBase) {
ops.push(self.transformOperation(o));
temp = self.transformOperation(o);

if(temp)
ops.push(temp);
}
else {
ops.push(self.transformComparison(o));
temp = self.transformComparison(o);

if(temp)
ops.push(self.transformComparison(o));
}
});
return '(' + ops.join(' ' + op.type.toUpperCase() + ' ') + ')';

if(ops.length)
return '(' + ops.join(' ' + op.type.toUpperCase() + ' ') + ')';
else
return null;

}
}
};
Expand Down

0 comments on commit 6f8517d

Please sign in to comment.