Add bulk update support

This commit is contained in:
Raymond Feng 2014-06-18 22:24:55 -07:00
parent 1553452346
commit 7d6f0f5ab9
1 changed files with 47 additions and 25 deletions

View File

@ -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);
}
});
};