[CDBI] Delay Inflate

Bill Moseley moseley at hank.org
Sat Jan 7 16:35:47 GMT 2006


I have a column in Essential that inflates to a DataTime object.  I
want to set the timezone on the object, and (for the sake of this
discussion) fetching the timezone requires two more database reads.

It seems CDBI inflates columns when they are first loaded from the
database, not the first time the column is fetched.  So the inflate
code (including those two extra selects) is run even if the column is
not read by the application.

I'd like the inflation to only happen when the column is accessed.
I don't suppose there's any way to do that in CDBI short of removing
the column from Essential, right?


Only other way I can think of is to override the column in my class.
This seem reasonable?


    __PACKAGE__->columns( TEMP => 'time_zone_set' );
    sub my_time_column {
        my ( $item, $dt ) = @_;
        return $item->set('my_time_column', $dt ) if defined $dt;

        $dt = $item->get('my_time_column');
        return $dt if $item->time_zone_set;

        warn "setting time zone\n";
        $dt->set_time_zone( $item->location->time_zone->name );
        $item->set('my_time_column', $dt );
        $item->time_zone_set(1);
        return $dt;

    }

And then hope I don't use $item->get('my_time_column') elsewhere.


-- 
Bill Moseley
moseley at hank.org





More information about the ClassDBI mailing list