Skip to content

Commit

Permalink
Merge pull request #88 from crystal-lang/crystal/0.34.0
Browse files Browse the repository at this point in the history
Update to Crystal 0.34.0
  • Loading branch information
Brian J. Cardiff authored Apr 7, 2020
2 parents e37351c + cfa4e09 commit 44aa04b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 53 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 0.10.0
dependencies:
db:
github: crystal-lang/crystal-db
version: ~> 0.8.0
version: ~> 0.9.0

authors:
- Juan Wajnerman <jwajnerman@manas.tech>
Expand Down
14 changes: 5 additions & 9 deletions spec/db_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ DB::DriverSpecs(MySql::Any).run do
sample_value Time.utc(2016, 2, 15), "datetime", "TIMESTAMP '2016-02-15 00:00:00.000'"
sample_value Time.utc(2016, 2, 15, 10, 15, 30), "datetime", "TIMESTAMP '2016-02-15 10:15:30.000'"
sample_value Time.utc(2016, 2, 15, 10, 15, 30), "timestamp", "TIMESTAMP '2016-02-15 10:15:30.000'"
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
sample_value Time.local(2016, 2, 15, 7, 15, 30, location: Time::Location.fixed("fixed", -3*3600)), "timestamp", "'2016-02-15 10:15:30.000'", type_safe_value: false
{% else %}
sample_value Time.new(2016, 2, 15, 7, 15, 30, location: Time::Location.fixed("fixed", -3*3600)), "timestamp", "'2016-02-15 10:15:30.000'", type_safe_value: false
{% end %}
sample_value Time.local(2016, 2, 15, 7, 15, 30, location: Time::Location.fixed("fixed", -3*3600)), "timestamp", "'2016-02-15 10:15:30.000'", type_safe_value: false
sample_value Time.utc(2016, 2, 29), "date", "LAST_DAY('2016-02-15')", type_safe_value: false
sample_value Time::Span.new(nanoseconds: 0), "Time", "TIME('00:00:00')"
sample_value Time::Span.new(10, 25, 21), "Time", "TIME('10:25:21')"
sample_value Time::Span.new(0, 0, 10, 5, 0), "Time", "TIME('00:10:05.000')"
sample_value Time::Span.new(hours: 10, minutes: 25, seconds: 21), "Time", "TIME('10:25:21')"
sample_value Time::Span.new(days: 0, hours: 0, minutes: 10, seconds: 5, nanoseconds: 0), "Time", "TIME('00:10:05.000')"

DB.open db_url do |db|
# needs to check version, microsecond support >= 5.7
Expand All @@ -56,8 +52,8 @@ DB::DriverSpecs(MySql::Any).run do
sample_value Time.utc(2016, 2, 15, 10, 15, 30, nanosecond: 543_012_000), "datetime(6)", "TIMESTAMP '2016-02-15 10:15:30.543012'"
sample_value Time.utc(2016, 2, 15, 10, 15, 30, nanosecond: 543_000_000), "timestamp(3)", "TIMESTAMP '2016-02-15 10:15:30.543'"
sample_value Time.utc(2016, 2, 15, 10, 15, 30, nanosecond: 543_012_000), "timestamp(6)", "TIMESTAMP '2016-02-15 10:15:30.543012'"
sample_value Time::Span.new(0, 10, 15, 30, nanoseconds: 543_000_000), "Time(3)", "TIME '10:15:30.543'"
sample_value Time::Span.new(0, 10, 15, 30, nanoseconds: 543_012_000), "Time(6)", "TIME '10:15:30.543012'"
sample_value Time::Span.new(days: 0, hours: 10, minutes: 15, seconds: 30, nanoseconds: 543_000_000), "Time(3)", "TIME '10:15:30.543'"
sample_value Time::Span.new(days: 0, hours: 10, minutes: 15, seconds: 30, nanoseconds: 543_012_000), "Time(6)", "TIME '10:15:30.543012'"
end
end

Expand Down
8 changes: 7 additions & 1 deletion src/mysql/connection.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require "socket"

class MySql::Connection < DB::Connection
{% if compare_versions(Crystal::VERSION, "0.34.0-0") > 0 %}
alias IOErrorOrErrno = IO::Error
{% else %}
alias IOErrorOrErrno = Errno
{% end %}

