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

Added JSON Type #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/mysql/types.cr
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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