[Dbix-class] Re: inflate_column with DateTime::Format::MySQL and invalid dates

Dagfinn Ilmari Mannsåker ilmari at ilmari.org
Sat Oct 27 14:34:33 GMT 2007


Jonathan Rockway <jon at jrock.us> writes:

> Ash Berlin wrote:
>> I'm not sure if InflateColumn::DateTime will handle this better or not,
>> but its the first thing to try.
>>
>> Ash
> InflateColumn::DateTime basically does the same thing.  The solution
> would be to remove the corrupt data from the database, or maybe modify
> DT::Format::MySQL to convert 0000-00-00 00:00:00 into "undef".  I think
> 0000-00-00 00:00:00 is meant to be treated as NULL, right?

I did this for (the inflate-side only) by creating a result base class
which overrides inflate_column and adding it to ->load_components().

    package FixBrokenDates;

    use strict;
    use warnings;

    use base qw/DBIx::Class/;


    sub inflate_result {
        my $class = shift;
        my ($source, $me, $prefetch) = @_;

        # Nullify MySQL zero dates
        for my $col (keys %{$me}) {
            my $datatype = lc $source->column_info($col)->{data_type};
            if (($datatype eq 'date' && $me->{$col} eq '0000-00-00') ||
                (($datatype eq 'datetime' || $datatype eq 'timestamp')
                 && $me->{$col} eq '0000-00-00 00:00:00')) {
                $me->{$col} = undef;
            }
        }

        $class->next::method(@_);
    }

If the schema doesn't allow null columns you have to override
deflate_result similarly and fix up (or should that be "break up" :)
undef values in columns with is_nullable == 0.

-- 
ilmari
"A disappointingly low fraction of the human race is,
 at any given time, on fire." - Stig Sandbeck Mathisen




More information about the DBIx-Class mailing list