Add support for logical operators (AND/OR)
This commit is contained in:
parent
a170621e1c
commit
1103a9e919
20
lib/mysql.js
20
lib/mysql.js
|
@ -388,11 +388,22 @@ MySQL.prototype.getColumns = function (model, props) {
|
|||
return names.join(', ');
|
||||
};
|
||||
|
||||
function buildWhere(self, model, conds) {
|
||||
MySQL.prototype.buildWhere = function (model, conds) {
|
||||
var self = this;
|
||||
var props = self._models[model].properties;
|
||||
|
||||
var cs = [];
|
||||
Object.keys(conds).forEach(function (key) {
|
||||
if (key === 'and' || key === 'or') {
|
||||
var clauses = conds[key];
|
||||
if (Array.isArray(clauses)) {
|
||||
clauses = clauses.map(function (c) {
|
||||
return '(' + self.buildWhere(model, c) + ')';
|
||||
});
|
||||
return clauses.join(' ' + key.toUpperCase() + ' ');
|
||||
}
|
||||
// The value is not an array, fall back to regular fields
|
||||
}
|
||||
var keyEscaped = self.columnEscaped(model, key);
|
||||
var val = self.toDatabase(props[key], conds[key]);
|
||||
if (conds[key] === null || conds[key] === undefined) {
|
||||
|
@ -482,7 +493,7 @@ MySQL.prototype.all = function all(model, filter, callback) {
|
|||
if (filter) {
|
||||
|
||||
if (filter.where) {
|
||||
sql += ' ' + buildWhere(self, model, filter.where);
|
||||
sql += ' ' + self.buildWhere(model, filter.where);
|
||||
}
|
||||
|
||||
if (filter.order) {
|
||||
|
@ -528,8 +539,9 @@ MySQL.prototype.destroyAll = function destroyAll(model, where, callback) {
|
|||
where = undefined;
|
||||
}
|
||||
this.query('DELETE FROM '
|
||||
+ this.tableEscaped(model) + ' ' + buildWhere(this, model, where || {}), function (err, data) {
|
||||
callback && callback(err, data);
|
||||
+ this.tableEscaped(model) + ' ' + this.buildWhere(model, where || {}),
|
||||
function (err, data) {
|
||||
callback && callback(err, data);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue