From 1c648b0faf959caf3108cb3e7e10ec43e5e31d0e Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Sun, 18 Aug 2013 10:04:58 -0700 Subject: [PATCH] Add an optional where object for delete --- lib/mysql.js | 152 +++++++++++++++++++++++++++++---------------------- 1 file changed, 87 insertions(+), 65 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 700eeec..32c1aaa 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -278,6 +278,75 @@ MySQL.prototype.getColumns = function(model, props){ return self.columnEscaped(model, c); }); return names.join(', '); +}; + +function buildWhere(self, model, conds) { + var props = self._models[model].properties; + + var cs = []; + Object.keys(conds).forEach(function (key) { + var keyEscaped = '`' + key.replace(/\./g, '`.`') + '`'; + var val = self.toDatabase(props[key], conds[key]); + if (conds[key] === null || conds[key] === undefined) { + cs.push(keyEscaped + ' IS NULL'); + } else if (conds[key] && conds[key].constructor.name === 'Object') { + var condType = Object.keys(conds[key])[0]; + var sqlCond = keyEscaped; + if ((condType === 'inq' || condType === 'nin') && val.length === 0) { + cs.push(condType === 'inq' ? 0 : 1); + return true; + } + switch (condType) { + case 'gt': + sqlCond += ' > '; + break; + case 'gte': + sqlCond += ' >= '; + break; + case 'lt': + sqlCond += ' < '; + break; + case 'lte': + sqlCond += ' <= '; + break; + case 'between': + sqlCond += ' BETWEEN '; + break; + case 'inq': + sqlCond += ' IN '; + break; + case 'nin': + sqlCond += ' NOT IN '; + break; + case 'neq': + sqlCond += ' != '; + break; + } + sqlCond += (condType === 'inq' || condType === 'nin') ? '(' + val + ')' : val; + cs.push(sqlCond); + } else { + cs.push(keyEscaped + ' = ' + val); + } + }); + if (cs.length === 0) { + return ''; + } + return 'WHERE ' + cs.join(' AND '); +} + +function buildOrderBy(order) { + if (typeof order === 'string') { + order = [order]; + } + return 'ORDER BY ' + order.map(function(o) { + var t = o.split(/\s+/); + if (t.length === 1) return '`' + o + '`'; + return '`' + t[0] + '` ' + t[1]; + }).join(', '); +} + +function buildLimit(limit, offset) { + return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit); } MySQL.prototype.all = function all(model, filter, callback) { @@ -292,12 +361,11 @@ MySQL.prototype.all = function all(model, filter, callback) { var sql = 'SELECT '+ this.getColumns(model, filter.fields) + ' FROM ' + this.tableEscaped(model); var self = this; - var props = this._models[model].properties; if (filter) { if (filter.where) { - sql += ' ' + buildWhere(filter.where); + sql += ' ' + buildWhere(self, model, filter.where); } if (filter.order) { @@ -327,71 +395,25 @@ MySQL.prototype.all = function all(model, filter, callback) { return sql; - function buildWhere(conds) { - var cs = []; - Object.keys(conds).forEach(function (key) { - var keyEscaped = '`' + key.replace(/\./g, '`.`') + '`' - var val = self.toDatabase(props[key], conds[key]); - if (conds[key] === null || conds[key] === undefined) { - cs.push(keyEscaped + ' IS NULL'); - } else if (conds[key] && conds[key].constructor.name === 'Object') { - var condType = Object.keys(conds[key])[0]; - var sqlCond = keyEscaped; - if ((condType == 'inq' || condType == 'nin') && val.length == 0) { - cs.push(condType == 'inq' ? 0 : 1); - return true; - } - switch (condType) { - case 'gt': - sqlCond += ' > '; - break; - case 'gte': - sqlCond += ' >= '; - break; - case 'lt': - sqlCond += ' < '; - break; - case 'lte': - sqlCond += ' <= '; - break; - case 'between': - sqlCond += ' BETWEEN '; - break; - case 'inq': - sqlCond += ' IN '; - break; - case 'nin': - sqlCond += ' NOT IN '; - break; - case 'neq': - sqlCond += ' != '; - break; - } - sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + val + ')' : val; - cs.push(sqlCond); - } else { - cs.push(keyEscaped + ' = ' + val); - } - }); - if (cs.length === 0) { - return ''; - } - return 'WHERE ' + cs.join(' AND '); - } +}; - function buildOrderBy(order) { - if (typeof order === 'string') order = [order]; - return 'ORDER BY ' + order.map(function(o) { - var t = o.split(/\s+/); - if (t.length === 1) return '`' + o + '`'; - return '`' + t[0] + '` ' + t[1]; - }).join(', '); +/** + * Delete instances for the given model + * + * @param {String} model The model name + * @param {Object} [where] The filter for where + * @param {Function} [callback] The callback function + * + */ +MySQL.prototype.destroyAll = function destroyAll(model, where, callback) { + if(!callback && 'function' === typeof where) { + callback = where; + where = undefined; } - - function buildLimit(limit, offset) { - return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit); - } - + this.query('DELETE FROM ' + + this.tableEscaped(model) + ' ' + buildWhere(this, model, where || {}), function (err, data) { + callback && callback(err, data); + }.bind(this)); }; MySQL.prototype.autoupdate = function (models, cb) {