| 1 | package DBIx::Class::ResultClass::TrackColumns; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | our $VERSION = '0.001001'; | |||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use parent 'DBIx::Class'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | __PACKAGE__->mk_classdata('_storage_tracked_columns'); | |||||||||||
| 10 | |||||||||||||
| 11 | sub _track_storage_value { | ||||||||||||
| 12 | 1 | ▶ | my ($self, $col) = @_; | ||||||||||
|
Subroutine
_track_storage_value: called CC=3 SCAR=11.7
| |||||||||||||
| 13 | 1 | ▶ | return 1 if $self->next::method($col); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 14 | 1 | ▶ | return $self->_storage_tracked_columns->{$col} ? 1:0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | } | ||||||||||||
| 16 | |||||||||||||
| 17 | sub register_column { | ||||||||||||
| 18 | 3 | ▶ | my ($self, $column, $info, @rest) = @_; | ||||||||||
|
Subroutine
register_column: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 19 | 3 | ▶ | if(delete $info->{track_storage}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | $self->_storage_tracked_columns({ | ||||||||||||
| 21 | 1 | ▶ | %{ $self->_storage_tracked_columns || {} }, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | $column => 1, | ||||||||||||
| 23 | }); | ||||||||||||
| 24 | } | ||||||||||||
| 25 | 3 | return $self->next::method($column, $info, @rest); | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | sub get_column_storage { | ||||||||||||
| 29 | 2 | ▶ | my ($self, $col) = @_; | ||||||||||
|
Subroutine
get_column_storage: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 30 | 2 | ▶ | return $self->{_column_data_in_storage}{$col} || ''; | ||||||||||
|
Condition100%2/2
$self->{'_column_data_in_storage'}{$col} || ''
MC/DC100%1/1
$self->{'_column_data_in_storage'}{$col} || ''
$self->{'_column_data_in_storage'}{$col}
Truth table100%2/2
A
A= $self->{'_column_data_in_storage'}{$col}
| |||||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 1 | 1; | |||||||||||
| 34 | |||||||||||||
| 35 | =head1 NAME | ||||||||||||
| 36 | |||||||||||||
| 37 | DBIx::Class::ResultClass::TrackColumns - track changed columns | ||||||||||||
| 38 | |||||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||||
| 40 | |||||||||||||
| 41 | package Example::Schema::Result; | ||||||||||||
| 42 | |||||||||||||
| 43 | use strict; | ||||||||||||
| 44 | use warnings; | ||||||||||||
| 45 | |||||||||||||
| 46 | use base 'DBIx::Class'; | ||||||||||||
| 47 | |||||||||||||
| 48 | __PACKAGE__->load_components(qw/ | ||||||||||||
| 49 | ResultClass::TrackColumns | ||||||||||||
| 50 | Core | ||||||||||||
| 51 | /); | ||||||||||||
| 52 | |||||||||||||
| 53 | package Example::Schema::Result::Todo; | ||||||||||||
| 54 | |||||||||||||
| 55 | use warnings; | ||||||||||||
| 56 | use strict; | ||||||||||||
| 57 | |||||||||||||
| 58 | use base 'Example::Schema::Result'; | ||||||||||||
| 59 | |||||||||||||
| 60 | __PACKAGE__->table("todo"); | ||||||||||||
| 61 | __PACKAGE__->add_columns( | ||||||||||||
| 62 | id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 }, | ||||||||||||
| 63 | title => { data_type => 'varchar', is_nullable => 0, size => 60 }, | ||||||||||||
| 64 | status => { data_type => 'varchar', is_nullable => 0, default=>'active', size => 60, track_storage => 1}, | ||||||||||||
| 65 | ); | ||||||||||||
| 66 | |||||||||||||
| 67 | Now the column 'status' is tracked such that if you change it we preserve the original | ||||||||||||
| 68 | loaded from storage value until you persist the row. | ||||||||||||
| 69 | |||||||||||||
| 70 | =head1 DESCRIPTION | ||||||||||||
| 71 | |||||||||||||
| 72 | Allows you to preserve the original value of a column as it was loaded from storage | ||||||||||||
| 73 | if you change it (via ->set_column, for example) but haven't saved the change to storage | ||||||||||||
| 74 | yet. I wrote this because for the L<Valiant> DBIC glue (L<DBIx::Class::Valiant>) you | ||||||||||||
| 75 | sometimes want the original storage value for doing certain types of constraints (for | ||||||||||||
| 76 | example you might have a status field which accepts an enum but there's rules about | ||||||||||||
| 77 | which states you can set based on the existing state). | ||||||||||||
| 78 | |||||||||||||
| 79 | =head1 METHODS | ||||||||||||
| 80 | |||||||||||||
| 81 | This component defines the following public methods | ||||||||||||
| 82 | |||||||||||||
| 83 | =head2 get_column_storage | ||||||||||||
| 84 | |||||||||||||
| 85 | my $old = $row->get_column_storage($column); | ||||||||||||
| 86 | |||||||||||||
| 87 | If a column has been changed since you loaded it from storage but has not yet been persisted you | ||||||||||||
| 88 | can get the original value using this method, if the column is marked as 'tracked'. | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 SEE ALSO | ||||||||||||
| 91 | |||||||||||||
| 92 | L<DBIx::Class> | ||||||||||||
| 93 | |||||||||||||
| 94 | =head1 AUTHOR | ||||||||||||
| 95 | |||||||||||||
| 96 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 99 | |||||||||||||
| 100 | Copyright 2022, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||
| 101 | |||||||||||||
| 102 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||
| 103 | the same terms as Perl itself. | ||||||||||||
| 104 | |||||||||||||
| 105 | =cut | ||||||||||||