rafl refactored last changes
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn / DateTime.pm
1 package DBIx::Class::InflateColumn::DateTime;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class/;
6
7 =head1 NAME
8
9 DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns.
10
11 =head1 SYNOPSIS
12
13 Load this component and then declare one or more 
14 columns to be of the datetime, timestamp or date datatype.
15
16   package Event;
17   __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/);
18   __PACKAGE__->add_columns(
19     starts_when => { data_type => 'datetime' }
20   );
21
22 Then you can treat the specified column as a L<DateTime> object.
23
24   print "This event starts the month of ".
25     $event->starts_when->month_name();
26
27 If you want to set a specific timezone for that field, use:
28
29   __PACKAGE__->add_columns(
30     starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
31   );
32
33 If you want to inflate no matter what data_type your column is,
34 use inflate_datetime or inflate_date:
35
36   __PACKAGE__->add_columns(
37     starts_when => { data_type => 'varchar', inflate_datetime => 1 }
38   );
39   
40   __PACKAGE__->add_columns(
41     starts_when => { data_type => 'varchar', inflate_date => 1 }
42   );
43
44 It's also possible to explicitly skip inflation:
45   
46   __PACKAGE__->add_columns(
47     starts_when => { data_type => 'datetime', inflate_datetime => 0 }
48   );
49
50 =head1 DESCRIPTION
51
52 This module figures out the type of DateTime::Format::* class to 
53 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::* 
54 that you are using.  If you switch from one database to a different 
55 one your code should continue to work without modification (though note
56 that this feature is new as of 0.07, so it may not be perfect yet - bug
57 reports to the list very much welcome).
58
59 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
60
61 =cut
62
63 __PACKAGE__->load_components(qw/InflateColumn/);
64
65 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
66
67 =head2 register_column
68
69 Chains with the L<DBIx::Class::Row/register_column> method, and sets
70 up datetime columns appropriately.  This would not normally be
71 directly called by end users.
72
73 In the case of an invalid date, L<DateTime> will throw an exception.  To
74 bypass these exceptions and just have the inflation return undef, use
75 the C<datetime_undef_if_invalid> option in the column info:
76   
77     "broken_date",
78     {
79         data_type => "datetime",
80         default_value => '0000-00-00',
81         is_nullable => 1,
82         datetime_undef_if_invalid => 1
83     }
84
85 =cut
86
87 sub register_column {
88   my ($self, $column, $info, @rest) = @_;
89   $self->next::method($column, $info, @rest);
90   return unless defined($info->{data_type});
91
92   my $type;
93
94   for (qw/date datetime/) {
95     my $key = "inflate_${_}";
96
97     next unless exists $info->{$key};
98     return unless $info->{$key};
99
100     $type = $_;
101     last;
102   }
103
104   unless ($type) {
105     $type = lc($info->{data_type});
106     $type = 'datetime' if ($type =~ /^timestamp/);
107   }
108
109   my $timezone;
110   if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
111     $timezone = $info->{extra}{timezone};
112   }
113
114   my $undef_if_invalid = $info->{datetime_undef_if_invalid};
115
116   if ($type eq 'datetime' || $type eq 'date') {
117     my ($parse, $format) = ("parse_${type}", "format_${type}");
118     $self->inflate_column(
119       $column =>
120         {
121           inflate => sub {
122             my ($value, $obj) = @_;
123             my $dt = eval { $obj->_datetime_parser->$parse($value); };
124             die "Error while inflating ${value} for ${column} on ${self}: $@"
125               if $@ and not $undef_if_invalid;
126             $dt->set_time_zone($timezone) if $timezone;
127             return $dt;
128           },
129           deflate => sub {
130             my ($value, $obj) = @_;
131             $value->set_time_zone($timezone) if $timezone;
132             $obj->_datetime_parser->$format($value);
133           },
134         }
135     );
136   }
137 }
138
139 sub _datetime_parser {
140   my $self = shift;
141   if (my $parser = $self->__datetime_parser) {
142     return $parser;
143   }
144   my $parser = $self->result_source->storage->datetime_parser(@_);
145   return $self->__datetime_parser($parser);
146 }
147
148 1;
149 __END__
150
151 =head1 SEE ALSO
152
153 =over 4
154
155 =item More information about the add_columns method, and column metadata, 
156       can be found in the documentation for L<DBIx::Class::ResultSource>.
157
158 =back
159
160 =head1 AUTHOR
161
162 Matt S. Trout <mst@shadowcatsystems.co.uk>
163
164 =head1 CONTRIBUTORS
165
166 Aran Deltac <bluefeet@cpan.org>
167
168 =head1 LICENSE
169
170 You may distribute this code under the same terms as Perl itself.
171