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 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
4 changes: 3 additions & 1 deletion ext/mysql2/result.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ 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
if (seconds == 0) {
val = Qnil;
} else 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);
Expand Down
5 changes: 5 additions & 0 deletions spec/mysql2/result_spec.rb
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
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