Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

trying to make query.where readable

  • Loading branch information...
commit 186f061d2ce16d7f625c50c55823d5e2fa6258c9 1 parent f1fc82b
Karl Seguin authored
Showing with 23 additions and 31 deletions.
  1. +23 −31 lib/query.js
View
54 lib/query.js
@@ -65,38 +65,30 @@ var Query = function(sql, params, table) {
return self._append(" \nWHERE \"%s\" = %s", self.table.pk, self.db.placeholder(self.params.length));
}
- var sql = '';
- var params = [];
-
- if(_.isObject(conditions) &! _.isArray(conditions)) {
- var _conditions = [];
- var seed = self.params.length;
-
- _.each(conditions, function(value, key) {
- var parts = key.trim().split(/ +/);
- var property = parts[0];
- var operation = operationsMap[parts[1]] || '=';
-
- if (_.isBoolean(value) || _.isNumber(value)) {
- _conditions.push(util.format('"%s" %s %d', property, operation, value));
- } else if (_.isArray(value)) {
- var arrayConditions = [];
- _.each(value, function(v) {
- arrayConditions.push(self.db.placeholder(++seed));
- params.push(v);
- });
- _conditions.push(util.format('"%s" %s (%s)', property, operation == '!=' || operation == '<>' ? 'NOT IN' : 'IN', arrayConditions.join(', ')));
- } else {
- _conditions.push(util.format('"%s" %s %s', property, operation, self.db.placeholder(++seed)));
- params.push(value);
- }
+ var _conditions = [];
+ _.each(conditions, function(value, key) {
+ var parts = key.trim().split(/ +/);
+ var property = parts[0];
+ var operation = operationsMap[parts[1]] || '=';
+
+ if (_.isBoolean(value) || _.isNumber(value)) {
+ return _conditions.push(util.format('"%s" %s %d', property, operation, value));
+ }
+
+ if (!_.isArray(value)) {
+ self.params.push(value);
+ return _conditions.push(util.format('"%s" %s %s', property, operation, self.db.placeholder(self.params.length)));
+ }
+
+ var arrayConditions = [];
+ _.each(value, function(v) {
+ self.params.push(v);
+ arrayConditions.push(self.db.placeholder(self.params.length));
});
- sql = ' \nWHERE ' + _conditions.join(' \nAND ');
- };
- self.sql += sql;
- self.params.push(params);
- self.params = _.flatten(self.params);
- return self;
+ _conditions.push(util.format('"%s" %s (%s)', property, operation == '!=' || operation == '<>' ? 'NOT IN' : 'IN', arrayConditions.join(', ')));
+ });
+
+ return self._append(' \nWHERE ' + _conditions.join(' \nAND '));
};
//execution uses the Client
Please sign in to comment.
Something went wrong with that request. Please try again.