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

Fix zero-datetime in statement result raises ArgumentError #1054

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 23 additions & 19 deletions ext/mysql2/result.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -419,27 +419,31 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
ts = (MYSQL_TIME*)result_buffer->buffer;
seconds = (ts->year*31557600ULL) + (ts->month*2592000ULL) + (ts->day*86400ULL) + (ts->hour*3600ULL) + (ts->minute*60ULL) + ts->second;

if (seconds < MYSQL2_MIN_TIME || seconds > MYSQL2_MAX_TIME) { // use DateTime instead
VALUE offset = INT2NUM(0);
if (args->db_timezone == intern_local) {
offset = rb_funcall(cMysql2Client, intern_local_offset, 0);
}
val = rb_funcall(cDateTime, intern_civil, 7, UINT2NUM(ts->year), UINT2NUM(ts->month), UINT2NUM(ts->day), UINT2NUM(ts->hour), UINT2NUM(ts->minute), UINT2NUM(ts->second), offset);
if (!NIL_P(args->app_timezone)) {
if (args->app_timezone == intern_local) {
if (seconds == 0) {
val = Qnil;
} else {
if (seconds < MYSQL2_MIN_TIME || seconds > MYSQL2_MAX_TIME) { // use DateTime instead
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be equivalent to move this up as } else if (seconds...) {, no need to indent the entire if block.

VALUE offset = INT2NUM(0);
if (args->db_timezone == intern_local) {
offset = rb_funcall(cMysql2Client, intern_local_offset, 0);
val = rb_funcall(val, intern_new_offset, 1, offset);
} else { // utc
val = rb_funcall(val, intern_new_offset, 1, opt_utc_offset);
}
}
} else {
val = rb_funcall(rb_cTime, args->db_timezone, 7, UINT2NUM(ts->year), UINT2NUM(ts->month), UINT2NUM(ts->day), UINT2NUM(ts->hour), UINT2NUM(ts->minute), UINT2NUM(ts->second), ULONG2NUM(ts->second_part));
if (!NIL_P(args->app_timezone)) {
if (args->app_timezone == intern_local) {
val = rb_funcall(val, intern_localtime, 0);
} else { // utc
val = rb_funcall(val, intern_utc, 0);
val = rb_funcall(cDateTime, intern_civil, 7, UINT2NUM(ts->year), UINT2NUM(ts->month), UINT2NUM(ts->day), UINT2NUM(ts->hour), UINT2NUM(ts->minute), UINT2NUM(ts->second), offset);
if (!NIL_P(args->app_timezone)) {
if (args->app_timezone == intern_local) {
offset = rb_funcall(cMysql2Client, intern_local_offset, 0);
val = rb_funcall(val, intern_new_offset, 1, offset);
} else { // utc
val = rb_funcall(val, intern_new_offset, 1, opt_utc_offset);
}
}
} else {
val = rb_funcall(rb_cTime, args->db_timezone, 7, UINT2NUM(ts->year), UINT2NUM(ts->month), UINT2NUM(ts->day), UINT2NUM(ts->hour), UINT2NUM(ts->minute), UINT2NUM(ts->second), ULONG2NUM(ts->second_part));
if (!NIL_P(args->app_timezone)) {
if (args->app_timezone == intern_local) {
val = rb_funcall(val, intern_localtime, 0);
} else { // utc
val = rb_funcall(val, intern_utc, 0);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions spec/mysql2/result_spec.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@
expect(r.first['test']).to be_an_instance_of(Time)
end

it "should return nil when timestamp is 0000-00-00T00:00:00" do
r = @client.query("SELECT CAST('0000-00-00 00:00:00' AS DATETIME) as test")
expect(r.first['test']).to be_nil
end

it "should return Time for a TIMESTAMP value when within the supported range" do
expect(test_result['timestamp_test']).to be_an_instance_of(Time)
expect(test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
Expand Down
5 changes: 5 additions & 0 deletions spec/mysql2/statement_spec.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ def stmt_count
expect(r.first['test']).to be_an_instance_of(Time)
end

it "should return nil when timestamp is 0000-00-00T00:00:00" do
r = @client.prepare("SELECT CAST('0000-00-00 00:00:00' AS DATETIME) as test").execute
expect(r.first['test']).to be_nil
end

it "should return Time for a TIMESTAMP value when within the supported range" do
expect(test_result['timestamp_test']).to be_an_instance_of(Time)
expect(test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
Expand Down