blib/lib/DBIO/InflateColumn/DateTime.pm

filter: Statementstmt 94.9% 132 / 139 Branchbran 81.5% 75 / 92 Conditioncond 66.6% 22 / 33 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 100% 23 / 23 Podpod 100% 1 / 1 totaltotal 85.0% 262 / 308 CC 62 SCAR 24.2
CC
62
Cov
87%
register_column
27.4
_parse_time_core
11.0
_pre_deflate_datetime
10.1
CRAP 11.3
« blib/lib/DBIO/Storage.pm ↑ Summary blib/lib/DBIO/Schema.pm »
1 package DBIO::InflateColumn::DateTime;
2 # ABSTRACT: Auto-create DateTime objects from date and datetime columns.
3  
4 263 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 263 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 263 our $VERSION = '0.900007';
8  
9 263 ▶use base qw/DBIO::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 263 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 263 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 263 ▶use DBIO::Util qw(mask_bound_values);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 263 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 263 __PACKAGE__->load_components(qw/InflateColumn/);
17  
18  
19 3886 ▶sub register_column {
Subroutine register_column: called CC=21 SCAR=33.1
Pod documented
20 3886 my ($self, $column, $info, @rest) = @_;
21  
22 3886 $self->next::method($column, $info, @rest);
23  
24 3886 my $requested_type;
25 3886 for (qw/datetime timestamp date/) {
26 10609 my $key = "inflate_${_}";
27 10609 ▶ if (exists $info->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{$key}) 785 9824 10609
28  
29 # this bailout is intentional
30 785 ▶ return unless $info->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{$key} 257 528 785
31  
32 528 $requested_type = $_;
33 528 last;
34 }
35 }
36  
37 3629 ▶ return if (!$requested_type and !$info->{data_type});
Branch: 1 branch
BranchTrueFalseTotal
unless $requested_type or $info->{"data_type"} 0 3629 3629
Condition66%2/3
$requested_type or $info->{"data_type"}
l!l&&r!l&&!r
52831010
MC/DC0%0/2
$requested_type or $info->{"data_type"}
$requested_type $info->{"data_type"}
Truth table66%2/3
A or B
A= $requested_typeB= $info->{"data_type"}
ABresult
1X1
011
000
38  
39 3629 ▶ my $data_type = lc( $info->{data_type} || '' );
Condition50%1/2
$info->{'data_type'} || ''
l!l
36290
MC/DC0%0/1
$info->{'data_type'} || ''
$info->{'data_type'}
Truth table50%1/2
A
A= $info->{'data_type'}
Aresult
11
00
40  
41 # _ic_dt_method will follow whatever the registration requests
42 # thus = instead of ||=
43 3629 $info->{_ic_dt_fallback} = 'datetime';
44 3629 delete $info->{_ic_dt_time};
45 3629 ▶ if ($data_type eq 'timestamp with time zone' || $data_type eq 'timestamptz') {
Branch: 5 branches
BranchTrueFalseTotal
if ($data_type eq 'timestamp with time zone' or $data_type eq 'timestamptz') { } 0 3629 3629
elsif ($data_type eq 'timestamp without time zone') { } 0 3629 3629
elsif ($data_type eq 'smalldatetime') { } 3 3626 3629
elsif ($data_type =~ /^ (?: date | datetime | timestamp ) $/x) { } 1290 2336 3626
elsif ($requested_type) { } 528 1808 2336
Condition33%1/3
$data_type eq "timestamp with time zone" or $data_type eq "timestamptz"
l!l&&r!l&&!r
003629
MC/DC0%0/2
$data_type eq "timestamp with time zone" or $data_type eq "timestamptz"
$data_type eq "timestamp with time zone" $data_type eq "timestamptz"
Truth table33%1/3
A or B
A= $data_type eq "timestamp with time zone"B= $data_type eq "timestamptz"
ABresult
1X1
011
000
46 0 $info->{_ic_dt_method} = 'timestamp_with_time_zone';
47 }
48 elsif ($data_type eq 'timestamp without time zone') {
49 0 $info->{_ic_dt_method} = 'timestamp_without_time_zone';
50 }
51 elsif ($data_type eq 'smalldatetime') {
52 3 $info->{_ic_dt_method} = 'smalldatetime';
53 }
54 elsif ($data_type =~ /^ (?: date | datetime | timestamp ) $/x) {
55 1290 $info->{_ic_dt_method} = $data_type;
56 }
57 elsif ($requested_type) {
58 # a type of its own gets its own channel; the rest follows the request
59 528 (my $channel = $data_type) =~ s/\s*\(.*?\)//g;
60 528 $channel =~ s/^\s+|\s+$//g;
61 528 $channel =~ s/\s+/_/g;
62 528 $channel =~ s/\W//g;
63 528 ▶ $info->{_ic_dt_method} = length $channel ? $channel : $requested_type;
Branch: 1 branch
BranchTrueFalseTotal
length $channel ? : 528 0 528
64 528 ▶ $info->{_ic_dt_fallback} = $requested_type eq 'date' ? 'date' : 'datetime';
Branch: 1 branch
BranchTrueFalseTotal
$requested_type eq 'date' ? : 260 268 528
65  
66 # a time of day has channels of its own and never falls back to datetime
67 528 ▶ if ($channel =~ /^ (?: time | time_without_time_zone ) $/x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($channel =~ /^ (?: time | time_without_time_zone ) $/x) { } 6 522 528
elsif ($channel =~ /^ (?: time_with_time_zone | timetz ) $/x) { } 4 518 522
68 6 $info->{_ic_dt_time} = 'time';
69 }
70 elsif ($channel =~ /^ (?: time_with_time_zone | timetz ) $/x) {
71 4 $info->{_ic_dt_time} = 'timetz';
72 }
73 }
74 else {
75 1808 return;
76 }
77  
78 1821 ▶ if ($info->{extra}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"extra"}) 0 1821 1821
79 0 for my $slot (qw/timezone locale floating_tz_ok/) {
80 0 ▶ if ( defined $info->{extra}{$slot} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"extra"}{$slot}) 0 0 0
81 0 carp "Putting $slot into extra => { $slot => '...' } has been deprecated, ".
82 "please put it directly into the '$column' column definition.";
83 0 ▶ $info->{$slot} = $info->{extra}{$slot} unless defined $info->{$slot};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{$slot} 0 0 0
84 }
85 }
86 }
87  
88 # shallow copy to avoid unfounded(?) Devel::Cycle complaints
89 1821 my $infcopy = {%$info};
90  
91 $self->inflate_column(
92 $column =>
93 {
94 50 ▶ inflate => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
95 50 my ($value, $obj) = @_;
96  
97 # propagate for error reporting
98 50 $infcopy->{__dbic_colname} = $column;
99  
100 50 my $dt = $obj->_inflate_to_datetime( $value, $infcopy );
101  
102 48 ▶ return (defined $dt)
Branch: 1 branch
BranchTrueFalseTotal
defined $dt ? : 48 0 48
103 ? $obj->_post_inflate_datetime( $dt, $infcopy )
104 : undef
105 ;
106 },
107 39 ▶ deflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 39 my ($value, $obj) = @_;
109  
110 39 $value = $obj->_pre_deflate_datetime( $value, $infcopy );
111 39 $obj->_deflate_from_datetime( $value, $infcopy );
112 },
113 }
114 1821 );
115 }
116  
117 # the method of the column's own channel when the parser has it, else the
118 # method of the fallback channel
119 64 ▶sub _flate_method {
Subroutine _flate_method: called CC=3 SCAR=11.0
120 64 my( $self, $parser, $info, $method_fmt ) = @_;
121  
122 64 my $preferred_method = sprintf($method_fmt, $info->{ _ic_dt_method });
123 64 ▶ return $preferred_method if $parser->can($preferred_method);
Branch: 1 branch
BranchTrueFalseTotal
if $parser->can($preferred_method) 38 26 64
124 26 ▶ return sprintf($method_fmt, $info->{ _ic_dt_fallback } || 'datetime');
Condition50%1/2
$info->{'_ic_dt_fallback'} || 'datetime'
l!l
260
MC/DC0%0/1
$info->{'_ic_dt_fallback'} || 'datetime'
$info->{'_ic_dt_fallback'}
Truth table50%1/2
A
A= $info->{'_ic_dt_fallback'}
Aresult
11
00
125 }
126  
127 # the method a time of day goes through: its own channel, then the plain time
128 # channel (the one that carries an offset for a type with a zone); undef when
129 # the core reads or writes itself
130 49 ▶sub _time_method {
Subroutine _time_method: called CC=3 SCAR=11.0
131 49 my( $self, $parser, $info, $method_fmt ) = @_;
132  
133 49 for my $channel ($info->{_ic_dt_method}, $info->{_ic_dt_time}) {
134 90 my $method = sprintf($method_fmt, $channel);
135 90 ▶ return $method if $parser->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $parser->can($method) 12 78 90
136 }
137 37 return undef;
138 }
139  
140 sub _flate_or_fallback
141 89 ▶{
Subroutine _flate_or_fallback: called CC=2 SCAR=7.1
142 89 my( $self, $value, $info, $method_fmt ) = @_;
143  
144 89 my $parser = $self->_datetime_parser;
145 89 my $reading = $method_fmt =~ /^parse/;
146 my $method = $info->{_ic_dt_time}
147 89 ▶ ? $self->_time_method($parser, $info, $method_fmt)
Branch: 1 branch
BranchTrueFalseTotal
$info->{'_ic_dt_time'} ? : 49 40 89
148 : $self->_flate_method($parser, $info, $method_fmt);
149  
150 89 ▶ return try {
Subroutine __ANON__: called CC=3 SCAR=11.0
151 89 ▶ defined $method ? $parser->$method($value)
Branch: 2 branches
BranchTrueFalseTotal
defined $method ? : 52 37 89
$reading ? : 20 17 37
152 : $reading ? $self->_parse_time_core($value, $info)
153 : $self->_format_time_core($value, $info);
154 }
155 3 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=15.4
156 $self->throw_exception (
157 "Error while inflating column '$info->{__dbic_colname}' of class "
158 . (ref($self) || $self) . ': ' . mask_bound_values("$_", [$value])
159 )
160 3 ▶ unless $info->{datetime_undef_if_invalid};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"datetime_undef_if_invalid"} 3 0 3
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
161 0 undef; # rv
162 89 };
163 }
164  
165 50 ▶sub _inflate_to_datetime {
Subroutine _inflate_to_datetime: called CC=1 SCAR=0.0
166 50 my( $self, $value, $info ) = @_;
167 50 return $self->_flate_or_fallback( $value, $info, 'parse_%s' );
168 }
169  
170 39 ▶sub _deflate_from_datetime {
Subroutine _deflate_from_datetime: called CC=1 SCAR=0.0
171 39 my( $self, $value, $info ) = @_;
172 39 return $self->_flate_or_fallback( $value, $info, 'format_%s' );
173 }
174  
175 113 ▶sub _datetime_parser {
Subroutine _datetime_parser: called CC=1 SCAR=0.0
176 113 shift->result_source->storage->datetime_parser (@_);
177 }
178  
179 # does the column go through the date channel in this direction, as a calendar
180 # day: a date that carries a time of day (Oracle DATE) keeps it
181 39 ▶sub _is_date_channel {
Subroutine _is_date_channel: called CC=3 SCAR=11.0
182 39 my( $self, $info, $method_fmt ) = @_;
183  
184 39 ▶ return 0 if $self->result_source->storage->datetime_date_carries_time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result_source->storage->datetime_date_carries_time 3 36 39
185 36 ▶ return 1 if $info->{_ic_dt_method} eq 'date';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"_ic_dt_method"} eq "date" 12 24 36
186 24 return $self->_flate_method($self->_datetime_parser, $info, $method_fmt)
187 eq sprintf($method_fmt, 'date');
188 }
189  
190 # HH:MM:SS with an optional fraction, for a type with a zone Z, +HH or +HH:MM
191 # as well; the date is the day of the Unix epoch
192 20 ▶sub _parse_time_core {
Subroutine _parse_time_core: called CC=11 SCAR=24.0
193 20 my( $self, $value, $info ) = @_;
194  
195 20 require DateTime;
196  
197 20 my $zone = $info->{_ic_dt_time} eq 'timetz';
198 20 ▶ my $re = $zone
Branch: 1 branch
BranchTrueFalseTotal
$zone ? : 11 9 20
199 ? qr/^\s*(\d\d):(\d\d):(\d\d)(?:\.(\d+))?\s*(?:(Z)|([+-])(\d\d)(?::?(\d\d))?)?\s*\z/i
200 : qr/^\s*(\d\d):(\d\d):(\d\d)(?:\.(\d+))?\s*\z/;
201  
202 20 ▶ my @m = defined $value ? $value =~ $re : ()
Branch: 3 branches
BranchTrueFalseTotal
my(@m) = defined $value ? $value =~ /$re/ : () or die q[Unreadable time of day '] . (defined $value ? $value : "NULL") . "'\n" 19 1 20
defined $value ? : 20 0 20
defined $value ? : 1 0 1
203 or die "Unreadable time of day '" . (defined $value ? $value : 'NULL') . "'\n";
204 19 my ($h, $mi, $s, $frac, $z, $sign, $oh, $om) = @m;
205  
206 19 my %zone = (time_zone => 'floating');
207 19 ▶ if ($zone and defined $z) {
Branch: 2 branches
BranchTrueFalseTotal
if ($zone and defined $z) { } 2 17 19
elsif ($zone and defined $sign) { } 8 17 25
Condition100%3/3
$zone and defined $z
!ll&&!rl&&r
892
MC/DC100%2/2
$zone and defined $z
$zone defined $z
Truth table100%3/3
A and B
A= $zoneB= defined $z
ABresult
0X0
100
111
208 2 $zone{time_zone} = 'UTC';
209 }
210 ▶ elsif ($zone and defined $sign) {
Condition100%3/3
$zone and defined $sign
!ll&&!rl&&r
818
MC/DC100%2/2
$zone and defined $sign
$zone defined $sign
Truth table100%3/3
A and B
A= $zoneB= defined $sign
ABresult
0X0
100
111
211 8 ▶ $zone{time_zone} = sprintf('%s%02d%02d', $sign, $oh, $om || 0);
Condition100%2/2
$om || 0
l!l
62
MC/DC100%1/1
$om || 0
$om
Truth table100%2/2
A
A= $om
Aresult
11
00
212 }
213  
214 19 ▶ my $ns = defined $frac ? substr($frac . '0' x 9, 0, 9) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $frac ? : 8 11 19
215  
216 19 return DateTime->new(
217 year => 1970, month => 1, day => 1,
218 hour => $h, minute => $mi, second => $s, nanosecond => $ns + 0,
219 %zone,
220 );
221 }
222  
223 # the time in the value's own zone, the offset with it for a type with a zone
224 17 ▶sub _format_time_core {
Subroutine _format_time_core: called CC=5 SCAR=16.1
225 17 my( $self, $dt, $info ) = @_;
226  
227 17 my $out = sprintf('%02d:%02d:%02d', $dt->hour, $dt->minute, $dt->second);
228  
229 17 ▶ if (my $ns = $dt->nanosecond) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $dt->nanosecond) 9 8 17
230 9 (my $frac = sprintf('%09d', $ns)) =~ s/0+\z//;
231 9 $out .= ".$frac";
232 }
233  
234 # a floating value has no offset to write; the server applies its own rule
235 17 ▶ if ($info->{_ic_dt_time} eq 'timetz' and not $dt->time_zone->is_floating) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"_ic_dt_time"} eq "timetz" and not $dt->time_zone->is_floating) 6 11 17
Condition100%3/3
$info->{"_ic_dt_time"} eq "timetz" and not $dt->time_zone->is_floating
!ll&&!rl&&r
926
MC/DC100%2/2
$info->{"_ic_dt_time"} eq "timetz" and not $dt->time_zone->is_floating
$info->{"_ic_dt_time"} eq "timetz" not $dt->time_zone->is_floating
Truth table100%3/3
A and not(B)
A= $info->{"_ic_dt_time"} eq "timetz"B= not $dt->time_zone->is_floating
ABresult
0X0
100
111
236 6 my $off = $dt->offset;
237 6 ▶ my $sign = $off < 0 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$off < 0 ? : 2 4 6
238 6 $off = abs $off;
239 6 $out .= sprintf('%s%02d:%02d', $sign, int($off / 3600), int(($off % 3600) / 60));
240 }
241  
242 17 return $out;
243 }
244  
245 48 ▶sub _post_inflate_datetime {
Subroutine _post_inflate_datetime: called CC=8 SCAR=20.8
246 48 my( $self, $dt, $info ) = @_;
247  
248 # a time of day stands on the day of the Unix epoch and is not converted;
249 # without a zone it is floating, with one it keeps the offset it was read
250 # with (a named zone could give 1970-01-01 another offset)
251 48 ▶ if (my $kind = $info->{_ic_dt_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kind = $info->{"_ic_dt_time"}) 27 21 48
252 27 ▶ if ($kind eq 'time') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'time') { } 12 15 27
elsif ($dt->time_zone->is_olson) { } 1 14 15
253 12 $dt->set_time_zone('floating');
254 }
255 elsif ($dt->time_zone->is_olson) {
256 1 $dt->set_time_zone( DateTime::TimeZone->offset_as_string($dt->offset) );
257 }
258 27 $dt->set( year => 1970, month => 1, day => 1 );
259 27 ▶ $dt->set_locale($info->{locale}) if defined $info->{locale};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"locale"} 1 26 27
260 27 return $dt;
261 }
262  
263 # a date has no time of day and no zone: it is the calendar day the parser
264 # read, floating at noon, so a conversion by less than 12 hours either way
265 # keeps the day
266 21 ▶ if ($self->_is_date_channel($info, 'parse_%s')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_date_channel($info, "parse_%s")) 12 9 21
267 12 $dt->set_time_zone('floating');
268 12 $dt->set( hour => 12, minute => 0, second => 0, nanosecond => 0 );
269 }
270  
271 21 ▶ $dt->set_time_zone($info->{timezone}) if defined $info->{timezone};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"timezone"} 9 12 21
272 21 ▶ $dt->set_locale($info->{locale}) if defined $info->{locale};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"locale"} 0 21 21
273  
274 21 return $dt;
275 }
276  
277 39 ▶sub _pre_deflate_datetime {
Subroutine _pre_deflate_datetime: called CC=9 SCAR=23.2
278 39 my( $self, $dt, $info ) = @_;
279  
280 # a day stays the day it is in the value's own zone, a time of day the time
281 # it is: no conversion, and the caller's object stays as it is
282 39 ▶ if ($info->{_ic_dt_time} or $self->_is_date_channel($info, 'format_%s')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"_ic_dt_time"} or $self->_is_date_channel($info, "format_%s")) 29 10 39
Condition100%3/3
$info->{"_ic_dt_time"} or $self->_is_date_channel($info, "format_%s")
l!l&&r!l&&!r
21810
MC/DC100%2/2
$info->{"_ic_dt_time"} or $self->_is_date_channel($info, "format_%s")
$info->{"_ic_dt_time"} $self->_is_date_channel($info, "format_%s")
Truth table100%3/3
A or B
A= $info->{"_ic_dt_time"}B= $self->_is_date_channel($info, "format_%s")
ABresult
1X1
011
000
283 29 $dt = $dt->clone;
284 29 ▶ $dt->set_locale($info->{locale}) if defined $info->{locale};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"locale"} 0 29 29
285 29 return $dt;
286 }
287  
288 10 ▶ if (defined $info->{timezone}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"timezone"}) 3 7 10
289 carp "You're using a floating timezone, please see the documentation of"
290 . " DBIO::InflateColumn::DateTime for an explanation"
291 if ref( $dt->time_zone ) eq 'DateTime::TimeZone::Floating'
292 and not $info->{floating_tz_ok}
293 3 ▶ and not $ENV{DBIO_FLOATING_TZ_OK};
Branch: 1 branch
BranchTrueFalseTotal
if ref $dt->time_zone eq "DateTime::TimeZone::Floating" and not $info->{"floating_tz_ok"} and not $ENV{"DBIO_FLOATING_TZ_OK"} 0 3 3
Condition33%2/6
ref $dt->time_zone eq "DateTime::TimeZone::Floating" and not $info->{"floating_tz_ok"} and not $ENV{"DBIO_FLOATING_TZ_OK"}
!ll&&!rl&&r
300
ref $dt->time_zone eq "DateTime::TimeZone::Floating" and not $info->{"floating_tz_ok"}
!ll&&!rl&&r
300
MC/DC0%0/3
ref $dt->time_zone eq "DateTime::TimeZone::Floating" and not $info->{"floating_tz_ok"} and not $ENV{"DBIO_FLOATING_TZ_OK"}
ref $dt->time_zone eq "DateTime::TimeZone::Floating" not $info->{"floating_tz_ok"} not $ENV{"DBIO_FLOATING_TZ_OK"}
Truth table25%1/4
A and not(B) and not(C)
A= ref $dt->time_zone eq "DateTime::TimeZone::Floating"B= not $info->{"floating_tz_ok"}C= not $ENV{"DBIO_FLOATING_TZ_OK"}
ABCresult
0XX0
10X0
1100
1111
294  
295 3 $dt = $dt->clone->set_time_zone($info->{timezone});
296 }
297  
298 10 ▶ $dt = $dt->clone->set_locale($info->{locale}) if defined $info->{locale};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"locale"} 0 10 10
299  
300 10 return $dt;
301 }
302  
303 263 1;
304  
305 __END__
« blib/lib/DBIO/Storage.pm ↑ Summary blib/lib/DBIO/Schema.pm »