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

typeCast does not work for the execute method #3368

Open
joakimbeng opened this issue Feb 3, 2025 · 4 comments
Open

typeCast does not work for the execute method #3368

joakimbeng opened this issue Feb 3, 2025 · 4 comments
Labels

Comments

@joakimbeng
Copy link

I am unable to get the typeCast option to work at all for the execute method. I have tried configuring it both on the connection level and per query, but with no success.

I have set up a minimal reproduction example here: https://github.com/joakimbeng/mysql2-bug

The reproduction repo uses mysql2 v3.12.0 and node v22.

Am I missing something or shouldn't the typeCast option work with both the query and execute methods since v3.9.0?

@sidorares
Copy link
Owner

Hi @joakimbeng - please read #1446 and linked issues, hope that will give you a context to explain current behavior and future plans

@wellwelwel wellwelwel added the bug label Feb 4, 2025
@wellwelwel
Copy link
Collaborator

wellwelwel commented Feb 4, 2025

This is occurring because the null value is not being passed through typeCast:

parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit}) `);
parserFn(`${lvalue} = null;`);

An approach would be to check if the typeCast is a function and, if so, return the null through typeCast, for example:

parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit}) {`);
if (typeof options.typeCast === 'function') {
  parserFn(`${lvalue} = options.typeCast(wrap(fields[${i}], packet), () => null);`);
} else {
  parserFn(`${lvalue} = null;`);
}
parserFn('} else {');
  • Not a real fix, but it would fix this bug:

Note

typeCast is not an actively maintained feature because of its planned deprecation, but I'm open to fix it 🙋🏻‍♂️

@joakimbeng
Copy link
Author

@wellwelwel I see!
So it's only for NULL values that it behaves differently than typeCast for the query method then?

If it's such an easy fix I'm all for it! 😃 It would be greatly appreciated if you would release that fix!

@wellwelwel
Copy link
Collaborator

wellwelwel commented Feb 4, 2025

@joakimbeng, there is a check to ensure if typeCast is a function, but it occurs after the null has already been returned:

if (typeof options.typeCast === 'function') {
parserFn(
`${lvalue} = options.typeCast(${fieldWrapperVar}, function() { return ${readCode} });`,
);
} else {

The idea for a fix follows the same idea I mentioned, but would require a small refactoring in the order of the logic.

If it's such an easy fix I'm all for it!

Feel free to do so 🚀

  • I'll merge feat(disableEval): add static parsers #3365 tomorrow (already merged), which will allow to use both the query and execute methods without eval. We can apply this fix to both sides afterwards 🙋🏻‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants