From 3fdd4d48bcfafae9815a07bbb7f1ef85c044fdad Mon Sep 17 00:00:00 2001 From: Aurimas Niekis Date: Tue, 17 Jan 2017 16:07:10 +0100 Subject: [PATCH 1/2] Added JSON Type --- src/mysql/types.cr | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/mysql/types.cr b/src/mysql/types.cr index 47ba1c8..894f6e4 100644 --- a/src/mysql/types.cr +++ b/src/mysql/types.cr @@ -3,6 +3,10 @@ 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 +60,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 +241,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 From 53772223a3a559bd3e6df096e821816cfebdb54f Mon Sep 17 00:00:00 2001 From: Aurimas Niekis Date: Tue, 17 Jan 2017 16:11:55 +0100 Subject: [PATCH 2/2] Forgot to add import for `json` --- src/mysql/types.cr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mysql/types.cr b/src/mysql/types.cr index 894f6e4..524f44c 100644 --- a/src/mysql/types.cr +++ b/src/mysql/types.cr @@ -1,3 +1,5 @@ +require "json" + # :nodoc: abstract struct MySql::Type # Column types