| 1 | package DBIx::Class::AuditAny::Role::Storage; | ||||||||||||||||||||||||||||
| 2 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Role to apply to tracked DBIx::Class::Storage objects | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ## TODO: | ||||||||||||||||||||||||||||
| 11 | ## 1. track rekey in update | ||||||||||||||||||||||||||||
| 12 | ## 2. track changes in FK with cascade | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | DBIx::Class::AuditAny::Role::Storage - Role to apply to tracked DBIx::Class::Storage objects | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | This role adds the hooks to the DBIC Storage object to be able to sniff and collect change data | ||||||||||||||||||||||||||||
| 22 | has it happens in real time. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 14 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 14 | ▶ | use DBIx::Class::AuditAny::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 14 | ▶ | use Term::ANSIColor qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 REQUIRES | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head2 txn_do | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 insert | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head2 update | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 delete | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 insert_bulk | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | 14 | requires 'txn_do'; | |||||||||||||||||||||||||||
| 47 | 14 | requires 'insert'; | |||||||||||||||||||||||||||
| 48 | 14 | requires 'update'; | |||||||||||||||||||||||||||
| 49 | 14 | requires 'delete'; | |||||||||||||||||||||||||||
| 50 | 14 | requires 'insert_bulk'; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | head2 auditors | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | List of Auditor objects which we are collecting data for. Typically there will be only one | ||||||||||||||||||||||||||||
| 57 | Auditor, but there can be many, allowing for data to be logged to a file by one, logged in | ||||||||||||||||||||||||||||
| 58 | a database by another, and then some other random watcher which takes some action when a | ||||||||||||||||||||||||||||
| 59 | certain event is detected. All of these can run simultaneously, and receive the sniffed data | ||||||||||||||||||||||||||||
| 60 | which we will collect only once. | ||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | 17 | ▶ | has 'auditors', is => 'ro', lazy => 1, default => sub {[]}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 METHODS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 all_auditors | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Returns a list of all configured Auditor objects | ||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | 21 | ▶ | sub all_auditors { @{(shift)->auditors} } | ||||||||||||||||||||||||||
|
Subroutine
all_auditors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 auditor_count | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | The number of configured auditors | ||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | 17 | ▶ | sub auditor_count { scalar (shift)->all_auditors } | ||||||||||||||||||||||||||
|
Subroutine
auditor_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 add_auditor | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Adds a new Auditor object(s) to report to | ||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | 17 | ▶ | sub add_auditor { push @{(shift)->auditors},(shift) } | ||||||||||||||||||||||||||
|
Subroutine
add_auditor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | before 'txn_begin' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | return if ($ENV{DBIX_CLASS_AUDITANY_SKIP}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $_->start_unless_changeset for ($self->all_auditors); | |||||||||||||||||||||||||||
| 90 | 14 | }; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # txn_commit | ||||||||||||||||||||||||||||
| 93 | # Note that we're hooking into -before- txn_commit rather than | ||||||||||||||||||||||||||||
| 94 | # -after- which would conceptually make better sense. The reason | ||||||||||||||||||||||||||||
| 95 | # is that we provide for the ability for collectors that store | ||||||||||||||||||||||||||||
| 96 | # their change data within the same schema being tracked, which | ||||||||||||||||||||||||||||
| 97 | # means the stored data will end up being a part of the same | ||||||||||||||||||||||||||||
| 98 | # transaction, thus hooking into after on the outermost commit | ||||||||||||||||||||||||||||
| 99 | # could cause deep recursion. | ||||||||||||||||||||||||||||
| 100 | # TODO/FIXME: What about collectors that | ||||||||||||||||||||||||||||
| 101 | # *don't* do this, and an exception occurring within that final | ||||||||||||||||||||||||||||
| 102 | # commit??? It could possibly lead to recording a change that | ||||||||||||||||||||||||||||
| 103 | # didn't actually happen (i.e. was rolled back). I think the way | ||||||||||||||||||||||||||||
| 104 | # to handle this is for the collector to declare if it is storing | ||||||||||||||||||||||||||||
| 105 | # to the tracked schema or not, and handle each case differently | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | before 'txn_commit' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Only finish in the outermost transaction | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if($self->transaction_depth == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $_->finish_if_changeset for ($self->all_auditors); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 14 | }; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | around 'txn_rollback' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my @ret; | |||||||||||||||||||||||||||
| 119 | 0 | my $want = wantarray; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 121 | ############################################################# | ||||||||||||||||||||||||||||
| 122 | # --- Call original - scalar/list/void context agnostic --- | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | @ret = !defined $want ? do { $self->$orig(@args); undef } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 124 | : $want ? $self->$orig(@args) | ||||||||||||||||||||||||||||
| 125 | : scalar $self->$orig(@args); | ||||||||||||||||||||||||||||
| 126 | # --- | ||||||||||||||||||||||||||||
| 127 | ############################################################# | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 130 | 0 | my $err = shift; | |||||||||||||||||||||||||||
| 131 | 0 | $_->_exception_cleanup($err) for ($self->all_auditors); | |||||||||||||||||||||||||||
| 132 | 0 | die $err; | |||||||||||||||||||||||||||
| 133 | 0 | }; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Should never get here because txn_rollback throws an exception | ||||||||||||||||||||||||||||
| 136 | # per-design. But, we still handle the case for good measure: | ||||||||||||||||||||||||||||
| 137 | 0 | $_->_exception_cleanup('txn_rollback') for ($self->all_auditors); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return $want ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 14 | }; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # insert is the most simple. Always applies to exactly 1 row: | ||||||||||||||||||||||||||||
| 144 | 4 | ▶ | around 'insert' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=20.5
| |||||||||||||||||||||||||||||
| 145 | 4 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 146 | 4 | ▶ | return $self->$orig(@args) if ($ENV{DBIX_CLASS_AUDITANY_SKIP}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 4 | my ($Source, $to_insert) = @args; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Start new insert operation within each Auditor and get back | ||||||||||||||||||||||||||||
| 151 | # all the created ChangeContexts from all auditors. The auditors | ||||||||||||||||||||||||||||
| 152 | # will keep track of their own changes temporarily in a "group": | ||||||||||||||||||||||||||||
| 153 | my @ChangeContexts = map { | ||||||||||||||||||||||||||||
| 154 | 4 | $_->_start_current_change_group($Source, 0,'insert',{ | |||||||||||||||||||||||||||
| 155 | to_columns => $to_insert | ||||||||||||||||||||||||||||
| 156 | }) | ||||||||||||||||||||||||||||
| 157 | } $self->all_auditors; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 4 | my @ret; | |||||||||||||||||||||||||||
| 160 | 4 | my $want = wantarray; | |||||||||||||||||||||||||||
| 161 | 4 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=15.4
| |||||||||||||||||||||||||||||
| 162 | ############################################################# | ||||||||||||||||||||||||||||
| 163 | # --- Call original - scalar/list/void context agnostic --- | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | @ret = !defined $want ? do { $self->$orig(@args); undef } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 165 | : $want ? $self->$orig(@args) | ||||||||||||||||||||||||||||
| 166 | : scalar $self->$orig(@args); | ||||||||||||||||||||||||||||
| 167 | # --- | ||||||||||||||||||||||||||||
| 168 | ############################################################# | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 171 | 0 | my $err = shift; | |||||||||||||||||||||||||||
| 172 | 0 | $_->_exception_cleanup($err) for ($self->all_auditors); | |||||||||||||||||||||||||||
| 173 | 0 | die $err; | |||||||||||||||||||||||||||
| 174 | 4 | }; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # Update each ChangeContext with the result data: | ||||||||||||||||||||||||||||
| 177 | 4 | $_->record($ret[0]) for (@ChangeContexts); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Tell each auditor that we're done and to record the change group | ||||||||||||||||||||||||||||
| 180 | # into the active changeset: | ||||||||||||||||||||||||||||
| 181 | 0 | $_->_finish_current_change_group for ($self->all_auditors); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | return $want ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 14 | }; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | ### TODO: ### | ||||||||||||||||||||||||||||
| 188 | # insert_bulk is a tricky case. It exists for the purpose of performance, | ||||||||||||||||||||||||||||
| 189 | # and skips reading back in the inserted row(s). BUT, we need to read back | ||||||||||||||||||||||||||||
| 190 | # in the inserted row, and we have no safe way of doing that with a bulk | ||||||||||||||||||||||||||||
| 191 | # insert (auto-generated auto-inc keys, etc). DBIC was already designed with | ||||||||||||||||||||||||||||
| 192 | # with this understanding, and so insert_bulk is already only called when | ||||||||||||||||||||||||||||
| 193 | # no result is needed/expected back: DBIx::Class::ResultSet->populate() called | ||||||||||||||||||||||||||||
| 194 | # in *void* context. | ||||||||||||||||||||||||||||
| 195 | # | ||||||||||||||||||||||||||||
| 196 | # Based on this fact, I think that the only rational way to be able to | ||||||||||||||||||||||||||||
| 197 | # Audit the inserted rows is to override and convert any calls to insert_bulk() | ||||||||||||||||||||||||||||
| 198 | # into calls to regular calls to insert(). Interfering with the original | ||||||||||||||||||||||||||||
| 199 | # flow/operation is certainly not ideal, but I don't see any alternative. | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | around 'insert_bulk' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 201 | 0 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | return $self->$orig(@args) if ($ENV{DBIX_CLASS_AUDITANY_SKIP}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | my ($Source, $cols, $data) = @args; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # | ||||||||||||||||||||||||||||
| 207 | # TODO .... | ||||||||||||||||||||||||||||
| 208 | # | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | my @ret; | |||||||||||||||||||||||||||
| 211 | 0 | my $want = wantarray; | |||||||||||||||||||||||||||
| 212 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 213 | ############################################################# | ||||||||||||||||||||||||||||
| 214 | # --- Call original - scalar/list/void context agnostic --- | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | @ret = !defined $want ? do { $self->$orig(@args); undef } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 216 | : $want ? $self->$orig(@args) | ||||||||||||||||||||||||||||
| 217 | : scalar $self->$orig(@args); | ||||||||||||||||||||||||||||
| 218 | # --- | ||||||||||||||||||||||||||||
| 219 | ############################################################# | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 222 | 0 | my $err = shift; | |||||||||||||||||||||||||||
| 223 | 0 | $_->_exception_cleanup($err) for ($self->all_auditors); | |||||||||||||||||||||||||||
| 224 | 0 | die $err; | |||||||||||||||||||||||||||
| 225 | 0 | }; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | return $want ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 14 | }; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | ▶ | has '_change_contexts', is => 'rw', isa => ArrayRef[Object], lazy => 1, default => sub {[]}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub _add_change_contexts { push @{shift->_change_contexts},@_ } | ||||||||||||||||||||||||||
|
Subroutine
_add_change_contexts: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | ▶ | sub _follow_row_changes($$) { | ||||||||||||||||||||||||||
|
Subroutine
_follow_row_changes: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 235 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 236 | 0 | my $cnf = shift; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | my $Source = $cnf->{Source}; | |||||||||||||||||||||||||||
| 239 | 0 | my $change = $cnf->{change}; | |||||||||||||||||||||||||||
| 240 | 0 | my $cond = $cnf->{cond}; | |||||||||||||||||||||||||||
| 241 | 0 | my $action = $cnf->{action}; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | my $orig = $cnf->{method}; | |||||||||||||||||||||||||||
| 244 | 0 | ▶ | my $args = $cnf->{args} || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$cnf->{'args'} || []
MC/DC0%0/1
$cnf->{'args'} || []
$cnf->{'args'}
Truth table0%0/2
A
A= $cnf->{'args'}
| |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my $want = $cnf->{want} || wantarray; | ||||||||||||||||||||||||||
|
Condition0%0/3
$cnf->{'want'} || wantarray
MC/DC0%0/2
$cnf->{'want'} || wantarray
$cnf->{'want'}
wantarray
Truth table0%0/3
A || B
A= $cnf->{'want'}B= wantarray
| |||||||||||||||||||||||||||||
| 246 | 0 | my $rows = $cnf->{rows}; | |||||||||||||||||||||||||||
| 247 | 0 | ▶ | my $nested = $cnf->{nested} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$cnf->{'nested'} || 0
MC/DC0%0/1
$cnf->{'nested'} || 0
$cnf->{'nested'}
Truth table0%0/2
A
A= $cnf->{'nested'}
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | my $source_name = $Source->source_name; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | $self->_change_contexts([]) unless ($nested); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # Get the current rows if they haven't been supplied and a | ||||||||||||||||||||||||||||
| 254 | # condition has been supplied ($cond): | ||||||||||||||||||||||||||||
| 255 | 0 | ▶ | $rows = get_raw_source_rows($Source,$cond) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $rows and defined $cond
MC/DC0%0/2
not defined $rows and defined $cond
not defined $rows
defined $cond
Truth table0%0/3
not(A) and B
A= not defined $rowsB= defined $cond
| |||||||||||||||||||||||||||||
| 256 | if (!defined $rows && defined $cond); | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | my @change_datam = map {{ | |||||||||||||||||||||||||||
| 259 | 0 | old_columns => $_, | |||||||||||||||||||||||||||
| 260 | to_columns => $change, | ||||||||||||||||||||||||||||
| 261 | condition => $cond | ||||||||||||||||||||||||||||
| 262 | }} @$rows; | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # Start new change operation within each Auditor and store the | ||||||||||||||||||||||||||||
| 266 | # created ChangeContexts (from all auditors) in the _change_contexts. | ||||||||||||||||||||||||||||
| 267 | # attribute to be updated and recorded at the end of the update. The | ||||||||||||||||||||||||||||
| 268 | # auditors will keep track of their own changes temporarily in a "group": | ||||||||||||||||||||||||||||
| 269 | $self->_add_change_contexts( | ||||||||||||||||||||||||||||
| 270 | map { | ||||||||||||||||||||||||||||
| 271 | 0 | $_->_start_current_change_group($Source, $nested, $action, @change_datam) | |||||||||||||||||||||||||||
| 272 | } $self->all_auditors | ||||||||||||||||||||||||||||
| 273 | ); | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # ----- | ||||||||||||||||||||||||||||
| 276 | # Recursively follow effective changes in other tables that will | ||||||||||||||||||||||||||||
| 277 | # be caused by any db-side cascades defined in relationships: | ||||||||||||||||||||||||||||
| 278 | 0 | $self->_follow_relationship_cascades($Source,$cond,$change); | |||||||||||||||||||||||||||
| 279 | # ----- | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | # Run the original/supplied method: | ||||||||||||||||||||||||||||
| 282 | 0 | my @ret; | |||||||||||||||||||||||||||
| 283 | 0 | ▶ | if($orig) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 285 | ############################################################# | ||||||||||||||||||||||||||||
| 286 | # --- Call original - scalar/list/void context agnostic --- | ||||||||||||||||||||||||||||
| 287 | 0 | ▶ | @ret = !defined $want ? do { $self->$orig(@$args); undef } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 288 | : $want ? $self->$orig(@$args) | ||||||||||||||||||||||||||||
| 289 | : scalar $self->$orig(@$args); | ||||||||||||||||||||||||||||
| 290 | # --- | ||||||||||||||||||||||||||||
| 291 | ############################################################# | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 294 | 0 | my $err = shift; | |||||||||||||||||||||||||||
| 295 | 0 | $_->_exception_cleanup($err) for ($self->all_auditors); | |||||||||||||||||||||||||||
| 296 | 0 | die $err; | |||||||||||||||||||||||||||
| 297 | 0 | }; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Tell each auditor that we're done and to record the change group | ||||||||||||||||||||||||||||
| 301 | # into the active changeset (unless the action we're following is nested): | ||||||||||||||||||||||||||||
| 302 | 0 | ▶ | unless ($nested) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 0 | $self->_record_change_contexts; | |||||||||||||||||||||||||||
| 304 | 0 | $_->_finish_current_change_group for ($self->all_auditors); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | ▶ | return $want ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub _follow_relationship_cascades { | ||||||||||||||||||||||||||
|
Subroutine
_follow_relationship_cascades: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 312 | 0 | my ($self, $Source, $cond, $change) = @_; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | ## IN PROGRESS..... | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # If any of these columns are being changed, we have to also watch the | ||||||||||||||||||||||||||||
| 317 | # corresponding relationhips for changes (from cascades) during the | ||||||||||||||||||||||||||||
| 318 | # course of the current database operation. This can be expensive, but | ||||||||||||||||||||||||||||
| 319 | # we prefer accuracy over speed | ||||||||||||||||||||||||||||
| 320 | 0 | my $cascade_cols = $self->_get_cascading_rekey_columns($Source); | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # temp: just get all of themfor now | ||||||||||||||||||||||||||||
| 323 | # this should be limited to only rels associated with columns | ||||||||||||||||||||||||||||
| 324 | # being changed | ||||||||||||||||||||||||||||
| 325 | 0 | my @rels = uniq(map { @{$cascade_cols->{$_}} } keys %$cascade_cols); | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 0 | foreach my $rel (@rels) { | |||||||||||||||||||||||||||
| 328 | 0 | my $rinfo = $Source->relationship_info($rel); | |||||||||||||||||||||||||||
| 329 | #my $rrinfo = $Source->reverse_relationship_info($rel); | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # Generate a virtual 'change' to describe what will happen in the related table | ||||||||||||||||||||||||||||
| 332 | 0 | my $map = &_cond_foreign_keymap($rinfo->{cond}); | |||||||||||||||||||||||||||
| 333 | 0 | my $rel_change = {}; | |||||||||||||||||||||||||||
| 334 | 0 | foreach my $col (keys %$change) { | |||||||||||||||||||||||||||
| 335 | 0 | ▶ | my $fcol = $map->{$col} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 0 | $rel_change->{$fcol} = $change->{$col}; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | # Only track related rows if there is at least one related change: | ||||||||||||||||||||||||||||
| 340 | 0 | ▶ | if(scalar(keys %$rel_change) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | # Get related rows that will be changed from the cascade: | ||||||||||||||||||||||||||||
| 342 | 0 | my $rel_rows = get_raw_source_related_rows($Source,$rel,$cond); | |||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # Follow these rows via special nested call: | ||||||||||||||||||||||||||||
| 345 | 0 | ▶ | $self->_follow_row_changes({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | Source => $Source->related_source($rel), | ||||||||||||||||||||||||||||
| 347 | rows => $rel_rows, | ||||||||||||||||||||||||||||
| 348 | cond => {}, | ||||||||||||||||||||||||||||
| 349 | nested => 1, | ||||||||||||||||||||||||||||
| 350 | action => 'update', | ||||||||||||||||||||||||||||
| 351 | change => $rel_change | ||||||||||||||||||||||||||||
| 352 | }) if(scalar @$rel_rows > 0); | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Builds a map that can be used to convert column names into | ||||||||||||||||||||||||||||
| 359 | # their fk name on the other side of a relationship | ||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub _cond_foreign_keymap { | ||||||||||||||||||||||||||
|
Subroutine
_cond_foreign_keymap: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 361 | 0 | my $cond = shift; | |||||||||||||||||||||||||||
| 362 | 0 | my $alias = shift; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | my $map = {}; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | # TODO: doesn't support all valid conditions, but *DOES* | ||||||||||||||||||||||||||||
| 367 | # support those that can express a valid db-side CASCADE, | ||||||||||||||||||||||||||||
| 368 | # which is what this is for: | ||||||||||||||||||||||||||||
| 369 | 0 | foreach my $k (keys %$cond) { | |||||||||||||||||||||||||||
| 370 | 0 | my @f = ($k,$cond->{$k}); | |||||||||||||||||||||||||||
| 371 | 0 | my $d = {}; | |||||||||||||||||||||||||||
| 372 | 0 | $d->{$_->[0]} = $_->[1] for (map {[split(/\./,$_,2)]} @f); | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | die "error parsing condition" | ||||||||||||||||||||||||||||
| 375 | 0 | ▶ | unless (exists $d->{foreign} && exists $d->{self}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $d->{"foreign"} and exists $d->{"self"}
MC/DC0%0/2
exists $d->{"foreign"} and exists $d->{"self"}
exists $d->{"foreign"}
exists $d->{"self"}
Truth table0%0/3
A and B
A= exists $d->{"foreign"}B= exists $d->{"self"}
| |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 0 | $map->{$d->{self}} = $d->{foreign}; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | 0 | return $map; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | sub _record_change_contexts { | ||||||||||||||||||||||||||
|
Subroutine
_record_change_contexts: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 385 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # Fetch the new values for -each- row, independently. | ||||||||||||||||||||||||||||
| 388 | # Build a condition specific to this row and fetch it, | ||||||||||||||||||||||||||||
| 389 | # taking into account the change that was just made, and | ||||||||||||||||||||||||||||
| 390 | # then record the new columns in the ChangeContext: | ||||||||||||||||||||||||||||
| 391 | 0 | foreach my $ChangeContext (@{$self->_change_contexts}) { | |||||||||||||||||||||||||||
| 392 | 0 | my $Source = $ChangeContext->SourceContext->ResultSource; | |||||||||||||||||||||||||||
| 393 | # Get the primry keys, or all columns if there are none: | ||||||||||||||||||||||||||||
| 394 | 0 | my @pri_cols = $Source->primary_columns; | |||||||||||||||||||||||||||
| 395 | 0 | ▶ | @pri_cols = $Source->columns unless (scalar @pri_cols > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 0 | my $change = $ChangeContext->to_columns; | |||||||||||||||||||||||||||
| 398 | 0 | my $old = $ChangeContext->old_columns; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | # TODO: cache the new columns to prevent duplicate fetches for multiple auditors | ||||||||||||||||||||||||||||
| 401 | my $new_rows = get_raw_source_rows($Source,{ map { | ||||||||||||||||||||||||||||
| 402 | 0 | ▶ | $_ => (exists $change->{$_} ? $change->{$_} : $old->{$_}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | } @pri_cols }); | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | # TODO/FIXME: How should we handle it if we got back | ||||||||||||||||||||||||||||
| 406 | # something other than exactly one row here? | ||||||||||||||||||||||||||||
| 407 | 0 | ▶ | die "Unexpected error while trying to read updated row" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | unless (scalar @$new_rows == 1); | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 0 | my $new = pop @$new_rows; | |||||||||||||||||||||||||||
| 411 | 0 | $ChangeContext->record($new); | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | # Clear: | ||||||||||||||||||||||||||||
| 415 | 0 | $self->_change_contexts([]); | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | 0 | ▶ | around 'update' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 420 | 0 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 421 | 0 | ▶ | return $self->$orig(@args) if ($ENV{DBIX_CLASS_AUDITANY_SKIP}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | my ($Source,$change,$cond) = @args; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 0 | return $self->_follow_row_changes({ | |||||||||||||||||||||||||||
| 426 | Source => $Source, | ||||||||||||||||||||||||||||
| 427 | change => $change, | ||||||||||||||||||||||||||||
| 428 | cond => $cond, | ||||||||||||||||||||||||||||
| 429 | method => $orig, | ||||||||||||||||||||||||||||
| 430 | action => 'update', | ||||||||||||||||||||||||||||
| 431 | args => \@args, | ||||||||||||||||||||||||||||
| 432 | want => wantarray | ||||||||||||||||||||||||||||
| 433 | }); | ||||||||||||||||||||||||||||
| 434 | 14 | }; | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 0 | ▶ | around 'delete' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 437 | 0 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 438 | 0 | ▶ | return $self->$orig(@args) if ($ENV{DBIX_CLASS_AUDITANY_SKIP}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | my ($Source, $cond) = @args; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | # Get the current rows that are going to be deleted: | ||||||||||||||||||||||||||||
| 443 | 0 | my $rows = get_raw_source_rows($Source,$cond); | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 0 | my @change_datam = map {{ | |||||||||||||||||||||||||||
| 446 | 0 | old_columns => $_, | |||||||||||||||||||||||||||
| 447 | condition => $cond | ||||||||||||||||||||||||||||
| 448 | }} @$rows; | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | ########################### | ||||||||||||||||||||||||||||
| 451 | # TODO: find cascade deletes here | ||||||||||||||||||||||||||||
| 452 | ########################### | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # Start new change operation within each Auditor and get back | ||||||||||||||||||||||||||||
| 456 | # all the created ChangeContexts from all auditors. Each auditor | ||||||||||||||||||||||||||||
| 457 | # will keep track of its own changes temporarily in a "group": | ||||||||||||||||||||||||||||
| 458 | my @ChangeContexts = map { | ||||||||||||||||||||||||||||
| 459 | 0 | $_->_start_current_change_group($Source, 0,'delete', @change_datam) | |||||||||||||||||||||||||||
| 460 | } $self->all_auditors; | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | # Do the actual deletes: | ||||||||||||||||||||||||||||
| 464 | 0 | my @ret; | |||||||||||||||||||||||||||
| 465 | 0 | my $want = wantarray; | |||||||||||||||||||||||||||
| 466 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 467 | ############################################################# | ||||||||||||||||||||||||||||
| 468 | # --- Call original - scalar/list/void context agnostic --- | ||||||||||||||||||||||||||||
| 469 | 0 | ▶ | @ret = !defined $want ? do { $self->$orig(@args); undef } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 470 | : $want ? $self->$orig(@args) | ||||||||||||||||||||||||||||
| 471 | : scalar $self->$orig(@args); | ||||||||||||||||||||||||||||
| 472 | # --- | ||||||||||||||||||||||||||||
| 473 | ############################################################# | ||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 476 | 0 | my $err = shift; | |||||||||||||||||||||||||||
| 477 | 0 | $_->_exception_cleanup($err) for ($self->all_auditors); | |||||||||||||||||||||||||||
| 478 | 0 | die $err; | |||||||||||||||||||||||||||
| 479 | 0 | }; | |||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | # TODO: should we go back to the db to make sure the rows are | ||||||||||||||||||||||||||||
| 483 | # now gone as expected? | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | $_->record for (@ChangeContexts); | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | # Tell each auditor that we're done and to record the change group | ||||||||||||||||||||||||||||
| 488 | # into the active changeset: | ||||||||||||||||||||||||||||
| 489 | 0 | $_->_finish_current_change_group for ($self->all_auditors); | |||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 0 | ▶ | return $want ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | 14 | }; | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | # _get_cascading_rekey_cols: gets a map of column names to relationships. These | ||||||||||||||||||||||||||||
| 497 | # are the relationships that *could* be changed via a cascade when the column (fk) | ||||||||||||||||||||||||||||
| 498 | # is changed. | ||||||||||||||||||||||||||||
| 499 | # TODO: use 'cascade_rekey' attr from DBIx::Class::Shadow | ||||||||||||||||||||||||||||
| 500 | # (DBIx::Class::Relationship::Cascade::Rekey) ? | ||||||||||||||||||||||||||||
| 501 | 0 | ▶ | sub _get_cascading_rekey_columns { | ||||||||||||||||||||||||||
|
Subroutine
_get_cascading_rekey_columns: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 502 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 503 | 0 | my $Source = shift; | |||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | # cache for next time (should I even bother? since if rels are added to the ResultSource | ||||||||||||||||||||||||||||
| 506 | # later this won't get updated? Is that a bigger risk than the performance boost?) | ||||||||||||||||||||||||||||
| 507 | 0 | ▶ | $self->_source_cascade_rekey_cols->{$Source->source_name} ||= do { | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->_source_cascade_rekey_cols->{$Source->source_name} ||= do {
my $rels = {map({$_, $Source->relationship_info($_);} $Source->relationships)};
my $cascade_cols = {};
foreach my $rel (keys %$rels) {
next unless $rels->{$rel}{"attrs"}{"accessor"} eq "multi";
next if (ref $rels->{$rel}{"cond"} || "") eq "CODE";
my(@cols) = $self->_parse_cond_cols_by_alias($rels->{$rel}{"cond"}, "self");
push @{$$cascade_cols{$_};}, $rel foreach (@cols);
};
return $cascade_cols
}
MC/DC0%0/2
$self->_source_cascade_rekey_cols->{$Source->source_name} ||= do {
my $rels = {map({$_, $Source->relationship_info($_);} $Source->relationships)};
my $cascade_cols = {};
foreach my $rel (keys %$rels) {
next unless $rels->{$rel}{"attrs"}{"accessor"} eq "multi";
next if (ref $rels->{$rel}{"cond"} || "") eq "CODE";
my(@cols) = $self->_parse_cond_cols_by_alias($rels->{$rel}{"cond"}, "self");
push @{$$cascade_cols{$_};}, $rel foreach (@cols);
};
return $cascade_cols
}
$self->_source_cascade_rekey_cols->{$Source->source_name}
do {
my $rels = {map({$_, $Source->relationship_info($_);} $Source->relationships)};
my $cascade_cols = {};
foreach my $rel (keys %$rels) {
next unless $rels->{$rel}{"attrs"}{"accessor"} eq "multi";
next if (ref $rels->{$rel}{"cond"} || "") eq "CODE";
my(@cols) = $self->_parse_cond_cols_by_alias($rels->{$rel}{"cond"}, "self");
push @{$$cascade_cols{$_};}, $rel foreach (@cols);
};
return $cascade_cols
}
Truth table0%0/3
A ||= B
A= $self->_source_cascade_rekey_cols->{$Source->source_name}B= do {
my $rels = {map({$_, $Source->relationship_info($_);} $Source->relationships)};
my $cascade_cols = {};
foreach my $rel (keys %$rels) {
next unless $rels->{$rel}{"attrs"}{"accessor"} eq "multi";
next if (ref $rels->{$rel}{"cond"} || "") eq "CODE";
my(@cols) = $self->_parse_cond_cols_by_alias($rels->{$rel}{"cond"}, "self");
push @{$$cascade_cols{$_};}, $rel foreach (@cols);
};
return $cascade_cols
}
| |||||||||||||||||||||||||||||
| 508 | 0 | my $rels = { map { $_ => $Source->relationship_info($_) } $Source->relationships }; | |||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 0 | my $cascade_cols = {}; | |||||||||||||||||||||||||||
| 511 | 0 | foreach my $rel (keys %$rels) { | |||||||||||||||||||||||||||
| 512 | # Only multi rels apply: | ||||||||||||||||||||||||||||
| 513 | 0 | ▶ | next unless ($rels->{$rel}{attrs}{accessor} eq 'multi'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | # NEW: We can't currently do anything with CodeRef conditions | ||||||||||||||||||||||||||||
| 516 | 0 | ▶ | next if ((ref($rels->{$rel}{cond})||'') eq 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $rels->{$rel}{'cond'} || ''
MC/DC0%0/1
ref $rels->{$rel}{'cond'} || ''
ref $rels->{$rel}{'cond'}
Truth table0%0/2
A
A= ref $rels->{$rel}{'cond'}
| |||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | # Get all the local columns that effect (i.e. might cascade to) this relationship: | ||||||||||||||||||||||||||||
| 519 | 0 | my @cols = $self->_parse_cond_cols_by_alias($rels->{$rel}{cond},'self'); | |||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | # Add the relationship to list for each column. | ||||||||||||||||||||||||||||
| 522 | #$cascade_cols->{$_} ||= [] for (@cols); #<-- don't need this | ||||||||||||||||||||||||||||
| 523 | 0 | push @{$cascade_cols->{$_}}, $rel for (@cols); | |||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | 0 | return $cascade_cols; | |||||||||||||||||||||||||||
| 527 | }; | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 0 | return $self->_source_cascade_rekey_rels->{$Source->source_name}; | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | 0 | ▶ | has '_source_cascade_rekey_cols', is => 'ro', isa => HashRef, lazy => 1, default => sub {{}}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 0 | ▶ | sub _parse_cond_cols_by_alias { | ||||||||||||||||||||||||||
|
Subroutine
_parse_cond_cols_by_alias: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 535 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 536 | 0 | my $cond = shift; | |||||||||||||||||||||||||||
| 537 | 0 | my $alias = shift; | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | # Get the string elements (keys and values) | ||||||||||||||||||||||||||||
| 540 | # (TODO: deep walk any hahs/array structure) | ||||||||||||||||||||||||||||
| 541 | 0 | my @elements = %$cond; | |||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | 0 | ▶ | ref($_) and die "Complex conditions aren't supported yet" for (@elements); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | 0 | my @cols = map { $_->[1] } # <-- 3. just the column names | |||||||||||||||||||||||||||
| 546 | # 2. exclude all but the alias name we want | ||||||||||||||||||||||||||||
| 547 | 0 | grep { $_->[0] eq $alias } | |||||||||||||||||||||||||||
| 548 | # 1. Convert all the element strings into alias/column pairs | ||||||||||||||||||||||||||||
| 549 | 0 | map { [split(/\./,$_,2)] } @elements; | |||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 0 | return @cols; | |||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | =head2 changeset_do | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | TODO... currently is just a wrapper around a native txn_do call. Not sure what this is meant | ||||||||||||||||||||||||||||
| 558 | to do... | ||||||||||||||||||||||||||||
| 559 | =cut | ||||||||||||||||||||||||||||
| 560 | 0 | ▶ | sub changeset_do { | ||||||||||||||||||||||||||
|
Subroutine
changeset_do: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 561 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | # TODO ... | ||||||||||||||||||||||||||||
| 564 | 0 | return $self->txn_do(@_); | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 14 | 1; | |||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | __END__ | ||||||||||||||||||||||||||||