| 1 | package DBIx::Class::OptimisticLocking; | ||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 9 | $DBIx::Class::OptimisticLocking::VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Optimistic locking support for DBIx::Class | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use DBIx::Class 0.08195; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use base 'DBIx::Class'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 9 | __PACKAGE__->mk_classdata( optimistic_locking_strategy => 'dirty' ); | |||||||||||||||||||||||||||||||
| 18 | 9 | __PACKAGE__->mk_classdata('optimistic_locking_ignore_columns'); | |||||||||||||||||||||||||||||||
| 19 | 9 | __PACKAGE__->mk_classdata( optimistic_locking_version_column => 'version' ); | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 9 | my %valid_strategies = map { $_ => undef } qw(dirty all none version); | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | sub optimistic_locking_strategy { | ||||||||||||||||||||||||||||||||
| 24 | my @args = @_; | ||||||||||||||||||||||||||||||||
| 25 | my $class = shift(@args); | ||||||||||||||||||||||||||||||||
| 26 | my ($strategy) = $args[0]; | ||||||||||||||||||||||||||||||||
| 27 | croak "invalid optimistic_locking_strategy $strategy" unless exists $valid_strategies{$strategy}; | ||||||||||||||||||||||||||||||||
| 28 | return $class->_opt_locking_strategy_accessor(@args); | ||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 28 | ▶ | sub update { | ||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 33 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||
| 34 | 28 | my $upd = shift; | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | # we have to do this ahead of time to make sure our WHERE | ||||||||||||||||||||||||||||||||
| 37 | # clause is computed correctly | ||||||||||||||||||||||||||||||||
| 38 | 28 | ▶ | $self->set_inflated_columns($upd) if($upd); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | # short-circuit if we're not changed | ||||||||||||||||||||||||||||||||
| 41 | 28 | ▶ | return $self if !$self->is_changed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 27 | ▶ | if ( $self->optimistic_locking_strategy eq 'version' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | # increment the version number but only if there are dirty | ||||||||||||||||||||||||||||||||
| 45 | # columns that are not being ignored by the optimistic | ||||||||||||||||||||||||||||||||
| 46 | # locking | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 8 | my %dirty_columns = $self->get_dirty_columns; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | delete(@dirty_columns{ @{ $self->optimistic_locking_ignore_columns || [] } }); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->optimistic_locking_ignore_columns || []
MC/DC100%1/1
$self->optimistic_locking_ignore_columns || []
$self->optimistic_locking_ignore_columns
Truth table100%2/2
A
A= $self->optimistic_locking_ignore_columns
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 8 | ▶ | if(%dirty_columns){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 7 | my $v_col = $self->optimistic_locking_version_column; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 7 | my $current_version = $self->{_column_data_in_storage}{$v_col}; | |||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | $current_version = $self->get_column($v_col) || 0 if ! defined $current_version; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->get_column($v_col) || 0
MC/DC100%1/1
$self->get_column($v_col) || 0
$self->get_column($v_col)
Truth table100%2/2
A
A= $self->get_column($v_col)
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | # increment the version | ||||||||||||||||||||||||||||||||
| 59 | 7 | $self->set_column( $v_col, $current_version + 1); | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 27 | my $return = $self->next::method(); | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 20 | return $return; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 35 | ▶ | sub _track_storage_value { | ||||||||||||||||||||||||||||||
|
Subroutine
_track_storage_value: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 70 | 35 | my ( $self, $col ) = @_; | |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 35 | ▶ | return 1 if $self->next::method($col); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 35 | my $mode = $self->optimistic_locking_strategy; | |||||||||||||||||||||||||||||||
| 75 | 35 | ▶ | my $ignore_columns = $self->optimistic_locking_ignore_columns || []; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->optimistic_locking_ignore_columns || []
MC/DC100%1/1
$self->optimistic_locking_ignore_columns || []
$self->optimistic_locking_ignore_columns
Truth table100%2/2
A
A= $self->optimistic_locking_ignore_columns
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 35 | ▶ | if ( $mode eq 'dirty' || $mode eq 'all' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$mode eq "dirty" or $mode eq "all"
MC/DC100%2/2
$mode eq "dirty" or $mode eq "all"
$mode eq "dirty"
$mode eq "all"
Truth table100%3/3
A or B
A= $mode eq "dirty"B= $mode eq "all"
| |||||||||||||||||||||||||||||||||
| 78 | 17 | ▶ | return !first { $col eq $_ } @$ignore_columns; # implicit return from do block | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 79 | } elsif ( $mode eq 'version' ) { | ||||||||||||||||||||||||||||||||
| 80 | 16 | return $col eq $self->optimistic_locking_version_column; # implicit return from do block | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 2 | return 0; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 41 | ▶ | sub _storage_ident_condition { | ||||||||||||||||||||||||||||||
|
Subroutine
_storage_ident_condition: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||
| 88 | 41 | my $self = shift; | |||||||||||||||||||||||||||||||
| 89 | 41 | my $ident_condition = $self->next::method(@_); | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | # YUCK YUCK YUCK | ||||||||||||||||||||||||||||||||
| 92 | 41 | my(undef,undef,undef,$caller) = caller(1); | |||||||||||||||||||||||||||||||
| 93 | 41 | ▶ | return $ident_condition if $caller eq 'DBIx::Class::Row::get_from_storage'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 27 | my $mode = $self->optimistic_locking_strategy; | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 27 | ▶ | my $ignore_columns = $self->optimistic_locking_ignore_columns || []; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->optimistic_locking_ignore_columns || []
MC/DC100%1/1
$self->optimistic_locking_ignore_columns || []
$self->optimistic_locking_ignore_columns
Truth table100%2/2
A
A= $self->optimistic_locking_ignore_columns
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 27 | ▶ | if ( $mode eq 'dirty' ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 100 | 9 | ▶ | my %orig = %{$self->{_column_data_in_storage} || {}}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'_column_data_in_storage'} || {}
MC/DC0%0/1
$self->{'_column_data_in_storage'} || {}
$self->{'_column_data_in_storage'}
Truth table50%1/2
A
A= $self->{'_column_data_in_storage'}
| |||||||||||||||||||||||||||||||||
| 101 | 9 | delete @orig{@$ignore_columns}; | |||||||||||||||||||||||||||||||
| 102 | 9 | $ident_condition = {%orig, %$ident_condition }; | |||||||||||||||||||||||||||||||
| 103 | } elsif ( $mode eq 'version' ) { | ||||||||||||||||||||||||||||||||
| 104 | 8 | my $v_col = $self->optimistic_locking_version_column; | |||||||||||||||||||||||||||||||
| 105 | 8 | ▶ | $ident_condition->{ $v_col } = defined $self->{_column_data_in_storage}{$v_col} ? $self->{_column_data_in_storage}{$v_col} : $self->get_column($v_col); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | } elsif ( $mode eq 'all' ) { | ||||||||||||||||||||||||||||||||
| 107 | 8 | ▶ | my %orig = ($self->get_columns, %{$self->{_column_data_in_storage} || {}}); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'_column_data_in_storage'} || {}
MC/DC0%0/1
$self->{'_column_data_in_storage'} || {}
$self->{'_column_data_in_storage'}
Truth table50%1/2
A
A= $self->{'_column_data_in_storage'}
| |||||||||||||||||||||||||||||||||
| 108 | 8 | delete @orig{@$ignore_columns}; | |||||||||||||||||||||||||||||||
| 109 | 8 | $ident_condition = { %orig, %$ident_condition }; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 27 | return $ident_condition; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 9 | 1; # End of DBIx::Class::OptimisticLocking | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||||||