blib/lib/DBIx/QuickORM/RowState.pm

filter: Statementstmt 87.7% 86 / 98 Branchbran 67.7% 42 / 62 Conditioncond 82.3% 28 / 34 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 85.0% 17 / 20 Podpod 93.3% 14 / 15 totaltotal 80.9% 200 / 247 CC 84 SCAR 19.3
CC
84
Cov
80%
carry_inserted
9.0
carry_inserted
9.0
state
9.0
CRAP 6.9
« blib/lib/DBIx/QuickORM/Dialect.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Row.pm »
1 package DBIx::QuickORM::RowState;
2 265 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 265 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 265 our $VERSION = '0.000033';
6  
7 265 ▶use Hash::Util::FieldHash qw/fieldhash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 265 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 265 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
11 <debug
12 +levels
13 265 };
14  
15 =pod
16  
17 =encoding UTF-8
18  
19 =head1 NAME
20  
21 DBIx::QuickORM::RowState - Whether a row hash is stored, kept beside it.
22  
23 =head1 DESCRIPTION
24  
25 A little state for row hashes, keyed by the hash itself (a fieldhash), never
26 stored in it. Each mark is present only when it applies:
27  
28 =over 4
29  
30 =item *
31  
32 C<deleted>: set by a row C<delete>, a C<delete_row>, and a refresh or find
33 that finds nothing in the database. A miss from the cache alone marks
34 nothing. A handle's bulk delete marks nothing, unless given C<mark_deleted>.
35  
36 =item *
37  
38 C<vivified>: the hash was built by the ORM's C<vivify>, never read from the
39 database, or its insert was rolled back.
40  
41 =back
42  
43 The marks are shared by every connection, so the class methods read and
44 write them with no instance. Each Connection also holds an instance, which
45 makes the marks follow its transactions:
46  
47 =over 4
48  
49 =item *
50  
51 A hash marked deleted inside a transaction is unmarked when that level rolls
52 back, and so is every copy made of it with C<copy>.
53  
54 =item *
55  
56 A hash inserted inside a transaction (C<mark_inserted>) is marked vivified
57 when that level rolls back. A hash inserted and then deleted in it ends up
58 vivified too, and so does the new hash of a row inserted and then updated or
59 refreshed in it (C<carry_inserted>).
60  
61 =back
62  
63 A savepoint commit carries both records to its parent. A root commit drops
64 them. The Connection's transaction code calls the level methods, for every
65 lookup table implementation alike.
66  
67 =head1 SYNOPSIS
68  
69 my $row_data = {
70 id => 1,
71 name => 'Ann',
72 };
73  
74 my $state = DBIx::QuickORM::RowState->state($row_data); # 'deleted', 'vivified', or undef
75  
76 # one of its own: a Connection's follows that Connection's transactions
77 my $rs = DBIx::QuickORM::RowState->new;
78 $rs->push_level; # a transaction or savepoint begins
79 $rs->mark_inserted($row_data);
80 $rs->rollback_level; # $row_data is now vivified
81  
82 =head1 ATTRIBUTES
83  
84 =over 4
85  
86 =item $debug = $rs->debug
87  
88 The ORM's L<DBIx::QuickORM::Debug>, or undef. With one, C<mark_deleted>
89 notes the call that marked each hash.
90  
91 =back
92  
93 =cut
94  
95 # {deleted => 1 or the hash a copy follows, vivified => 1}, each only when it applies.
96 265 fieldhash my %STATE;
97  
98 199 ▶sub init { $_[0]->{+LEVELS} = [] }
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
99  
100 =head1 PUBLIC METHODS
101  
102 These may be called on the class or on an instance.
103  
104 =over 4
105  
106 =item $state = DBIx::QuickORM::RowState->state(\%row_data)
107  
108 C<'deleted'>, C<'vivified'>, or undef for a hash with neither mark.
109  
110 =item DBIx::QuickORM::RowState->mark_vivified(\%row_data)
111  
112 Marks a hash vivified (built by C<vivify>). No rollback undoes it.
113  
114 =back
115  
116 =cut
117  
118 19 ▶sub mark_vivified { $STATE{$_[1]}->{vivified} = 1; return }
Subroutine mark_vivified: called CC=1 SCAR=0.0
Pod documented
119  
120 1124 ▶sub state {
Subroutine state: called CC=5 SCAR=21.9
Pod documented
121 0 ▶ my $state = $STATE{$_[1]} or return undef;
Subroutine state: not called CC=5 SCAR=21.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
my $state = $STATE{$_[1]} or return undef 228 896 1124
my $state = $STATE{$_[1]} or return undef 0 0 0
122 0 my $del = $state->{deleted};
123 0 ▶ return $_[0]->state($del) if ref $del; # a copy follows the hash it was copied from
Branch: 2 branches
BranchTrueFalseTotal
if ref $del 20 208 228
if ref $del 0 0 0
124 0 ▶ return $del ? 'deleted' : $state->{vivified} ? 'vivified' : undef;
Branch: 4 branches
BranchTrueFalseTotal
$del ? : 129 79 208
$state->{'vivified'} ? : 79 0 79
$del ? : 0 0 0
$state->{'vivified'} ? : 0 0 0
125 }
126  
127 =over 4
128  
129 =item $origin = DBIx::QuickORM::RowState->deleted_origin(\%row_data)
130  
131 For a hash that reads as deleted, the hash first marked: itself, or for a copy,
132 the hash it follows.
133  
134 =back
135  
136 =cut
137  
138 5 ▶sub deleted_origin {
Subroutine deleted_origin: called CC=3 SCAR=15.3
Pod documented
139 0 ▶ my $data = $_[1];
Subroutine deleted_origin: not called CC=3 SCAR=15.3
Pod documented
140 0 ▶ $data = $STATE{$data}->{deleted} while $STATE{$data} && ref $STATE{$data}->{deleted};
Branch: 2 branches
BranchTrueFalseTotal
$STATE{$data} and ref $STATE{$data}{"deleted"} and do { $data = $STATE{$data}{"deleted"} } 1 5 6
$STATE{$data} and ref $STATE{$data}{"deleted"} and do { $data = $STATE{$data}{"deleted"} } 0 0 0
Condition33%2/6
$STATE{$data} and ref $STATE{$data}{"deleted"}
!ll&&!rl&&r
051
$STATE{$data} and ref $STATE{$data}{"deleted"}
!ll&&!rl&&r
000
MC/DC50%1/2
$STATE{$data} and ref $STATE{$data}{"deleted"}
$STATE{$data} ref $STATE{$data}{"deleted"}
MC/DC0%0/2
$STATE{$data} and ref $STATE{$data}{"deleted"}
$STATE{$data} ref $STATE{$data}{"deleted"}
Truth table66%2/3
A and B
A= $STATE{$data}B= ref $STATE{$data}{"deleted"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $STATE{$data}B= ref $STATE{$data}{"deleted"}
ABresult
0X0
100
111
141 0 return $data;
142 }
143  
144 =over 4
145  
146 =item DBIx::QuickORM::RowState->copy(\%from, \%to)
147  
148 Gives a copy of a row hash the original's state.
149  
150 my $rs = DBIx::QuickORM::RowState->new;
151 my $data = {
152 id => 1,
153 name => 'Ann',
154 };
155  
156 $rs->push_level;
157 $rs->mark_deleted($data);
158  
159 my $copy = {%$data};
160 DBIx::QuickORM::RowState->copy($data, $copy);
161 my $origin = DBIx::QuickORM::RowState->deleted_origin($copy); # $data
162 my $gone = DBIx::QuickORM::RowState->state($copy); # 'deleted'
163  
164 $rs->rollback_level;
165 my $state = DBIx::QuickORM::RowState->state($copy); # undef: unmarked with $data
166  
167 A deleted copy follows the hash first marked deleted, so a rollback that
168 unmarks that hash unmarks the copy too.
169  
170 =back
171  
172 =cut
173  
174 11 ▶sub copy {
Subroutine copy: called CC=5 SCAR=20.9
Pod documented
175 0 ▶ my (undef, $from, $to) = @_;
Subroutine copy: not called CC=5 SCAR=20.9
Pod documented
176 0 ▶ my $state = $STATE{$from} or return;
Branch: 2 branches
BranchTrueFalseTotal
my $state = $STATE{$from} or return 10 1 11
my $state = $STATE{$from} or return 0 0 0
177 0 ▶ $STATE{$to}->{vivified} = 1 if $state->{vivified};
Branch: 2 branches
BranchTrueFalseTotal
if $state->{"vivified"} 2 8 10
if $state->{"vivified"} 0 0 0
178 0 ▶ $STATE{$to}->{deleted} = ref($state->{deleted}) ? $state->{deleted} : $from if $state->{deleted};
Branch: 4 branches
BranchTrueFalseTotal
if $state->{"deleted"} 8 2 10
ref $state->{'deleted'} ? : 1 7 8
if $state->{"deleted"} 0 0 0
ref $state->{'deleted'} ? : 0 0 0
179 0 return;
180 }
181  
182 =pod
183  
184 The rest are instance methods.
185  
186 =over 4
187  
188 =item $rs->push_level
189  
190 A transaction or savepoint began.
191  
192 =item $rs->commit_level
193  
194 The innermost one committed: a savepoint carries its records to its parent,
195 a root transaction drops them.
196  
197 my $rs = DBIx::QuickORM::RowState->new;
198 my $data = {
199 id => 1,
200 name => 'Ann',
201 };
202  
203 $rs->push_level; # a transaction
204 $rs->push_level; # a savepoint
205 $rs->mark_inserted($data);
206 $rs->commit_level; # the savepoint hands its record to the transaction
207 $rs->rollback_level; # so the transaction's rollback marks $data vivified
208  
209 =item $rs->rollback_level
210  
211 The innermost one rolled back: unmarks the hashes it marked deleted, then
212 marks the hashes it inserted vivified.
213  
214 =item $rs->clear_levels
215  
216 For a transaction whose outcome is unknown: forgets every level's records,
217 keeping the number of levels, so nothing is unmarked or marked later.
218  
219 my $rs = DBIx::QuickORM::RowState->new;
220 my $data = {
221 id => 1,
222 name => 'Ann',
223 };
224  
225 $rs->push_level;
226 $rs->mark_inserted($data);
227 $rs->clear_levels; # the outcome is unknown
228 $rs->rollback_level; # marks nothing: $data has no state
229  
230 =back
231  
232 =cut
233  
234 193 ▶sub push_level { push @{$_[0]->{+LEVELS}} => undef; return }
Subroutine push_level: called CC=1 SCAR=0.0
Pod documented
235  
236 7 ▶sub clear_levels { $_ = undef for @{$_[0]->{+LEVELS}}; return }
Subroutine clear_levels: called CC=2 SCAR=6.9
Pod documented
237  
238 81 ▶sub commit_level {
Subroutine commit_level: called CC=7 SCAR=19.5
Pod documented
239 81 my $self = shift;
240 81 my $levels = $self->{+LEVELS};
241 81 ▶ my $mark = pop @$levels or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mark = pop @$levels or return 34 47 81
242 34 ▶ return unless @$levels;
Branch: 1 branch
BranchTrueFalseTotal
unless @$levels 26 8 34
243  
244 8 ▶ my $into = $levels->[-1] //= {};
Condition100%2/2
$levels->[-1] //= {}
l!l
44
MC/DC100%1/1
$levels->[-1] //= {}
$levels->[-1]
Truth table100%2/2
A
A= $levels->[-1]
Aresult
11
00
245 8 ▶ push @{$into->{deleted}} => @{$mark->{deleted}} if $mark->{deleted};
Branch: 1 branch
BranchTrueFalseTotal
if $mark->{"deleted"} 2 6 8
246 8 ▶ $into->{inserted}->{refaddr($_)} = $_ for values %{$mark->{inserted} // {}};
Condition100%2/2
$mark->{"inserted"} // {}
l!l
62
MC/DC100%1/1
$mark->{"inserted"} // {}
$mark->{"inserted"}
Truth table100%2/2
A
A= $mark->{"inserted"}
Aresult
11
00
247 8 return;
248 }
249  
250 112 ▶sub rollback_level {
Subroutine rollback_level: called CC=7 SCAR=19.5
Pod documented
251 112 my $self = shift;
252 112 ▶ my $mark = pop @{$self->{+LEVELS}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mark = pop @{$$self{"levels"};} or return 34 78 112
253 34 ▶ for my $data (@{$mark->{deleted} // []}) {
Condition100%2/2
$mark->{"deleted"} // []
l!l
1717
MC/DC100%1/1
$mark->{"deleted"} // []
$mark->{"deleted"}
Truth table100%2/2
A
A= $mark->{"deleted"}
Aresult
11
00
254 19 delete $STATE{$data}->{deleted};
255 19 ▶ delete $STATE{$data} unless %{$STATE{$data}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$STATE{$data};} 19 0 19
256 }
257 34 ▶ $STATE{$_}->{vivified} = 1 for values %{$mark->{inserted} // {}};
Condition100%2/2
$mark->{"inserted"} // {}
l!l
1915
MC/DC100%1/1
$mark->{"inserted"} // {}
$mark->{"inserted"}
Truth table100%2/2
A
A= $mark->{"inserted"}
Aresult
11
00
258 34 return;
259 }
260  
261 =over 4
262  
263 =item $rs->mark_deleted(@row_data)
264  
265 Marks each hash deleted, unless it already reads as deleted. Inside a
266 transaction the top level records the hashes it newly marked, so rolling that
267 level back unmarks them. A copy that follows a hash a rollback unmarked is
268 marked in its own right.
269  
270 =back
271  
272 =cut
273  
274 143 ▶sub mark_deleted {
Subroutine mark_deleted: called CC=8 SCAR=20.8
Pod documented
275 143 my $self = shift;
276 143 my $levels = $self->{+LEVELS};
277 143 ▶ my $level = @$levels ? ($levels->[-1] //= {}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$levels ? : 37 106 143
Condition100%2/2
$levels->[-1] //= {}
l!l
1423
MC/DC100%1/1
$levels->[-1] //= {}
$levels->[-1]
Truth table100%2/2
A
A= $levels->[-1]
Aresult
11
00
278 143 for my $data (@_) {
279 118 ▶ next if ($self->state($data) // '') eq 'deleted';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state($data) // "") eq "deleted" 36 82 118
Condition100%2/2
$self->state($data) // ""
l!l
3682
MC/DC100%1/1
$self->state($data) // ""
$self->state($data)
Truth table100%2/2
A
A= $self->state($data)
Aresult
11
00
280 82 $STATE{$data}->{deleted} = 1; # also replaces a copy's stale follow of an unmarked hash
281 82 ▶ $self->{+DEBUG}->note_deleted($data) if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 5 77 82
282 82 ▶ push @{$level->{deleted}} => $data if $level;
Branch: 1 branch
BranchTrueFalseTotal
if $level 27 55 82
283 }
284 143 return;
285 }
286  
287 =over 4
288  
289 =item $rs->mark_inserted(@row_data)
290  
291 Records each hash as inserted in the top level, so rolling that level back
292 marks it vivified. Does nothing outside a transaction.
293  
294 =item $rs->carry_inserted(\%old, \%new)
295  
296 For a write or refresh that gave a row new row data: when an open level
297 recorded the old hash as inserted, it records the new one too, so a rollback
298 of that level marks both vivified.
299  
300 my $rs = DBIx::QuickORM::RowState->new;
301 my $old = {
302 id => 1,
303 name => 'Ann',
304 };
305  
306 $rs->push_level;
307 $rs->mark_inserted($old);
308  
309 # an update gave the row new row data
310 my $new = {
311 id => 1,
312 name => 'Annie',
313 };
314 $rs->carry_inserted($old, $new);
315  
316 $rs->rollback_level; # both are now vivified
317  
318 Does nothing outside a transaction.
319  
320 =back
321  
322 =cut
323  
324 208 ▶sub mark_inserted {
Subroutine mark_inserted: called CC=5 SCAR=16.1
Pod documented
325 208 my $self = shift;
326 208 my $levels = $self->{+LEVELS};
327 208 ▶ return unless @$levels;
Branch: 1 branch
BranchTrueFalseTotal
unless @$levels 157 51 208
328 51 ▶ my $set = ($levels->[-1] //= {})->{inserted} //= {};
Condition100%4/4
+($levels->[-1] //= {})->{"inserted"} //= {}
l!l
546
$levels->[-1] //= {}
l!l
645
MC/DC100%1/1
+($levels->[-1] //= {})->{"inserted"} //= {}
+($levels->[-1] //= {})->{"inserted"}
MC/DC100%1/1
$levels->[-1] //= {}
$levels->[-1]
Truth table100%2/2
A
A= +($levels->[-1] //= {})->{"inserted"}
Aresult
11
00
Truth table100%2/2
A
A= $levels->[-1]
Aresult
11
00
329 51 $set->{refaddr($_)} = $_ for @_;
330 51 return;
331 }
332  
333 255 ▶sub carry_inserted {
Subroutine carry_inserted: called CC=9 SCAR=22.0
Pod documented
334 255 my $self = shift;
335 255 my ($old, $new) = @_;
336 255 my $levels = $self->{+LEVELS};
337 255 ▶ return unless @$levels && $old && $new && $old != $new;
Branch: 1 branch
BranchTrueFalseTotal
unless @$levels and $old and $new and $old != $new 220 35 255
Condition77%7/9
@$levels and $old and $new and $old != $new
!ll&&!rl&&r
217335
@$levels and $old and $new
!ll&&!rl&&r
217038
@$levels and $old
!ll&&!rl&&r
217038
MC/DC50%2/4
@$levels and $old and $new and $old != $new
@$levels $old $new $old != $new
Truth table60%3/5
A and B and C and D
A= @$levelsB= $oldC= $newD= $old != $new
ABCDresult
0XXX0
10XX0
110X0
11100
11111
338 35 my $id = refaddr($old);
339 35 for my $mark (@$levels) {
340 44 ▶ my $set = $mark && $mark->{inserted} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $set = $mark && $mark->{"inserted"} or next 21 23 44
Condition100%3/3
$mark && $mark->{'inserted'}
!ll&&!rl&&r
22121
MC/DC100%2/2
$mark && $mark->{'inserted'}
$mark $mark->{'inserted'}
Truth table100%3/3
A && B
A= $markB= $mark->{'inserted'}
ABresult
0X0
100
111
341 21 ▶ $set->{refaddr($new)} = $new if $set->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $set->{$id} 17 4 21
342 }
343 35 return;
344 }
345  
346 265 1;
347  
348 __END__
« blib/lib/DBIx/QuickORM/Dialect.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Row.pm »