Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn / DateTime.pm
index 1b0127d..3162223 100644 (file)
@@ -3,7 +3,7 @@ package DBIx::Class::InflateColumn::DateTime;
 use strict;
 use warnings;
 use base qw/DBIx::Class/;
-use Carp::Clan qw/^DBIx::Class/;
+use DBIx::Class::Carp;
 use Try::Tiny;
 use namespace::clean;
 
@@ -109,38 +109,46 @@ the C<datetime_undef_if_invalid> option in the column info:
 
 sub register_column {
   my ($self, $column, $info, @rest) = @_;
-  $self->next::method($column, $info, @rest);
-  return unless defined($info->{data_type});
 
-  my $type;
+  $self->next::method($column, $info, @rest);
 
-  for (qw/date datetime timestamp/) {
+  my $requested_type;
+  for (qw/datetime timestamp date/) {
     my $key = "inflate_${_}";
+    if (exists $info->{$key}) {
 
-    next unless exists $info->{$key};
-    return unless $info->{$key};
-
-    $type = $_;
-    last;
-  }
+      # this bailout is intentional
+      return unless $info->{$key};
 
-  unless ($type) {
-    $type = lc($info->{data_type});
-    if ($type eq "timestamp with time zone" || $type eq "timestamptz") {
-      $type = "timestamp";
-      $info->{_ic_dt_method} ||= "timestamp_with_timezone";
-    } elsif ($type eq "timestamp without time zone") {
-      $type = "timestamp";
-      $info->{_ic_dt_method} ||= "timestamp_without_timezone";
-    } elsif ($type eq "smalldatetime") {
-      $type = "datetime";
-      $info->{_ic_dt_method} ||= "smalldatetime";
-    } else {
-      $info->{_ic_dt_method} ||= $type;
+      $requested_type = $_;
+      last;
     }
   }
 
-  return unless ($type eq 'datetime' || $type eq 'date' || $type eq 'timestamp');
+  return if (!$requested_type and !$info->{data_type});
+
+  my $data_type = lc( $info->{data_type} || '' );
+
+  # _ic_dt_method will follow whatever the registration requests
+  # thus = instead of ||=
+  if ($data_type eq 'timestamp with time zone' || $data_type eq 'timestamptz') {
+    $info->{_ic_dt_method} = 'timestamp_with_timezone';
+  }
+  elsif ($data_type eq 'timestamp without time zone') {
+    $info->{_ic_dt_method} = 'timestamp_without_timezone';
+  }
+  elsif ($data_type eq 'smalldatetime') {
+    $info->{_ic_dt_method} = 'smalldatetime';
+  }
+  elsif ($data_type =~ /^ (?: date | datetime | timestamp ) $/x) {
+    $info->{_ic_dt_method} = $data_type;
+  }
+  elsif ($requested_type) {
+    $info->{_ic_dt_method} = $requested_type;
+  }
+  else {
+    return;
+  }
 
   if ($info->{extra}) {
     for my $slot (qw/timezone locale floating_tz_ok/) {
@@ -161,13 +169,10 @@ sub register_column {
         inflate => sub {
           my ($value, $obj) = @_;
 
-          my $dt = try
-            { $obj->_inflate_to_datetime( $value, $infcopy ) }
-            catch {
-              $self->throw_exception ("Error while inflating ${value} for ${column} on ${self}: $_")
-                unless $infcopy->{datetime_undef_if_invalid};
-              undef;  # rv
-            };
+          # propagate for error reporting
+          $infcopy->{__dbic_colname} = $column;
+
+          my $dt = $obj->_inflate_to_datetime( $value, $infcopy );
 
           return (defined $dt)
             ? $obj->_post_inflate_datetime( $dt, $infcopy )
@@ -190,8 +195,16 @@ sub _flate_or_fallback
 
   my $parser = $self->_datetime_parser;
   my $preferred_method = sprintf($method_fmt, $info->{ _ic_dt_method });
-  my $method = $parser->can($preferred_method) ? $preferred_method : sprintf($method_fmt, 'datetime');
-  return $parser->$method($value);
+  my $method = $parser->can($preferred_method) || sprintf($method_fmt, 'datetime');
+
+  return try {
+    $parser->$method($value);
+  }
+  catch {
+    $self->throw_exception ("Error while inflating '$value' for $info->{__dbic_colname} on ${self}: $_")
+      unless $info->{datetime_undef_if_invalid};
+    undef;  # rv
+  };
 }
 
 sub _inflate_to_datetime {
@@ -292,7 +305,7 @@ use the old way you'll see a warning - please fix your code then!
       can be found in the documentation for L<DBIx::Class::ResultSource>.
 
 =item Further discussion of problems inherent to the Floating timezone:
-      L<Floating DateTimes|DateTime/Floating_DateTimes>
+      L<Floating DateTimes|DateTime/Floating DateTimes>
       and L<< $dt->set_time_zone|DateTime/"Set" Methods >>
 
 =back