1use strict; 2use Test::More; 3 4package main; 5 6BEGIN { 7 eval { require 't/testlib/MyBase.pm' }; 8 plan skip_all => "Need MySQL for this test" if $@; 9 10 eval "use DateTime::Format::MySQL"; 11 plan skip_all => "Need DateTime::Format::MySQL for this test" if $@; 12} 13 14package Temp::DBI; 15 16BEGIN { 17 eval { require 't/testlib/MyBase.pm' }; 18} 19use base 'MyBase'; 20 21# typically provided by Class::DBI::mysql 22 23sub autoinflate_dates { 24 my $class = shift; 25 my $columns = $class->_column_info; 26 27 foreach my $c (keys %$columns) { 28 my $type = $columns->{$c}->{type}; 29 next unless ($type =~ m/^(date)/); 30 my $i_method = "parse_$type"; 31 my $d_method = "format_$type"; 32 $class->has_a( 33 $c => 'DateTime', 34 inflate => sub { DateTime::Format::MySQL->$i_method(shift) }, 35 deflate => sub { DateTime::Format::MySQL->$d_method(shift) }, 36 ); 37 } 38} 39 40package Temp::DateTable; 41use base 'Temp::DBI'; 42 43__PACKAGE__->set_table(); 44__PACKAGE__->columns(All => qw/my_id my_datetime my_date/,); 45 46__PACKAGE__->autoinflate_dates; 47 48sub create_sql { 49 return qq{ 50 my_id integer not null auto_increment primary key, 51 my_datetime datetime, 52 my_date date 53 }; 54} 55 56# typically provided by Class::DBI::mysql 57 58sub _column_info { 59 return { map { 'my_' . $_ => { type => $_ } } qw(datetime date) }; 60} 61 62package main; 63 64plan tests => 1; 65my $dt = DateTime->now(); 66my $row = Temp::DateTable->create( 67 { 68 map { 69 my $method = "format_$_"; 70 ("my_$_" => DateTime::Format::MySQL->$method($dt)) 71 } 72 qw(date datetime) 73 } 74); 75 76my $date = eval { $row->my_date }; 77isa_ok($date, 'DateTime'); 78 791; 80