def initialize(context : DB::ConnectionContext)
super(context)
@socket = uninitialized TCPSocket
Expand Down Expand Up @@ -28,7 +34,7 @@ class MySql::Connection < DB::Connection
read_ok_or_err do |packet, status|
raise "packet #{status} not implemented"
end
rescue Errno
rescue IOErrorOrErrno
raise DB::ConnectionRefused.new
end
end
Expand Down
52 changes: 10 additions & 42 deletions src/mysql/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ abstract struct MySql::Type
d = v.days
raise ArgumentError.new("MYSQL TIME over 34 days cannot be saved - https://dev.mysql.com/doc/refman/5.7/en/time.html") if d > 34
microsecond : Int32
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
microsecond = (v.nanoseconds // 1000).to_i32
{% else %}
microsecond = (v.nanoseconds / 1000).to_i32
{% end %}
microsecond = (v.nanoseconds // 1000).to_i32
packet.write_blob UInt8.slice(
negative, d.to_i8, (d >> 8).to_i8, (d >> 16).to_i8, (d >> 24).to_i8,
v.hours.to_i8, v.minutes.to_i8, v.seconds.to_i8,
Expand All @@ -211,7 +207,7 @@ abstract struct MySql::Type
minute = packet.read_byte!.to_i32
second = packet.read_byte!.to_i32
ns = pkt > 8 ? (packet.read_int.to_i32 * 1000) : nil
time = ns ? ::Time::Span.new(days, hour, minute, second, nanoseconds: ns) : ::Time::Span.new(days, hour, minute, second)
time = ns ? ::Time::Span.new(days: days, hours: hour, minutes: minute, seconds: second, nanoseconds: ns) : ::Time::Span.new(days: days, hours: hour, minutes: minute, seconds: second)
negative > 0 ? (::Time::Span.new(nanoseconds: 0) - time) : time
end

Expand All @@ -222,25 +218,17 @@ abstract struct MySql::Type
rescue
time = ::Time.parse(str, "%H:%M:%S", location: MySql::TIME_ZONE)
end
::Time::Span.new(0, time.hour, time.minute, time.second, nanoseconds: time.nanosecond)
::Time::Span.new(days: 0, hours: time.hour, minutes: time.minute, seconds: time.second, nanoseconds: time.nanosecond)
end
end
decl_type DateTime, 0x0cu8, ::Time do
def self.write(packet, v : ::Time)
v = v.in(location: MySql::TIME_ZONE)
microsecond : Int32
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
microsecond = (v.nanosecond // 1000).to_i32
{% else %}
microsecond = (v.nanosecond / 1000).to_i32
{% end %}
microsecond = (v.nanosecond // 1000).to_i32
packet.write_blob UInt8.slice(
v.year.to_i16,
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
v.year.to_i16 // 256
{% else %}
v.year.to_i16 / 256
{% end %},
v.year.to_i16 // 256,
v.month.to_i8, v.day.to_i8,
v.hour.to_i8, v.minute.to_i8, v.second.to_i8,
(microsecond & 0x000000FF).to_u8,
Expand All @@ -252,41 +240,21 @@ abstract struct MySql::Type

def self.read(packet)
pkt = packet.read_byte!
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
return ::Time.local(0, 0, 0, location: MySql::TIME_ZONE) if pkt < 1
{% else %}
return ::Time.new(0, 0, 0, location: MySql::TIME_ZONE) if pkt < 1
{% end %}
return ::Time.local(0, 0, 0, location: MySql::TIME_ZONE) if pkt < 1
year = packet.read_fixed_int(2).to_i32
month = packet.read_byte!.to_i32
day = packet.read_byte!.to_i32
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
return ::Time.local(year, month, day, location: MySql::TIME_ZONE) if pkt < 6
{% else %}
return ::Time.new(year, month, day, location: MySql::TIME_ZONE) if pkt < 6
{% end %}
return ::Time.local(year, month, day, location: MySql::TIME_ZONE) if pkt < 6
hour = packet.read_byte!.to_i32
minute = packet.read_byte!.to_i32
second = packet.read_byte!.to_i32
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
return ::Time.local(year, month, day, hour, minute, second, location: MySql::TIME_ZONE) if pkt < 8
{% else %}
return ::Time.new(year, month, day, hour, minute, second, location: MySql::TIME_ZONE) if pkt < 8
{% end %}
return ::Time.local(year, month, day, hour, minute, second, location: MySql::TIME_ZONE) if pkt < 8
ns = packet.read_int.to_i32 * 1000
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
return ::Time.local(year, month, day, hour, minute, second, nanosecond: ns, location: MySql::TIME_ZONE)
{% else %}
return ::Time.new(year, month, day, hour, minute, second, nanosecond: ns, location: MySql::TIME_ZONE)
{% end %}
return ::Time.local(year, month, day, hour, minute, second, nanosecond: ns, location: MySql::TIME_ZONE)
end

def self.parse(str : ::String)
{% if compare_versions(Crystal::VERSION, "0.28.0") >= 0 %}
return ::Time.local(0, 0, 0, location: MySql::TIME_ZONE) if str.starts_with?("0000-00-00")
{% else %}
return ::Time.new(0, 0, 0, location: MySql::TIME_ZONE) if str.starts_with?("0000-00-00")
{% end %}
return ::Time.local(0, 0, 0, location: MySql::TIME_ZONE) if str.starts_with?("0000-00-00")
begin
begin
::Time.parse(str, "%F %H:%M:%S.%N", location: MySql::TIME_ZONE)
Expand Down

0 comments on commit 44aa04b

Please sign in to comment.