**** While we're talking about Pg ****

I'd like to have a way of getting all data from Pg (and all DBDs) to pass DateTime objects rather than strings. Where should this be done?


I am using Class::DBI for a object abstraction layer from the DB. Among other really nice features, it lets you specify column inflation and deflation subs to go to/from DB and objects. The DT::F parse_foo() functions fit in here really well.

Same here. I have the following in my Class::DBI base class:


   package My::Data;
   use strict;
   use base qw(Class::DBI);
   use DateTime;
   use DateTime::Format::Pg;

....

   sub pg2datetime { DateTime::Format::Pg->parse_timestamp(shift) }
   sub datetime2pg { DateTime::Format::Pg->format_timestamp(shift) }

   sub has_datetime
   {
      my($class, $name) = @_;
      $class->has_a(
         $name   => 'DateTime',
         inflate => \&pg2datetime,
         deflate => \&datetime2pg
      );
   }

Then in my subclass, I'd do

   package My::OtherData;
   use strict;
   use base qw(My::Data);

   __PACKAGE__->table("some_table");
   __PACKAGE__->columns(All => qw(pk foo));
   __PACKAGE__->has_datetime('foo');

1;

After that you can just use DateTime

   my $data = My::OtherData->retrieve($pk);
   my $dt   = $data->foo;

Note that in my case I don't care about timezones, so I'm promptly ignoring them.

--d

Reply via email to