X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FInflateColumn%2FDateTime.pm;h=2b40608952090cbc8623a96cbfa88abeacd64e16;hb=26283ee38f220f6c6bae720ea5a189c9c0f47f6f;hp=25ed7e74eef70f1990e77a15b410386c28c3f05c;hpb=d6aed6385781443c0a65cacabaa3a4f071d63f88;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm index 25ed7e7..2b40608 100644 --- a/lib/DBIx/Class/InflateColumn/DateTime.pm +++ b/lib/DBIx/Class/InflateColumn/DateTime.pm @@ -18,6 +18,7 @@ columns to be of the datetime, timestamp or date datatype. __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/); __PACKAGE__->add_columns( starts_when => { data_type => 'datetime' } + create_date => { data_type => 'date' } ); NOTE: You B load C B C. See @@ -40,21 +41,22 @@ 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 } ); It's also possible to explicitly skip inflation: - + __PACKAGE__->add_columns( starts_when => { data_type => 'datetime', inflate_datetime => 0 } ); -NOTE: Don't rely on C to validate user input, this -may have unexpected security implications. Instead, use an input parser to -create a DateTime object. For instance, if your user input comes as a -'YYYY-MM-DD' string, you can use C thusly: +NOTE: Don't rely on C to parse date strings for you. +The column is set directly for any non-references and C +is completely bypassed. Instead, use an input parser to create a DateTime +object. For instance, if your user input comes as a 'YYYY-MM-DD' string, you can +use C thusly: use DateTime::Format::ISO8601; my $dt = DateTime::Format::ISO8601->parse_datetime('YYYY-MM-DD'); @@ -68,14 +70,22 @@ one your code should continue to work without modification (though note that this feature is new as of 0.07, so it may not be perfect yet - bug reports to the list very much welcome). +If the data_type of a field is C, C or C (or +a derivative of these datatypes, e.g. C), this +module will automatically call the appropriate parse/format method for +deflation/inflation as defined in the storage class. For instance, for +a C field the methods C and C +would be called on deflation/inflation. If the storage class does not +provide a specialized inflator/deflator, C<[parse|format]_datetime> will +be used as a fallback. See L for more information on +date formatting. + For more help with using components, see L. =cut __PACKAGE__->load_components(qw/InflateColumn/); -__PACKAGE__->mk_group_accessors('simple' => '__datetime_parser'); - =head2 register_column Chains with the L method, and sets @@ -85,7 +95,7 @@ directly called by end users. In the case of an invalid date, L will throw an exception. To bypass these exceptions and just have the inflation return undef, use the C option in the column info: - + "broken_date", { data_type => "datetime", @@ -118,6 +128,12 @@ sub register_column { if ($type eq "timestamp with time zone" || $type eq "timestamptz") { $type = "timestamp"; $info->{_ic_dt_method} ||= "timestamp_with_timezone"; + } elsif ($type eq "timestamp without time zone") { + $type = "timestamp"; + $info->{_ic_dt_method} ||= "timestamp_without_timezone"; + } elsif ($type eq "smalldatetime") { + $type = "datetime"; + $info->{_ic_dt_method} ||= "datetime"; } } @@ -134,7 +150,7 @@ sub register_column { "please put it directly into the '$column' column definition."; $locale = $info->{extra}{locale}; } - + $locale = $info->{locale} if defined $info->{locale}; $timezone = $info->{timezone} if defined $info->{timezone}; @@ -206,12 +222,7 @@ sub _deflate_from_datetime { } sub _datetime_parser { - my $self = shift; - if (my $parser = $self->__datetime_parser) { - return $parser; - } - my $parser = $self->result_source->storage->datetime_parser(@_); - return $self->__datetime_parser($parser); + shift->result_source->storage->datetime_parser (@_); } 1;