fixed build of where statement when filter contains non-existing column

This commit is contained in:
Maor Hayun 2016-02-06 19:07:49 +02:00
parent 26712a7014
commit eba7e68059
1 changed files with 16 additions and 8 deletions

View File

@ -754,10 +754,12 @@ SQLConnector.prototype._buildWhere = function(model, where) {
if (Array.isArray(clauses)) { if (Array.isArray(clauses)) {
for (var i = 0, n = clauses.length; i < n; i++) { for (var i = 0, n = clauses.length; i < n; i++) {
var stmtForClause = self._buildWhere(model, clauses[i]); var stmtForClause = self._buildWhere(model, clauses[i]);
if (stmtForClause.sql) {
stmtForClause.sql = '(' + stmtForClause.sql + ')'; stmtForClause.sql = '(' + stmtForClause.sql + ')';
branchParams = branchParams.concat(stmtForClause.params); branchParams = branchParams.concat(stmtForClause.params);
branches.push(stmtForClause.sql); branches.push(stmtForClause.sql);
} }
}
stmt.merge({ stmt.merge({
sql: branches.join(' ' + key.toUpperCase() + ' '), sql: branches.join(' ' + key.toUpperCase() + ' '),
params: branchParams params: branchParams
@ -767,6 +769,12 @@ SQLConnector.prototype._buildWhere = function(model, where) {
} }
// The value is not an array, fall back to regular fields // The value is not an array, fall back to regular fields
} }
var p = props[key];
if (p == null) {
// Unknown property, ignore it
debug('Unknown property %s is skipped for model %s', key, model);
continue;
}
var columnName = self.columnEscaped(model, key); var columnName = self.columnEscaped(model, key);
var expression = where[key]; var expression = where[key];
var columnValue; var columnValue;
@ -782,10 +790,10 @@ SQLConnector.prototype._buildWhere = function(model, where) {
if (Array.isArray(expression)) { if (Array.isArray(expression)) {
// Column value is a list // Column value is a list
for (var j = 0, m = expression.length; j < m; j++) { for (var j = 0, m = expression.length; j < m; j++) {
columnValue.push(this.toColumnValue(props[key], expression[j])); columnValue.push(this.toColumnValue(p, expression[j]));
} }
} else { } else {
columnValue.push(this.toColumnValue(props[key], expression)); columnValue.push(this.toColumnValue(p, expression));
} }
if (operator === 'between') { if (operator === 'between') {
// BETWEEN v1 AND v2 // BETWEEN v1 AND v2
@ -807,14 +815,14 @@ SQLConnector.prototype._buildWhere = function(model, where) {
// do not coerce RegExp based on property definitions // do not coerce RegExp based on property definitions
columnValue = expression; columnValue = expression;
} else { } else {
columnValue = this.toColumnValue(props[key], expression); columnValue = this.toColumnValue(p, expression);
} }
sqlExp = self.buildExpression( sqlExp = self.buildExpression(
columnName, operator, columnValue, props[key]); columnName, operator, columnValue, p);
stmt.merge(sqlExp); stmt.merge(sqlExp);
} else { } else {
// The expression is the field value, not a condition // The expression is the field value, not a condition
columnValue = self.toColumnValue(props[key], expression); columnValue = self.toColumnValue(p, expression);
if (columnValue === null) { if (columnValue === null) {
stmt.merge(columnName + ' IS NULL'); stmt.merge(columnName + ' IS NULL');
} else { } else {