Skip to content

Commit

Permalink
Fix: mysql2 query argument order compatibility (#381)
Browse files Browse the repository at this point in the history
Fixes issue with mysql2 query where 1st argument is of type Object, 2nd argument is an array of values for binding to sql query, and 3rd argument is callback.

fixes #380
  • Loading branch information
bryanvaz authored Feb 25, 2021
1 parent a53ef08 commit 072c0a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/mysql/lib/mysql_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,20 @@ function resolveArguments(argsObj) {
if (argsObj && argsObj.length > 0) {
if (argsObj[0] instanceof Object) {
args.sql = argsObj[0];
args.values = argsObj[0].values;
args.callback = argsObj[1];

// Patch for mysql2
if (argsObj[0].values) {
args.values = argsObj[0].values; // mysql implementation
} else if(typeof argsObj[2] === 'function'){
args.values = typeof argsObj[1] !== 'function' ? argsObj[1] : null; // mysql2 implementation
}
args.callback = typeof argsObj[1] === 'function'
? argsObj[1]
: (
typeof argsObj[2] === 'function'
? argsObj[2]
: undefined
);
if (!argsObj[1] && argsObj[0].on instanceof Function) args.sql = argsObj[0];
} else {
args.sql = argsObj[0];
Expand Down
19 changes: 19 additions & 0 deletions packages/mysql/test/unit/mysql_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ describe('captureMySQL', function() {

assert.equal(stubBaseQuery.args[0][2].name, 'autoContext');
});

it('should capture arguments correctly when using mysql2 argument order', function (done) {
var stubClose = sandbox.stub(subsegment, 'close');
var session = { run: function (fcn) { fcn(); } };
var stubRun = sandbox.stub(session, 'run');
var typeCast = sinon.stub();

sandbox.stub(AWSXRay, 'getNamespace').returns(session);
query.call(connectionObj, { sql: 'sql with values binding = ?', timeout: 234, typeCast, nestTables: true },['binding value'], function () { });

stubBaseQuery.should.have.been.calledWith({ sql: 'sql with values binding = ?', timeout: 234, typeCast, nestTables: true }, ['binding value'], sinon.match.func);
stubBaseQuery.args[0][2].call(queryObj);

setTimeout(function () {
stubClose.should.always.have.been.calledWith();
stubRun.should.have.been.calledOnce;
done();
}, 50);
});
});
});

Expand Down

0 comments on commit 072c0a9

Please sign in to comment.