diff --git a/src/mysql/types.cr b/src/mysql/types.cr index 47ba1c8..524f44c 100644 --- a/src/mysql/types.cr +++ b/src/mysql/types.cr @@ -1,8 +1,14 @@ +require "json" + # :nodoc: abstract struct MySql::Type # Column types # http://dev.mysql.com/doc/internals/en/com-query-response.html#column-type + # Remove after https://github.com/crystal-lang/crystal/pull/3908 is accepted + alias JsonType = ::JSON::Type | Int32 | Array(Int32) | Hash(String, Int32) + + @@types_by_code = Hash(UInt8, MySql::Type.class).new @@hex_value : UInt8 = 0x00u8 @@ -56,6 +62,10 @@ abstract struct MySql::Type MySql::Type::Null end + def self.type_for(t : JsonType.class) + MySql::Type::JSON + end + def self.type_for(t) raise "MySql::Type does not support #{t} values" end @@ -233,5 +243,18 @@ abstract struct MySql::Type str end end + decl_type JSON, 0xf5u8, ::String do + def self.write(packet, v : JsonType) + packet.write_lenenc_string v.to_json + end + + def self.read(packet) + JSON.parse packet.read_lenenc_string + end + + def self.parse(str : ::String) + JSON.parse str + end + end decl_type Geometry, 0xffu8 end