From: Johannes Plunien Date: Sat, 25 Oct 2008 02:05:00 +0000 (+0200) Subject: added new option to enforce Date or DateTime inflation, no matter what type the colum... X-Git-Tag: v0.08240~310 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a97fe7e08bd7b821caec5f0093f52f4fbfdfcad6;p=dbsrgits%2FDBIx-Class.git added new option to enforce Date or DateTime inflation, no matter what type the column is, rafl++ for having this idea --- diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm index d2597ac..bd35dfe 100644 --- a/lib/DBIx/Class/InflateColumn/DateTime.pm +++ b/lib/DBIx/Class/InflateColumn/DateTime.pm @@ -30,6 +30,17 @@ If you want to set a specific timezone for that field, use: starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } } ); +If you want to inflate no matter what data_type your column is, +use inflate_datetime or inflate_date: + + __PACKAGE__->add_columns( + starts_when => { data_type => 'varchar', inflate_datetime => 1 } + ); + + __PACKAGE__->add_columns( + starts_when => { data_type => 'varchar', inflate_date => 1 } + ); + =head1 DESCRIPTION This module figures out the type of DateTime::Format::* class to @@ -73,6 +84,8 @@ sub register_column { return unless defined($info->{data_type}); my $type = lc($info->{data_type}); $type = 'datetime' if ($type =~ /^timestamp/); + $type = 'datetime' if exists $info->{inflate_datetime} and $info->{inflate_datetime}; + $type = 'date' if exists $info->{inflate_date} and $info->{inflate_date}; my $timezone; if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) { $timezone = $info->{extra}{timezone}; diff --git a/t/89inflate_datetime.t b/t/89inflate_datetime.t index 2313426..00005f2 100644 --- a/t/89inflate_datetime.t +++ b/t/89inflate_datetime.t @@ -10,7 +10,7 @@ my $schema = DBICTest->init_schema(); eval { require DateTime::Format::MySQL }; plan skip_all => "Need DateTime::Format::MySQL for inflation tests" if $@; -plan tests => 21; +plan tests => 25; # inflation test my $event = $schema->resultset("Event")->find(1); @@ -34,6 +34,10 @@ is("$created_start", '2006-06-18T00:00:00', 'Correct date/time'); ## timestamp field isa_ok($event->created_on, 'DateTime', 'DateTime returned'); +## varchar fields +isa_ok($event->varchar_date, 'DateTime', 'DateTime returned'); +isa_ok($event->varchar_datetime, 'DateTime', 'DateTime returned'); + # klunky, but makes older Test::More installs happy my $createo = $event->created_on; is("$createo", '2006-06-22T21:00:05', 'Correct date/time'); @@ -92,3 +96,10 @@ $invalid->update; like( $@, qr/invalid date format/i, "Invalid date format exception"); } +## varchar field using inflate_date => 1 +my $varchar_date = $event->varchar_date; +is("$varchar_date", '2006-07-23T00:00:00', 'Correct date/time'); + +## varchar field using inflate_datetime => 1 +my $varchar_datetime = $event->varchar_datetime; +is("$varchar_datetime", '2006-05-22T19:05:07', 'Correct date/time'); diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index a75f485..aca09bb 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -271,8 +271,8 @@ sub populate_schema { ]); $schema->populate('Event', [ - [ qw/id starts_at created_on/ ], - [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'], + [ qw/id starts_at created_on varchar_date varchar_datetime/ ], + [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05', '2006-07-23', '2006-05-22 19:05:07'], ]); $schema->populate('Link', [ diff --git a/t/lib/DBICTest/Schema/Event.pm b/t/lib/DBICTest/Schema/Event.pm index ea787f5..7dda075 100644 --- a/t/lib/DBICTest/Schema/Event.pm +++ b/t/lib/DBICTest/Schema/Event.pm @@ -11,7 +11,9 @@ __PACKAGE__->table('event'); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1 }, starts_at => { data_type => 'datetime', datetime_undef_if_invalid => 1 }, - created_on => { data_type => 'timestamp' } + created_on => { data_type => 'timestamp' }, + varchar_date => { data_type => 'varchar', inflate_date => 1, size => 20, is_nullable => 1 }, + varchar_datetime => { data_type => 'varchar', inflate_datetime => 1, size => 20, is_nullable => 1 }, ); __PACKAGE__->set_primary_key('id'); diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql index d0e0bfd..8de5cb5 100644 --- a/t/lib/sqlite.sql +++ b/t/lib/sqlite.sql @@ -1,6 +1,6 @@ -- -- Created by SQL::Translator::Producer::SQLite --- Created on Fri Oct 24 14:20:32 2008 +-- Created on Fri Oct 24 21:02:41 2008 -- BEGIN TRANSACTION; @@ -114,7 +114,9 @@ CREATE TABLE employee ( CREATE TABLE event ( id INTEGER PRIMARY KEY NOT NULL, starts_at datetime NOT NULL, - created_on timestamp NOT NULL + created_on timestamp NOT NULL, + varchar_date varchar(20), + varchar_datetime varchar(20) );