Skip to content

Commit

Permalink
MySQLTable: Fix bug in .insert() where sqlString was ignored if data …
Browse files Browse the repository at this point in the history
…was an array
  • Loading branch information
nwoltman committed Mar 19, 2017
1 parent f4c9d92 commit 55cc324
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
7 changes: 5 additions & 2 deletions lib/MySQLTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ class MySQLTable {
}

if (data instanceof Array) {
data = data.length > 1
? ' (' + this._db.escapeId(data[0]) + ') VALUES ' + this._db.escape(data[1])
: ' VALUES ' + this._db.escape(data[0]);

return this._db.pquery(
'INSERT INTO ' + this._escapedName + (data.length > 1 ? ' (??)' : '') + ' VALUES ?',
data,
'INSERT INTO ' + this._escapedName + data + ' ' + sqlString,
cb
);
}
Expand Down
81 changes: 61 additions & 20 deletions test/unit/MySQLTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,47 @@ describe('MySQLTable', () => {

it('should insert the specified data into the table with an ON DUPLICATE KEY UPDATE clause', done => {
const data = {id: 1, email: 'one@email.com'};
const onDuplicateKey = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'";
testTable.insert(data, onDuplicateKey, (err, result) => {
const onDuplicateKey1 = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'";

testTable.insert(data, onDuplicateKey1, (err, result1) => {
if (err) throw err;
result.affectedRows.should.equal(2); // Updated rows are affected twice
result.insertId.should.equal(1);
done();
result1.affectedRows.should.equal(2); // Updated rows are affected twice
result1.insertId.should.equal(1);

const columns = Object.keys(data);
const rows = [
[data[columns[0]], data[columns[1]]],
];
const onDuplicateKey2 = "ON DUPLICATE KEY UPDATE `email` = 'one2b@email.com'";

testTable.insert([columns, rows], onDuplicateKey2, (err, result2) => {
if (err) throw err;
result2.affectedRows.should.equal(2);
result2.insertId.should.equal(1);
done();
});
});
});

it('should insert data with question marks into the table when using the `sqlString` and `values` parameters', done => {
const data = {id: 1, email: '??one?@email.com'};
const onDuplicateKey = 'ON DUPLICATE KEY UPDATE ?? = ?';
testTable.insert(data, onDuplicateKey, ['email', 'one3@email.com'], (err, result) => {

testTable.insert(data, onDuplicateKey, ['email', 'one3@email.com'], (err, result1) => {
if (err) throw err;
result.affectedRows.should.equal(2); // Updated rows are affected twice
result.insertId.should.equal(1);
done();
result1.affectedRows.should.equal(2); // Updated rows are affected twice
result1.insertId.should.equal(1);

const columns = Object.keys(data);
const rows = [
[data[columns[0]], data[columns[1]]],
];
testTable.insert([columns, rows], onDuplicateKey, ['email', 'one4@email.com'], (err, result2) => {
if (err) throw err;
result2.affectedRows.should.equal(2);
result2.insertId.should.equal(1);
done();
});
});
});

Expand Down Expand Up @@ -311,24 +335,41 @@ describe('MySQLTable', () => {

it('should insert the specified data into the table with an ON DUPLICATE KEY UPDATE clause', () => {
const data = {id: 1, email: 'one@email.com'};
const onDuplicateKey = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'";
const columns = Object.keys(data);
const rows = [
[data[columns[0]], data[columns[1]]],
];
const onDuplicateKey1 = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'";
const onDuplicateKey2 = "ON DUPLICATE KEY UPDATE `email` = 'one2b@email.com'";

return testTable.insert(data, onDuplicateKey)
.then(result => {
result.affectedRows.should.equal(2); // Updated rows are affected twice
result.insertId.should.equal(1);
});
return Promise.all([
testTable.insert(data, onDuplicateKey1),
testTable.insert([columns, rows], onDuplicateKey2),
]).then(results => {
results[0].affectedRows.should.equal(2); // Updated rows are affected twice
results[0].insertId.should.equal(1);
results[1].affectedRows.should.equal(2);
results[1].insertId.should.equal(1);
});
});

it('should insert data with question marks into the table when using the `sqlString` and `values` parameters', () => {
const data = {id: 1, email: '??one?@email.com'};
const columns = Object.keys(data);
const rows = [
[data[columns[0]], data[columns[1]]],
];
const onDuplicateKey = 'ON DUPLICATE KEY UPDATE ?? = ?';

return testTable.insert(data, onDuplicateKey, ['email', 'one3@email.com'])
.then(result => {
result.affectedRows.should.equal(2); // Updated rows are affected twice
result.insertId.should.equal(1);
});
return Promise.all([
testTable.insert(data, onDuplicateKey, ['email', 'one3@email.com']),
testTable.insert([columns, rows], onDuplicateKey, ['email', 'one3b@email.com']),
]).then(results => {
results[0].affectedRows.should.equal(2); // Updated rows are affected twice
results[0].insertId.should.equal(1);
results[1].affectedRows.should.equal(2);
results[1].insertId.should.equal(1);
});
});

it('should be able to perform bulk inserts', () => {
Expand Down

0 comments on commit 55cc324

Please sign in to comment.