blib/lib/DBIx/QuickORM/LookupTable/Identity.pm

filter: Statementstmt 98.1% 326 / 332 Branchbran 97.0% 163 / 168 Conditioncond 92.7% 115 / 124 MC/DCmcdc 83.5% 56 / 67 Subroutinesub 97.5% 39 / 40 Podpod 90.4% 19 / 21 totaltotal 95.4% 718 / 752 CC 292 SCAR 23.6
CC
292
Cov
97%
ingest_many
25.1
ingest_many
25.1
_key
14.0
CRAP 10.5
« blib/lib/DBIx/QuickORM/Iterator.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/LookupTable.pm »
1 package DBIx::QuickORM::LookupTable::Identity;
2 261 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 261 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 261 our $VERSION = '0.000033';
6  
7 261 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 261 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 261 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 261 ▶use DBIx::QuickORM::RowState();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 261 ▶use parent 'DBIx::QuickORM::LookupTable';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 261 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 <stack
16 +marks
17 +lookup_overridden
18 261 };
19  
20 =pod
21  
22 =encoding UTF-8
23  
24 =head1 NAME
25  
26 DBIx::QuickORM::LookupTable::Identity - The canon row hash for each table and primary key, cached.
27  
28 =head1 DESCRIPTION
29  
30 The default lookup table (C<< lookup => 'Identity' >>). It implements
31 L<DBIx::QuickORM::Role::LookupTable> and inherits L<DBIx::QuickORM::LookupTable>.
32  
33 The lookup table maps each table and primary key to the I<canon> row hash:
34 the ORM's canonical copy of that row. A hash is canon when it is the same
35 reference as the entry the levels give for its key. Rows arrive through
36 C<ingest()> (reads) and C<store()> (writes), and leave through C<forget()>.
37  
38 The table is a stack of levels: the outside level, plus one per open
39 transaction or savepoint. Writes go to the top level. Reads inside a
40 transaction look from the top level down to the root transaction level, never
41 the outside level, and stop at the first level that knows the key.
42 L<DBIx::QuickORM/Transactions> describes what users see.
43  
44 =over 4
45  
46 =item *
47  
48 A root transaction pushes an empty level, so nothing from before it is canon
49 inside it. The outside level stays as a cache.
50  
51 =item *
52  
53 A savepoint pushes an empty level, so the parent's entries stay canon in it.
54  
55 =item *
56  
57 C<clear()> or C<forget()> inside a transaction also marks the key, the
58 table, or everything as cleared in its level, hiding the levels below.
59  
60 =item *
61  
62 A savepoint commit merges its entries and marks into its parent. A savepoint
63 rollback discards it, so the parent's entries are canon again.
64  
65 =item *
66  
67 A root commit clears what its marks name from the outside level, then merges
68 its entries into it, replacing those for the same keys. A root rollback
69 discards it.
70  
71 =item *
72  
73 Inside a transaction, a key that a C<store()>, a C<forget()>, or a replacing
74 C<ingest()> touches leaves the outside level at once. C<forget()> with no table
75 empties the outside level.
76  
77 =item *
78  
79 An C<ingest()> of a key the transaction lacks reuses the outside hash when it
80 agrees on every field both hold and holds no field the read lacks. New fields
81 are added to it in place, and it
82 stays outside too, so an object holding it from before the transaction is
83 canon again.
84  
85 =back
86  
87 Fields C<ingest()> adds in place to a hash a savepoint's parent holds stay
88 visible to the parent after a savepoint rollback.
89  
90 Outside a transaction, entries are held weakly unless C<retain> is set.
91 Inside one they are strong. A root commit weakens the entries it merges,
92 again unless C<retain> is set. A freed weak entry reads as a miss and is
93 deleted when met.
94  
95 A primary key argument (C<$pk> below) is a hashref holding the key fields, so
96 row data works. Callers convert other key forms before calling, as the ORM's
97 key arguments do.
98  
99 An untyped key field is keyed by its value as it is. A typed one is keyed by
100 its type's C<canonical_key_value>, so its database text, its inflated value,
101 and other forms of the same value all find one entry. The map's C<pk_types>
102 says which fields are typed.
103  
104 It croaks on a table with no primary key, a missing or undef key field, and a
105 reference in an untyped key field.
106  
107 =head2 Row state
108  
109 Whether a row hash is stored (deleted, vivified) is kept by
110 L<DBIx::QuickORM::RowState>. The lookup table marks the hashes only it holds
111 (the entries C<gone> drops, and an entry C<ingest> or an update replaces)
112 through the Connection's row state, passed as C<row_states>.
113  
114 =head2 Who made a hash stop being canon
115  
116 Only with C<debug> (the ORM's C<< debug => 1 >>, a L<DBIx::QuickORM::Debug>).
117 Without it nothing below is recorded, and C<canon_status> works out what it
118 can.
119  
120 The public APIs that may write or replace canon row data set the Debug
121 object's C<caller> slot (see L<DBIx::QuickORM::Debug>). Building row objects
122 from rows already read runs outside the reader's frame: a public call a row
123 class's C<init> makes is recorded as itself, unless an enclosing call (a
124 C<txn>, say) already set the slot.
125  
126 When a hash stops being canon, the lookup table notes why in the Debug
127 object, with that caller when one is set:
128  
129 =over 4
130  
131 =item *
132  
133 C<replaced>: another hash became the entry for its key (a read of changed
134 data or of fewer fields, a write, or a primary key change).
135  
136 =item *
137  
138 C<cache_cleared>: its key was cleared on its own (C<clear> or C<forget> with
139 a key).
140  
141 =back
142  
143 Clearing a whole table or everything walks no entries. It records the
144 caller once, per table or for everything, and C<canon_status> reports it as
145 the last clear. A transaction rollback records nothing.
146  
147 Nothing is noted when a hash becomes canon again (a savepoint rollback, say),
148 so a note can be older than a later clear of the whole table.
149  
150 =head1 SYNOPSIS
151  
152 # one of its own, with its own row state, apart from the ORM's
153 my $lookup = DBIx::QuickORM::LookupTable::Identity->new(
154 connection => $orm->connection,
155 row_states => DBIx::QuickORM::RowState->new,
156 );
157  
158 my $bob = $lookup->ingest(
159 users => {
160 id => 1,
161 name => 'Bob',
162 },
163 );
164 my $canon = $lookup->is_canon(users => $bob); # 1
165  
166 $lookup->begin_root; # a transaction
167 my $held = $lookup->lookup(users => {id => 1}); # undef: it never reads the outside level
168  
169 $lookup->push_level; # a savepoint
170 my $robert = $lookup->store(
171 users => {
172 id => 1,
173 name => 'Robert',
174 },
175 );
176 $lookup->commit_level; # savepoint commit
177 $lookup->commit_level; # root commit
178  
179 my $now = $lookup->lookup(users => {id => 1}); # $robert
180 my $old = $lookup->is_canon(users => $bob); # 0
181  
182 =head1 ATTRIBUTES
183  
184 Those of L<DBIx::QuickORM::LookupTable>, and:
185  
186 =over 4
187  
188 =item $levels = $lookup->stack
189  
190 The levels, each C<< {table => {key => $row_hash}} >>. The first is the
191 outside level, the last is the top. Each transaction level's clear marks are
192 kept beside it, privately.
193  
194 =back
195  
196 =cut
197  
198 186 ▶sub init {
Subroutine init: called CC=1 SCAR=1.2
Pod undocumented
199 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=1.2
Pod undocumented
200 0 $self->SUPER::init();
201 0 $self->{+STACK} = [{}];
202 0 $self->{+MARKS} = [undef];
203  
204 # Decided once, so the fast find pays a hash read instead of a can() call.
205 0 $self->{+LOOKUP_OVERRIDDEN} = $self->can('lookup') != \&lookup;
206 }
207  
208 =head1 PUBLIC METHODS
209  
210 The examples use a lookup table of their own, built as the SYNOPSIS shows,
211 and drive its levels by hand. The ORM's lookup table follows its
212 Connection's transactions instead.
213  
214 =over 4
215  
216 =item $bool = $lookup->caches
217  
218 True: C<lookup> finds rows by primary key.
219  
220 =item $row_data = $lookup->lookup_value($table, $field, $value)
221  
222 C<lookup> by a plain value for a table keyed by one untyped field, which is
223 its key string as it is. A subclass that overrides C<lookup> gets it called
224 here too.
225  
226 =back
227  
228 =cut
229  
230 11 ▶sub caches { 1 }
Subroutine caches: called CC=1 SCAR=0.0
Pod documented
231  
232 # Outside a transaction this reads the outside level directly, as _find would, saving a call on the fast find.
233 128 ▶sub lookup_value {
Subroutine lookup_value: called CC=3 SCAR=11.0
Pod documented
234 128 my ($self, $table, $field, $value) = @_;
235 128 ▶ return $self->lookup($table, {$field => $value}) if $self->{+LOOKUP_OVERRIDDEN};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lookup_overridden"} 1 127 128
236 127 ▶ return $self->_find($table, "$value") if @{$self->{+STACK}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"stack"};} > 1 15 112 127
237 112 return $self->_get(0, $table, "$value");
238 }
239  
240 =over 4
241  
242 =item $lookup->begin_root
243  
244 Starts a root transaction: pushes an empty level above the outside level.
245 Croaks when a transaction is already open.
246  
247 =back
248  
249 =cut
250  
251 170 ▶sub begin_root {
Subroutine begin_root: called CC=2 SCAR=6.9
Pod documented
252 170 my $self = shift;
253 170 ▶ croak "A transaction is already open" if $self->_in_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_in_txn 1 169 170
254 169 push @{$self->{+STACK}} => {};
255 169 push @{$self->{+MARKS}} => undef;
256 }
257  
258 =over 4
259  
260 =item $lookup->push_level
261  
262 Starts a savepoint: pushes an empty level, so its cost does not depend on the
263 rows held. Croaks when no transaction is open.
264  
265 =back
266  
267 =cut
268  
269 57 ▶sub push_level {
Subroutine push_level: called CC=2 SCAR=6.9
Pod documented
270 57 my $self = shift;
271 57 ▶ croak "No transaction is open" unless $self->_in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_txn 1 56 57
272 56 push @{$self->{+STACK}} => {};
273 56 push @{$self->{+MARKS}} => undef;
274 }
275  
276 =over 4
277  
278 =item $lookup->commit_level
279  
280 Commits the innermost savepoint, or the root transaction when no savepoint is
281 open. Croaks when no transaction is open.
282  
283 A savepoint commit merges its entries and marks into its parent, at a cost
284 that grows with the keys it touched. A root commit clears what its marks name
285 from the outside level, then merges its entries into it, weakened unless
286 C<retain>.
287  
288 =back
289  
290 =cut
291  
292 96 ▶sub commit_level {
Subroutine commit_level: called CC=8 SCAR=20.8
Pod documented
293 96 my $self = shift;
294 96 my $stack = $self->{+STACK};
295 96 ▶ croak "No transaction is open" unless $self->_in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_txn 1 95 96
296 95 my $child = pop @$stack;
297 95 my $mark = pop @{$self->{+MARKS}};
298 95 ▶ return $self->_merge($child, $mark) if @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack > 1 20 75 95
299  
300 75 my $outside = $stack->[0];
301 75 ▶ $self->_apply_marks($outside, $mark) if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 15 60 75
302 75 for my $table (keys %$child) {
303 41 ▶ my $rows = $outside->{$table} //= {};
Condition100%2/2
$outside->{$table} //= {}
l!l
2813
MC/DC100%1/1
$outside->{$table} //= {}
$outside->{$table}
Truth table100%2/2
A
A= $outside->{$table}
Aresult
11
00
304 41 for my $key (keys %{$child->{$table}}) {
305 37 $rows->{$key} = $child->{$table}->{$key};
306 37 ▶ weaken($rows->{$key}) unless $self->{+RETAIN};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"retain"} 32 5 37
307 }
308 }
309 75 return;
310 }
311  
312 =over 4
313  
314 =item $lookup->rollback_level
315  
316 Rolls back the innermost savepoint, or the root transaction when no savepoint
317 is open: discards the top level. Croaks when no transaction is open.
318  
319 =back
320  
321 =cut
322  
323 124 ▶sub rollback_level {
Subroutine rollback_level: called CC=2 SCAR=6.9
Pod documented
324 124 my $self = shift;
325 124 ▶ croak "No transaction is open" unless $self->_in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_in_txn 1 123 124
326 123 pop @{$self->{+STACK}};
327 123 pop @{$self->{+MARKS}};
328 123 return;
329 }
330  
331 =over 4
332  
333 =item $bool = $lookup->is_canon($table, \%row_data)
334  
335 True when the hash is the entry for its primary key. False when it is not, when
336 no entry exists, and for row data that is deleted or not in the database, as
337 C<canon_status> says.
338  
339 =back
340  
341 =cut
342  
343 218 ▶sub is_canon {
Subroutine is_canon: called CC=4 SCAR=13.9
Pod documented
344 218 my $self = shift;
345 218 my ($table, $data) = @_;
346 218 ▶ return 0 if DBIx::QuickORM::RowState->state($data);
Branch: 1 branch
BranchTrueFalseTotal
if "DBIx::QuickORM::RowState"->state($data) 16 202 218
347 202 ▶ my $canon = $self->lookup($table, $data) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $canon = $self->lookup($table, $data) or return 0 136 62 198
348 136 ▶ return $canon == $data ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$canon == $data ? : 103 33 136
349 }
350  
351 =over 4
352  
353 =item $status = $lookup->canon_status($table, \%row_data)
354  
355 Undef while the hash is canon. Otherwise a hashref saying why not.
356  
357 my $one = $lookup->ingest(
358 users => {
359 id => 1,
360 name => 'Ann',
361 },
362 );
363 my $two = $lookup->ingest(
364 users => {
365 id => 1,
366 name => 'Annie',
367 },
368 );
369  
370 my $canon = $lookup->canon_status(users => $two); # undef
371 my $stale = $lookup->canon_status(users => $one); # {cause => 'replaced'}
372  
373 $lookup->begin_root;
374 my $before = $lookup->canon_status(users => $two); # {cause => 'txn'}
375 $lookup->rollback_level;
376  
377 $lookup->clear('users');
378 my $cleared = $lookup->canon_status(users => $two); # {cause => 'not_cached'}
379  
380 Its C<cause> is one of:
381  
382 =over 4
383  
384 =item C<deleted>
385  
386 The row was deleted, or found gone. C<by> is the call that marked it, when
387 known.
388  
389 =item C<not_in_db>
390  
391 The hash was built by C<vivify>, or its insert was rolled back.
392  
393 =item C<txn>
394  
395 A transaction is open, and the hash is the entry from before it began.
396  
397 =item C<replaced> or C<cache_cleared>
398  
399 As noted when it stopped being canon (see L</Who made a hash stop being
400 canon>), with C<by> when that call is known. Without C<debug> nothing is
401 noted, so a hash whose key another hash holds is C<replaced> with no C<by>,
402 and a copy that was never canon looks the same.
403  
404 =item C<not_cached>
405  
406 No entry holds it: it was read around the cache (C<no_cache>, C<bare>), its
407 transaction rolled back, or its table or everything was cleared.
408 C<last_clear> is the call that last cleared its table or everything, when
409 there was one and it is known.
410  
411 =back
412  
413 C<by> and C<last_clear> are the first four elements of C<caller(0)>: package,
414 file, line, and the public method called. Croaks as C<lookup> does for a hash
415 without its key, unless it is deleted or not in the database.
416  
417 =back
418  
419 =cut
420  
421 222 ▶sub canon_status {
Subroutine canon_status: called CC=14 SCAR=26.4
Pod documented
422 222 my $self = shift;
423 222 my ($table, $data) = @_;
424  
425 222 my $stored = $self->_stored_status($data);
426 222 ▶ return $stored if $stored;
Branch: 1 branch
BranchTrueFalseTotal
if $stored 18 204 222
427  
428 204 my $key = $self->_key($table, $data);
429 204 my $canon = $self->_find($table, $key);
430 204 ▶ return undef if $canon && $canon == $data;
Branch: 1 branch
BranchTrueFalseTotal
if $canon and $canon == $data 110 94 204
Condition100%3/3
$canon and $canon == $data
!ll&&!rl&&r
4945110
MC/DC100%2/2
$canon and $canon == $data
$canon $canon == $data
Truth table100%3/3
A and B
A= $canonB= $canon == $data
ABresult
0X0
100
111
431 94 ▶ return {cause => 'txn'} if $self->_in_txn && ($self->_get(0, $table, $key) // 0) == $data;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_in_txn and ($self->_get(0, $table, $key) // 0) == $data 7 87 94
Condition100%5/5
$self->_in_txn and ($self->_get(0, $table, $key) // 0) == $data
!ll&&!rl&&r
76117
$self->_get(0, $table, $key) // 0
l!l
99
MC/DC100%2/2
$self->_in_txn and ($self->_get(0, $table, $key) // 0) == $data
$self->_in_txn $self->_get(0, $table, $key)
Truth table100%3/3
A and B
A= $self->_in_txnB= $self->_get(0, $table, $key)
ABresult
0X0
100
111
432  
433 87 ▶ return {cause => 'replaced'} if $canon && !$self->{+DEBUG}; # no note without debug, but another hash holds the key
Branch: 1 branch
BranchTrueFalseTotal
if $canon and not $self->{"debug"} 31 56 87
Condition100%3/3
$canon and not $self->{"debug"}
!ll&&!rl&&r
421431
MC/DC100%2/2
$canon and not $self->{"debug"}
$canon not $self->{"debug"}
Truth table100%3/3
A and not(B)
A= $canonB= not $self->{"debug"}
ABresult
0X0
100
111
434 56 ▶ if (my $note = $self->{+DEBUG} && $self->{+DEBUG}->decanon($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $note = $self->{"debug"} && $self->{"debug"}->decanon($data)) 21 35 56
Condition100%3/3
$self->{'debug'} && $self->{'debug'}->decanon($data)
!ll&&!rl&&r
251021
MC/DC100%2/2
$self->{'debug'} && $self->{'debug'}->decanon($data)
$self->{'debug'} $self->{'debug'}->decanon($data)
Truth table100%3/3
A && B
A= $self->{'debug'}B= $self->{'debug'}->decanon($data)
ABresult
0X0
100
111
435 21 ▶ return {cause => $note->[0], $note->[1] ? (by => $note->[1]) : ()};
Branch: 1 branch
BranchTrueFalseTotal
$note->[1] ? : 18 3 21
436 }
437  
438 35 ▶ my $clear = $self->{+DEBUG} && $self->{+DEBUG}->last_clear($table);
Condition100%3/3
$self->{'debug'} && $self->{'debug'}->last_clear($table)
!ll&&!rl&&r
2528
MC/DC100%2/2
$self->{'debug'} && $self->{'debug'}->last_clear($table)
$self->{'debug'} $self->{'debug'}->last_clear($table)
Truth table100%3/3
A && B
A= $self->{'debug'}B= $self->{'debug'}->last_clear($table)
ABresult
0X0
100
111
439 35 ▶ return {cause => 'not_cached', $clear ? (last_clear => $clear) : ()};
Branch: 1 branch
BranchTrueFalseTotal
$clear ? : 8 27 35
440 }
441  
442 =over 4
443  
444 =item $row_data = $lookup->lookup($table, $pk)
445  
446 The canon hash for C<$pk>, or undef.
447  
448 =back
449  
450 =cut
451  
452 494 ▶sub lookup {
Subroutine lookup: called CC=1 SCAR=0.0
Pod documented
453 494 my $self = shift;
454 494 my ($table, $pk) = @_;
455 494 return $self->_find($table, $self->_key($table, $pk));
456 }
457  
458 =over 4
459  
460 =item $row_data = $lookup->ingest($table, \%row_data)
461  
462 Takes in a row read from the database and returns the canon hash.
463  
464 my $ann = $lookup->ingest(
465 users => {
466 id => 1,
467 name => 'Ann',
468 },
469 );
470  
471 # it agrees on every field both hold: the read's new field joins the entry
472 my $same = $lookup->ingest(
473 users => {
474 id => 1,
475 name => 'Ann',
476 email => 'ann@example.com',
477 },
478 ); # $ann, now with an email
479  
480 # a field differs: the read replaces the entry
481 my $new = $lookup->ingest(
482 users => {
483 id => 1,
484 name => 'Annie',
485 email => 'ann@example.com',
486 },
487 );
488 my $stale = $lookup->is_canon(users => $ann); # 0
489  
490 When an entry exists, holds no field the given hash lacks, and agrees on
491 every field both hold (NULL and missing differ), new fields are added to it
492 and it is returned. Otherwise the given hash becomes the entry and is
493 returned. A read of fewer fields than the entry holds therefore replaces it:
494 rows holding the old hash keep those fields but are no longer canon, and need
495 a refresh (or C<cas>) before a write.
496  
497 Inside a transaction, a key no transaction level holds is compared the same
498 way with the outside level's entry, which is reused in the top level when it
499 agrees.
500  
501 =back
502  
503 =cut
504  
505 366 ▶sub ingest {
Subroutine ingest: called CC=7 SCAR=19.5
Pod documented
506 366 my $self = shift;
507 366 my ($table, $data) = @_;
508 366 my $key = $self->_key($table, $data);
509  
510 365 my $canon = $self->_find($table, $key);
511 365 ▶ my $reuse = !$canon && @{$self->{+STACK}} > 1 && ($canon = $self->_get(0, $table, $key));
Condition100%6/6
!$canon && @{$self->{'stack'};} > 1 && ($canon = $self->_get(0, $table, $key))
!ll&&!rl&&r
3351416
not $canon and @{$self->{"stack"};} > 1
!ll&&!rl&&r
12920630
MC/DC100%3/3
!$canon && @{$self->{'stack'};} > 1 && ($canon = $self->_get(0, $table, $key))
not $canon @{$self->{"stack"};} > 1 ($canon = $self->_get(0, $table, $key))
Truth table100%4/4
not(A) and B && C
A= not $canonB= @{$self->{"stack"};} > 1C= ($canon = $self->_get(0, $table, $key))
ABCresult
0XX0
10X0
1100
1111
512 365 ▶ return $self->_store($table, $data, $key) unless $canon;
Branch: 1 branch
BranchTrueFalseTotal
unless $canon 220 145 365
513  
514 # The same row either way, so a replacement carries over a mark that it was inserted.
515 145 ▶ return $self->_replace($table, $canon, $data, $key) if $self->merge_or_replace($table, $canon, $data) eq 'replace';
Branch: 1 branch
BranchTrueFalseTotal
if $self->merge_or_replace($table, $canon, $data) eq "replace" 30 115 145
516  
517 # A merge only adds fields: rows holding the hash keep inflated values built from the fields it had.
518 115 $canon->{$_} = $data->{$_} for grep { !exists $canon->{$_} } keys %$data;
519 115 ▶ $self->{+STACK}->[-1]->{$table}->{$key} = $canon if $reuse;
Branch: 1 branch
BranchTrueFalseTotal
if $reuse 14 101 115
520 115 return $canon;
521 }
522  
523 =over 4
524  
525 =item \@row_data = $lookup->ingest_many($table, \@rows)
526  
527 C<ingest> for each row in order, returning the canon hashes. For a table keyed
528 by one untyped field, outside a transaction, it reads the key and the table's
529 entries directly and compares fields inline, which makes a fetch of many rows
530 cheaper. Otherwise, or when a subclass overrides C<ingest>, C<merge_or_replace>,
531 or C<differ>, it calls C<ingest> for each row.
532  
533 =back
534  
535 =cut
536  
537 230 ▶sub ingest_many {
Subroutine ingest_many: called CC=25 SCAR=32.2
Pod documented
538 230 my $self = shift;
539 230 my ($table, $rows) = @_;
540  
541 230 ▶ return [] unless @$rows;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rows 34 196 230
542 196 ▶ return [map { $self->ingest($table, $_) } @$rows] unless @{$self->{+STACK}} == 1 && $self->can('ingest') == \&ingest && $self->can('merge_or_replace') == \&merge_or_replace && $self->can('differ') == \&differ;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"stack"};} == 1 and $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingest and $self->can("merge_or_replace") == \&DBIx::QuickORM::LookupTable::Identity::merge_or_replace and $self->can("differ") == \&DBIx::QuickORM::LookupTable::Identity::differ 9 187 196
Condition88%8/9
@{$self->{"stack"};} == 1 and $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingest and $self->can("merge_or_replace") == \&DBIx::QuickORM::LookupTable::Identity::merge_or_replace and $self->can("differ") == \&DBIx::QuickORM::LookupTable::Identity::differ
!ll&&!rl&&r
81187
@{$self->{"stack"};} == 1 and $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingest and $self->can("merge_or_replace") == \&DBIx::QuickORM::LookupTable::Identity::merge_or_replace
!ll&&!rl&&r
71188
@{$self->{"stack"};} == 1 and $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingest
!ll&&!rl&&r
70189
MC/DC75%3/4
@{$self->{"stack"};} == 1 and $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingest and $self->can("merge_or_replace") == \&DBIx::QuickORM::LookupTable::Identity::merge_or_replace and $self->can("differ") == \&DBIx::QuickORM::LookupTable::Identity::differ
@{$self->{"stack"};} == 1 $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingest $self->can("merge_or_replace") == \&DBIx::QuickORM::LookupTable::Identity::merge_or_replace $self->can("differ") == \&DBIx::QuickORM::LookupTable::Identity::differ
Truth table80%4/5
A and B and C and D
A= @{$self->{"stack"};} == 1B= $self->can("ingest") == \&DBIx::QuickORM::LookupTable::Identity::ingestC= $self->can("merge_or_replace") == \&DBIx::QuickORM::LookupTable::Identity::merge_or_replaceD= $self->can("differ") == \&DBIx::QuickORM::LookupTable::Identity::differ
ABCDresult
0XXX0
10XX0
110X0
11100
11111
543  
544 # pk() croaks for a table not in the map.
545 187 my $map = $self->{+DB_MAP};
546 187 ▶ my $t = $map->table($table) || $map->pk($table);
Condition33%1/3
$map->table($table) || $map->pk($table)
l!l&&r!l&&!r
18700
MC/DC0%0/2
$map->table($table) || $map->pk($table)
$map->table($table) $map->pk($table)
Truth table33%1/3
A || B
A= $map->table($table)B= $map->pk($table)
ABresult
1X1
011
000
547 187 my $pk = $t->{pk};
548 187 ▶ return [map { $self->ingest($table, $_) } @$rows] unless $pk && @$pk == 1 && !$t->{pk_types};
Branch: 1 branch
BranchTrueFalseTotal
unless $pk and @$pk == 1 and not $t->{"pk_types"} 14 173 187
Condition100%6/6
$pk and @$pk == 1 and not $t->{"pk_types"}
!ll&&!rl&&r
311173
$pk and @$pk == 1
!ll&&!rl&&r
12184
MC/DC100%3/3
$pk and @$pk == 1 and not $t->{"pk_types"}
$pk @$pk == 1 not $t->{"pk_types"}
Truth table100%4/4
A and B and not(C)
A= $pkB= @$pk == 1C= not $t->{"pk_types"}
ABCresult
0XX0
10X0
1100
1111
549  
550 173 my $field = $pk->[0];
551 173 my $retain = $self->{+RETAIN};
552  
553 173 my @out;
554 173 ROW: for my $row (@$rows) {
555 # A copy of the element, not of the hash: reading the key of an undef row would otherwise autovivify a
556 # hash into the caller's array. A replacement may open a transaction (a hook), so each row checks.
557 281 my $data = $row;
558 281 ▶ if (@{$self->{+STACK}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"stack"};} > 1) 1 280 281
559 1 push @out => $self->ingest($table, $data);
560 1 next ROW;
561 }
562  
563 280 ▶ my $v = $data->{$field} // confess "The primary key field '$field' of the table '$table' is missing or undefined";
Condition100%2/2
$data->{$field} // confess("The primary key field '${field}' of the table '${table}' is missing or undefined")
l!l
2782
MC/DC100%1/1
$data->{$field} // confess("The primary key field '${field}' of the table '${table}' is missing or undefined")
$data->{$field}
Truth table100%2/2
A
A= $data->{$field}
Aresult
11
00
564 278 ▶ confess "The primary key field '$field' of the table '$table' has no type, so its value cannot be a reference" if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 278 278
565 278 my $key = "$v";
566 # Looked up for each row, since a replacement may clear the cache.
567 278 ▶ my $entries = $self->{+STACK}->[0]->{$table} //= {};
Condition100%2/2
$self->{"stack"}[0]{$table} //= {}
l!l
23345
MC/DC100%1/1
$self->{"stack"}[0]{$table} //= {}
$self->{"stack"}[0]{$table}
Truth table100%2/2
A
A= $self->{"stack"}[0]{$table}
Aresult
11
00
568 278 my $canon = $entries->{$key};
569  
570 278 ▶ unless ($canon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($canon) 198 80 278
571 198 $entries->{$key} = $data;
572 198 ▶ weaken($entries->{$key}) unless $retain;
Branch: 1 branch
BranchTrueFalseTotal
unless $retain 190 8 198
573 198 push @out => $data;
574 198 next ROW;
575 }
576  
577 # As ingest, with differ inlined: NULL equals only NULL, otherwise compare as strings.
578 80 my @new;
579 80 for my $f (keys %$data) {
580 208 ▶ if (exists $canon->{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $canon->{$f}) 205 3 208
581 205 my ($x, $y) = ($canon->{$f}, $data->{$f});
582 205 ▶ next if defined($x) ? (defined($y) && $x eq $y) : !defined($y);
Branch: 2 branches
BranchTrueFalseTotal
if defined $x ? defined $y && $x eq $y : !defined($y) 198 7 205
defined $x ? : 199 6 205
Condition66%2/3
defined $y and $x eq $y
!ll&&!rl&&r
06193
MC/DC50%1/2
defined $y and $x eq $y
defined $y $x eq $y
Truth table66%2/3
A and B
A= defined $yB= $x eq $y
ABresult
0X0
100
111
583 7 push @out => $self->_replace($table, $canon, $data, $key);
584 7 next ROW;
585 }
586 3 push @new => $f;
587 }
588  
589 73 for my $f (keys %$canon) {
590 197 ▶ next if exists $data->{$f};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$f} 187 10 197
591 10 push @out => $self->_replace($table, $canon, $data, $key);
592 10 next ROW;
593 }
594  
595 63 $canon->{$_} = $data->{$_} for @new;
596 63 push @out => $canon;
597 }
598  
599 171 return \@out;
600 }
601  
602 =over 4
603  
604 =item $choice = $lookup->merge_or_replace($table, \%canon, \%row_data)
605  
606 The policy C<ingest> follows for a read of a row the lookup table holds:
607 C<'merge'> keeps the held hash, adding the read's new fields to it, and
608 C<'replace'> makes the read the entry.
609  
610 package My::LookupTable::Trusting;
611 use parent 'DBIx::QuickORM::LookupTable::Identity';
612  
613 # a read of fewer fields keeps the held hash, instead of replacing it
614 sub merge_or_replace {
615 my ($self, $table, $canon, $data) = @_;
616 for my $field (keys %$data) {
617 next unless exists $canon->{$field};
618 return 'replace' if $self->differ($canon->{$field}, $data->{$field});
619 }
620 return 'merge';
621 }
622  
623 Then:
624  
625 my $orm = DBIx::QuickORM->new(credentials => \%credentials, lookup => '+My::LookupTable::Trusting');
626  
627 my $ann = $orm->insert(
628 users => {
629 name => 'Ann',
630 email => 'ann@example.com',
631 },
632 );
633 my $short = $orm->handle('users')
634 ->fields(['id', 'name'])
635 ->where({id => $ann->field('id')})
636 ->only;
637 my $kept = $ann->is_canon; # 1, though the read lacks email (the default would replace it)
638  
639 It replaces when a field both hold differs (by C<differ>), or when the read
640 lacks a field the held hash has, since a read cannot vouch for a field it did
641 not fetch. Otherwise it merges.
642  
643 A subclass may override it. A merge never changes a field the held hash
644 already has, so a merge despite a difference keeps the stale value in it,
645 which is the subclass's choice to make.
646  
647 =back
648  
649 =cut
650  
651 143 ▶sub merge_or_replace {
Subroutine merge_or_replace: called CC=5 SCAR=16.1
Pod documented
652 143 my $self = shift;
653 143 my ($table, $canon, $data) = @_;
654 143 for my $field (keys %$data) {
655 398 ▶ next unless exists $canon->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $canon->{$field} 25 373 398
656 373 ▶ return 'replace' if $self->differ($canon->{$field}, $data->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $self->differ($canon->{$field}, $data->{$field}) 21 352 373
657 }
658 122 ▶ return 'replace' if grep { !exists $data->{$_} } keys %$canon;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $data->{$_};} keys %$canon 9 113 122
659 113 return 'merge';
660 }
661  
662 =over 4
663  
664 =item $row_data = $lookup->store($table, \%row_data)
665  
666 Makes the hash the entry for its primary key, replacing any other, and returns
667 it. This is the write path.
668  
669 Inside a transaction the key is removed from the outside level. To re-key
670 after a primary key change, C<forget> the old key first.
671  
672 =back
673  
674 =cut
675  
676 10721 ▶sub store {
Subroutine store: called CC=2 SCAR=6.9
Pod documented
677 10721 my $self = shift;
678 10721 my ($table, $data) = @_;
679 10721 my $key = $self->_key($table, $data);
680 10720 ▶ return $self->_store($table, $data, $key, $self->{+DEBUG} ? $self->_find($table, $key) : undef); # the entry it replaces is only needed for the note
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} ? : 16 10704 10720
681 }
682  
683 =over 4
684  
685 =item $lookup->clear
686  
687 =item $lookup->clear($table)
688  
689 =item $lookup->clear($table, $pk)
690  
691 Clears everything, one table, or one row from the top level only.
692  
693 $lookup->begin_root;
694 my $ann = $lookup->store(
695 users => {
696 id => 1,
697 name => 'Ann',
698 },
699 );
700  
701 $lookup->push_level;
702 $lookup->clear('users'); # hides the rows the levels below hold
703 my $hidden = $lookup->lookup(users => {id => 1}); # undef
704 $lookup->rollback_level;
705  
706 my $back = $lookup->lookup(users => {id => 1}); # $ann
707 $lookup->commit_level;
708  
709 Inside a transaction it also marks them as cleared there, so the levels below
710 no longer show them. A savepoint rollback brings them back. A root commit
711 clears them from the outside level, and a root rollback leaves the outside
712 level as it was.
713  
714 With a key and C<debug>, the hash that was canon for it is noted as
715 C<cache_cleared> (see L</Who made a hash stop being canon>).
716  
717 =back
718  
719 =cut
720  
721 103 ▶sub clear {
Subroutine clear: called CC=9 SCAR=22.0
Pod documented
722 103 my $self = shift;
723 103 my ($table, $pk) = @_;
724 103 my $stack = $self->{+STACK};
725 103 my $top = $stack->[-1];
726  
727 # Inside a transaction a mark hides the same from the levels below, and a root commit applies it outside.
728 103 ▶ my $mark = @$stack > 1 ? ($self->{+MARKS}->[-1] //= {}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$stack > 1 ? : 26 77 103
Condition100%2/2
$self->{"marks"}[-1] //= {}
l!l
818
MC/DC100%1/1
$self->{"marks"}[-1] //= {}
$self->{"marks"}[-1]
Truth table100%2/2
A
A= $self->{"marks"}[-1]
Aresult
11
00
729 103 ▶ if (!defined $table) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $table) { } 50 53 103
elsif (@_ < 2) { } 38 15 53
730 50 $stack->[-1] = {};
731 50 ▶ $mark->{all} = 1 if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 8 42 50
732 50 ▶ $self->{+DEBUG}->note_clear if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 1 49 50
733 }
734 elsif (@_ < 2) {
735 38 delete $top->{$table};
736 38 ▶ $mark->{tables}->{$table} = 1 if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 11 27 38
737 38 ▶ $self->{+DEBUG}->note_clear($table) if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 3 35 38
738 }
739 else {
740 15 $self->_clear_key($table, $self->_key($table, $pk), 'cache_cleared');
741 }
742 102 return;
743 }
744  
745 =over 4
746  
747 =item $row_data = $lookup->update_entry($table, $old_pk, \%old_data, \%row_data)
748  
749 The write path of an update: makes C<%row_data> the entry for its primary key
750 in place of the entry for C<$old_pk>, and returns it.
751  
752 my $old = $lookup->store(
753 users => {
754 id => 1,
755 name => 'Ann',
756 },
757 );
758 my $new = $lookup->update_entry(
759 users => {id => 1},
760 $old,
761 {
762 id => 2,
763 name => 'Ann',
764 },
765 );
766  
767 my $was = $lookup->lookup(users => {id => 1}); # undef: the old key is gone
768 my $now = $lookup->lookup(users => {id => 2}); # $new
769  
770 A changed key first clears the old key (noting C<replaced>), as C<forget>
771 does. So does a new key that cannot be read (a key field set to NULL, say),
772 which then croaks as C<lookup> does.
773  
774 Inside a transaction a mark that the entry for C<$old_pk> was inserted
775 carries over, when that entry is not C<%old_data> (the caller carries that
776 one).
777  
778 =back
779  
780 =cut
781  
782 114 ▶sub update_entry {
Subroutine update_entry: called CC=8 SCAR=20.8
Pod documented
783 114 my $self = shift;
784 114 my ($table, $old_pk, $old_data, $data) = @_;
785  
786 114 my $old_key = $self->_key($table, $old_pk);
787  
788 # A mark that the row was inserted in the open transaction also carries over from the canonical copy when the
789 # row data is another hash (a bare one, say). Marks exist only inside a transaction, so only then is it looked up.
790 114 ▶ my $canon = @{$self->{+STACK}} > 1 ? $self->_find($table, $old_key) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'stack'};} > 1 ? : 21 93 114
791  
792 114 my ($key, $err);
793 {
794 114 local $@;
795 114 $key = eval { $self->_key($table, $data) };
796 114 $err = $@
797 }
798 # A changed key, or one the row cache cannot hold (the write set it to NULL, say), drops the old entry.
799 114 ▶ unless (defined($key) && $key eq $old_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and $key eq $old_key) 8 106 114
Condition100%3/3
defined $key and $key eq $old_key
!ll&&!rl&&r
17106
MC/DC100%2/2
defined $key and $key eq $old_key
defined $key $key eq $old_key
Truth table100%3/3
A and B
A= defined $keyB= $key eq $old_key
ABresult
0X0
100
111
800 8 $self->_clear_key($table, $old_key, 'replaced');
801 8 $self->_touch($table, $old_key, 'replaced');
802 8 ▶ die $err unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 7 8
803 }
804 113 ▶ $self->{+ROW_STATES}->carry_inserted($canon, $data) if $canon && $canon != $old_data;
Branch: 1 branch
BranchTrueFalseTotal
if $canon and $canon != $old_data 2 111 113
Condition100%3/3
$canon and $canon != $old_data
!ll&&!rl&&r
98132
MC/DC100%2/2
$canon and $canon != $old_data
$canon $canon != $old_data
Truth table100%3/3
A and B
A= $canonB= $canon != $old_data
ABresult
0X0
100
111
805  
806 # The entry being replaced is only needed for the note.
807 113 ▶ return $self->_store($table, $data, $key, $self->{+DEBUG} ? $self->_find($table, $key) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} ? : 10 103 113
808 }
809  
810 =over 4
811  
812 =item $lookup->forget
813  
814 =item $lookup->forget($table)
815  
816 =item $lookup->forget($table, $pk)
817  
818 What C<clear> does, and inside a transaction the same from the outside level,
819 noting its entry the same way.
820  
821 my $ann = $lookup->store(
822 users => {
823 id => 1,
824 name => 'Ann',
825 },
826 );
827  
828 $lookup->begin_root;
829 $lookup->clear(users => {id => 1}); # the outside level keeps it
830 $lookup->rollback_level;
831 my $kept = $lookup->is_canon(users => $ann); # 1
832  
833 $lookup->begin_root;
834 $lookup->forget(users => {id => 1}); # the outside level drops it at once
835 $lookup->rollback_level;
836 my $lost = $lookup->is_canon(users => $ann); # 0
837  
838 A root commit then merges what the transaction holds at that point (rows
839 read or written again after the forget). A root rollback brings back
840 nothing.
841  
842 This is how C<clear_cache>, a delete, a primary key change, a row found gone,
843 or a bulk update or delete drops entries.
844  
845 =back
846  
847 =cut
848  
849 178 ▶sub forget {
Subroutine forget: called CC=2 SCAR=6.9
Pod documented
850 178 my $self = shift;
851 178 my ($table, @pk) = @_;
852 178 ▶ unless (@pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@pk) 73 105 178
853 73 $self->clear(@_);
854 73 $self->_touch(@_);
855 73 return;
856 }
857  
858 105 my $key = $self->_key($table, $pk[0]);
859 105 $self->_clear_key($table, $key, 'cache_cleared');
860 105 $self->_touch($table, $key, 'cache_cleared');
861 105 return;
862 }
863  
864 =over 4
865  
866 =item $lookup->gone($table, \%key)
867  
868 Forgets a row the database no longer has and marks its entry deleted.
869  
870 my $ann = $lookup->store(
871 users => {
872 id => 1,
873 name => 'Ann',
874 },
875 );
876 $lookup->gone(users => {id => 1});
877  
878 my $held = $lookup->lookup(users => {id => 1}); # undef
879 my $state = DBIx::QuickORM::RowState->state($ann); # 'deleted'
880  
881 The caller marks the row data it holds itself.
882  
883 Inside a transaction the outside level's entry is marked too. Rolling back
884 the row state's level unmarks it, as the Connection's rollback does. The
885 lookup table's own C<rollback_level> does not.
886  
887 =back
888  
889 =cut
890  
891 76 ▶sub gone {
Subroutine gone: called CC=2 SCAR=6.9
Pod documented
892 76 my $self = shift;
893 76 my ($table, $key) = @_;
894 76 my $canon = $self->lookup($table, $key);
895  
896 # A lookup inside a transaction never reads the outside level, so its held hash is fetched directly before forget drops it.
897 76 ▶ my $outside = $self->_in_txn ? $self->_get(0, $table, $self->_key($table, $key)) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_in_txn ? : 16 60 76
898  
899 76 $self->forget($table, $key);
900 76 $self->{+ROW_STATES}->mark_deleted(grep { $_ } $canon, $outside);
901 }
902  
903 =over 4
904  
905 =item $lookup->remove_table($table)
906  
907 Drops every entry for the table from every level, with no marks: for a
908 temporary table or view that no longer exists, whose rows nothing should find
909 again.
910  
911 =back
912  
913 =cut
914  
915 46 ▶sub remove_table {
Subroutine remove_table: called CC=2 SCAR=6.9
Pod documented
916 46 my $self = shift;
917 46 my ($table) = @_;
918 46 delete $_->{$table} for @{$self->{+STACK}};
919 46 return;
920 }
921  
922 =pod
923  
924 =over 4
925  
926 =item $lookup->clear_txn_levels
927  
928 Empties every level above the outside level, keeping the number of levels.
929  
930 $lookup->begin_root;
931 my $ann = $lookup->store(
932 users => {
933 id => 1,
934 name => 'Ann',
935 },
936 );
937  
938 $lookup->clear_txn_levels; # the outcome is unknown
939 my $held = $lookup->lookup(users => {id => 1}); # undef
940 $lookup->rollback_level; # the level is still there to end
941  
942 For a transaction whose outcome is unknown, after which no row held from
943 inside it can be trusted. The outside level is kept: keys the transaction
944 stored, replaced, or forgot are already gone from it. With C<debug> it
945 records a clear of everything.
946  
947 =back
948  
949 =cut
950  
951 9 ▶sub clear_txn_levels {
Subroutine clear_txn_levels: called CC=4 SCAR=13.9
Pod documented
952 9 my $self = shift;
953 9 my $stack = $self->{+STACK};
954 9 $_ = {} for @$stack[1 .. $#$stack];
955 9 $_ = undef for @{$self->{+MARKS}}[1 .. $#$stack];
956 9 ▶ $self->{+DEBUG}->note_clear if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 9 9
957 9 return;
958 }
959  
960 =head1 PRIVATE METHODS
961  
962 =over 4
963  
964 =item $bool = $lookup->_in_txn
965  
966 True while a root transaction is open: its levels, as the Connection's
967 transaction events pushed them.
968  
969 =back
970  
971 =cut
972  
973 623 ▶sub _in_txn { @{$_[0]->{+STACK}} > 1 ? 1 : 0 }
Subroutine _in_txn: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0]{'stack'};} > 1 ? : 310 313 623
974  
975 =over 4
976  
977 =item $lookup->_clear_key($table, $key, $cause)
978  
979 C<clear> of one row by its key string. With C<debug>, notes the hash that was
980 canon for it, at any level the mark hides, with C<$cause>.
981  
982 =back
983  
984 =cut
985  
986 127 ▶sub _clear_key {
Subroutine _clear_key: called CC=3 SCAR=11.0
987 127 my $self = shift;
988 127 my ($table, $key, $cause) = @_;
989 127 my $stack = $self->{+STACK};
990 127 ▶ $self->_decanon($cause, $self->_find($table, $key)) if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 7 120 127
991 127 delete $stack->[-1]->{$table}->{$key};
992 127 ▶ $self->{+MARKS}->[-1]->{keys}->{$table}->{$key} = 1 if @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack > 1 31 96 127
993 127 return;
994 }
995  
996 =pod
997  
998 =over 4
999  
1000 =item $key = $lookup->_key($table, \%pk)
1001  
1002 The key string for the key fields in the hash, typed fields through their
1003 type's C<canonical_key_value>. Croaks as the DESCRIPTION says.
1004  
1005 =back
1006  
1007 =cut
1008  
1009 12149 ▶sub _key {
Subroutine _key: called CC=14 SCAR=26.4
1010 12149 my $self = shift;
1011 12149 my ($table, $pk) = @_;
1012  
1013 12149 my $map = $self->{+DB_MAP};
1014 12149 ▶ my $t = $map->table($table) || $map->pk($table); # pk() croaks for a table not in the map
Condition100%3/3
$map->table($table) || $map->pk($table)
l!l&&r!l&&!r
1214711
MC/DC0%0/2
$map->table($table) || $map->pk($table)
$map->table($table) $map->pk($table)
Truth table33%1/3
A || B
A= $map->table($table)B= $map->pk($table)
ABresult
1X1
011
000
1015 12147 my $fields = $t->{pk};
1016 12147 ▶ confess "The table '$table' has no primary key" unless $fields && @$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields and @$fields 3 12144 12147
Condition66%2/3
$fields and @$fields
!ll&&!rl&&r
3012144
MC/DC50%1/2
$fields and @$fields
$fields @$fields
Truth table66%2/3
A and B
A= $fieldsB= @$fields
ABresult
0X0
100
111
1017 12144 my $types = $t->{pk_types};
1018  
1019 # The common case: one untyped key field, keyed by its value.
1020 12144 ▶ if (@$fields == 1 && !$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fields == 1 and not $types) 11925 219 12144
Condition100%3/3
@$fields == 1 and not $types
!ll&&!rl&&r
8013911925
MC/DC100%2/2
@$fields == 1 and not $types
@$fields == 1 not $types
Truth table100%3/3
A and not(B)
A= @$fields == 1B= not $types
ABresult
0X0
100
111
1021 11925 ▶ my $v = $pk->{$fields->[0]} // confess "The primary key field '$fields->[0]' of the table '$table' is missing or undefined";
Condition100%2/2
$$pk{$fields->[0]} // confess("The primary key field '$fields->[0]' of the table '${table}' is missing or undefined")
l!l
119214
MC/DC100%1/1
$$pk{$fields->[0]} // confess("The primary key field '$fields->[0]' of the table '${table}' is missing or undefined")
$$pk{$fields->[0]}
Truth table100%2/2
A
A= $$pk{$fields->[0]}
Aresult
11
00
1022 11921 ▶ confess "The primary key field '$fields->[0]' of the table '$table' has no type, so its value cannot be a reference" if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 1 11920 11921
1023 11920 return "$v";
1024 }
1025  
1026 219 ▶ my @vals = map { $pk->{$_} // confess "The primary key field '$_' of the table '$table' is missing or undefined" } @$fields;
Condition100%2/2
$pk->{$_} // confess("The primary key field '${_}' of the table '${table}' is missing or undefined")
l!l
2981
MC/DC100%1/1
$pk->{$_} // confess("The primary key field '${_}' of the table '${table}' is missing or undefined")
$pk->{$_}
Truth table100%2/2
A
A= $pk->{$_}
Aresult
11
00
1027 218 for my $i (0 .. $#vals) {
1028 297 ▶ my $type = $types && $types->[$i];
Condition100%3/3
$types && $types->[$i]
!ll&&!rl&&r
11223162
MC/DC100%2/2
$types && $types->[$i]
$types $types->[$i]
Truth table100%3/3
A && B
A= $typesB= $types->[$i]
ABresult
0X0
100
111
1029 297 ▶ if ($type) { $vals[$i] = $type->canonical_key_value($vals[$i], $map->field($table, $fields->[$i]), $self->{+CONNECTION}) }
Branch: 2 branches
BranchTrueFalseTotal
if ($type) { } 162 135 297
elsif (ref $vals[$i]) { } 0 135 135
1030 0 elsif (ref $vals[$i]) { confess "The primary key field '$fields->[$i]' of the table '$table' has no type, so its value cannot be a reference" }
1031 }
1032  
1033 218 ▶ return "$vals[0]" if @vals == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @vals == 1 139 79 218
1034 79 return join '' => map { length($_) . ':' . $_ } @vals;
1035 }
1036  
1037 =over 4
1038  
1039 =item $row_data = $lookup->_replace($table, \%old, \%row_data, $key)
1040  
1041 Makes the hash the entry for C<$key> in place of C<%old>, the entry an ingest
1042 replaces, and returns it. A mark that C<%old> was inserted in an open
1043 transaction carries over to the new hash, as for a re-read of the same row.
1044 Every ingest that replaces an entry goes through here. Writes (C<store>,
1045 C<update_entry>) go to C<_store> directly, and a transaction's commit moves
1046 entries between levels on its own.
1047  
1048 =back
1049  
1050 =cut
1051  
1052 47 ▶sub _replace {
Subroutine _replace: called CC=1 SCAR=0.0
1053 47 my $self = shift;
1054 47 my ($table, $old, $data, $key) = @_;
1055 47 $self->{+ROW_STATES}->carry_inserted($old, $data);
1056 47 return $self->_store($table, $data, $key, $old);
1057 }
1058  
1059 =over 4
1060  
1061 =item $row_data = $lookup->_store($table, \%row_data, $key, \%old)
1062  
1063 C<store> with the key string already built, so C<ingest> builds it once.
1064 C<\%old> is the entry it replaces, if any, which it notes as C<replaced>, as
1065 it does an outside entry a transaction drops.
1066  
1067 =back
1068  
1069 =cut
1070  
1071 11100 ▶sub _store {
Subroutine _store: called CC=12 SCAR=24.9
1072 11100 my $self = shift;
1073 11100 my ($table, $data, $key, $old) = @_;
1074 11100 my $stack = $self->{+STACK};
1075 11100 $stack->[-1]->{$table}->{$key} = $data;
1076 11100 ▶ $self->_decanon(replaced => $old) if $self->{+DEBUG} && $old && $old != $data;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} and $old and $old != $data 19 11081 11100
Condition83%5/6
$self->{"debug"} and $old and $old != $data
!ll&&!rl&&r
11081019
$self->{"debug"} and $old
!ll&&!rl&&r
110552619
MC/DC66%2/3
$self->{"debug"} and $old and $old != $data
$self->{"debug"} $old $old != $data
Truth table75%3/4
A and B and C
A= $self->{"debug"}B= $oldC= $old != $data
ABCresult
0XX0
10X0
1100
1111
1077  
1078 11100 ▶ if (@$stack > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$stack > 1) { } 10108 992 11100
elsif (not $self->{'retain'}) { } 908 84 992
1079 10108 my $outside = $stack->[0]->{$table};
1080 10108 ▶ my $held = $outside && delete $outside->{$key};
Condition100%3/3
$outside && delete $outside->{$key}
!ll&&!rl&&r
100276912
MC/DC100%2/2
$outside && delete $outside->{$key}
$outside delete $outside->{$key}
Truth table100%3/3
A && B
A= $outsideB= delete $outside->{$key}
ABresult
0X0
100
111
1081 10108 ▶ $self->_decanon(replaced => $held) if $self->{+DEBUG} && $held && $held != $data && (!$old || $held != $old);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} and $held and $held != $data and !$old || $held != $old 1 10107 10108
Condition83%10/12
$self->{"debug"} and $held and $held != $data and !$old || $held != $old
!ll&&!rl&&r
1010611
$self->{"debug"} and $held and $held != $data
!ll&&!rl&&r
1010602
$self->{"debug"} and $held
!ll&&!rl&&r
1010062
not $old or $held != $old
l!l&&r!l&&!r
101
MC/DC60%3/5
$self->{"debug"} and $held and $held != $data and !$old || $held != $old
$self->{"debug"} $held $held != $data not $old $held != $old
Truth table66%4/6
A and B and C and not(D) or E
A= $self->{"debug"}B= $heldC= $held != $dataD= not $oldE= $held != $old
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
1082 }
1083 elsif (!$self->{+RETAIN}) {
1084 908 weaken($stack->[-1]->{$table}->{$key});
1085 }
1086  
1087 11100 return $data;
1088 }
1089  
1090 =over 4
1091  
1092 =item $lookup->_decanon($cause, \%row_data)
1093  
1094 Notes in the Debug object that the hash stopped being canon, and why. Does
1095 nothing for undef.
1096  
1097 =back
1098  
1099 =cut
1100  
1101 27 ▶sub _decanon {
Subroutine _decanon: called CC=2 SCAR=7.0
1102 27 my $self = shift;
1103 27 my ($cause, $data) = @_;
1104 27 ▶ $self->{+DEBUG}->note_decanon($cause, $data) if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 27 0 27
1105 27 return;
1106 }
1107  
1108 =over 4
1109  
1110 =item $row_data = $lookup->_find($table, $key)
1111  
1112 The canon hash for the key string, or undef. Outside a transaction it is the
1113 outside level's entry. Inside one, the levels are read from the top down to
1114 the root transaction level, never the outside level. The first level that
1115 holds the key, or marks it, its table, or everything as cleared, decides.
1116  
1117 =back
1118  
1119 =cut
1120  
1121 1125 ▶sub _find {
Subroutine _find: called CC=12 SCAR=24.8
1122 1125 my $self = shift;
1123 1125 my ($table, $key) = @_;
1124 1125 my $stack = $self->{+STACK};
1125 1125 ▶ return $self->_get(0, $table, $key) unless @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack > 1 917 208 1125
1126  
1127 208 my $marks = $self->{+MARKS};
1128 208 ▶ for (my $i = $#$stack; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 262 96 358
1129 262 my $rows = $stack->[$i]->{$table};
1130 262 ▶ return $rows->{$key} if $rows && exists $rows->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $rows and exists $rows->{$key} 103 159 262
Condition100%3/3
$rows and exists $rows->{$key}
!ll&&!rl&&r
13029103
MC/DC100%2/2
$rows and exists $rows->{$key}
$rows exists $rows->{$key}
Truth table100%3/3
A and B
A= $rowsB= exists $rows->{$key}
ABresult
0X0
100
111
1131 159 ▶ my $mark = $marks->[$i] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $mark = $marks->[$i] or next 15 144 159
1132 15 ▶ my $keys = $mark->{keys} && $mark->{keys}->{$table};
Condition100%3/3
$mark->{'keys'} && $mark->{'keys'}{$table}
!ll&&!rl&&r
717
MC/DC100%2/2
$mark->{'keys'} && $mark->{'keys'}{$table}
$mark->{'keys'} $mark->{'keys'}{$table}
Truth table100%3/3
A && B
A= $mark->{'keys'}B= $mark->{'keys'}{$table}
ABresult
0X0
100
111
1133 15 ▶ return undef if $mark->{all} || ($mark->{tables} && $mark->{tables}->{$table}) || ($keys && $keys->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $mark->{"all"} or $mark->{"tables"} and $mark->{"tables"}{$table} or $keys and $keys->{$key} 9 6 15
Condition100%12/12
$mark->{"all"} or $mark->{"tables"} and $mark->{"tables"}{$table} or $keys and $keys->{$key}
l!l&&r!l&&!r
636
$mark->{"all"} or $mark->{"tables"} and $mark->{"tables"}{$table}
l!l&&r!l&&!r
339
$mark->{"tables"} and $mark->{"tables"}{$table}
!ll&&!rl&&r
813
$keys and $keys->{$key}
!ll&&!rl&&r
333
MC/DC100%5/5
$mark->{"all"} or $mark->{"tables"} and $mark->{"tables"}{$table} or $keys and $keys->{$key}
$mark->{"all"} $mark->{"tables"} $mark->{"tables"}{$table} $keys $keys->{$key}
Truth table75%6/8
A or B and C or D and E
A= $mark->{"all"}B= $mark->{"tables"}C= $mark->{"tables"}{$table}D= $keysE= $keys->{$key}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
1134 }
1135 96 return undef;
1136 }
1137  
1138 =over 4
1139  
1140 =item $lookup->_merge(\%child, \%mark)
1141  
1142 A savepoint commit: applies the popped level's marks to its parent with
1143 C<_apply_marks>, carrying them into the parent's marks, then copies the popped level's entries into the parent.
1144  
1145 =back
1146  
1147 =cut
1148  
1149 20 ▶sub _merge {
Subroutine _merge: called CC=6 SCAR=17.9
1150 20 my $self = shift;
1151 20 my ($child, $mark) = @_;
1152 20 my $parent = $self->{+STACK}->[-1];
1153  
1154 20 ▶ $self->_apply_marks($parent, $mark, $self->{+MARKS}->[-1] //= {}) if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 6 14 20
Condition100%2/2
$self->{"marks"}[-1] //= {}
l!l
15
MC/DC100%1/1
$self->{"marks"}[-1] //= {}
$self->{"marks"}[-1]
Truth table100%2/2
A
A= $self->{"marks"}[-1]
Aresult
11
00
1155  
1156 20 for my $table (keys %$child) {
1157 13 ▶ my $rows = $parent->{$table} //= {};
Condition100%2/2
$parent->{$table} //= {}
l!l
76
MC/DC100%1/1
$parent->{$table} //= {}
$parent->{$table}
Truth table100%2/2
A
A= $parent->{$table}
Aresult
11
00
1158 13 $rows->{$_} = $child->{$table}->{$_} for keys %{$child->{$table}};
1159 }
1160 20 return;
1161 }
1162  
1163 =pod
1164  
1165 =over 4
1166  
1167 =item $lookup->_apply_marks(\%level, \%mark)
1168  
1169 =item $lookup->_apply_marks(\%level, \%mark, \%into)
1170  
1171 Clears what the marks name (everything, tables, keys) from the level, and
1172 adds them to the C<\%into> marks when given.
1173  
1174 =back
1175  
1176 =cut
1177  
1178 21 ▶sub _apply_marks {
Subroutine _apply_marks: called CC=11 SCAR=24.0
1179 21 my $self = shift;
1180 21 my ($level, $mark, $into) = @_;
1181  
1182 21 ▶ if ($mark->{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark->{"all"}) 7 14 21
1183 7 %$level = ();
1184 7 ▶ $into->{all} = 1 if $into;
Branch: 1 branch
BranchTrueFalseTotal
if $into 2 5 7
1185 }
1186 21 ▶ for my $table (keys %{$mark->{tables} // {}}) {
Condition100%2/2
$mark->{"tables"} // {}
l!l
417
MC/DC100%1/1
$mark->{"tables"} // {}
$mark->{"tables"}
Truth table100%2/2
A
A= $mark->{"tables"}
Aresult
11
00
1187 4 delete $level->{$table};
1188 4 ▶ $into->{tables}->{$table} = 1 if $into;
Branch: 1 branch
BranchTrueFalseTotal
if $into 1 3 4
1189 }
1190 21 ▶ my $keys = $mark->{keys} // {};
Condition100%2/2
$mark->{"keys"} // {}
l!l
147
MC/DC100%1/1
$mark->{"keys"} // {}
$mark->{"keys"}
Truth table100%2/2
A
A= $mark->{"keys"}
Aresult
11
00
1191 21 for my $table (keys %$keys) {
1192 14 for my $key (keys %{$keys->{$table}}) {
1193 15 ▶ delete $level->{$table}->{$key} if $level->{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $level->{$table} 10 5 15
1194 15 ▶ $into->{keys}->{$table}->{$key} = 1 if $into;
Branch: 1 branch
BranchTrueFalseTotal
if $into 3 12 15
1195 }
1196 }
1197 21 return;
1198 }
1199  
1200 =over 4
1201  
1202 =item $row_data = $lookup->_get($index, $table, $key)
1203  
1204 The entry for the key string in the level at C<$index> of the stack, or undef.
1205 A freed weak entry is deleted.
1206  
1207 =back
1208  
1209 =cut
1210  
1211 1093 ▶sub _get {
Subroutine _get: called CC=4 SCAR=13.9
1212 1093 my $self = shift;
1213 1093 my ($index, $table, $key) = @_;
1214 1093 ▶ my $rows = $self->{+STACK}->[$index]->{$table} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $self->{"stack"}[$index]{$table} or return undef 906 187 1093
1215 906 ▶ return undef unless exists $rows->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rows->{$key} 258 648 906
1216 648 ▶ return $rows->{$key} // delete $rows->{$key};
Condition66%2/3
$rows->{$key} // delete $rows->{$key}
l!l&&r!l&&!r
568080
MC/DC50%1/2
$rows->{$key} // delete $rows->{$key}
$rows->{$key} delete $rows->{$key}
Truth table66%2/3
A // B
A= $rows->{$key}B= delete $rows->{$key}
ABresult
1X1
011
000
1217 }
1218  
1219 =over 4
1220  
1221 =item $lookup->_touch
1222  
1223 =item $lookup->_touch($table)
1224  
1225 =item $lookup->_touch($table, $key, $cause)
1226  
1227 Inside a transaction, removes everything, the table, or the key string from
1228 the outside level. With C<debug>, a removed entry for a key is noted with
1229 C<$cause>.
1230  
1231 =back
1232  
1233 =cut
1234  
1235 186 ▶sub _touch {
Subroutine _touch: called CC=6 SCAR=17.9
1236 186 my $self = shift;
1237 186 my ($table, $key, $cause) = @_;
1238 186 my $stack = $self->{+STACK};
1239 186 ▶ return unless @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack > 1 149 37 186
1240 37 ▶ if (!defined $table) { $stack->[0] = {}; return }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table) 3 34 37
1241 34 ▶ my $rows = $stack->[0]->{$table} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $stack->[0]{$table} or return 27 7 34
1242 27 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 20 7 27
1243 20 my $gone = delete $rows->{$key};
1244 20 ▶ $self->_decanon($cause, $gone) if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 20 20
1245 }
1246 7 else { delete $stack->[0]->{$table} }
1247 27 return;
1248 }
1249  
1250 261 with 'DBIx::QuickORM::Role::LookupTable';
1251  
1252 261 1;
1253  
1254 __END__
« blib/lib/DBIx/QuickORM/Iterator.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/LookupTable.pm »