| 1 | package DBIx::Class::BatchUpdate::Update; | ||||||||||||||||||||
| 2 | 1 | $DBIx::Class::BatchUpdate::Update::VERSION = '1.004'; | |||||||||||||||||||
| 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 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use DBIx::Class::BatchUpdate::Batch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | has rows => ( is => "ro", required => 1 ); | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | has result_source => ( is => "lazy" ); | |||||||||||||||||||
| 17 | 4 | ▶ | sub _build_result_source { | ||||||||||||||||||
|
Subroutine
_build_result_source: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 18 | 4 | my $self = shift; | |||||||||||||||||||
| 19 | 4 | ▶ | my $row = $self->rows->[0] or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 3 | my $result_source = $row->result_source; | |||||||||||||||||||
| 21 | 3 | return $result_source; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | has resultset => (is => "lazy"); | |||||||||||||||||||
| 25 | 1 | ▶ | sub _build_resultset { | ||||||||||||||||||
|
Subroutine
_build_resultset: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 26 | 1 | my $self = shift; | |||||||||||||||||||
| 27 | 1 | ▶ | my $result_source = $self->result_source or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 1 | return $result_source->resultset(); | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 1 | has pk_column => ( is => "lazy" ); | |||||||||||||||||||
| 32 | 4 | ▶ | sub _build_pk_column { | ||||||||||||||||||
|
Subroutine
_build_pk_column: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 33 | 4 | my $self = shift; | |||||||||||||||||||
| 34 | 4 | ▶ | my $result_source = $self->result_source or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 3 | my $result_source_name = ref($result_source); | |||||||||||||||||||
| 37 | 3 | my @columns = $result_source->primary_columns; | |||||||||||||||||||
| 38 | 3 | ▶ | @columns > 1 and croak("DBIx::Class::BatchUpdate::Update does not work on result sources ($result_source_name) with multi-column PKs\n"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 2 | return $columns[0]; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | has batches => ( is => "lazy"); | |||||||||||||||||||
| 44 | 4 | ▶ | sub _build_batches { | ||||||||||||||||||
|
Subroutine
_build_batches: called CC=6 SCAR=18.0
| |||||||||||||||||||||
| 45 | 4 | my $self = shift; | |||||||||||||||||||
| 46 | 4 | ▶ | my $pk_column = $self->pk_column or return []; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 2 | my $result_source = $self->result_source; | |||||||||||||||||||
| 48 | 2 | my $result_source_name = ref($result_source); | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | my $key_batch = {}; | |||||||||||||||||||
| 51 | 2 | for my $row ($self->rows->elements) { | |||||||||||||||||||
| 52 | 4 | my $key_value = { $row->get_dirty_columns }; | |||||||||||||||||||
| 53 | 4 | ▶ | my $batch_key = $self->batch_key($key_value) or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 4 | ▶ | exists $key_value->{$pk_column} and croak("Primary key ($key_value->{$pk_column}) for ResultSource ($result_source_name) is dirty, can't BatchUpdate"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 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, "pk_column", $pk_column})
MC/DC0%0/2
$key_batch->{$batch_key} //= "DBIx::Class::BatchUpdate::Batch"->new({"key_value", $key_value, "resultset", $self->resultset, "key", $batch_key, "pk_column", $pk_column})
$key_batch->{$batch_key}
"DBIx::Class::BatchUpdate::Batch"->new({"key_value", $key_value, "resultset", $self->resultset, "key", $batch_key, "pk_column", $pk_column})
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, "pk_column", $pk_column})
| |||||||||||||||||||||
| 58 | key_value => $key_value, | ||||||||||||||||||||
| 59 | resultset => $self->resultset, | ||||||||||||||||||||
| 60 | key => $batch_key, | ||||||||||||||||||||
| 61 | pk_column => $pk_column, | ||||||||||||||||||||
| 62 | }); | ||||||||||||||||||||
| 63 | 3 | $batch->ids->push( $row->id ); | |||||||||||||||||||
| 64 | }; | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # Sort to get some semblance of determinism wrt insert ordering | ||||||||||||||||||||
| 67 | 1 | return [ sort { $a->key cmp $b->key } $key_batch->values ]; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 5 | ▶ | sub batch_key { | ||||||||||||||||||
|
Subroutine
batch_key: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 71 | 5 | my $self = shift; | |||||||||||||||||||
| 72 | 5 | my ($key_value) = @_; | |||||||||||||||||||
| 73 | 5 | ▶ | keys %$key_value or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # sort hash keys for a stable representation | ||||||||||||||||||||
| 76 | 5 | local $Storable::canonical = 1; | |||||||||||||||||||
| 77 | return nfreeze( | ||||||||||||||||||||
| 78 | { | ||||||||||||||||||||
| 79 | # Assume the pk isn't dirty | ||||||||||||||||||||
| 80 | map { | ||||||||||||||||||||
| 81 | 5 | my $value = $key_value->{$_}; | |||||||||||||||||||
| 82 | 9 | ▶ | $_ => defined($value) ? "$value" : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | keys %$key_value, | ||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | ▶ | sub update { | ||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 90 | 3 | my $self = shift; | |||||||||||||||||||
| 91 | 3 | for my $batch ( $self->batches->elements ) { | |||||||||||||||||||
| 92 | 2 | $batch->update(); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||