-
-
Notifications
You must be signed in to change notification settings - Fork 37
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 basic JSON column support #65
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require "json" | ||
|
||
# :nodoc: | ||
abstract struct MySql::Type | ||
# Column types | ||
|
@@ -60,6 +62,10 @@ abstract struct MySql::Type | |
MySql::Type::Null | ||
end | ||
|
||
def self.type_for(t : ::JSON::Any.class) | ||
MySql::Type::Json | ||
end | ||
|
||
def self.type_for(t) | ||
raise "MySql::Type does not support #{t} values" | ||
end | ||
|
@@ -144,6 +150,7 @@ abstract struct MySql::Type | |
nil | ||
end | ||
end | ||
|
||
decl_type Timestamp, 0x07u8, ::Time do | ||
def self.write(packet, v : ::Time) | ||
MySql::Type::DateTime.write(packet, v) | ||
|
@@ -351,4 +358,28 @@ abstract struct MySql::Type | |
end | ||
end | ||
decl_type Geometry, 0xffu8 | ||
|
||
# Parse the JSON column type | ||
decl_type Json, 245_u8, JSON::Any do | ||
def self.write(packet, v : String) | ||
packet.write_lenenc_string v | ||
end | ||
|
||
def self.write(packet, v : ::JSON::Any) | ||
packet.write_lenenc_string v.to_json | ||
end | ||
|
||
def self.read(packet) | ||
str = packet.read_lenenc_string.lchop("\u0013") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does MySQL sends trailing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems to be a prefix for all the JSON data. I have not been able to find mysql documentation on it however. |
||
JSON.parse(str) | ||
rescue e | ||
nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why exceptions should be swallowed? |
||
end | ||
|
||
def self.parse(str : ::String) | ||
JSON.parse(str) | ||
rescue e | ||
nil | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it would be a
type_safe_value
why it's not?