logo       
Google Custom Search
    AddThis Social Bookmark Button

Re: inflate_column with DateTime::Format::MySQL and invalid dates: msg#00157

Subject: Re: inflate_column with DateTime::Format::MySQL and invalid dates

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.

  
Very nice.    I'll see if I can get that working in combination with DBIx::Class::InflateColumn::DateTime

Thanks very much.

-- 
Randy Moore
Axion Information Technologies, Inc.

phone: 301-587-3300 x 511
fax:   301-585-7450

http://www.axion-it.com


Try Searching:
servers, voip, java, networking, microsoft ...
<Prev in Thread] Current Thread [Next in Thread>