diff --git a/lib/sql.js b/lib/sql.js index dcad57a..0c6b349 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,28 +316,15 @@ 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 = buildWhere(self, model, 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); } 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 ') : ''; - } }; /** @@ -400,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); + } + }); + }; +