Merge branch 'release/1.1.0' into production

This commit is contained in:
Raymond Feng 2014-06-20 13:41:56 -07:00
commit 5ec091597c
2 changed files with 49 additions and 21 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 * Delete all model instances
* *
@ -280,11 +298,14 @@ SqlConnector.prototype._escapeIdValue = function(model, idValue) {
* @param {Function} callback The callback function * @param {Function} callback The callback function
*/ */
SqlConnector.prototype.deleteAll = SqlConnector.prototype.deleteAll =
SqlConnector.prototype.destroyAll = function destroyAll(model, callback) { SqlConnector.prototype.destroyAll = function destroyAll(model, where, callback) {
this.command('DELETE FROM ' + this.tableEscaped(model), function (err, result) { this.command('DELETE FROM ' + this.tableEscaped(model) +
if (callback) callback(err, result); buildWhere(this, model, where), function (err, result) {
}); if (callback) {
}; callback(err, result);
}
});
};
/** /**
* Count all model instances by the where filter * 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) { SqlConnector.prototype.count = function count(model, callback, where) {
var self = this; var self = this;
var props = this._models[model].properties;
var whereClause = buildWhere(self, model, where);
this.queryOne('SELECT count(*) as cnt FROM ' + this.queryOne('SELECT count(*) as cnt FROM ' +
this.tableEscaped(model) + ' ' + buildWhere(where), function (err, res) { this.tableEscaped(model) + ' ' + whereClause, function (err, res) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
callback(err, res && res.cnt); 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); ' (\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);
}
});
};

View File

@ -1,6 +1,6 @@
{ {
"name": "loopback-connector", "name": "loopback-connector",
"version": "1.0.0", "version": "1.1.0",
"description": "Building blocks for LoopBack connectors", "description": "Building blocks for LoopBack connectors",
"keywords": [ "keywords": [
"StrongLoop", "StrongLoop",