Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaned up query.parseArgs and made it more flexible in terms of how …

…it can be used
  • Loading branch information...
commit c9721a0b26701f9522be4570e6979affd1e75eb0 1 parent 85a6bfd
Karl Seguin authored
Showing with 20 additions and 18 deletions.
  1. +20 −18 lib/query.js
View
38 lib/query.js
@@ -30,28 +30,30 @@ var Query = function(sql, params, table) {
};
self.parseArgs = function(args) {
- if(_.isFunction(args[0])) { return self; }
-
- var where = {};
-
- if(args.length > 0 && _.isArray(args[0])) { //if the first argument is an array, columns are specified
- self.sql = self.sql.replace("*", args[0].join(","));
- } else if(args.length > 1 && args[1].columns) { //if the second arg has {columns : "..."} then columns are also specified
- self.sql = self.sql.replace("*", args[1].columns);
- } else if (args.length > 0 && _.isNumber(args[0])) { //if the argument is numeric (instead of an object) - default it to a PK lookup
- var criteria = {};
- criteria[self.table.pk] = args[0];
- self.where(criteria);
- } else if (args.length > 0 && _.isObject(args[0])){ //if the argument is an object, parse a where statement
- self.where(args[0]);
- }
+ if(args.length == 0 || _.isFunction(args[0])) { return self; }
+
+ _.each(args, function(arg) {
+ if (_.isNumber(arg) || _.isString(arg)) {
+ var criteria = {};
+ criteria[self.table.pk] = args[0];
+ return self.where(criteria);
+ }
+
+ var columns = arg.columns || arg;
+ if (_.isArray(columns)) { self.sql = self.sql.replace("*", columns.join(",")); }
+ if (_.isString(columns)) { self.sql = self.sql.replace("*", columns); }
+ delete arg.columns;
+
+ var where = arg.where || arg;
+ if (!_.isArray(where) && _.isObject(where) && _.size(where) > 0) { self.where(where); }
+
+ });
return self;
};
self.where = function(conditions) {
- if (_.isUndefined(conditions)) {
- return self;
- }
+ if (_.isUndefined(conditions)) { return self; }
+
if(_.isNumber(conditions)) {
return self._append(" \nWHERE \"%s\" = %d", self.table.pk, conditions);
}
Please sign in to comment.
Something went wrong with that request. Please try again.