Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: mysql2 query argument order compatibility #381

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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