From 00e010197d8f09c542b16125b2ad2fa0c5abf3b4 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Wed, 25 Mar 2020 10:40:28 -0300 Subject: [PATCH 1/4] Use IO::Error instead of Errno when possible --- src/mysql/connection.cr | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mysql/connection.cr b/src/mysql/connection.cr index fbcab3c..ee50db6 100644 --- a/src/mysql/connection.cr +++ b/src/mysql/connection.cr @@ -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 @@ -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 From 04c833967761040cffb87fcbcff8b28ff78eef12 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Wed, 25 Mar 2020 11:42:49 -0300 Subject: [PATCH 2/4] Use named arguments for Time::Span.new --- spec/db_spec.cr | 8 ++++---- src/mysql/types.cr | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/db_spec.cr b/spec/db_spec.cr index 973bf89..05a6acc 100644 --- a/spec/db_spec.cr +++ b/spec/db_spec.cr @@ -45,8 +45,8 @@ DB::DriverSpecs(MySql::Any).run do {% end %} 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 @@ -56,8 +56,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 diff --git a/src/mysql/types.cr b/src/mysql/types.cr index 01c8ee0..c99d84c 100644 --- a/src/mysql/types.cr +++ b/src/mysql/types.cr @@ -211,7 +211,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 @@ -222,7 +222,7 @@ 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 From 3f22b0266f576ff9ed9536ea0e79f9324deb5c86 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Wed, 25 Mar 2020 11:44:44 -0300 Subject: [PATCH 3/4] Code cleanup since Crystal >= 0.30 is required --- spec/db_spec.cr | 6 +----- src/mysql/types.cr | 48 ++++++++-------------------------------------- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/spec/db_spec.cr b/spec/db_spec.cr index 05a6acc..325a752 100644 --- a/spec/db_spec.cr +++ b/spec/db_spec.cr @@ -38,11 +38,7 @@ 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(hours: 10, minutes: 25, seconds: 21), "Time", "TIME('10:25:21')" diff --git a/src/mysql/types.cr b/src/mysql/types.cr index c99d84c..a16f7cc 100644 --- a/src/mysql/types.cr +++ b/src/mysql/types.cr @@ -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, @@ -229,18 +225,10 @@ abstract struct MySql::Type 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, @@ -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) From cfa4e09df86340d08a4c22f130e1f4db56650370 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Mon, 6 Apr 2020 20:15:43 -0300 Subject: [PATCH 4/4] Update to crystal-db ~> 0.9.0 --- shard.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shard.yml b/shard.yml index 00524f8..0327cbe 100644 --- a/shard.yml +++ b/shard.yml @@ -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