Commit | Line | Data |
445e5e31 |
1 | package DBIx::Class::InflateColumn::DateTime; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw/DBIx::Class/; |
6 | |
0b375b68 |
7 | =head1 NAME |
8 | |
5d0a2955 |
9 | DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns. |
0b375b68 |
10 | |
11 | =head1 SYNOPSIS |
12 | |
13 | Load this component and then declare one or more |
679a304a |
14 | columns to be of the datetime, timestamp or date datatype. |
0b375b68 |
15 | |
16 | package Event; |
517d5d64 |
17 | __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/); |
0b375b68 |
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 | |
dda9af55 |
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 | |
0b375b68 |
33 | =head1 DESCRIPTION |
34 | |
35 | This module figures out the type of DateTime::Format::* class to |
36 | inflate/deflate with based on the type of DBIx::Class::Storage::DBI::* |
37 | that you are using. If you switch from one database to a different |
5d0a2955 |
38 | one your code should continue to work without modification (though note |
39 | that this feature is new as of 0.07, so it may not be perfect yet - bug |
40 | reports to the list very much welcome). |
0b375b68 |
41 | |
943f93f2 |
42 | For more help with using components, see L<DBIx::Class::Manual::Component/USING>. |
de78905b |
43 | |
0b375b68 |
44 | =cut |
45 | |
445e5e31 |
46 | __PACKAGE__->load_components(qw/InflateColumn/); |
47 | |
48 | __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser'); |
49 | |
9b83fccd |
50 | =head2 register_column |
51 | |
52 | Chains with the L<DBIx::Class::Row/register_column> method, and sets |
53 | up datetime columns appropriately. This would not normally be |
54 | directly called by end users. |
55 | |
33a126ef |
56 | In the case of an invalid date, L<DateTime> will throw an exception. To |
57 | bypass these exceptions and just have the inflation return undef, use |
58 | the C<datetime_undef_if_invalid> option in the column info: |
59 | |
60 | "broken_date", |
61 | { |
62 | data_type => "datetime", |
63 | default_value => '0000-00-00', |
64 | is_nullable => 1, |
65 | datetime_undef_if_invalid => 1 |
66 | } |
67 | |
9b83fccd |
68 | =cut |
69 | |
445e5e31 |
70 | sub register_column { |
71 | my ($self, $column, $info, @rest) = @_; |
72 | $self->next::method($column, $info, @rest); |
c209c4fd |
73 | return unless defined($info->{data_type}); |
74 | my $type = lc($info->{data_type}); |
f011970e |
75 | $type = 'datetime' if ($type =~ /^timestamp/); |
dda9af55 |
76 | my $timezone; |
77 | if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) { |
78 | $timezone = $info->{extra}{timezone}; |
79 | } |
80 | |
dceeddc7 |
81 | my $undef_if_invalid = $info->{datetime_undef_if_invalid}; |
82 | |
c209c4fd |
83 | if ($type eq 'datetime' || $type eq 'date') { |
84 | my ($parse, $format) = ("parse_${type}", "format_${type}"); |
445e5e31 |
85 | $self->inflate_column( |
86 | $column => |
87 | { |
88 | inflate => sub { |
89 | my ($value, $obj) = @_; |
33a126ef |
90 | my $dt = eval { $obj->_datetime_parser->$parse($value); }; |
dceeddc7 |
91 | die "Error while inflating ${value} for ${column} on ${self}: $@" |
92 | if $@ and not $undef_if_invalid; |
dda9af55 |
93 | $dt->set_time_zone($timezone) if $timezone; |
94 | return $dt; |
445e5e31 |
95 | }, |
96 | deflate => sub { |
97 | my ($value, $obj) = @_; |
dda9af55 |
98 | $value->set_time_zone($timezone) if $timezone; |
c209c4fd |
99 | $obj->_datetime_parser->$format($value); |
445e5e31 |
100 | }, |
101 | } |
102 | ); |
103 | } |
104 | } |
105 | |
106 | sub _datetime_parser { |
107 | my $self = shift; |
108 | if (my $parser = $self->__datetime_parser) { |
109 | return $parser; |
110 | } |
111 | my $parser = $self->result_source->storage->datetime_parser(@_); |
112 | return $self->__datetime_parser($parser); |
113 | } |
114 | |
115 | 1; |
0b375b68 |
116 | __END__ |
117 | |
118 | =head1 SEE ALSO |
119 | |
120 | =over 4 |
121 | |
122 | =item More information about the add_columns method, and column metadata, |
123 | can be found in the documentation for L<DBIx::Class::ResultSource>. |
124 | |
125 | =back |
126 | |
127 | =head1 AUTHOR |
128 | |
129 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
130 | |
131 | =head1 CONTRIBUTORS |
132 | |
133 | Aran Deltac <bluefeet@cpan.org> |
134 | |
135 | =head1 LICENSE |
136 | |
137 | You may distribute this code under the same terms as Perl itself. |
138 | |