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