| 1 | package DBIx::Class::BatchUpdate::Update; | ||||||||||||||||||||
| 2 | 1 | $DBIx::Class::BatchUpdate::Update::VERSION = '1.003'; | |||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use autobox::Core; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use true; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Storable qw/ nfreeze /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use DBIx::Class::BatchUpdate::Batch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | has rows => ( is => "ro", required => 1 ); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | has resultset => (is => "lazy"); | |||||||||||||||||||
| 16 | sub _build_resultset { | ||||||||||||||||||||
| 17 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_resultset: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 18 | 2 | ▶ | my $row = $self->rows->[0] or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 1 | return $row->result_source->resultset(); | |||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | has batches => ( is => "lazy"); | |||||||||||||||||||
| 23 | sub _build_batches { | ||||||||||||||||||||
| 24 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_batches: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 25 | 2 | ▶ | $self->resultset or return []; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | my $key_batch = {}; | |||||||||||||||||||
| 28 | 1 | for my $row ($self->rows->elements) { | |||||||||||||||||||
| 29 | 3 | my $key_value = { $row->get_dirty_columns }; | |||||||||||||||||||
| 30 | 3 | ▶ | my $batch_key = $self->batch_key($key_value) or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 3 | ▶ | my $batch = $key_batch->{ $batch_key } //= DBIx::Class::BatchUpdate::Batch->new({ | ||||||||||||||||||
|
Condition66%2/3
$key_batch->{$batch_key} //= "DBIx::Class::BatchUpdate::Batch"->new({"key_value", $key_value, "resultset", $self->resultset, "key", $batch_key})
MC/DC0%0/2
$key_batch->{$batch_key} //= "DBIx::Class::BatchUpdate::Batch"->new({"key_value", $key_value, "resultset", $self->resultset, "key", $batch_key})
$key_batch->{$batch_key}
"DBIx::Class::BatchUpdate::Batch"->new({"key_value", $key_value, "resultset", $self->resultset, "key", $batch_key})
Truth table66%2/3
A //= B
A= $key_batch->{$batch_key}B= "DBIx::Class::BatchUpdate::Batch"->new({"key_value", $key_value, "resultset", $self->resultset, "key", $batch_key})
| |||||||||||||||||||||
| 32 | key_value => $key_value, | ||||||||||||||||||||
| 33 | resultset => $self->resultset, | ||||||||||||||||||||
| 34 | key => $batch_key, | ||||||||||||||||||||
| 35 | }); | ||||||||||||||||||||
| 36 | 3 | $batch->ids->push( $row->id ); | |||||||||||||||||||
| 37 | }; | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # Sort to get some semblance of determinism wrt insert ordering | ||||||||||||||||||||
| 40 | 1 | return [ sort { $a->key cmp $b->key } $key_batch->values ]; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | sub batch_key { | ||||||||||||||||||||
| 44 | 4 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
batch_key: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 45 | 4 | my ($key_value) = @_; | |||||||||||||||||||
| 46 | 4 | ▶ | keys %$key_value or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # sort hash keys for a stable representation | ||||||||||||||||||||
| 49 | 4 | local $Storable::canonical = 1; | |||||||||||||||||||
| 50 | return nfreeze( | ||||||||||||||||||||
| 51 | { | ||||||||||||||||||||
| 52 | # Assume the pk isn't dirty | ||||||||||||||||||||
| 53 | map { | ||||||||||||||||||||
| 54 | 4 | my $value = $key_value->{$_}; | |||||||||||||||||||
| 55 | 7 | ▶ | $_ => defined($value) ? "$value" : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | keys %$key_value, | ||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub update { | ||||||||||||||||||||
| 63 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 64 | 1 | for my $batch ( $self->batches->elements ) { | |||||||||||||||||||
| 65 | 2 | $batch->update(); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||