From 1553452346c4b24e507a444244504c2d2e0c286a Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 3 Jun 2014 07:59:27 -0700 Subject: [PATCH 1/4] Fix the count() impl to use buildWhere() from the subclass --- lib/sql.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/sql.js b/lib/sql.js index dcad57a..f7a2b32 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -297,8 +297,14 @@ SqlConnector.prototype.count = function count(model, callback, where) { var self = this; var props = this._models[model].properties; + var whereClause = ''; + if (typeof this.buildWhere === 'function') { + whereClause = this.buildWhere(model, where); + } else { + whereClause = buildWhere(where); + } this.queryOne('SELECT count(*) as cnt FROM ' + - this.tableEscaped(model) + ' ' + buildWhere(where), function (err, res) { + this.tableEscaped(model) + ' ' + whereClause, function (err, res) { if (err) { return callback(err); } From 7d6f0f5ab9d0ab905e805b6a55ae25c85d101c6c Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 18 Jun 2014 22:24:55 -0700 Subject: [PATCH 2/4] Add bulk update support --- lib/sql.js | 72 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/lib/sql.js b/lib/sql.js index f7a2b32..23e9042 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -273,6 +273,24 @@ SqlConnector.prototype._escapeIdValue = function(model, idValue) { } }; +function buildWhere(self, model, where) { + if (typeof self.buildWhere === 'function') { + return self.buildWhere(model, where); + } else { + var props = self._models[model].properties; + var cs = []; + Object.keys(where || {}).forEach(function (key) { + var keyEscaped = self.columnEscaped(model, key); + if (where[key] === null) { + cs.push(keyEscaped + ' IS NULL'); + } else { + cs.push(keyEscaped + ' = ' + self.toDatabase(props[key], where[key])); + } + }); + return cs.length ? ' WHERE ' + cs.join(' AND ') : ''; + } +} + /** * Delete all model instances * @@ -280,11 +298,14 @@ SqlConnector.prototype._escapeIdValue = function(model, idValue) { * @param {Function} callback The callback function */ SqlConnector.prototype.deleteAll = -SqlConnector.prototype.destroyAll = function destroyAll(model, callback) { - this.command('DELETE FROM ' + this.tableEscaped(model), function (err, result) { - if (callback) callback(err, result); - }); -}; + SqlConnector.prototype.destroyAll = function destroyAll(model, where, callback) { + this.command('DELETE FROM ' + this.tableEscaped(model) + + buildWhere(this, model, where), function (err, result) { + if (callback) { + callback(err, result); + } + }); + }; /** * Count all model instances by the where filter @@ -295,14 +316,8 @@ SqlConnector.prototype.destroyAll = function destroyAll(model, callback) { */ SqlConnector.prototype.count = function count(model, callback, where) { var self = this; - var props = this._models[model].properties; - var whereClause = ''; - if (typeof this.buildWhere === 'function') { - whereClause = this.buildWhere(model, where); - } else { - whereClause = buildWhere(where); - } + var whereClause = buildWhere(self, model, where); this.queryOne('SELECT count(*) as cnt FROM ' + this.tableEscaped(model) + ' ' + whereClause, function (err, res) { if (err) { @@ -310,19 +325,6 @@ SqlConnector.prototype.count = function count(model, callback, where) { } callback(err, res && res.cnt); }); - - function buildWhere(conds) { - var cs = []; - Object.keys(conds || {}).forEach(function (key) { - var keyEscaped = self.columnEscaped(model, key); - if (conds[key] === null) { - cs.push(keyEscaped + ' IS NULL'); - } else { - cs.push(keyEscaped + ' = ' + self.toDatabase(props[key], conds[key])); - } - }); - return cs.length ? ' WHERE ' + cs.join(' AND ') : ''; - } }; /** @@ -406,3 +408,23 @@ SqlConnector.prototype.createTable = function (model, cb) { ' (\n ' + this.propertiesSQL(model) + '\n)', cb); }; +/** + * Update all instances that match the where clause with the given data + * @param {String} model The model name + * @param {Object} data The property/value object representing changes to be made + * @param {Function} callback The callback function + */ +SqlConnector.prototype.update = + SqlConnector.prototype.updateAll = function (model, where, data, callback) { + var whereClause = buildWhere(this, model, where); + + var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + + this.toFields(model, data) + whereClause; + + this.query(sql, function (err, result) { + if (callback) { + callback(err, result); + } + }); + }; + From b4a45603cbb3904f0668b7916d8ec3facf1ae4c3 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 18 Jun 2014 22:29:34 -0700 Subject: [PATCH 3/4] Add space --- lib/sql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sql.js b/lib/sql.js index 23e9042..b362b4e 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -419,7 +419,7 @@ SqlConnector.prototype.update = var whereClause = buildWhere(this, model, where); var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + - this.toFields(model, data) + whereClause; + this.toFields(model, data) + ' ' + whereClause; this.query(sql, function (err, result) { if (callback) { From 614ac2b419df52136bdecc55c2ac584bfb0fac37 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 18 Jun 2014 22:35:52 -0700 Subject: [PATCH 4/4] Fix style to pass jlint --- lib/sql.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sql.js b/lib/sql.js index b362b4e..0c6b349 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -299,8 +299,8 @@ function buildWhere(self, model, where) { */ SqlConnector.prototype.deleteAll = SqlConnector.prototype.destroyAll = function destroyAll(model, where, callback) { - this.command('DELETE FROM ' + this.tableEscaped(model) - + buildWhere(this, model, where), function (err, result) { + this.command('DELETE FROM ' + this.tableEscaped(model) + + buildWhere(this, model, where), function (err, result) { if (callback) { callback(err, result); }