crystal-lang / crystal-mysql

MySQL connector for Crystal
MIT License
107 stars 36 forks source link

Register custom decoder? #66

Closed Blacksmoke16 closed 5 years ago

Blacksmoke16 commented 6 years ago

Are there any docs on how to register a custom decoder, similar to the PG adapter shard? Or is that not a thing?

Use case being wanting to control how a custom type gets saved and read from db.

bcardiff commented 6 years ago

No there is not.

The best I can say today is to check https://github.com/crystal-lang/crystal-mysql/blob/master/src/mysql/types.cr . The decl_type macro declares a struct that write/read (binary protocol)/parse (text protocol) values from the connection.

MySql::Type are grabbed by the column type id and those are stored in a class variable hash. That is filled by the decl_type directly. Later included types should override previous ones since code should be run in order.

ysbaddaden commented 6 years ago

Here is an example to transparently encode/decode the UUID type to MySQL: https://github.com/ysbaddaden/rome/blob/master/src/mysql_ext.cr