Skip to content

Commit

Permalink
Handle array values when creating operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Aug 16, 2012
1 parent a81f7ba commit 5bc48f4
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions lib/query/query.js
Expand Up @@ -47,25 +47,55 @@ query.Query.prototype = new (function () {
};
}

, _createConditions = function (conditions, key) {
var type = key || 'and'
, _createOperation = function (conditions, key) {
var self = this
, type = key || 'and'
, cond
, item
, op = operation.create(type)
, notOperand
, operand;

// TODO: Handle associations
for (var k in conditions) {
cond = conditions[k];

// Operation type, can contain other operations/conditions
if (_operationTypes[k]) {
if (k == 'not') {
operand = operation.create(k);
operand.add(_createConditions.apply(this, [conditions[k], 'and']));
// Base operation-type to create: if the type is a 'not',
// create a single 'and' with the same conditions to wrap
// in a 'not'
type = k == 'not' ? 'and' : k;

// If the conditions are an array, create a single 'and'
// op that wraps each set of conditions in each item, and
// add to the wrapper
if (Array.isArray(cond)) {
// Create empty wrapper
operand = operation.create(type);
cond.forEach(function (c) {
operand.add(_createOperation.apply(self, [c, 'and']));
});
}
// Simple object-literal, just create an operation
else {
operand = _createConditions.apply(this, [conditions[k], k]);
operand = _createOperation.apply(this, [cond, type]);
}

// If this was a 'not' operation, create a wrapping 'not'
// to contain the single operation created
if (k == 'not') {
notOperand = operation.create(k);
notOperand.add(operand);
operand = notOperand;
}
}

// Simple condition (leaf-node)
else {
operand = _createComparison.apply(this, [conditions[k], k]);
operand = _createComparison.apply(this, [cond, k]);
}

op.add(operand);
}
return op;
Expand Down Expand Up @@ -178,7 +208,7 @@ query.Query.prototype = new (function () {
this.model = model;
this.isValidField = _createIsValidField.apply(this);
this.opts = _parseOpts.apply(this, [opts || {}]);
this.conditions = _createConditions.apply(this, [conditions]);
this.conditions = _createOperation.apply(this, [conditions]);
};


Expand Down

0 comments on commit 5bc48f4

Please sign in to comment.