sidorares/node-mysql2

typeCast not supported with .execute

aikar opened this issue ยท 8 comments

aikar commented

using typeCast option in .query works, but does not work in .execute. The field is not type casted.

typeCast only documented to work with .query()

The aim of typeCast() is to 1) give access to internal raw representation of data and 2) conrol what's generated out of this raw data

With .execute() this raw data is completely different ( prepared statements use so called 'binary protocol' and plain queries 'text protocol' )

Also I think current api of typeCast is flawed: you as consumer responsible of reading all raw data in full and in correct order. If you miss something everything breaks.

aikar commented

@sidorares I understand why it's more difficult to do, but the options really should work for both methods...

I ultimately had to re-implement typeCast in an abstraction layer that any time .execute is called on my layer, it immediately iterates the fields response and does type conversion.

For example, someone decided to use a VARBINARY column type for string keys in order to enforce case sensitivity (very old table before mysql supported this natively), and I now need to convert that to string.

A solution is to simply take the fields metadata response, and rebuild the same data structure the .query based call is supplied.

I'll try to check how easy to do that, maybe actually not that difficult

I am trying to make Sequelize use binded parameters here. The Sequelize team prefers node-mysql2 as a mysql driver, but unfortunately Sequelize needs typeCast() functionality.

Also, the overall information returned by the execute() method is poorer than for query().

Apart from applying timezone, typeCast is also useful to convert geometries to geoJSON format. If it is not possible to add typeCast support for .execute, perhaps some of these common typeCasts can be implemented as built-in options for node-mysql2, e.g. options.geoJSON = true.

I've been using typeCast to convert bit(1) columns from Buffer to boolean. I couldn't figure out why it stopped working until I realized a changed query to execute and found this issue. Even if it there's not an easy fix, it would be a big help to document the difference and possibly generate a warning the first time execute is used on a connection with a typeCast. I'll submit a PR for either or both if you want @sidorares.

While I'm on the subject, would it be reasonable to have a built-in conversion for bit(1) returned as boolean? For compatibility, it could be enabled via an option. I could submit a PR for that as well.

@aikar, @javiertury, @trevorr and @coreyjv (#717)

It's been a while, but there is a PR (#2398) that implements typeCast for the execute method in both connection level and query level with usage exactly as it is for query.

It would be great if you could check.
I'd like to hear positive or negative feedbacks too ๐Ÿ™‹๐Ÿปโ€โ™‚๏ธ

Closing due to #2398 ๐ŸŽ‰

Please feel free to ask anything.