| 1 | package DBIO::Timestamp; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Automatically set and update timestamp columns | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 378 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 378 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 378 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 378 | ▶ | use base qw/DBIO::Base/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 378 | ▶ | use DateTime::Tiny (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 378 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 378 | __PACKAGE__->mk_classdata('timestamp_class' => 'DateTime::Tiny'); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 12055 | ▶ | sub add_columns { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_columns: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 12055 | my ($self, @cols) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 12055 | my @columns; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 12055 | ▶ | while (my $col = shift @cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 37230 | ▶ | my $info = ref $cols[0] ? shift @cols : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 37230 | ▶ | if (delete $info->{set_on_create}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 27 | $info->{_timestamp_on_create} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 37230 | ▶ | if (delete $info->{set_on_update}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 13 | $info->{_timestamp_on_update} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 37230 | push @columns, $col => $info; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 12055 | return $self->next::method(@columns); | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3238 | ▶ | sub prepare_insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_insert: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3238 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 3238 | my $columns_info = $self->result_source->columns_info; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 3238 | for my $col (keys %$columns_info) { | |||||||||||||||||||||||||||||||||||||||||
| 45 | 13737 | my $info = $columns_info->{$col}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 13737 | ▶ | next unless $info->{_timestamp_on_create}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 21 | ▶ | next if defined $self->get_column($col); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 20 | $self->store_column($col => $self->_timestamp_for_column($info)); | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 3238 | return $self->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 128 | ▶ | sub prepare_update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_update: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 128 | my ($self, $upd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 128 | my $columns_info = $self->result_source->columns_info; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 128 | for my $col (keys %$columns_info) { | |||||||||||||||||||||||||||||||||||||||||
| 60 | 530 | my $info = $columns_info->{$col}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 530 | ▶ | next unless $info->{_timestamp_on_update}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 11 | ▶ | delete $upd->{$col} if $upd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 11 | $self->set_inflated_columns({ $col => $self->_timestamp_for_column($info) }); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 128 | return $self->next::method($upd); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | sub get_timestamp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_timestamp: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | my $class = $self->timestamp_class; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | $self->ensure_class_loaded($class) unless $class->can('now'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | return $class->now; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # Resolve the value to store into a timestamp column. Columns managed by | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # InflateColumn::DateTime deflate through the storage datetime formatter | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # (format_datetime), which needs a full DateTime object — a DateTime::Tiny | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # would break there. For such columns inflate the Tiny to a real DateTime (the | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # lazy ->DateTime path, which loads DateTime on demand) so the deflate behaves | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # exactly as a hand-set DateTime would. Every other column keeps the value | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # get_timestamp returned as-is. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 31 | ▶ | sub _timestamp_for_column { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timestamp_for_column: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 31 | my ($self, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 31 | my $ts = $self->get_timestamp; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | return $ts->DateTime | ||||||||||||||||||||||||||||||||||||||||||
| 90 | if $info->{_ic_dt_method} | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 31 | ▶ | and Scalar::Util::blessed($ts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$info->{"_ic_dt_method"} and builtin::blessed($ts) and $ts->isa("DateTime::Tiny")
$info->{"_ic_dt_method"} and builtin::blessed($ts)
MC/DC33%1/3
$info->{"_ic_dt_method"} and builtin::blessed($ts) and $ts->isa("DateTime::Tiny")
$info->{"_ic_dt_method"}
builtin::blessed($ts)
$ts->isa("DateTime::Tiny")
Truth table50%2/4
A and B and C
A= $info->{"_ic_dt_method"}B= builtin::blessed($ts)C= $ts->isa("DateTime::Tiny")
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | and $ts->isa('DateTime::Tiny'); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | return $ts; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | sub col_created { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
col_created: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | ▶ | $name ||= 'created_at'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$name ||= "created_at"
MC/DC100%1/1
$name ||= "created_at"
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | $self->add_columns($name => { | |||||||||||||||||||||||||||||||||||||||||
| 102 | data_type => 'timestamp', | ||||||||||||||||||||||||||||||||||||||||||
| 103 | set_on_create => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | is_nullable => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | ▶ | sub col_updated { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
col_updated: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | $name ||= 'updated_at'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$name ||= "updated_at"
MC/DC100%1/1
$name ||= "updated_at"
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | $self->add_columns($name => { | |||||||||||||||||||||||||||||||||||||||||
| 113 | data_type => 'timestamp', | ||||||||||||||||||||||||||||||||||||||||||
| 114 | set_on_create => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | set_on_update => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | is_nullable => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | sub cols_updated_created { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cols_updated_created: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | $self->col_created; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | $self->col_updated; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 378 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||||||||||||||||