| 1 | package DBIx::Class::TimeStamp; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use base qw(DBIx::Class); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | __PACKAGE__->load_components( qw/DynamicDefault InflateColumn::DateTime/ ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | DBIx::Class::TimeStamp - DBIx::Class extension to update and create date and time based fields | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Works in conjunction with InflateColumn::DateTime to automatically set update | ||||||||||||||||||||||||||||
| 21 | and create date and time based fields in a table. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package My::Schema; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | __PACKAGE__->load_components(qw( TimeStamp ... Core )); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | __PACKAGE__->add_columns( | ||||||||||||||||||||||||||||
| 30 | id => { data_type => 'integer' }, | ||||||||||||||||||||||||||||
| 31 | t_created => { data_type => 'datetime', set_on_create => 1 }, | ||||||||||||||||||||||||||||
| 32 | t_updated => { data_type => 'datetime', | ||||||||||||||||||||||||||||
| 33 | set_on_create => 1, set_on_update => 1 }, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Now, any update or create actions will update the specified columns with the | ||||||||||||||||||||||||||||
| 37 | current time, using the DateTime inflator. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This is effectively trigger emulation to get consistent behavior across | ||||||||||||||||||||||||||||
| 40 | databases that either implement them poorly or not at all. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 16 | ▶ | sub add_columns { | ||||||||||||||||||||||||||
|
Subroutine
add_columns: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 16 | my ($self, @cols) = @_; | |||||||||||||||||||||||||||
| 46 | 16 | my @columns; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 16 | ▶ | while (my $col = shift @cols) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 64 | ▶ | my $info = ref $cols[0] ? shift @cols : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 64 | ▶ | if ( delete $info->{set_on_create} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 32 | $info->{dynamic_default_on_create} = 'get_timestamp'; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 64 | ▶ | if ( delete $info->{set_on_update} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 16 | $info->{dynamic_default_on_update} = 'get_timestamp'; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 16 | ▶ | if ( defined $info->{dynamic_default_on_create} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $info->{"dynamic_default_on_create"} and $info->{"dynamic_default_on_create"} eq "get_timestamp"
MC/DC0%0/2
defined $info->{"dynamic_default_on_create"} and $info->{"dynamic_default_on_create"} eq "get_timestamp"
defined $info->{"dynamic_default_on_create"}
$info->{"dynamic_default_on_create"} eq "get_timestamp"
Truth table33%1/3
A and B
A= defined $info->{"dynamic_default_on_create"}B= $info->{"dynamic_default_on_create"} eq "get_timestamp"
| |||||||||||||||||||||||||||||
| 59 | $info->{dynamic_default_on_create} eq 'get_timestamp' | ||||||||||||||||||||||||||||
| 60 | ) { | ||||||||||||||||||||||||||||
| 61 | 16 | $info->{dynamic_default_on_update} = 'get_timestamp'; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 64 | push @columns, $col => $info; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 16 | return $self->next::method(@columns); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 METHODS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 get_timestamp | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Returns a DateTime object pointing to now. Override this method if you have | ||||||||||||||||||||||||||||
| 76 | different time accounting functions, or want to do anything special. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | The date and time objects in the database are expected to be inflated. As such | ||||||||||||||||||||||||||||
| 79 | you can be pretty flexible with what you want to return here. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 11 | ▶ | sub get_timestamp { | ||||||||||||||||||||||||||
|
Subroutine
get_timestamp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 11 | return DateTime->now | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | J. Shirley <jshirley@gmail.com> | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Florian Ragwitz (Porting to L<DBIx::Class::DynamicDefault>) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | LTJake/bricas | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Copyright 2009 J. Shirley, all rights reserved. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 102 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 5 | 1; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||