Helper/ResultSet/DateMethods1.pm

filter: Statementstmt 85.9% 92 / 107 Branchbran 65.0% 13 / 20 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 77.7% 14 / 18 Podpod 100% 10 / 10 totaltotal 77.1% 135 / 175 CC 16 SCAR 12.7
CC
16
Cov
79%
_flatten_thing
8.2
update
6.0
utc
3.0
CRAP 3.6
« Helper/Row/JoinTable.pm ↑ Summary Helper/ResultSet/Errors.pm »
1 package DBIx::Class::Helper::ResultSet::DateMethods1;
2 54 $DBIx::Class::Helper::ResultSet::DateMethods1::VERSION = '2.033001';
3 # ABSTRACT: Work with dates in your RDBMS nicely
4  
5 54 ▶use parent 'DBIx::Class::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶use DBIx::Introspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub _flatten_thing {
15 175 ▶ my ($self, $thing) = @_;
Subroutine _flatten_thing: called CC=8 SCAR=21.0
16  
17 175 ▶ die 'you dummy' unless defined $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing 0 170 170
18 175 my $ref = ref $thing;
19  
20 175 ▶ return ('?', $thing) if !$ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 45 125 170
21  
22 130 ▶ if ($ref eq 'HASH' && exists $thing->{'-ident'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH" and exists $thing->{"-ident"}) 124 1 125
Condition66%2/3
$ref eq "HASH" and exists $thing->{"-ident"}
!ll&&!rl&&r
10124
MC/DC50%1/2
$ref eq "HASH" and exists $thing->{"-ident"}
$ref eq "HASH" exists $thing->{"-ident"}
Truth table66%2/3
A and B
A= $ref eq "HASH"B= exists $thing->{"-ident"}
ABresult
0X0
100
111
23 129 my $thing = $thing->{'-ident'};
24 129 ▶ $thing = $self->current_source_alias . $thing if $thing =~ m/^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $thing =~ /^\./ 11 113 124
25 129 return $self->result_source->storage->sql_maker->_quote($thing)
26 }
27  
28 6 ▶ return ${$thing} if $ref eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" 0 1 1
29  
30 # FIXME: this should have the right bind type
31 6 ▶ return ('?', $self->utc($thing)) if $thing->$_isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->$DBIx::Class::Helper::ResultSet::DateMethods1::_isa("DateTime") 1 0 1
32 5 return @{${$thing}};
33 }
34  
35 sub _introspector {
36 5 my $d = DBIx::Introspector->new(drivers => '2013-12.01');
37  
38 5 $d->decorate_driver_unconnected(MSSQL => now_utc_sql => 'GETUTCDATE()');
39 5 $d->decorate_driver_unconnected(SQLite => now_utc_sql => q<DATETIME('now')>);
40 5 $d->decorate_driver_unconnected(mysql => now_utc_sql => 'UTC_TIMESTAMP()');
41 5 $d->decorate_driver_unconnected(Oracle => now_utc_sql => 'sys_extract_utc(SYSTIMESTAMP)');
42 5 $d->decorate_driver_unconnected(Pg => now_utc_sql => 'CURRENT_TIMESTAMP');
43 MSSQL: {
44 5 my %part_map = (
45 year => 'year',
46 quarter => 'quarter',
47 month => 'month',
48 day_of_year => 'dayofyear',
49 day_of_month => 'day',
50 week => 'week',
51 day_of_week => 'ISO_WEEK',
52 hour => 'hour',
53 minute => 'minute',
54 second => 'second',
55 millisecond => 'millisecond',
56 nanosecond => 'nanosecond',
57 non_iso_day_of_week => 'weekday',
58 timezone_as_minutes => 'TZoffset',
59 );
60  
61 $d->decorate_driver_unconnected(MSSQL => datepart_sql => sub {
62 sub {
63 my ($date_sql, $part) = @_;
64  
65 my ($sql, @args) = @$date_sql;
66  
67 return [
68 "DATEPART($part_map{$part}, $sql)",
69 @args
70 ]
71 }
72 5 });
73  
74  
75 5 my %diff_part_map = %part_map;
76 5 $diff_part_map{day} = delete $diff_part_map{day_of_year};
77 5 delete $diff_part_map{day_of_month};
78 5 delete $diff_part_map{day_of_week};
79  
80 $d->decorate_driver_unconnected(MSSQL => dateadd_sql => sub {
81 sub {
82 my ($date_sql, $unit, $amount_sql) = @_;
83  
84 my ($d_sql, @d_args) = @{$date_sql};
85 my ($a_sql, @a_args) = @{$amount_sql};
86  
87 return [
88 "DATEADD($diff_part_map{$unit}, CAST($a_sql AS int), $d_sql)",
89 @a_args, @d_args,
90 ];
91 }
92 5 });
93 }
94  
95 SQLITE: {
96 5 my %part_map = (
97 month => 'm',
98 day_of_month => 'd',
99 year => 'Y',
100 hour => 'H',
101 day_of_year => 'j',
102 minute => 'M',
103 second => 'S',
104 day_of_week => 'w',
105 week => 'W',
106 # maybe don't support these or prefix them with 'sqlite.'?
107 julian_day => 'J',
108 seconds_since_epoch => 's',
109 fractional_seconds => 'f',
110 );
111  
112 $d->decorate_driver_unconnected(SQLite => datepart_sql => sub {
113 sub {
114 my ($date_sql, $part) = @_;
115  
116 my ($sql, @args) = @$date_sql;
117  
118 return [
119 "STRFTIME('%$part_map{$part}', $sql)",
120 @args
121 ]
122 }
123 5 });
124  
125  
126 5 my %diff_part_map = (
127 day => 'days',
128 hour => 'hours',
129 minute => 'minutes',
130 second => 'seconds',
131 month => 'months',
132 year => 'years',
133 );
134  
135 $d->decorate_driver_unconnected(SQLite => dateadd_sql => sub {
136 sub {
137 my ($date_sql, $unit, $amount_sql) = @_;
138  
139 my ($d_sql, @d_args) = @{$date_sql};
140 my ($a_sql, @a_args) = @{$amount_sql};
141  
142 die "unknown part $unit" unless $diff_part_map{$unit};
143  
144 return [
145 "DATETIME($d_sql, $a_sql || ?)",
146 @d_args, @a_args, " $diff_part_map{$unit}"
147 ];
148 }
149 5 });
150 }
151  
152 PG: {
153 5 my %part_map = (
154 century => 'century',
155 decade => 'decade',
156 day_of_month => 'day',
157 day_of_week => 'dow',
158 day_of_year => 'doy',
159 seconds_since_epoch => 'epoch',
160 hour => 'hour',
161 iso_day_of_week => 'isodow',
162 iso_year => 'isoyear',
163 microsecond => 'microseconds',
164 millenium => 'millenium',
165 millisecond => 'milliseconds',
166 minute => 'minute',
167 month => 'month',
168 quarter => 'quarter',
169 second => 'second',
170 timezone => 'timezone',
171 timezone_hour => 'timezone_hour',
172 timezone_minute => 'timezone_minute',
173 week => 'week',
174 year => 'year',
175 );
176  
177 5 my %diff_part_map = %part_map;
178 5 delete $diff_part_map{qw(
179 day_of_week day_of_year iso_day_of_week iso_year millenium quarter
180 seconds_since_epoch timezone timezone_hour timezone_minute
181 )};
182 5 $diff_part_map{day} = delete $diff_part_map{day_of_month};
183  
184 $d->decorate_driver_unconnected(Pg => datepart_sql => sub {
185 sub {
186 my ($date_sql, $part) = @_;
187  
188 my ($sql, @args) = @$date_sql;
189 @args = ([{ dbd_attrs => SQL_TIMESTAMP }, $args[0]])
190 if $sql eq '?' && @args == 1;
191  
192 return [
193 "date_part(?, $sql)",
194 $part_map{$part}, @args
195 ]
196 }
197 5 });
198  
199 $d->decorate_driver_unconnected(Pg => dateadd_sql => sub {
200 sub {
201 my ($date_sql, $unit, $amount_sql) = @_;
202  
203 my ($d_sql, @d_args) = @{$date_sql};
204 my ($a_sql, @a_args) = @{$amount_sql};
205  
206 @d_args = ([{ dbd_attrs => SQL_TIMESTAMP }, $d_args[0]])
207 if $d_sql eq '?' && @d_args == 1;
208  
209 die "unknown part $unit" unless $diff_part_map{$unit};
210  
211 return [
212 "($d_sql + $a_sql * interval '1 $diff_part_map{$unit}')",
213 @d_args, @a_args,
214 ];
215 }
216 5 });
217 }
218  
219 MYSQL: {
220 5 my %part_map = (
221 microsecond => 'MICROSECOND',
222 second => 'SECOND',
223 minute => 'MINUTE',
224 hour => 'HOUR',
225 day_of_month => 'DAY',
226 week => 'WEEK',
227 month => 'MONTH',
228 quarter => 'QUARTER',
229 year => 'YEAR',
230 second_microsecond => 'SECOND_MICROSECOND',
231 minute_microsecond => 'MINUTE_MICROSECOND',
232 minute_second => 'MINUTE_SECOND',
233 hour_microsecond => 'HOUR_MICROSECOND',
234 hour_second => 'HOUR_SECOND',
235 hour_minute => 'HOUR_MINUTE',
236 day_microsecond => 'DAY_MICROSECOND',
237 day_second => 'DAY_SECOND',
238 day_minute => 'DAY_MINUTE',
239 day_hour => 'DAY_HOUR',
240 year_month => 'YEAR_MONTH',
241 );
242  
243 my %diff_part_map = %part_map;
244 $diff_part_map{day} = delete $diff_part_map{day_of_month};
245 delete $diff_part_map{qw(
246 second_microsecond minute_microsecond minute_second
247 hour_microsecond hour_second hour_minute day_microsecond
248 day_second day_minute day_hour year_month
249 )};
250  
251 $d->decorate_driver_unconnected(mysql => datepart_sql => sub {
252 sub {
253 my ($date_sql, $part) = @_;
254  
255 my ($sql, @args) = @$date_sql;
256  
257 return [
258 "EXTRACT($part_map{$part} FROM $sql)", @args
259 ]
260 }
261 });
262  
263 $d->decorate_driver_unconnected(mysql => dateadd_sql => sub {
264 sub {
265 my ($date_sql, $unit, $amount_sql) = @_;
266  
267 my ($d_sql, @d_args) = @{$date_sql};
268 my ($a_sql, @a_args) = @{$amount_sql};
269  
270 die "unknown part $unit" unless $diff_part_map{$unit};
271  
272 return [
273 "DATE_ADD($d_sql, INTERVAL $a_sql $diff_part_map{$unit})",
274 @d_args, @a_args,
275 ];
276 }
277 });
278 }
279  
280 ORACLE: {
281 my %part_map = (
282 second => 'SECOND',
283 minute => 'MINUTE',
284 hour => 'HOUR',
285 day_of_month => 'DAY',
286 month => 'MONTH',
287 year => 'YEAR',
288 );
289  
290 $d->decorate_driver_unconnected(Oracle => datepart_sql => sub {
291 sub {
292 my ($date_sql, $part) = @_;
293  
294 my ($sql, @args) = @$date_sql;
295  
296 return [
297 "EXTRACT($part_map{$part} FROM TO_TIMESTAMP($sql))", @args
298 ]
299 }
300 });
301  
302 my %diff_part_map = %part_map;
303 $diff_part_map{day} = delete $diff_part_map{day_of_month};
304 delete $diff_part_map{$_} for qw(year month);
305 $d->decorate_driver_unconnected(Oracle => dateadd_sql => sub {
306 sub {
307 my ($date_sql, $unit, $amount_sql) = @_;
308  
309 my ($d_sql, @d_args) = @{$date_sql};
310 my ($a_sql, @a_args) = @{$amount_sql};
311  
312 die "unknown unit $unit" unless $diff_part_map{$unit};
313  
314 return [
315 "(TO_TIMESTAMP($d_sql) + NUMTODSINTERVAL($a_sql, ?))",
316 @d_args, @a_args, $diff_part_map{$unit}
317 ];
318 }
319 });
320 }
321 return $d;
322 }
323  
324 54 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
325  
326  
327 sub delete {
328 1 ▶ my $self = shift;
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
329  
330 $self = $self->as_subselect_rs
331 1 ▶ if $self->_resolved_attrs->{_DBICH_DM1};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_resolved_attrs->{"_DBICH_DM1"} 1 0 1
332  
333 1 return $self->next::method(@_);
334 }
335  
336 sub update {
337 0 ▶ my $self = shift;
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
338  
339 $self = $self->as_subselect_rs
340 0 ▶ if $self->_resolved_attrs->{_DBICH_DM1};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_resolved_attrs->{"_DBICH_DM1"} 0 0 0
341  
342 0 return $self->next::method(@_);
343 }
344  
345 sub utc {
346 6 ▶ my ($self, $datetime) = @_;
Subroutine utc: called CC=3 SCAR=11.0
Pod documented
347  
348 6 my $tz_name = $datetime->time_zone->name;
349  
350 6 ▶ die "floating dates are not allowed"
Branch: 1 branch
BranchTrueFalseTotal
if $tz_name eq "floating" 5 1 6
351 if $tz_name eq 'floating';
352  
353 1 ▶ $datetime = $datetime->clone->set_time_zone('UTC')
Branch: 1 branch
BranchTrueFalseTotal
unless $tz_name eq "UTC" 0 1 1
354 unless $tz_name eq 'UTC';
355  
356 1 $_[0]->result_source->storage->datetime_parser->format_datetime($datetime)
357 }
358  
359 sub dt_before {
360 1 ▶ my ($self, $l, $r) = @_;
Subroutine dt_before: called CC=1 SCAR=0.0
Pod documented
361  
362 1 my ($l_sql, @l_args) = _flatten_thing($self, $l);
363 1 my ($r_sql, @r_args) = _flatten_thing($self, $r);
364  
365 1 return $self->search(\[
366 "$l_sql < $r_sql", @l_args, @r_args
367 ], { _DBICH_DM1 => 1 });
368 }
369  
370 sub dt_on_or_before {
371 0 ▶ my ($self, $l, $r) = @_;
Subroutine dt_on_or_before: not called CC=1 SCAR=6.9
Pod documented
372  
373 0 my ($l_sql, @l_args) = _flatten_thing($self, $l);
374 0 my ($r_sql, @r_args) = _flatten_thing($self, $r);
375  
376 0 $self->search(\[
377 "$l_sql <= $r_sql", @l_args, @r_args
378 ], { _DBICH_DM1 => 1 });
379 }
380  
381 sub dt_on_or_after {
382 0 ▶ my ($self, $l, $r) = @_;
Subroutine dt_on_or_after: not called CC=1 SCAR=6.9
Pod documented
383  
384 0 my ($l_sql, @l_args) = _flatten_thing($self, $l);
385 0 my ($r_sql, @r_args) = _flatten_thing($self, $r);
386  
387 0 return $self->search(\[
388 "$l_sql >= $r_sql", @l_args, @r_args
389 ], { _DBICH_DM1 => 1 });
390 }
391  
392 sub dt_after {
393 0 ▶ my ($self, $l, $r) = @_;
Subroutine dt_after: not called CC=1 SCAR=6.9
Pod documented
394  
395 0 my ($l_sql, @l_args) = _flatten_thing($self, $l);
396 0 my ($r_sql, @r_args) = _flatten_thing($self, $r);
397  
398 0 return $self->search(\[
399 "$l_sql > $r_sql", @l_args, @r_args
400 ], { _DBICH_DM1 => 1 });
401 }
402  
403 54 my $d;
404 sub utc_now {
405 5 ▶ my $self = shift;
Subroutine utc_now: called CC=2 SCAR=7.2
Pod documented
406 5 my $storage = $self->result_source->storage;
407 5 $storage->ensure_connected;
408  
409 5 ▶ $d ||= _introspector();
Condition33%1/3
$d ||= _introspector()
l!l&&r!l&&!r
050
MC/DC0%0/2
$d ||= _introspector()
$d _introspector()
Truth table33%1/3
A ||= B
A= $dB= _introspector()
ABresult
1X1
011
000
410  
411 5 return \( $d->get($storage->dbh, undef, 'now_utc_sql') );
412 }
413  
414 sub dt_SQL_add {
415 45 ▶ my ($self, $thing, $unit, $amount) = @_;
Subroutine dt_SQL_add: called CC=2 SCAR=7.2
Pod documented
416  
417 45 my $storage = $self->result_source->storage;
418 45 $storage->ensure_connected;
419  
420 45 ▶ $d ||= _introspector();
Condition33%1/3
$d ||= _introspector()
l!l&&r!l&&!r
4500
MC/DC0%0/2
$d ||= _introspector()
$d _introspector()
Truth table33%1/3
A ||= B
A= $dB= _introspector()
ABresult
1X1
011
000
421  
422 return \(
423 45 $d->get($storage->dbh, undef, 'dateadd_sql')->(
424 [ _flatten_thing($self, $thing) ],
425 $unit,
426 [ _flatten_thing($self, $amount) ],
427 )
428 );
429 }
430  
431 sub dt_SQL_pluck {
432 78 ▶ my ($self, $thing, $part) = @_;
Subroutine dt_SQL_pluck: called CC=2 SCAR=7.2
Pod documented
433  
434 78 my $storage = $self->result_source->storage;
435 78 $storage->ensure_connected;
436  
437 78 ▶ $d ||= _introspector();
Condition33%1/3
$d ||= _introspector()
l!l&&r!l&&!r
7800
MC/DC0%0/2
$d ||= _introspector()
$d _introspector()
Truth table33%1/3
A ||= B
A= $dB= _introspector()
ABresult
1X1
011
000
438  
439 return \(
440 78 $d->get($storage->dbh, undef, 'datepart_sql')->(
441 [ _flatten_thing($self, $thing) ],
442 $part,
443 )
444 );
445 }
446  
447 54 1;
448  
449 __END__
450  
451 =pod
452  
453 =head1 NAME
454  
455 DBIx::Class::Helper::ResultSet::DateMethods1 - Work with dates in your RDBMS nicely
456  
457 =head1 SYNOPSIS
458  
459 package MySchema::ResultSet::Bar;
460  
461 use strict;
462 use warnings;
463  
464 use parent 'DBIx::Class::ResultSet';
465  
466 __PACKAGE__->load_components('Helper::ResultSet::DateMethods1');
467  
468 # in code using resultset
469  
470 # get count per year/month
471 $rs->search(undef, {
472 columns => {
473 count => '*',
474 year => $rs->dt_SQL_pluck({ -ident => '.start' }, 'year'),
475 month => $rs->dt_SQL_pluck({ -ident => '.start' }, 'month'),
476 },
477 group_by => [
478 $rs->dt_SQL_pluck({ -ident => '.start' }, 'year'),
479 $rs->dt_SQL_pluck({ -ident => '.start' }, 'month'),
480 ],
481 });
482
483 # mysql
484 (SELECT `me`.*, EXTRACT(MONTH FROM `me`.`start`), EXTRACT(YEAR FROM `me`.`start`) FROM `HasDateOps` `me` GROUP BY EXTRACT(YEAR FROM `me`.`start`), EXTRACT(MONTH FROM `me`.`start`))
485  
486 # SQLite
487 (SELECT "me".*, STRFTIME('%m', "me"."start"), STRFTIME('%Y', "me"."start") FROM "HasDateOps" "me" GROUP BY STRFTIME('%Y', "me"."start"), STRFTIME('%m', "me"."start"))
488  
489 =head1 DESCRIPTION
490  
491 See L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it
492 to your entire schema.
493  
494 This ResultSet component gives the user tools to do B<mostly> portable date
495 manipulation in the database. Before embarking on a cross database project,
496 take a look at L</IMPLEMENTATION> to see what might break on switching
497 databases.
498  
499 This package has a few types of methods.
500  
501 =over
502  
503 =item Search Shortcuts
504  
505 These, like typical ResultSet methods, return another ResultSet. See
506 L</dt_before>, L</dt_on_or_before>, L</dt_on_or_after>, and L</dt_after>.
507  
508 =item The date helper
509  
510 There is only one: L</utc>. Makes searching with dates a little easier.
511  
512 =item SQL generators
513  
514 These help generate more complex queries. The can be used in many different
515 parts of L<DBIx::Class::ResultSet/search>. See L</utc_now>, L</dt_SQL_pluck>,
516 and L</dt_SQL_add>.
517  
518 =back
519  
520 =head1 TYPES
521  
522 Because these methods are so limited in scope they can be a bit more smart
523 than typical C<SQL::Abstract> trees.
524  
525 There are "smart types" that this package supports.
526  
527 =over
528  
529 =item * vanilla scalars (C<1>, C<2012-12-12 12:12:12>)
530  
531 bound directly as untyped values
532  
533 =item * hashrefs with an C<-ident> (C<< { -ident => '.foo' } >>)
534  
535 As usual this gets flattened into a column. The one special feature in this
536 module is that columns starting with a dot will automatically be prefixed with
537 L<DBIx::Class::ResultSet/current_source_alias>.
538  
539 =item * L<DateTime> objects
540  
541 C<DateTime> objects work as if they were passed to L</utc>.
542  
543 =item * C<ScalarRef> (C<< \'NOW()' >>)
544  
545 As usual in C<DBIx::Class>, C<ScalarRef>'s will be flattened into regular SQL.
546  
547 =item * C<ArrayRefRef> (C<< \["SELECT foo FROM bar WHERE id = ?", [{}, 1]] >>)
548  
549 As usual in C<DBIx::Class>, C<ArrayRefRef>'s will be flattened into SQL with
550 bound values.
551  
552 =back
553  
554 Anything not mentioned in the above list will explode, one way or another.
555  
556 =head1 IMPLEMENTATION
557  
558 =encoding utf8
559  
560 The exact details for the functions your database engine provides.
561  
562 If a piece of functionality is flagged with ⚠, it means that the feature in
563 question is not portable at all, and only supported on that engine.
564  
565 =head2 C<SQL Server>
566  
567 =over
568  
569 =item * L</utc_now> - L<GETUTCDATE|http://msdn.microsoft.com/en-us/library/ms178635.aspx>
570  
571 =item * L</dt_SQL_pluck> - L<DATEPART|http://msdn.microsoft.com/en-us/library/ms174420.aspx>
572  
573 Supported units
574  
575 =over
576  
577 =item * year
578  
579 =item * quarter
580  
581 =item * month
582  
583 =item * day_of_year
584  
585 =item * day_of_month
586  
587 =item * week
588  
589 =item * day_of_week
590  
591 =item * hour
592  
593 =item * minute
594  
595 =item * second
596  
597 =item * millisecond
598  
599 =item * nanosecond ⚠
600  
601 =item * non_iso_day_of_week
602  
603 SQL Server offers both C<ISO_WEEK> and C<weekday>. For interop reasons
604 C<weekday> uses the C<ISO_WEEK> version.
605  
606 =item * timezone_as_minutes ⚠
607  
608 =back
609  
610 =item * L</dt_SQL_add> - L<DATEADD|http://msdn.microsoft.com/en-us/library/ms186819.aspx>
611  
612 Supported units
613  
614 =over
615  
616 =item * year
617  
618 =item * quarter
619  
620 =item * month
621  
622 =item * day
623  
624 =item * week
625  
626 =item * hour
627  
628 =item * minute
629  
630 =item * second
631  
632 =item * millisecond
633  
634 =item * nanosecond ⚠
635  
636 =item * iso_day_of_week
637  
638 =item * timezone_as_minutes ⚠
639  
640 =back
641  
642 =back
643  
644 =head2 C<SQLite>
645  
646 =over
647  
648 =item * L</utc_now> - L<DATETIME('now')|https://www.sqlite.org/lang_datefunc.html>
649  
650 =item * L</dt_SQL_pluck> - L<STRFTIME|https://www.sqlite.org/lang_datefunc.html>
651  
652 Note: C<SQLite>'s pluck implementation pads numbers with zeros, because it is
653 implemented on based on a formatting function. If you want your code to work
654 on SQLite you'll need to strip off (or just numify) what you get out of the
655 database first.
656  
657 Available units
658  
659 =over
660  
661 =item * month
662  
663 =item * day_of_month
664  
665 =item * year
666  
667 =item * hour
668  
669 =item * day_of_year
670  
671 =item * minute
672  
673 =item * second
674  
675 =item * day_of_week
676  
677 =item * week
678  
679 =item * julian_day ⚠
680  
681 =item * seconds_since_epoch
682  
683 =item * fractional_seconds ⚠
684  
685 =back
686  
687 =item * L</dt_SQL_add> - L<DATETIME|https://www.sqlite.org/lang_datefunc.html>
688  
689 Available units
690  
691 =over
692  
693 =item * day
694  
695 =item * hour
696  
697 =item * minute
698  
699 =item * second
700  
701 =item * month
702  
703 =item * year
704  
705 =back
706  
707 =back
708  
709 =head2 C<PostgreSQL>
710  
711 =over
712  
713 =item * L</utc_now> - L<CURRENT_TIMESTAMP|http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT>
714  
715 =item * L</dt_SQL_pluck> - L<date_part|http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT>
716  
717 Available units
718  
719 =over
720  
721 =item * century ⚠
722  
723 =item * decade ⚠
724  
725 =item * day_of_month
726  
727 =item * day_of_week
728  
729 =item * day_of_year
730  
731 =item * seconds_since_epoch
732  
733 =item * hour
734  
735 =item * iso_day_of_week
736  
737 =item * iso_year
738  
739 =item * microsecond
740  
741 =item * millenium ⚠
742  
743 =item * millisecond
744  
745 =item * minute
746  
747 =item * month
748  
749 =item * quarter
750  
751 =item * second
752  
753 =item * timezone ⚠
754  
755 =item * timezone_hour ⚠
756  
757 =item * timezone_minute ⚠
758  
759 =item * week
760  
761 =item * year
762  
763 =back
764  
765 =item * L</dt_SQL_add> - Addition and L<interval|http://www.postgresql.org/docs/current/static/functions-datetime.html#OPERATORS-DATETIME-TABLE>
766  
767 To be clear, it ends up looking like:
768 C<< ("some_column" + 5 * interval '1 minute') >>
769  
770 Available units
771  
772 =over
773  
774 =item * century ⚠
775  
776 =item * decade ⚠
777  
778 =item * day
779  
780 =item * hour
781  
782 =item * microsecond ⚠
783  
784 =item * millisecond
785  
786 =item * minute
787  
788 =item * month
789  
790 =item * second
791  
792 =item * week
793  
794 =item * year
795  
796 =back
797  
798 =back
799  
800 =head2 C<MySQL>
801  
802 =over
803  
804 =item * L</utc_now> - L<UTC_TIMESTAMP|https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_utc-timestamp>
805  
806 =item * L</dt_SQL_pluck> - L<EXTRACT|https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_extract>
807  
808 Available units
809  
810 =over
811  
812 =item * microsecond
813  
814 =item * second
815  
816 =item * minute
817  
818 =item * hour
819  
820 =item * day_of_month
821  
822 =item * week
823  
824 =item * month
825  
826 =item * quarter
827  
828 =item * year
829  
830 =item * second_microsecond ⚠
831  
832 =item * minute_microsecond ⚠
833  
834 =item * minute_second ⚠
835  
836 =item * hour_microsecond ⚠
837  
838 =item * hour_second ⚠
839  
840 =item * hour_minute ⚠
841  
842 =item * day_microsecond ⚠
843  
844 =item * day_second ⚠
845  
846 =item * day_minute ⚠
847  
848 =item * day_hour ⚠
849  
850 =item * year_month ⚠
851  
852 =back
853  
854 =item * L</dt_SQL_add> - L<DATE_ADD|https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add>
855  
856 Available units
857  
858 =over
859  
860 =item * microsecond
861  
862 =item * second
863  
864 =item * minute
865  
866 =item * hour
867  
868 =item * day
869  
870 =item * week
871  
872 =item * month
873  
874 =item * quarter
875  
876 =item * year
877  
878 =back
879  
880 =back
881  
882 =head2 C<Oracle>
883  
884 =over
885  
886 =item * L</utc_now> - L<sys_extract_utc(SYSTIMESTAMP)|http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions167.htm>
887  
888 =item * L</dt_SQL_pluck> - L<EXTRACT|docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm>
889  
890 Available units
891  
892 =over
893  
894 =item * second
895  
896 =item * minute
897  
898 =item * hour
899  
900 =item * day_of_month
901  
902 =item * month
903  
904 =item * year
905  
906 =back
907  
908 =item * L</dt_SQL_add> - Addition and L<NUMTODSINTERVAL|http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions103.htm>
909  
910 To be clear, it ends up looking like:
911 C<< ("some_column" + NUMTODSINTERVAL(4, 'MINUTE') >>
912  
913 Available units
914  
915 =over
916  
917 =item * second
918  
919 =item * minute
920  
921 =item * hour
922  
923 =item * day
924  
925 =back
926  
927 =back
928  
929 =head1 CONTRIBUTORS
930  
931 These people worked on the original implementation, and thus deserve some
932 credit for at least providing me a reference to implement this based off of:
933  
934 =over
935  
936 =item Alexander Hartmaier (abraxxa) for Oracle implementation details
937  
938 =item Devin Austin (dhoss) for Pg implementation details
939  
940 =item Rafael Kitover (caelum) for providing a test environment with lots of DBs
941  
942 =back
943  
944 =head1 WHENCE dt_SQL_diff?
945  
946 The original implementation of these date helpers (originally dubbed date
947 operators) included a third operator called C<"diff">. It existed to
948 subtract one date from another and return a duration. After using it a few
949 times and getting bitten every time, I decided to stop using it and instead
950 compare against actual dates always. If someone can come up with a good use
951 case I am interested in re-implementing C<dt_SQL_diff>, but I worry that it
952 will be very unportable and generally not very useful.
953  
954 =head1 METHODS
955  
956 =head2 utc
957  
958 $rs->search({
959 'some_date' => $rs->utc($datetime),
960 })->all
961  
962 Takes a L<DateTime> object, updates the C<time_zone> to C<UTC>, and formats it
963 according to whatever database engine you are using.
964  
965 Dies if you pass it a date with a C<< floating time_zone >>.
966  
967 =head2 utc_now
968  
969 Returns a C<ScalarRef> representing the way to get the current date and time
970 in C<UTC> for whatever database engine you are using.
971  
972 =head2 dt_before
973  
974 $rs->dt_before({ -ident => '.start' }, { -ident => '.end' })->all
975  
976 Takes two values, each an expression of L</TYPES>.
977  
978 =head2 dt_on_or_before
979  
980 $rs->dt_on_or_before({ -ident => '.start' }, DateTime->now)->all
981  
982 Takes two values, each an expression of L</TYPES>.
983  
984 =head2 dt_on_or_after
985  
986 $rs->dt_on_or_after(DateTime->now, { ident => '.end' })->all
987  
988 Takes two values, each an expression of L</TYPES>.
989  
990 =head2 dt_after
991  
992 $rs->dt_after({ ident => '.end' }, $rs->get_column('datecol')->as_query)->all
993  
994 Takes two values, each an expression of L</TYPES>.
995  
996 =head2 dt_SQL_add
997  
998 # which ones start in 3 minutes?
999 $rs->dt_on_or_after(
1000 { ident => '.start' },
1001 $rs->dt_SQL_add($rs->utc_now, 'minute', 3)
1002 )->all
1003  
1004 Takes three arguments: a date conforming to L</TYPES>, a unit, and an amount.
1005 The idea is to add the given unit to the datetime. See your L</IMPLEMENTATION>
1006 for what units are accepted.
1007  
1008 =head2 dt_SQL_pluck
1009  
1010 # get count per year
1011 $rs->search(undef, {
1012 columns => {
1013 count => '*',
1014 year => $rs->dt_SQL_pluck({ -ident => '.start' }, 'year'),
1015 },
1016 group_by => [$rs->dt_SQL_pluck({ -ident => '.start' }, 'year')],
1017 })->hri->all
1018  
1019 Takes two arguments: a date conforming to L</TYPES> and a unit. The idea
1020 is to pluck a given unit from the datetime. See your L</IMPLEMENTATION>
1021 for what units are accepted.
1022  
1023 =head1 AUTHOR
1024  
1025 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
1026  
1027 =head1 COPYRIGHT AND LICENSE
1028  
1029 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
1030  
1031 This is free software; you can redistribute it and/or modify it under
1032 the same terms as the Perl 5 programming language system itself.
1033  
1034 =cut
« Helper/Row/JoinTable.pm ↑ Summary Helper/ResultSet/Errors.pm »