From: Rafael Kitover Date: Wed, 18 Jan 2012 14:56:18 +0000 (-0500) Subject: issue warning for DateTimes passed to ->search X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e1038213;p=dbsrgits%2FDBIx-Class-Historic.git issue warning for DateTimes passed to ->search Issue a carp_unique when DateTime objects are detected in binds in ::Storage::DBI with a pointer to the Cookbook recipe for formatting DateTime objects for queries. Add a test for the warning in t/inflate/datetime.t, which already has TODO tests for DTs in ->search. --- diff --git a/Changes b/Changes index 3497d1c..18e68d0 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,7 @@ Revision history for DBIx::Class * Misc - Codebase is now trailing-whitespace-free + - Issue a warning when DateTime objects are passed to ->search 0.08196 2011-11-29 05:35 (UTC) * Fixes diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index b9d7489..6df0e1f 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1465,6 +1465,15 @@ sub _gen_sql_bind { }]; } + if ($op eq 'select' + && first { blessed($_->[1]) && $_->[1]->isa('DateTime') } @final_bind) { + + carp_unique 'DateTime objects passed to search() are not supported ' + . 'properly (InflateColumn::DateTime formats and settings are not ' + . 'respected.) See "Formatting DateTime objects in queries" in ' + . 'DBIx::Class::Manual::Cookbook'; + } + ($sql, \@final_bind); } diff --git a/t/inflate/datetime.t b/t/inflate/datetime.t index 061037a..9fa7648 100644 --- a/t/inflate/datetime.t +++ b/t/inflate/datetime.t @@ -2,6 +2,8 @@ use strict; use warnings; use Test::More; +use Test::Warn; +use Try::Tiny; use lib qw(t/lib); use DBICTest; @@ -19,15 +21,28 @@ isa_ok($event->starts_at, 'DateTime', 'DateTime returned'); my $starts = $event->starts_at; is("$starts", '2006-04-25T22:24:33', 'Correct date/time'); +my $row; + +warnings_exist { + $row = try { + $schema->resultset('Event')->search({ starts_at => $starts })->single + }; +} [qr/DateTime objects.+not supported/], + 'using a DateTime object in ->search generates a warning'; + TODO: { local $TODO = "We can't do this yet before 0.09" if DBIx::Class->VERSION < 0.09; - ok(my $row = - $schema->resultset('Event')->search({ starts_at => $starts })->single); is(eval { $row->id }, 1, 'DT in search'); + local $SIG{__WARN__} = sub { + warn @_ unless $_[0] =~ /DateTime objects.+not supported/; + }; + ok($row = - $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } })->single); + $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } }) + ->single); + is(eval { $row->id }, 1, 'DT in search with condition'); }