Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaning up query.js to make use of execute and _append internally...…

…removed unused fields
  • Loading branch information...
commit f1fc82b8969e6fe6b14d7a049b7290ebaa881ded 1 parent a94bed8
Karl Seguin authored
Showing with 22 additions and 35 deletions.
  1. +22 −35 lib/query.js
View
57 lib/query.js
@@ -11,27 +11,17 @@ var Query = function(sql, params, table) {
self.params = params || [];
self.table = table;
self.db = table.db;
- self.columns = ""
- self.where = {};
- self.limit = "";
- self.order = "";
self.on('newListener', function(eventName) {
if(eventName === 'row') { self.each(); }
});
- self.order = function() {
- self.sql += util.format(" \nORDER BY %s", arguments[0]);
- return self;
+ self.order = function(where) {
+ return self._append(" \nORDER BY %s", where);
};
- self.limit = function() {
- if(arguments.length > 1) {
- self.sql += util.format(" \nLIMIT(%d,%d)", arguments[0], arguments[1]);
- } else {
- self.sql += " \nLIMIT " + arguments[0];
- }
- return self;
+ self.limit = function(count, offset) {
+ return _.isUndefined(offset) ? self._append(" \nLIMIT %d", count) : self._append(" \nLIMIT(%d,%d)", count, offset);
};
self.raiseError = function(err) {
@@ -64,15 +54,15 @@ var Query = function(sql, params, table) {
};
self.where = function(conditions) {
- if (_.isUndefined(conditions)) { return self; }
-
- if(_.isNumber(conditions)) {
- self.sql += util.format(" \nWHERE \"%s\" = %d", self.table.pk, conditions);
+ if (_.isUndefined(conditions)) {
return self;
- }else if (_.isString(conditions)) {
- self.sql += util.format(" \nWHERE \"%s\" = %s", self.table.pk, self.db.placeholder(self.params.length+1));
+ }
+ if(_.isNumber(conditions)) {
+ return self._append(" \nWHERE \"%s\" = %d", self.table.pk, conditions);
+ }
+ if (_.isString(conditions)) {
self.params.push(conditions);
- return self;
+ return self._append(" \nWHERE \"%s\" = %s", self.table.pk, self.db.placeholder(self.params.length));
}
var sql = '';
@@ -111,8 +101,8 @@ var Query = function(sql, params, table) {
//execution uses the Client
self.execute = function(callback) {
- self.db.execute(self.sql, self.params, function(err,result,client) {
- if(callback) { callback(err,result,client); }
+ self.db.execute(self.sql, self.params, function(err,result) {
+ if(callback) { callback(err, result); }
self.emit("executed",client);
});
};
@@ -122,10 +112,10 @@ var Query = function(sql, params, table) {
self.each = function(callback) {
self.db.execute(self.sql,self.params, function(err,results){
if(err && callback) {
- return callback(err,self.raiseError(err));
+ return callback(err, self.raiseError(err));
}
_.each(results, function(r) {
- if(callback) { callback(null,r); }
+ if(callback) { callback(null, r); }
self.emit("row", r);
});
self.emit("end");
@@ -133,19 +123,16 @@ var Query = function(sql, params, table) {
};
self.first = function(callback) {
- self.sql += " LIMIT(1) ";
- self.db.execute(self.sql,self.params, function(err,results) {
- if (err) { callback(err,self.raiseError(err)); }
- else { callback(null,results[0]); }
- });
+ self.append(" LIMIT(1) ").execute(callback);
};
self.last = function(callback) {
- self.sql += util.format(" ORDER BY %s DESC LIMIT(1) ", this.table.pk);
- self.db.execute(self.sql,self.params, function(err,results) {
- if(err) { callback(err,self.raiseError(err)); }
- else { callback(null,results[0]); }
- });
+ self.append("ORDER BY %s DESC LIMIT(1) ", this.table.pk).execute(callback);
+ };
+
+ self._append = function(sql) {
+ self.sql += arguments.length == 1 ? sql : util.format.apply(null, _.toArray(arguments));
+ return self;
};
};

0 comments on commit f1fc82b

Please sign in to comment.
Something went wrong with that request. Please try again.