From ab99062e6379092e0338452b44eec816f671c2d2 Mon Sep 17 00:00:00 2001 From: Marvin Irwin Date: Wed, 21 Nov 2018 13:23:11 -0800 Subject: [PATCH] Fix SqlConnector to ignore empty WHERE statements --- lib/sql.js | 1 + test/sql.test.js | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/sql.js b/lib/sql.js index 69aa88d..4176996 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -1182,6 +1182,7 @@ SQLConnector.prototype._buildWhere = function(model, where) { let params = []; const sqls = []; for (let k = 0, s = whereStmts.length; k < s; k++) { + if (!whereStmts[k].sql) continue; sqls.push(whereStmts[k].sql); params = params.concat(whereStmts[k].params); } diff --git a/test/sql.test.js b/test/sql.test.js index a05dbd9..7243f29 100644 --- a/test/sql.test.js +++ b/test/sql.test.js @@ -247,6 +247,14 @@ describe('sql connector', function() { }); }); + it('builds where and ignores invalid clauses in or', function() { + const where = connector.buildWhere('customer', { + name: 'icecream', + or: [{notAColumnName: ''}, {notAColumnNameEither: ''}], + }); + expect(where.sql).to.not.match(/ AND $/); + }); + it('builds order by with one field', function() { const orderBy = connector.buildOrderBy('customer', 'name'); expect(orderBy).to.eql('ORDER BY `NAME`');