| 1 | package DBIx::QuickORM::Type::DateTime::Lazy; | ||||||||||||||||||||||||||||
| 2 | 67 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 67 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 67 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 67 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 67 | ▶ | use overload( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | '""' => sub { $_[0]->string }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 67 | '<=>' => \&_compare, | |||||||||||||||||||||||||||
| 13 | 'cmp' => \&_compare, | ||||||||||||||||||||||||||||
| 14 | 67 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =pod | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | DBIx::QuickORM::Type::DateTime::Lazy - A DateTime built on first use. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | What L<DBIx::QuickORM::Type::DateTime> inflates to. It holds the value as the | ||||||||||||||||||||||||||||
| 27 | database returned it and builds the L<DateTime> the first time one is needed. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | It has three methods: C<datetime> (a copy to use and change), C<datetime_ref> | ||||||||||||||||||||||||||||
| 30 | (the DateTime itself, read only), and C<string>. Call DateTime methods on one | ||||||||||||||||||||||||||||
| 31 | of the first two: | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $year = $at->datetime_ref->year; # read: no copy | ||||||||||||||||||||||||||||
| 34 | my $next = $at->datetime->add(days => 1); # change a copy | ||||||||||||||||||||||||||||
| 35 | $row->update({created => $next}); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Comparisons work as DateTime's do, against a DateTime or another lazy value: | ||||||||||||||||||||||||||||
| 38 | C<==>, C<< < >>, C<eq>, C<lt>, C<sort>, and the rest. Comparing with | ||||||||||||||||||||||||||||
| 39 | anything else, such as a plain string, croaks. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | With a DateTime on the left, DateTime's own comparison runs, and it croaks on | ||||||||||||||||||||||||||||
| 42 | a lazy value. Put the lazy value first, or compare its C<datetime_ref>. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | It stringifies to C<string>, an ISO 8601 form for display. It is always true. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | A row hands out the same cached lazy value on every read (see | ||||||||||||||||||||||||||||
| 47 | L<DBIx::QuickORM::Role::Type/Cached inflated values>), so never change the | ||||||||||||||||||||||||||||
| 48 | DateTime C<datetime_ref> returns. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | The object is a blessed closure, so dumps show only a code reference. A | ||||||||||||||||||||||||||||
| 51 | fetched value that does not parse croaks when the DateTime is built, not when | ||||||||||||||||||||||||||||
| 52 | it is fetched. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | my $at = $row->field('created'); # nothing parsed yet | ||||||||||||||||||||||||||||
| 57 | my $year = $at->datetime_ref->year; # builds the DateTime once | ||||||||||||||||||||||||||||
| 58 | my $text = "$at"; # 2024-02-03T04:05:06 | ||||||||||||||||||||||||||||
| 59 | my $soon = $at < DateTime->now; # compares the dates | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =over 4 | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item $lazy = DBIx::QuickORM::Type::DateTime::Lazy->new(\&build, $kind) | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | A lazy value. C<build> returns the DateTime, and is called once, on first | ||||||||||||||||||||||||||||
| 68 | need. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | package My::Type::Compact; # dates stored as 20240203 | ||||||||||||||||||||||||||||
| 71 | use Role::Tiny::With qw/with/; | ||||||||||||||||||||||||||||
| 72 | with 'DBIx::QuickORM::Role::Type'; | ||||||||||||||||||||||||||||
| 73 | use DBIx::QuickORM::Type::DateTime::Lazy; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub inflate { | ||||||||||||||||||||||||||||
| 76 | my ($class, $raw, $field, $conn) = @_; | ||||||||||||||||||||||||||||
| 77 | return DBIx::QuickORM::Type::DateTime::Lazy->new( | ||||||||||||||||||||||||||||
| 78 | sub { | ||||||||||||||||||||||||||||
| 79 | my ($y, $m, $d) = unpack('A4 A2 A2', $raw); | ||||||||||||||||||||||||||||
| 80 | return DateTime->new( | ||||||||||||||||||||||||||||
| 81 | year => $y, | ||||||||||||||||||||||||||||
| 82 | month => $m, | ||||||||||||||||||||||||||||
| 83 | day => $d, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||||||||||
| 86 | 'date', | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub deflate { | ||||||||||||||||||||||||||||
| 91 | my ($class, $value, $field, $conn) = @_; | ||||||||||||||||||||||||||||
| 92 | $value = $value->datetime_ref if $value->isa('DBIx::QuickORM::Type::DateTime::Lazy'); | ||||||||||||||||||||||||||||
| 93 | return $value->ymd(''); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # share one lazy value: dclone cannot copy it | ||||||||||||||||||||||||||||
| 97 | sub inflated_is_immutable_for_cache { 1 } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | The default C<clone_inflated> croaks on a lazy value, so a type that inflates | ||||||||||||||||||||||||||||
| 100 | to them needs another path (see | ||||||||||||||||||||||||||||
| 101 | L<DBIx::QuickORM::Role::Type/Cached inflated values>). This one declares | ||||||||||||||||||||||||||||
| 102 | C<inflated_is_immutable_for_cache> true, so every reader shares one lazy value | ||||||||||||||||||||||||||||
| 103 | and the text is parsed once. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | L<DateTime> is loaded just before C<build> runs, so C<build> may call | ||||||||||||||||||||||||||||
| 106 | C<< DateTime->new >> without loading it. C<$kind> is the field's: C<date>, | ||||||||||||||||||||||||||||
| 107 | C<timestamp>, or C<timestamptz>. It picks the C<string> form. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 40 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 40 | my $class = shift; | |||||||||||||||||||||||||||
| 115 | 40 | my ($build, $kind) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 40 | my $dt; | |||||||||||||||||||||||||||
| 118 | 88 | ▶ | my $value = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 119 | 88 | ▶ | return $kind if $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 71 | ▶ | return $dt if defined $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 38 | { local $@; require DateTime } | |||||||||||||||||||||||||||
| 122 | 38 | return $dt = $build->(); | |||||||||||||||||||||||||||
| 123 | 40 | }; | |||||||||||||||||||||||||||
| 124 | 40 | return bless($value, $class); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =pod | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =over 4 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item $dt = $lazy->datetime | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | The DateTime: a copy, so changing it does not change this value. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item $dt = $lazy->datetime_ref | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | The DateTime itself, without copying it. Fast, but discouraged: changing it | ||||||||||||||||||||||||||||
| 138 | changes every reader's value. Use it only to read. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =back | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | sub datetime { $_[0]->()->clone } | ||||||||||||||||||||||||||
|
Subroutine
datetime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 31 | ▶ | sub datetime_ref { $_[0]->() } | ||||||||||||||||||||||||||
|
Subroutine
datetime_ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =pod | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =over 4 | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item $text = $lazy->string | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | The value in ISO 8601, for display. It is never sent to the database. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | use DateTime; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | my $dt = DateTime->new( | ||||||||||||||||||||||||||||
| 158 | year => 2024, | ||||||||||||||||||||||||||||
| 159 | month => 2, | ||||||||||||||||||||||||||||
| 160 | day => 3, | ||||||||||||||||||||||||||||
| 161 | hour => 4, | ||||||||||||||||||||||||||||
| 162 | minute => 5, | ||||||||||||||||||||||||||||
| 163 | second => 6, | ||||||||||||||||||||||||||||
| 164 | nanosecond => 500_000_000, | ||||||||||||||||||||||||||||
| 165 | time_zone => '-07:00', | ||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # a lazy value of the given kind over $dt | ||||||||||||||||||||||||||||
| 169 | my $lazy = sub { DBIx::QuickORM::Type::DateTime::Lazy->new(sub { $dt }, $_[0]) }; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | my $instant = $lazy->('timestamptz')->string; # 2024-02-03T04:05:06.5-07:00 | ||||||||||||||||||||||||||||
| 172 | my $stamp = $lazy->('timestamp')->string; # 2024-02-03T04:05:06.5 | ||||||||||||||||||||||||||||
| 173 | my $day = $lazy->('date')->string; # 2024-02-03 | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =over 4 | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item * | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | A timestamp with time zone: C<YYYY-MM-DDTHH:MM:SS+HH:MM>. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | A timestamp without one: the same without the offset. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | A date: C<YYYY-MM-DD>. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =back | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Fractional seconds follow the seconds when there are any. Croaks, as | ||||||||||||||||||||||||||||
| 192 | C<datetime_ref> does, when the value does not parse. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =back | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 18 | ▶ | sub string { | ||||||||||||||||||||||||||
|
Subroutine
string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 200 | 18 | my $dt = $self->(); | |||||||||||||||||||||||||||
| 201 | 17 | my $kind = $self->('kind'); | |||||||||||||||||||||||||||
| 202 | 17 | ▶ | return $dt->ymd if $kind eq 'date'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 14 | my $out = $dt->ymd . 'T' . $dt->hms; | |||||||||||||||||||||||||||
| 205 | 14 | ▶ | $out .= sprintf('.%09d', $dt->nanosecond) =~ s/0+$//r if $dt->nanosecond; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 14 | ▶ | $out .= DateTime::TimeZone->offset_as_string($dt->offset, ':') if $kind eq 'timestamptz'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 14 | return $out; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =pod | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =over 4 | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =item $cmp = $lazy->_compare($other, $swapped) | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | The overloaded C<< <=> >> and C<cmp>: L<DateTime>'s C<compare>, which croaks | ||||||||||||||||||||||||||||
| 219 | when C<$other> is not a DateTime. A lazy C<$other> counts as its DateTime. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 13 | ▶ | sub _compare { | ||||||||||||||||||||||||||
|
Subroutine
_compare: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 226 | 13 | my ($self, $other, $swap) = @_; | |||||||||||||||||||||||||||
| 227 | 13 | ▶ | $other = $other->() if blessed($other) && $other->isa(__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
MC/DC100%2/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other)
$other->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 228 | 13 | my $c = DateTime->compare($self->(), $other); | |||||||||||||||||||||||||||
| 229 | 11 | ▶ | return $swap ? -$c : $c; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 67 | 1; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||