Add an optional where object for delete

This commit is contained in:
Raymond Feng 2013-08-18 10:04:58 -07:00
parent 044f629d7e
commit 1c648b0faf
1 changed files with 87 additions and 65 deletions

View File

@ -278,6 +278,75 @@ MySQL.prototype.getColumns = function(model, props){
return self.columnEscaped(model, c); return self.columnEscaped(model, c);
}); });
return names.join(', '); 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) { 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 sql = 'SELECT '+ this.getColumns(model, filter.fields) + ' FROM ' + this.tableEscaped(model);
var self = this; var self = this;
var props = this._models[model].properties;
if (filter) { if (filter) {
if (filter.where) { if (filter.where) {
sql += ' ' + buildWhere(filter.where); sql += ' ' + buildWhere(self, model, filter.where);
} }
if (filter.order) { if (filter.order) {
@ -327,71 +395,25 @@ MySQL.prototype.all = function all(model, filter, callback) {
return sql; 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]; * Delete instances for the given model
return 'ORDER BY ' + order.map(function(o) { *
var t = o.split(/\s+/); * @param {String} model The model name
if (t.length === 1) return '`' + o + '`'; * @param {Object} [where] The filter for where
return '`' + t[0] + '` ' + t[1]; * @param {Function} [callback] The callback function
}).join(', '); *
*/
MySQL.prototype.destroyAll = function destroyAll(model, where, callback) {
if(!callback && 'function' === typeof where) {
callback = where;
where = undefined;
} }
this.query('DELETE FROM '
function buildLimit(limit, offset) { + this.tableEscaped(model) + ' ' + buildWhere(this, model, where || {}), function (err, data) {
return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit); callback && callback(err, data);
} }.bind(this));
}; };
MySQL.prototype.autoupdate = function (models, cb) { MySQL.prototype.autoupdate = function (models, cb) {