Skip to content

Commit 78d59bc

Browse files
committed
Merge pull request #528 from jakedouglas/better_bad_datetime_error
state the name of the field that contains a bad datetime
2 parents b4bffb7 + 33e2893 commit 78d59bc

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

ext/mysql2/result.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
325325
val = Qnil;
326326
} else {
327327
if (month < 1 || day < 1) {
328-
rb_raise(cMysql2Error, "Invalid date: %s", row[i]);
328+
rb_raise(cMysql2Error, "Invalid date in field '%.*s': %s", fields[i].name_length, fields[i].name, row[i]);
329329
val = Qnil;
330330
} else {
331331
if (seconds < MYSQL2_MIN_TIME || seconds > MYSQL2_MAX_TIME) { /* use DateTime for larger date range, does not support microseconds */
@@ -370,7 +370,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
370370
val = Qnil;
371371
} else {
372372
if (month < 1 || day < 1) {
373-
rb_raise(cMysql2Error, "Invalid date: %s", row[i]);
373+
rb_raise(cMysql2Error, "Invalid date in field '%.*s': %s", fields[i].name_length, fields[i].name, row[i]);
374374
val = Qnil;
375375
} else {
376376
val = rb_funcall(cDate, intern_new, 3, UINT2NUM(year), UINT2NUM(month), UINT2NUM(day));

spec/mysql2/result_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@
335335
@test_result['enum_test'].should eql('val1')
336336
end
337337

338+
it "should raise an error given an invalid DATETIME" do
339+
begin
340+
@client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each
341+
rescue Mysql2::Error => e
342+
error = e
343+
end
344+
345+
error.message.should eql("Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
346+
end
347+
338348
if defined? Encoding
339349
context "string encoding for ENUM values" do
340350
it "should default to the connection's encoding if Encoding.default_internal is nil" do

0 commit comments

Comments
 (0)