Group clauses for AND/OR conditions

This is required to preserve the order of conditions


Signed-off-by: Raymond Feng <enjoyjava@gmail.com>
This commit is contained in:
Raymond Feng 2020-11-06 09:47:25 -08:00
parent 576e956dc1
commit 8d8361337d
2 changed files with 19 additions and 4 deletions

View File

@ -1049,7 +1049,7 @@ SQLConnector.prototype._buildWhere = function(model, where) {
} }
} }
stmt.merge({ stmt.merge({
sql: branches.join(' ' + key.toUpperCase() + ' '), sql: '(' + branches.join(' ' + key.toUpperCase() + ' ') + ')',
params: branchParams, params: branchParams,
}); });
whereStmts.push(stmt); whereStmts.push(stmt);

View File

@ -148,7 +148,7 @@ describe('sql connector', function() {
const where = connector.buildWhere('customer', const where = connector.buildWhere('customer',
{or: [{name: 'John'}, {name: 'Mary'}]}); {or: [{name: 'John'}, {name: 'Mary'}]});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE (`NAME`=?) OR (`NAME`=?)', sql: 'WHERE ((`NAME`=?) OR (`NAME`=?))',
params: ['John', 'Mary'], params: ['John', 'Mary'],
}); });
}); });
@ -157,7 +157,7 @@ describe('sql connector', function() {
const where = connector.buildWhere('customer', const where = connector.buildWhere('customer',
{and: [{name: 'John'}, {vip: true}]}); {and: [{name: 'John'}, {vip: true}]});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE (`NAME`=?) AND (`VIP`=?)', sql: 'WHERE ((`NAME`=?) AND (`VIP`=?))',
params: ['John', true], params: ['John', true],
}); });
}); });
@ -238,7 +238,7 @@ describe('sql connector', function() {
const where = connector.buildWhere('customer', const where = connector.buildWhere('customer',
{and: [{name: 'John'}, {or: [{vip: true}, {address: null}]}]}); {and: [{name: 'John'}, {or: [{vip: true}, {address: null}]}]});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE (`NAME`=?) AND ((`VIP`=?) OR (`ADDRESS` IS NULL))', sql: 'WHERE ((`NAME`=?) AND (((`VIP`=?) OR (`ADDRESS` IS NULL))))',
params: ['John', true], params: ['John', true],
}); });
}); });
@ -341,6 +341,21 @@ describe('sql connector', function() {
}); });
it('builds SELECT', function() { it('builds SELECT', function() {
const sql = connector.buildSelect('customer', {
order: 'name',
limit: 5,
where: {or: [{name: 'Top Cat'}, {address: 'Trash can'}], vip: true},
});
expect(sql.toJSON()).to.eql({
sql:
'SELECT `NAME`,`middle_name`,`LASTNAME`,`VIP`,`primary_address`,' +
'`ADDRESS` FROM `CUSTOMER` WHERE ((`NAME`=$1) OR (`ADDRESS`=$2)) ' +
'AND `VIP`=$3 ORDER BY `NAME` LIMIT 5',
params: ['Top Cat', 'Trash can', true],
});
});
it('builds SELECT with where', function() {
const sql = connector.buildSelect('customer', const sql = connector.buildSelect('customer',
{order: 'name', limit: 5, where: {name: 'John'}}); {order: 'name', limit: 5, where: {name: 'John'}});
expect(sql.toJSON()).to.eql({ expect(sql.toJSON()).to.eql({