blib/lib/DBIx/QuickORM/LookupTable.pm

filter: Statementstmt 97.7% 256 / 262 Branchbran 95.2% 139 / 146 Conditioncond 95.2% 80 / 84 MC/DCmcdc 89.1% 41 / 46 Subroutinesub 97.1% 34 / 35 Podpod 90.9% 20 / 22 totaltotal 95.8% 570 / 595 CC 244 SCAR 20.3
CC
244
Cov
97%
_key
14.1
_key
14.1
_find
12.0
CRAP 7.6
« blib/lib/DBIx/QuickORM/Handle.pm ↑ Summary blib/lib/DBIx/QuickORM/WhereBuilder/RawSQL.pm »
1 package DBIx::QuickORM::LookupTable;
2 259 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 259 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 259 our $VERSION = '0.000031';
6  
7 259 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 259 ▶use Hash::Util::FieldHash qw/fieldhash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 259 ▶use Scalar::Util qw/refaddr weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 259 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 <db_map
13 <connection
14 <retain
15 <stack
16 +marks
17 259 };
18  
19 =pod
20  
21 =encoding UTF-8
22  
23 =head1 NAME
24  
25 DBIx::QuickORM::LookupTable - The canon row hash for each table and primary key.
26  
27 =head1 DESCRIPTION
28  
29 The lookup table maps each table and primary key to the I<canon> row hash:
30 the ORM's canonical copy of that row. A hash is canon when it is the same
31 reference as the entry the levels give for its key. Rows arrive through
32 C<ingest()> (reads) and C<store()> (writes), and leave through C<forget()>.
33  
34 The table is a stack of levels: the outside level, plus one per open
35 transaction or savepoint. Writes go to the top level. Reads inside a
36 transaction look from the top level down to the root transaction level, never
37 the outside level, and stop at the first level that knows the key.
38 L<DBIx::QuickORM/Transactions> describes what users see.
39  
40 =over 4
41  
42 =item *
43  
44 A root transaction pushes an empty level, so nothing from before it is canon
45 inside it. The outside level stays as a cache.
46  
47 =item *
48  
49 A savepoint pushes an empty level, so the parent's entries stay canon in it.
50  
51 =item *
52  
53 C<clear()> or C<forget()> inside a transaction also marks the key, the
54 table, or everything as cleared in its level, hiding the levels below.
55  
56 =item *
57  
58 A savepoint commit merges its entries and marks into its parent. A savepoint
59 rollback discards it, so the parent's entries are canon again.
60  
61 =item *
62  
63 A root commit clears what its marks name from the outside level, then merges
64 its entries into it, replacing those for the same keys. A root rollback
65 discards it.
66  
67 =item *
68  
69 Inside a transaction, a key that a C<store()>, a C<forget()>, or a replacing
70 C<ingest()> touches leaves the outside level at once. C<forget()> with no table
71 empties the outside level.
72  
73 =item *
74  
75 An C<ingest()> of a key the transaction lacks reuses the outside hash when it
76 agrees on every field both hold. New fields are added to it in place, and it
77 stays outside too, so an object holding it from before the transaction is
78 canon again.
79  
80 =back
81  
82 Fields C<ingest()> adds in place to a hash a savepoint's parent holds stay
83 visible to the parent after a savepoint rollback.
84  
85 Outside a transaction, entries are held weakly unless C<retain> is set.
86 Inside one they are strong. A root commit weakens the entries it merges,
87 again unless C<retain> is set. A freed weak entry reads as a miss and is
88 deleted when met.
89  
90 A primary key argument (C<$pk> below) is a hashref holding the key fields, so
91 row data works. Callers convert other key forms before calling, as the ORM's
92 key arguments do.
93  
94 An untyped key field is keyed by its value as it is. A typed one is keyed by
95 its type's C<canonical_key_value>, so its database text, its inflated value,
96 and other forms of the same value all find one entry. The map's C<pk_types>
97 says which fields are typed.
98  
99 It croaks on a table with no primary key, a missing or undef key field, and a
100 reference in an untyped key field.
101  
102 =head2 Row state
103  
104 Beside the levels, the lookup table keeps a little state for row hashes,
105 keyed by the hash itself (a fieldhash), never stored in it:
106  
107 =over 4
108  
109 =item *
110  
111 C<deleted>: set by a row C<delete>, a C<delete_row>, and a refresh or find
112 that finds nothing in the database. A miss from the cache alone marks
113 nothing. A handle's bulk delete marks nothing, unless given C<mark_deleted>.
114  
115 =item *
116  
117 C<vivified>: the hash was built by the ORM's C<vivify>, never read from the
118 database, or its insert was rolled back.
119  
120 =back
121  
122 Each is present only when it applies. Row state follows the transaction:
123  
124 =over 4
125  
126 =item *
127  
128 A hash marked deleted inside a transaction is unmarked when that level rolls
129 back, and so is every copy made of it with C<copy_row_state>.
130  
131 =item *
132  
133 A hash inserted inside a transaction (C<mark_inserted>) is marked vivified
134 when that level rolls back. A hash inserted and then deleted in it ends up
135 vivified too, and so does the new hash of a row inserted and then updated or
136 refreshed in it (C<carry_inserted>).
137  
138 =back
139  
140 A savepoint commit carries both lists to its parent. A root commit drops
141 them.
142  
143 =head1 SYNOPSIS
144  
145 my $lookup = DBIx::QuickORM::LookupTable->new(
146 db_map => $map,
147 retain => 0,
148 );
149  
150 my $canon = $lookup->ingest(
151 users => {
152 id => 1,
153 name => 'bob',
154 },
155 );
156 $lookup->is_canon(users => $canon); # true
157  
158 $lookup->begin_root;
159 $lookup->push_level;
160 $lookup->store(users => $new_data);
161 $lookup->rollback_level; # savepoint rollback
162 $lookup->commit_level; # root commit
163  
164 =head1 ATTRIBUTES
165  
166 =over 4
167  
168 =item $map = $lookup->db_map
169  
170 Required: the L<DBIx::QuickORM::Map> that gives each table's primary key.
171  
172 =item $conn = $lookup->connection
173  
174 The L<DBIx::QuickORM::Connection> passed to C<canonical_key_value>, held
175 weakly. Types whose key form depends on the connection need it: the DateTime
176 type reads the dialect and C<time_zone> from it. A connection passes itself
177 when it builds its lookup table.
178  
179 =item $bool = $lookup->retain
180  
181 When true, entries outside a transaction are held strongly until cleared.
182  
183 =item $levels = $lookup->stack
184  
185 The levels, each C<< {table => {key => $row_hash}} >>. The first is the
186 outside level, the last is the top. Each transaction level's clear marks,
187 and the row hashes it marked deleted or inserted, are kept beside it,
188 privately.
189  
190 =back
191  
192 =cut
193  
194 # Inside-out row state, keyed by row hash: {deleted => 1 or the hash a copy follows, vivified => 1}, each only when it applies.
195 259 fieldhash my %STATE;
196  
197 117 ▶sub init {
Subroutine init: called CC=3 SCAR=14.2
Pod undocumented
198 0 ▶ my $self = shift;
Subroutine init: not called CC=3 SCAR=14.2
Pod undocumented
199 0 ▶ croak "'db_map' is required" unless $self->{+DB_MAP};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"db_map"} 1 116 117
unless $self->{"db_map"} 0 0 0
200 0 ▶ weaken($self->{+CONNECTION}) if $self->{+CONNECTION};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"connection"} 91 25 116
if $self->{"connection"} 0 0 0
201 0 $self->{+STACK} = [{}];
202 0 $self->{+MARKS} = [undef];
203 }
204  
205 =head1 PUBLIC METHODS
206  
207 =over 4
208  
209 =item $bool = $lookup->in_txn
210  
211 True while a root transaction is open.
212  
213 =back
214  
215 =cut
216  
217 886 ▶sub in_txn { @{$_[0]->{+STACK}} > 1 ? 1 : 0 }
Subroutine in_txn: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0]{'stack'};} > 1 ? : 367 519 886
218  
219 =over 4
220  
221 =item $lookup->begin_root
222  
223 Starts a root transaction: pushes an empty level above the outside level.
224 Croaks when a transaction is already open.
225  
226 =back
227  
228 =cut
229  
230 140 ▶sub begin_root {
Subroutine begin_root: called CC=2 SCAR=6.9
Pod documented
231 140 my $self = shift;
232 140 ▶ croak "A transaction is already open" if $self->in_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_txn 1 139 140
233 139 push @{$self->{+STACK}} => {};
234 139 push @{$self->{+MARKS}} => undef;
235 }
236  
237 =over 4
238  
239 =item $lookup->push_level
240  
241 Starts a savepoint: pushes an empty level, so its cost does not depend on the
242 rows held. Croaks when no transaction is open.
243  
244 =back
245  
246 =cut
247  
248 52 ▶sub push_level {
Subroutine push_level: called CC=2 SCAR=6.9
Pod documented
249 52 my $self = shift;
250 52 ▶ croak "No transaction is open" unless $self->in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_txn 1 51 52
251 51 push @{$self->{+STACK}} => {};
252 51 push @{$self->{+MARKS}} => undef;
253 }
254  
255 =over 4
256  
257 =item $lookup->commit_level
258  
259 Commits the innermost savepoint, or the root transaction when no savepoint is
260 open. Croaks when no transaction is open.
261  
262 A savepoint commit merges its entries and marks into its parent, at a cost
263 that grows with the keys it touched. A root commit clears what its marks name
264 from the outside level, then merges its entries into it, weakened unless
265 C<retain>.
266  
267 =back
268  
269 =cut
270  
271 82 ▶sub commit_level {
Subroutine commit_level: called CC=8 SCAR=20.8
Pod documented
272 82 my $self = shift;
273 82 my $stack = $self->{+STACK};
274 82 ▶ croak "No transaction is open" unless $self->in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_txn 1 81 82
275 81 my $child = pop @$stack;
276 81 my $mark = pop @{$self->{+MARKS}};
277 81 ▶ return $self->_merge($child, $mark) if @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack > 1 20 61 81
278  
279 61 my $outside = $stack->[0];
280 61 ▶ $self->_apply_marks($outside, $mark) if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 36 25 61
281 61 for my $table (keys %$child) {
282 36 ▶ my $rows = $outside->{$table} //= {};
Condition100%2/2
$outside->{$table} //= {}
l!l
2412
MC/DC100%1/1
$outside->{$table} //= {}
$outside->{$table}
Truth table100%2/2
A
A= $outside->{$table}
Aresult
11
00
283 36 for my $key (keys %{$child->{$table}}) {
284 32 $rows->{$key} = $child->{$table}->{$key};
285 32 ▶ weaken($rows->{$key}) unless $self->{+RETAIN};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"retain"} 27 5 32
286 }
287 }
288 61 return;
289 }
290  
291 =over 4
292  
293 =item $lookup->rollback_level
294  
295 Rolls back the innermost savepoint, or the root transaction when no savepoint
296 is open: discards the top level, unmarks the hashes it marked deleted, then
297 marks the hashes it inserted vivified. Croaks when no transaction is open.
298  
299 =back
300  
301 =cut
302  
303 107 ▶sub rollback_level {
Subroutine rollback_level: called CC=8 SCAR=20.8
Pod documented
304 107 my $self = shift;
305 107 ▶ croak "No transaction is open" unless $self->in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_txn 1 106 107
306 106 pop @{$self->{+STACK}};
307 106 ▶ my $mark = pop @{$self->{+MARKS}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mark = pop @{$$self{"marks"};} or return 44 62 106
308 44 ▶ for my $data (@{$mark->{deleted} // []}) {
Condition100%2/2
$mark->{"deleted"} // []
l!l
1628
MC/DC100%1/1
$mark->{"deleted"} // []
$mark->{"deleted"}
Truth table100%2/2
A
A= $mark->{"deleted"}
Aresult
11
00
309 18 delete $STATE{$data}->{deleted};
310 18 ▶ delete $STATE{$data} unless %{$STATE{$data}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$STATE{$data};} 18 0 18
311 }
312 44 ▶ $STATE{$_}->{vivified} = 1 for values %{$mark->{inserted} // {}};
Condition100%2/2
$mark->{"inserted"} // {}
l!l
1430
MC/DC100%1/1
$mark->{"inserted"} // {}
$mark->{"inserted"}
Truth table100%2/2
A
A= $mark->{"inserted"}
Aresult
11
00
313 44 return;
314 }
315  
316 =over 4
317  
318 =item $bool = $lookup->is_canon($table, \%row_data)
319  
320 True when the hash is the entry for its primary key. False when it is not, or
321 no entry exists.
322  
323 =back
324  
325 =cut
326  
327 226 ▶sub is_canon {
Subroutine is_canon: called CC=3 SCAR=11.0
Pod documented
328 226 my $self = shift;
329 226 my ($table, $data) = @_;
330 226 ▶ my $canon = $self->lookup($table, $data) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $canon = $self->lookup($table, $data) or return 0 165 57 222
331 165 ▶ return $canon == $data ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$canon == $data ? : 137 28 165
332 }
333  
334 =over 4
335  
336 =item $row_data = $lookup->lookup($table, $pk)
337  
338 The canon hash for C<$pk>, or undef.
339  
340 =back
341  
342 =cut
343  
344 526 ▶sub lookup {
Subroutine lookup: called CC=1 SCAR=0.0
Pod documented
345 526 my $self = shift;
346 526 my ($table, $pk) = @_;
347 526 return $self->_find($table, $self->_key($table, $pk));
348 }
349  
350 =over 4
351  
352 =item $row_data = $lookup->ingest($table, \%row_data)
353  
354 Takes in a row read from the database and returns the canon hash.
355  
356 When an entry exists and agrees on every field both hashes hold (NULL and
357 missing differ), new fields are added to it and it is returned. Otherwise the
358 given hash becomes the entry and is returned.
359  
360 Inside a transaction, a key no transaction level holds is compared the same
361 way with the outside level's entry, which is reused in the top level when it
362 agrees.
363  
364 =back
365  
366 =cut
367  
368 449 ▶sub ingest {
Subroutine ingest: called CC=9 SCAR=22.0
Pod documented
369 449 my $self = shift;
370 449 my ($table, $data) = @_;
371 449 my $key = $self->_key($table, $data);
372  
373 449 my $canon = $self->_find($table, $key);
374 449 ▶ 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
4271012
not $canon and @{$self->{"stack"};} > 1
!ll&&!rl&&r
13729022
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
375 449 ▶ return $self->_store($table, $data, $key) unless $canon;
Branch: 1 branch
BranchTrueFalseTotal
unless $canon 300 149 449
376  
377 149 my @new;
378 149 for my $field (keys %$data) {
379 432 ▶ if (exists $canon->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $canon->{$field}) 422 10 432
380 422 ▶ next unless $self->differ($canon->{$field}, $data->{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->differ($canon->{$field}, $data->{$field}) 412 10 422
381 10 return $self->_store($table, $data, $key);
382 }
383 10 push @new => $field;
384 }
385  
386 139 $canon->{$_} = $data->{$_} for @new;
387 139 ▶ $self->{+STACK}->[-1]->{$table}->{$key} = $canon if $reuse;
Branch: 1 branch
BranchTrueFalseTotal
if $reuse 10 129 139
388 139 return $canon;
389 }
390  
391 =over 4
392  
393 =item $bool = DBIx::QuickORM::LookupTable->differ($old, $new)
394  
395 True when two raw field values differ: they compare unequal as strings, or
396 one is NULL (undef) and the other is not. NULL equals only NULL.
397  
398 C<ingest> uses it to tell whether a row read from the database changed, and a
399 row uses it to decide which inflated values to keep when its row data is
400 replaced. It may be called on the class or on an instance.
401  
402 =back
403  
404 =cut
405  
406 434 ▶sub differ {
Subroutine differ: called CC=6 SCAR=17.9
Pod documented
407 434 shift;
408 434 my ($x, $y) = @_;
409 434 ▶ return 0 if !defined($x) && !defined($y);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x or defined $y 20 414 434
Condition100%3/3
defined $x or defined $y
l!l&&r!l&&!r
412220
MC/DC100%2/2
defined $x or defined $y
defined $x defined $y
Truth table100%3/3
A or B
A= defined $xB= defined $y
ABresult
1X1
011
000
410 414 ▶ return 1 if !defined($x) || !defined($y);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y 5 409 414
Condition100%3/3
defined $x and defined $y
!ll&&!rl&&r
23409
MC/DC100%2/2
defined $x and defined $y
defined $x defined $y
Truth table100%3/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
411 409 ▶ return $x eq $y ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$x eq $y ? : 397 12 409
412 }
413  
414 =over 4
415  
416 =item $row_data = $lookup->store($table, \%row_data)
417  
418 Makes the hash the entry for its primary key, replacing any other, and returns
419 it. This is the write path.
420  
421 Inside a transaction the key is removed from the outside level. To re-key
422 after a primary key change, C<forget> the old key first.
423  
424 =back
425  
426 =cut
427  
428 10381 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod documented
429 10381 my $self = shift;
430 10381 my ($table, $data) = @_;
431 10381 return $self->_store($table, $data, $self->_key($table, $data));
432 }
433  
434 =over 4
435  
436 =item $lookup->clear
437  
438 =item $lookup->clear($table)
439  
440 =item $lookup->clear($table, $pk)
441  
442 Clears everything, one table, or one row from the top level only. Inside a
443 transaction it also marks them as cleared there, so the levels below no longer
444 show them. A savepoint rollback brings them back. A root commit clears them
445 from the outside level, and a root rollback leaves the outside level as it
446 was.
447  
448 =back
449  
450 =cut
451  
452 212 ▶sub clear {
Subroutine clear: called CC=8 SCAR=20.8
Pod documented
453 212 my $self = shift;
454 212 my ($table, $pk) = @_;
455 212 my $stack = $self->{+STACK};
456 212 my $top = $stack->[-1];
457  
458 # Inside a transaction a mark hides the same from the levels below, and a root commit applies it outside.
459 212 ▶ my $mark = @$stack > 1 ? ($self->{+MARKS}->[-1] //= {}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$stack > 1 ? : 62 150 212
Condition100%2/2
$self->{"marks"}[-1] //= {}
l!l
1547
MC/DC100%1/1
$self->{"marks"}[-1] //= {}
$self->{"marks"}[-1]
Truth table100%2/2
A
A= $self->{"marks"}[-1]
Aresult
11
00
460 212 ▶ if (!defined $table) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $table) { } 29 183 212
elsif (@_ < 2) { } 21 162 183
461 29 $stack->[-1] = {};
462 29 ▶ $mark->{all} = 1 if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 8 21 29
463 }
464 elsif (@_ < 2) {
465 21 delete $top->{$table};
466 21 ▶ $mark->{tables}->{$table} = 1 if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 11 10 21
467 }
468 else {
469 162 my $key = $self->_key($table, $pk);
470 161 delete $top->{$table}->{$key};
471 161 ▶ $mark->{keys}->{$table}->{$key} = 1 if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 43 118 161
472 }
473 211 return;
474 }
475  
476 =over 4
477  
478 =item $lookup->forget
479  
480 =item $lookup->forget($table)
481  
482 =item $lookup->forget($table, $pk)
483  
484 What C<clear> does, and inside a transaction the same from the outside level.
485 A root commit then merges what the transaction holds at that point (rows
486 read or written again after the forget). A root rollback brings back nothing.
487  
488 This is how C<clear_cache>, a delete, a primary key change, a row found gone,
489 or a bulk update or delete drops entries.
490  
491 =back
492  
493 =cut
494  
495 191 ▶sub forget {
Subroutine forget: called CC=2 SCAR=6.9
Pod documented
496 191 my $self = shift;
497 191 my ($table, @pk) = @_;
498 191 $self->clear(@_);
499 191 ▶ $self->_touch($table, @pk ? $self->_key($table, $pk[0]) : ());
Branch: 1 branch
BranchTrueFalseTotal
@pk ? : 149 42 191
500 191 return;
501 }
502  
503 =over 4
504  
505 =item $state = DBIx::QuickORM::LookupTable->row_state(\%row_data)
506  
507 C<'deleted'>, C<'vivified'>, or undef for a hash with neither mark (see
508 L</Row state>). It may be called on the class or on an instance.
509  
510 =back
511  
512 =cut
513  
514 414 ▶sub row_state {
Subroutine row_state: called CC=5 SCAR=16.1
Pod documented
515 414 ▶ my $state = $STATE{$_[1]} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $STATE{$_[1]} or return undef 146 268 414
516 146 my $del = $state->{deleted};
517 146 ▶ return $_[0]->row_state($del) if ref $del; # a copy follows the hash it was copied from
Branch: 1 branch
BranchTrueFalseTotal
if ref $del 19 127 146
518 127 ▶ return $del ? 'deleted' : $state->{vivified} ? 'vivified' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$del ? : 81 46 127
$state->{'vivified'} ? : 46 0 46
519 }
520  
521 =over 4
522  
523 =item DBIx::QuickORM::LookupTable->mark_vivified(\%row_data)
524  
525 Marks a hash vivified (built by C<vivify>). No rollback undoes it.
526  
527 =back
528  
529 =cut
530  
531 7 ▶sub mark_vivified { $STATE{$_[1]}->{vivified} = 1; return }
Subroutine mark_vivified: called CC=1 SCAR=0.0
Pod documented
532  
533 =over 4
534  
535 =item DBIx::QuickORM::LookupTable->copy_row_state(\%from, \%to)
536  
537 Gives a copy of a row hash the original's state. A deleted copy follows the
538 hash first marked deleted, so a rollback that unmarks that hash unmarks the
539 copy too. It may be called on the class or on an instance.
540  
541 =back
542  
543 =cut
544  
545 11 ▶sub copy_row_state {
Subroutine copy_row_state: called CC=5 SCAR=16.1
Pod documented
546 11 my (undef, $from, $to) = @_;
547 11 ▶ my $state = $STATE{$from} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $STATE{$from} or return 10 1 11
548 10 ▶ $STATE{$to}->{vivified} = 1 if $state->{vivified};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"vivified"} 2 8 10
549 10 ▶ $STATE{$to}->{deleted} = ref($state->{deleted}) ? $state->{deleted} : $from if $state->{deleted};
Branch: 2 branches
BranchTrueFalseTotal
if $state->{"deleted"} 8 2 10
ref $state->{'deleted'} ? : 1 7 8
550 10 return;
551 }
552  
553 =over 4
554  
555 =item $lookup->mark_deleted(@row_data)
556  
557 Marks each hash deleted, unless it already reads as deleted. Inside a
558 transaction the top level records the hashes it newly marked, so rolling that
559 level back unmarks them.
560  
561 A copy that follows a hash a rollback unmarked is marked in its own right.
562  
563 =back
564  
565 =cut
566  
567 67 ▶sub mark_deleted {
Subroutine mark_deleted: called CC=7 SCAR=19.5
Pod documented
568 67 my $self = shift;
569 67 ▶ my $level = $self->in_txn ? ($self->{+MARKS}->[-1] //= {}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->in_txn ? : 23 44 67
Condition100%2/2
$self->{"marks"}[-1] //= {}
l!l
176
MC/DC100%1/1
$self->{"marks"}[-1] //= {}
$self->{"marks"}[-1]
Truth table100%2/2
A
A= $self->{"marks"}[-1]
Aresult
11
00
570 67 for my $data (@_) {
571 92 ▶ next if ($self->row_state($data) // '') eq 'deleted';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->row_state($data) // "") eq "deleted" 29 63 92
Condition100%2/2
$self->row_state($data) // ""
l!l
2963
MC/DC100%1/1
$self->row_state($data) // ""
$self->row_state($data)
Truth table100%2/2
A
A= $self->row_state($data)
Aresult
11
00
572 63 $STATE{$data}->{deleted} = 1; # also replaces a copy's stale follow of an unmarked hash
573 63 ▶ push @{$level->{deleted}} => $data if $level;
Branch: 1 branch
BranchTrueFalseTotal
if $level 25 38 63
574 }
575 67 return;
576 }
577  
578 =over 4
579  
580 =item $lookup->mark_inserted(@row_data)
581  
582 Records each hash as inserted in the top level, so rolling that level back
583 marks it vivified. Does nothing outside a transaction.
584  
585 =back
586  
587 =cut
588  
589 159 ▶sub mark_inserted {
Subroutine mark_inserted: called CC=5 SCAR=16.1
Pod documented
590 159 my $self = shift;
591 159 ▶ return unless $self->in_txn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_txn 117 42 159
592 42 ▶ my $set = ($self->{+MARKS}->[-1] //= {})->{inserted} //= {};
Condition100%4/4
+($self->{"marks"}[-1] //= {})->{"inserted"} //= {}
l!l
537
$self->{"marks"}[-1] //= {}
l!l
537
MC/DC100%1/1
+($self->{"marks"}[-1] //= {})->{"inserted"} //= {}
+($self->{"marks"}[-1] //= {})->{"inserted"}
MC/DC100%1/1
$self->{"marks"}[-1] //= {}
$self->{"marks"}[-1]
Truth table100%2/2
A
A= +($self->{"marks"}[-1] //= {})->{"inserted"}
Aresult
11
00
Truth table100%2/2
A
A= $self->{"marks"}[-1]
Aresult
11
00
593 42 $set->{refaddr($_)} = $_ for @_;
594 42 return;
595 }
596  
597 =over 4
598  
599 =item $lookup->carry_inserted(\%old, \%new)
600  
601 For a write or refresh that gave a row new row data: when an open level
602 recorded the old hash as inserted, it records the new one too, so a rollback
603 of that level marks both vivified. Does nothing outside a transaction.
604  
605 =back
606  
607 =cut
608  
609 137 ▶sub carry_inserted {
Subroutine carry_inserted: called CC=8 SCAR=20.8
Pod documented
610 137 my $self = shift;
611 137 my ($old, $new) = @_;
612 137 ▶ return unless $self->in_txn && $new && $old != $new;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_txn and $new and $old != $new 115 22 137
Condition83%5/6
$self->in_txn and $new and $old != $new
!ll&&!rl&&r
112322
$self->in_txn and $new
!ll&&!rl&&r
112025
MC/DC66%2/3
$self->in_txn and $new and $old != $new
$self->in_txn $new $old != $new
Truth table75%3/4
A and B and C
A= $self->in_txnB= $newC= $old != $new
ABCresult
0XX0
10X0
1100
1111
613 22 my $id = refaddr($old);
614 22 for my $mark (@{$self->{+MARKS}}) {
615 52 ▶ my $set = $mark && $mark->{inserted} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $set = $mark && $mark->{"inserted"} or next 15 37 52
Condition100%3/3
$mark && $mark->{'inserted'}
!ll&&!rl&&r
29815
MC/DC100%2/2
$mark && $mark->{'inserted'}
$mark $mark->{'inserted'}
Truth table100%3/3
A && B
A= $markB= $mark->{'inserted'}
ABresult
0X0
100
111
616 15 ▶ $set->{refaddr($new)} = $new if $set->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $set->{$id} 12 3 15
617 }
618 22 return;
619 }
620  
621 =over 4
622  
623 =item $lookup->gone($table, \%key, \%row_data)
624  
625 Forgets a row the database no longer has and marks its entry, and the row
626 data when given, deleted.
627  
628 Inside a transaction the outside level's entry is marked too, and a rollback
629 unmarks it.
630  
631 =back
632  
633 =cut
634  
635 59 ▶sub gone {
Subroutine gone: called CC=2 SCAR=6.9
Pod documented
636 59 my $self = shift;
637 59 my ($table, $key, $data) = @_;
638 59 my $canon = $self->lookup($table, $key);
639  
640 # A lookup inside a transaction never reads the outside level, so its held hash is fetched directly before forget drops it.
641 59 ▶ my $outside = $self->in_txn ? $self->_get(0, $table, $self->_key($table, $key)) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->in_txn ? : 16 43 59
642  
643 59 $self->forget($table, $key);
644 59 $self->mark_deleted(grep { $_ } $data, $canon, $outside);
645 }
646  
647 =over 4
648  
649 =item $lookup->clear_txn_levels
650  
651 Empties every level above the outside level, keeping the number of levels.
652  
653 For a transaction whose outcome is unknown, after which no row held from
654 inside it can be trusted. The outside level is kept: keys the transaction
655 touched are already gone from it.
656  
657 =back
658  
659 =cut
660  
661 7 ▶sub clear_txn_levels {
Subroutine clear_txn_levels: called CC=3 SCAR=11.0
Pod documented
662 7 my $self = shift;
663 7 my $stack = $self->{+STACK};
664 7 $_ = {} for @$stack[1 .. $#$stack];
665 7 $_ = undef for @{$self->{+MARKS}}[1 .. $#$stack];
666 7 return;
667 }
668  
669 =head1 PRIVATE METHODS
670  
671 =over 4
672  
673 =item $key = $lookup->_key($table, \%pk)
674  
675 The key string for the key fields in the hash, typed fields through their
676 type's C<canonical_key_value>. Croaks as the DESCRIPTION says.
677  
678 =back
679  
680 =cut
681  
682 11683 ▶sub _key {
Subroutine _key: called CC=14 SCAR=26.5
683 11683 my $self = shift;
684 11683 my ($table, $pk) = @_;
685  
686 11683 my $map = $self->{+DB_MAP};
687 11683 ▶ my $t = $map->table($table) || $map->pk($table); # pk() croaks for a table not in the map
Condition66%2/3
$map->table($table) || $map->pk($table)
l!l&&r!l&&!r
1168102
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
688 11681 my $fields = $t->{pk};
689 11681 ▶ confess "The table '$table' has no primary key" unless $fields && @$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields and @$fields 2 11679 11681
Condition66%2/3
$fields and @$fields
!ll&&!rl&&r
2011679
MC/DC50%1/2
$fields and @$fields
$fields @$fields
Truth table66%2/3
A and B
A= $fieldsB= @$fields
ABresult
0X0
100
111
690 11679 my $types = $t->{pk_types};
691  
692 # The common case: one untyped key field, keyed by its value.
693 11679 ▶ if (@$fields == 1 && !$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fields == 1 and not $types) 11472 207 11679
Condition100%3/3
@$fields == 1 and not $types
!ll&&!rl&&r
7613111472
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
694 11472 ▶ 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
114693
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
695 11469 ▶ 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 11468 11469
696 11468 return "$v";
697 }
698  
699 207 ▶ 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
2821
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
700 206 for my $i (0 .. $#vals) {
701 281 ▶ my $type = $types && $types->[$i];
Condition100%3/3
$types && $types->[$i]
!ll&&!rl&&r
9627158
MC/DC100%2/2
$types && $types->[$i]
$types $types->[$i]
Truth table100%3/3
A && B
A= $typesB= $types->[$i]
ABresult
0X0
100
111
702 281 ▶ if ($type) { $vals[$i] = $type->canonical_key_value($vals[$i], $map->field($table, $fields->[$i]), $self->{+CONNECTION}) }
Branch: 2 branches
BranchTrueFalseTotal
if ($type) { } 158 123 281
elsif (ref $vals[$i]) { } 0 123 123
703 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" }
704 }
705  
706 206 ▶ return "$vals[0]" if @vals == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @vals == 1 131 75 206
707 75 return join '' => map { length($_) . ':' . $_ } @vals;
708 }
709  
710 =over 4
711  
712 =item $row_data = $lookup->_store($table, \%row_data, $key)
713  
714 C<store> with the key string already built, so C<ingest> builds it once.
715  
716 =back
717  
718 =cut
719  
720 10690 ▶sub _store {
Subroutine _store: called CC=4 SCAR=13.9
721 10690 my $self = shift;
722 10690 my ($table, $data, $key) = @_;
723 10690 my $stack = $self->{+STACK};
724 10690 $stack->[-1]->{$table}->{$key} = $data;
725  
726 10690 ▶ if (@$stack > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$stack > 1) { } 10082 608 10690
elsif (not $self->{'retain'}) { } 559 49 608
727 10082 my $outside = $stack->[0]->{$table};
728 10082 ▶ delete $outside->{$key} if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 58 10024 10082
729 }
730 elsif (!$self->{+RETAIN}) {
731 559 weaken($stack->[-1]->{$table}->{$key});
732 }
733  
734 10690 return $data;
735 }
736  
737 =over 4
738  
739 =item $row_data = $lookup->_find($table, $key)
740  
741 The canon hash for the key string, or undef. Outside a transaction it is the
742 outside level's entry. Inside one, the levels are read from the top down to
743 the root transaction level, never the outside level. The first level that
744 holds the key, or marks it, its table, or everything as cleared, decides.
745  
746 =back
747  
748 =cut
749  
750 968 ▶sub _find {
Subroutine _find: called CC=12 SCAR=24.8
751 968 my $self = shift;
752 968 my ($table, $key) = @_;
753 968 my $stack = $self->{+STACK};
754 968 ▶ return $self->_get(0, $table, $key) unless @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack > 1 840 128 968
755  
756 128 my $marks = $self->{+MARKS};
757 128 ▶ for (my $i = $#$stack; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 148 53 201
758 148 my $rows = $stack->[$i]->{$table};
759 148 ▶ return $rows->{$key} if $rows && exists $rows->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $rows and exists $rows->{$key} 66 82 148
Condition100%3/3
$rows and exists $rows->{$key}
!ll&&!rl&&r
73966
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
760 82 ▶ my $mark = $marks->[$i] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $mark = $marks->[$i] or next 14 68 82
761 14 ▶ my $keys = $mark->{keys} && $mark->{keys}->{$table};
Condition100%3/3
$mark->{'keys'} && $mark->{'keys'}{$table}
!ll&&!rl&&r
617
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
762 14 ▶ 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 5 14
Condition100%12/12
$mark->{"all"} or $mark->{"tables"} and $mark->{"tables"}{$table} or $keys and $keys->{$key}
l!l&&r!l&&!r
635
$mark->{"all"} or $mark->{"tables"} and $mark->{"tables"}{$table}
l!l&&r!l&&!r
338
$mark->{"tables"} and $mark->{"tables"}{$table}
!ll&&!rl&&r
713
$keys and $keys->{$key}
!ll&&!rl&&r
233
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
763 }
764 53 return undef;
765 }
766  
767 =over 4
768  
769 =item $lookup->_merge(\%child, \%mark)
770  
771 A savepoint commit: applies the popped level's marks to its parent with
772 C<_apply_marks>, carrying them and its deleted and inserted hashes into the
773 parent's marks, then copies the popped level's entries into the parent.
774  
775 =back
776  
777 =cut
778  
779 20 ▶sub _merge {
Subroutine _merge: called CC=9 SCAR=22.0
780 20 my $self = shift;
781 20 my ($child, $mark) = @_;
782 20 my $parent = $self->{+STACK}->[-1];
783  
784 20 ▶ if ($mark) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark) 16 4 20
785 16 ▶ my $into = $self->{+MARKS}->[-1] //= {};
Condition100%2/2
$self->{"marks"}[-1] //= {}
l!l
79
MC/DC100%1/1
$self->{"marks"}[-1] //= {}
$self->{"marks"}[-1]
Truth table100%2/2
A
A= $self->{"marks"}[-1]
Aresult
11
00
786 16 $self->_apply_marks($parent, $mark, $into);
787 16 ▶ push @{$into->{deleted}} => @{$mark->{deleted}} if $mark->{deleted};
Branch: 1 branch
BranchTrueFalseTotal
if $mark->{"deleted"} 2 14 16
788 16 ▶ $into->{inserted}->{refaddr($_)} = $_ for values %{$mark->{inserted} // {}};
Condition100%2/2
$mark->{"inserted"} // {}
l!l
79
MC/DC100%1/1
$mark->{"inserted"} // {}
$mark->{"inserted"}
Truth table100%2/2
A
A= $mark->{"inserted"}
Aresult
11
00
789 }
790  
791 20 for my $table (keys %$child) {
792 14 ▶ my $rows = $parent->{$table} //= {};
Condition100%2/2
$parent->{$table} //= {}
l!l
77
MC/DC100%1/1
$parent->{$table} //= {}
$parent->{$table}
Truth table100%2/2
A
A= $parent->{$table}
Aresult
11
00
793 14 $rows->{$_} = $child->{$table}->{$_} for keys %{$child->{$table}};
794 }
795 20 return;
796 }
797  
798 =pod
799  
800 =over 4
801  
802 =item $lookup->_apply_marks(\%level, \%mark)
803  
804 =item $lookup->_apply_marks(\%level, \%mark, \%into)
805  
806 Clears what the marks name (everything, tables, keys) from the level, and
807 adds them to the C<\%into> marks when given.
808  
809 =back
810  
811 =cut
812  
813 52 ▶sub _apply_marks {
Subroutine _apply_marks: called CC=11 SCAR=24.0
814 52 my $self = shift;
815 52 my ($level, $mark, $into) = @_;
816  
817 52 ▶ if ($mark->{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark->{"all"}) 7 45 52
818 7 %$level = ();
819 7 ▶ $into->{all} = 1 if $into;
Branch: 1 branch
BranchTrueFalseTotal
if $into 2 5 7
820 }
821 52 ▶ for my $table (keys %{$mark->{tables} // {}}) {
Condition100%2/2
$mark->{"tables"} // {}
l!l
448
MC/DC100%1/1
$mark->{"tables"} // {}
$mark->{"tables"}
Truth table100%2/2
A
A= $mark->{"tables"}
Aresult
11
00
822 4 delete $level->{$table};
823 4 ▶ $into->{tables}->{$table} = 1 if $into;
Branch: 1 branch
BranchTrueFalseTotal
if $into 1 3 4
824 }
825 52 ▶ my $keys = $mark->{keys} // {};
Condition100%2/2
$mark->{"keys"} // {}
l!l
2131
MC/DC100%1/1
$mark->{"keys"} // {}
$mark->{"keys"}
Truth table100%2/2
A
A= $mark->{"keys"}
Aresult
11
00
826 52 for my $table (keys %$keys) {
827 21 for my $key (keys %{$keys->{$table}}) {
828 22 ▶ delete $level->{$table}->{$key} if $level->{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $level->{$table} 17 5 22
829 22 ▶ $into->{keys}->{$table}->{$key} = 1 if $into;
Branch: 1 branch
BranchTrueFalseTotal
if $into 5 17 22
830 }
831 }
832 52 return;
833 }
834  
835 =over 4
836  
837 =item $row_data = $lookup->_get($index, $table, $key)
838  
839 The entry for the key string in the level at C<$index> of the stack, or undef.
840 A freed weak entry is deleted.
841  
842 =back
843  
844 =cut
845  
846 878 ▶sub _get {
Subroutine _get: called CC=4 SCAR=13.9
847 878 my $self = shift;
848 878 my ($index, $table, $key) = @_;
849 878 ▶ my $rows = $self->{+STACK}->[$index]->{$table} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $self->{"stack"}[$index]{$table} or return undef 739 139 878
850 739 ▶ return undef unless exists $rows->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rows->{$key} 201 538 739
851 538 ▶ return $rows->{$key} // delete $rows->{$key};
Condition66%2/3
$rows->{$key} // delete $rows->{$key}
l!l&&r!l&&!r
3880150
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
852 }
853  
854 =over 4
855  
856 =item $lookup->_touch
857  
858 =item $lookup->_touch($table)
859  
860 =item $lookup->_touch($table, $key)
861  
862 Inside a transaction, removes everything, the table, or the key string from
863 the outside level.
864  
865 =back
866  
867 =cut
868  
869 191 ▶sub _touch {
Subroutine _touch: called CC=5 SCAR=16.1
870 191 my $self = shift;
871 191 my ($table, @key) = @_;
872 191 my $stack = $self->{+STACK};
873 191 ▶ return unless @$stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack > 1 141 50 191
874 50 ▶ if (!defined $table) { $stack->[0] = {}; return }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table) 3 47 50
875 47 ▶ my $rows = $stack->[0]->{$table} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $stack->[0]{$table} or return 40 7 47
876 40 ▶ if (@key) { delete $rows->{$key[0]} }
Branch: 1 branch
BranchTrueFalseTotal
if (@key) { } 33 7 40
877 7 else { delete $stack->[0]->{$table} }
878 40 return;
879 }
880  
881 259 1;
882  
883 __END__
« blib/lib/DBIx/QuickORM/Handle.pm ↑ Summary blib/lib/DBIx/QuickORM/WhereBuilder/RawSQL.pm »