Skip to content

Commit

Permalink
Fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Farris committed Jul 26, 2018
1 parent e9f0545 commit ba13b55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TESTS = test/mysql/*.js test/mssql/*.js test/*.js
#TESTS = test/mssql/05-tests-multiple-pools.js
#TESTS = test/mysql/03-tests-update_batch.js
#TESTS = test/05-multiple-drivers.js
test:
mocha --timeout 5000 --reporter spec $(TESTS)
Expand Down
12 changes: 11 additions & 1 deletion drivers/query_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1529,8 +1529,18 @@ class GenericQueryBuilder {
cases += `ELSE ${l} END, `;
}

// Remove the trailing comma
sql += cases.substr(0, cases.length - 2);
sql += ` WHERE ${where + index} IN (${ids.join(',')})`;

// Make sure we don't double-up on the "WHERE" directive
if (where) {
sql += ` ${where}`;
} else {
sql += ' WHERE ';
}


sql += `${index} IN (${ids.join(',')})`;

// Add query to batch
batches.push(sql);
Expand Down
7 changes: 6 additions & 1 deletion test/mysql/03-tests-update_batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const expect = require('chai').expect;
const QueryBuilder = require('../../drivers/mysql/query_builder.js');
const qb = new QueryBuilder();

const test_where = {id:3};
const test_where = {quadrant: 'Alpha'};
const test_data = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'Andromeda', type: 'spiral'}];

describe('MySQL: update_batch()', () => {
Expand All @@ -18,4 +18,9 @@ describe('MySQL: update_batch()', () => {
const sql = qb.update_batch('galaxies', test_data, 'id');
sql.should.eql(["UPDATE (`galaxies`) SET `name` = CASE WHEN `id` = 3 THEN 'Milky Way' WHEN `id` = 4 THEN 'Andromeda' ELSE `name` END, `type` = CASE WHEN `id` = 3 THEN 'spiral' WHEN `id` = 4 THEN 'spiral' ELSE `type` END WHERE `id` IN (3,4)"]);
});
it('should build a proper batch UPDATE string when where clause is provided', () => {
qb.reset_query();
const sql = qb.update_batch('galaxies', test_data, 'id', test_where);
sql.should.eql(["UPDATE (`galaxies`) SET `name` = CASE WHEN `id` = 3 THEN 'Milky Way' WHEN `id` = 4 THEN 'Andromeda' ELSE `name` END, `type` = CASE WHEN `id` = 3 THEN 'spiral' WHEN `id` = 4 THEN 'spiral' ELSE `type` END WHERE `quadrant` = 'Alpha' AND `id` IN (3,4)"]);
});
});

0 comments on commit ba13b55

Please sign in to comment.