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

filter: Statementstmt 67.5% 27 / 40 Branchbran 50.0% 10 / 20 Conditioncond 50.0% 17 / 34 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 75.0% 6 / 8 Podpod 100% 5 / 5 totaltotal 58.9% 76 / 129 CC 23 SCAR 23.5
CC
23
Cov
57%
canon_reason
13.1
canon_reason
13.1
differ
10.5
CRAP 10.5
« blib/lib/DBIx/QuickORM/LookupTable/Identity.pm ↑ Summary blib/lib/DBIx/QuickORM/Command/GenerateSQL.pm »
1 package DBIx::QuickORM::Role::LookupTable;
2 264 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 264 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 264 our $VERSION = '0.000033';
6  
7 264 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =encoding UTF-8
12  
13 =head1 NAME
14  
15 DBIx::QuickORM::Role::LookupTable - What a Connection needs from a lookup table.
16  
17 =head1 DESCRIPTION
18  
19 A lookup table decides which row hashes are canonical, and may cache rows by
20 primary key. Each Connection builds one, of the class the ORM's C<lookup>
21 argument names. L<DBIx::QuickORM::LookupTable> describes the implementations
22 that ship, and is the base class most of them inherit.
23  
24 The Connection calls the methods below. It reports transaction changes to
25 the lookup table (C<begin_root>, C<push_level>, C<commit_level>,
26 C<rollback_level>, C<clear_txn_levels>) after the database has changed: a
27 begin after the C<BEGIN> or C<SAVEPOINT> but before its C<txns> lists the new
28 transaction, a commit after the database commit succeeded and the
29 transaction left C<txns>, a rollback likewise, and C<clear_txn_levels> when
30 an outcome is unknown. Whether a transaction is open
31 is the Connection's to say (its C<txns>), not the lookup table's.
32  
33 Row state (deleted, vivified) is not the lookup table's either: the
34 Connection keeps it in L<DBIx::QuickORM::RowState> and marks the row data it
35 is given. A lookup table that holds hashes of its own marks those through the
36 Connection's C<row_states>.
37  
38 The lookup table must hold its connection weakly (the Connection holds the
39 lookup table), so it cannot be used after the connection is gone. The base
40 class does this.
41  
42 =head2 Composing the role
43  
44 Consume the role in the root class of an implementation. A role's default
45 method is installed into a class that does not define the method itself, even
46 when a parent class does, so composing it again in a subclass replaces the
47 parent's override with the default.
48  
49 =head1 SYNOPSIS
50  
51 A complete lookup table, with every required method:
52  
53 package My::LookupTable; # no cache: every row canonical unless deleted or not in the database
54 use parent 'DBIx::QuickORM::LookupTable';
55 use Role::Tiny::With;
56  
57 use DBIx::QuickORM::RowState;
58  
59 sub caches { 0 } # so lookup finds nothing, and cache options always miss
60 sub lookup { undef }
61  
62 # reads and writes keep the row data they are given
63 sub ingest { my ($self, $table, $data) = @_; $data }
64 sub ingest_many { my ($self, $table, $rows) = @_; $rows }
65 sub store { my ($self, $table, $data) = @_; $data }
66 sub update_entry { my ($self, $table, $old_pk, $old, $data) = @_; $data }
67  
68 sub canon_status {
69 my ($self, $table, $data) = @_;
70 my $state = DBIx::QuickORM::RowState->state($data) or return undef;
71 return {cause => $state eq 'deleted' ? 'deleted' : 'not_in_db'};
72 }
73  
74 sub is_canon { my ($self, $table, $data) = @_; $self->canon_status($table, $data) ? 0 : 1 }
75  
76 # nothing held: nothing to drop, and no transaction levels to keep
77 sub gone { }
78 sub forget { }
79 sub clear { }
80 sub remove_table { }
81 sub begin_root { }
82 sub push_level { }
83 sub commit_level { }
84 sub rollback_level { }
85 sub clear_txn_levels { }
86  
87 with 'DBIx::QuickORM::Role::LookupTable';
88  
89 Then:
90  
91 my $orm = DBIx::QuickORM->new(credentials => \%credentials, lookup => '+My::LookupTable');
92  
93 my $ann = $orm->insert(users => {name => 'Ann'});
94 my $one = $orm->find(users => $ann->field('id')); # new row data, canonical too
95  
96 $ann->delete;
97 my $status = $orm->canon_status(users => $ann); # {cause => 'deleted'}
98  
99 C<debug> comes from the base class.
100  
101 =head1 REQUIRED METHODS
102  
103 The examples here use an ORM with the default lookup table
104 (L<DBIx::QuickORM::LookupTable::Identity>) as C<$orm>, its Connection's
105 lookup table as C<$lookup>, and a user's C<$id>.
106  
107 =over 4
108  
109 =item $bool = $lookup->caches
110  
111 =item $bool = $class->caches
112  
113 True when C<lookup> can find rows by primary key. When false, C<lookup> and
114 C<lookup_value> must give undef, C<cache> options then always miss, and
115 C<retain_cache> croaks. Callable on the class.
116  
117 =item $debug = $lookup->debug
118  
119 The ORM's L<DBIx::QuickORM::Debug>, or undef.
120  
121 =item $row_data = $lookup->ingest($table, \%row_data)
122  
123 A row read from the database. Returns the row data to use for it: the given
124 hash, or a canonical one the lookup table holds.
125  
126 =item \@row_data = $lookup->ingest_many($table, \@rows)
127  
128 C<ingest> for each row, in order. It may return the given arrayref.
129  
130 =item $row_data = $lookup->store($table, \%row_data)
131  
132 A row written to the database (an insert). Returns the row data to use.
133  
134 =item $row_data = $lookup->update_entry($table, \%old_pk, \%old_data, \%row_data)
135  
136 An update gave the row new row data, possibly under a new primary key.
137 Returns the row data to use.
138  
139 =item $row_data = $lookup->lookup($table, \%pk)
140  
141 The canonical row data for the key, or undef.
142  
143 =item $lookup->gone($table, \%pk)
144  
145 The database no longer has the row.
146  
147 =item $lookup->forget
148  
149 =item $lookup->forget($table)
150  
151 =item $lookup->forget($table, \%pk)
152  
153 Drop everything, a table, or one row, including what an open transaction
154 holds from before it began.
155  
156 # what the ORM's clear_cache calls
157 $orm->clear_cache(users => $id); # $lookup->forget(users => {id => $id})
158 $orm->clear_cache('users'); # $lookup->forget('users')
159 $orm->clear_cache; # $lookup->forget
160  
161 =item $lookup->clear
162  
163 =item $lookup->clear($table)
164  
165 =item $lookup->clear($table, \%pk)
166  
167 Drop everything, a table, or one row from the top level: the innermost
168 transaction level, or the outside level when no transaction is open.
169  
170 =item $lookup->remove_table($table)
171  
172 The table no longer exists (a temporary table or view ended): drop
173 everything held for it, at every level.
174  
175 =item $bool = $lookup->is_canon($table, \%row_data)
176  
177 Whether writes may use the row data.
178  
179 =item $status = $lookup->canon_status($table, \%row_data)
180  
181 Undef while canonical, otherwise C<< {cause => $cause, ...} >>. The ORM's
182 C<canon_status> lists the causes that ship (L<DBIx::QuickORM/canon_status>).
183  
184 =item $lookup->begin_root
185  
186 =item $lookup->push_level
187  
188 =item $lookup->commit_level
189  
190 =item $lookup->rollback_level
191  
192 =item $lookup->clear_txn_levels
193  
194 The transaction changes described above: a root transaction or a savepoint
195 began, the innermost one committed or rolled back, or an outcome is unknown
196 and every level above the outside one must hold nothing.
197  
198 =back
199  
200 =cut
201  
202 264 requires qw{
203 caches debug
204 ingest ingest_many store update_entry lookup gone forget clear remove_table
205 is_canon canon_status
206 begin_root push_level commit_level rollback_level clear_txn_levels
207 };
208  
209 =head1 PROVIDED METHODS
210  
211 Each may be overridden.
212  
213 =over 4
214  
215 =item $row_data = $lookup->lookup_value($table, $field, $value)
216  
217 C<lookup> for a table whose primary key is the single untyped field
218 C<$field>, by its plain value. The Connection's fast C<find> uses it.
219  
220 my $data = $lookup->lookup_value(users => 'id', $id); # as lookup(users => {id => $id})
221  
222 =back
223  
224 =cut
225  
226 12 ▶sub lookup_value { $_[0]->lookup($_[1], {$_[2] => $_[3]}) }
Subroutine lookup_value: called CC=1 SCAR=0.0
Pod documented
227  
228 =over 4
229  
230 =item $bool = $lookup->differ($old, $new)
231  
232 True when two raw field values differ: they compare unequal as strings, or
233 one is NULL (undef) and the other is not.
234  
235 my $same = $lookup->differ('1', 1); # 0: equal as strings
236 my $null = $lookup->differ(undef, undef); # 0: NULL equals only NULL
237 my $empty = $lookup->differ(undef, ''); # 1
238  
239 A row uses it to decide which inflated values to keep when its row data is
240 replaced. Callable on the class.
241  
242 =back
243  
244 =cut
245  
246 395 ▶sub differ {
Subroutine differ: called CC=6 SCAR=23.5
Pod documented
247 0 ▶ shift;
Subroutine differ: not called CC=6 SCAR=23.5
Pod documented
248 0 my ($x, $y) = @_;
249 0 ▶ return 0 if !defined($x) && !defined($y);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $x or defined $y 25 370 395
unless defined $x or defined $y 0 0 0
Condition50%3/6
defined $x or defined $y
l!l&&r!l&&!r
366425
defined $x or defined $y
l!l&&r!l&&!r
000
MC/DC100%2/2
defined $x or defined $y
defined $x defined $y
MC/DC0%0/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
Truth table0%0/3
A or B
A= defined $xB= defined $y
ABresult
1X1
011
000
250 0 ▶ return 1 if !defined($x) || !defined($y);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $x and defined $y 8 362 370
unless defined $x and defined $y 0 0 0
Condition50%3/6
defined $x and defined $y
!ll&&!rl&&r
44362
defined $x and defined $y
!ll&&!rl&&r
000
MC/DC100%2/2
defined $x and defined $y
defined $x defined $y
MC/DC0%0/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
Truth table0%0/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
251 0 ▶ return $x eq $y ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
$x eq $y ? : 336 26 362
$x eq $y ? : 0 0 0
252 }
253  
254 =over 4
255  
256 =item $text = $lookup->canon_reason(\%status)
257  
258 The cause in a C<canon_status> result as sentences for the non-canonical
259 croak, with the call behind it when known.
260  
261 my $text = $lookup->canon_reason({cause => 'deleted'});
262 # 'the row was deleted. Construct the ORM with debug => 1 to record which call did it.'
263  
264 For the causes that name a call (C<replaced>, C<cache_cleared>,
265 C<deleted>, C<not_cached>), it says how to record one when no call was
266 recorded and C<debug> is off.
267  
268 It covers the causes every shipped implementation may give. One with causes
269 of its own (C<rolled_back> in L<DBIx::QuickORM::LookupTable::Scoped>)
270 overrides it and calls this one for the rest:
271  
272 package My::LookupTable::Replica;
273 use parent 'My::LookupTable';
274  
275 # its canon_status (not shown) gives a 'replica' cause of its own
276 sub canon_reason {
277 my ($self, $status) = @_;
278 return 'it was read from a replica.' if $status->{cause} eq 'replica';
279 return $self->DBIx::QuickORM::Role::LookupTable::canon_reason($status);
280 }
281  
282 It calls the role's by its full name, which works in any class. C<SUPER::>
283 cannot reach it from a class that composes the role itself, as Scoped does.
284  
285 =back
286  
287 =cut
288  
289 264 my %CANON_REASON = (
290 replaced => ['the row was fetched or written again elsewhere in the code, leaving this copy stale.', 'Last replaced by'],
291 cache_cleared => ['its cached copy was cleared.', 'Last cleared by'],
292 deleted => ['the row was deleted.', 'Deleted by'],
293 not_in_db => ['the row is not in the database.'],
294 txn => ['it was read before the open transaction began, and rows from before a transaction are not canonical in it.'],
295 not_cached => ['the row cache holds no copy of it (it was read around the cache, its transaction rolled back, or the cache was cleared).', 'Last cache clear:'],
296 );
297  
298 79 ▶sub canon_reason {
Subroutine canon_reason: called CC=7 SCAR=25.7
Pod documented
299 0 ▶ my $self = shift;
Subroutine canon_reason: not called CC=7 SCAR=25.7
Pod documented
300 0 my ($status) = @_;
301 0 ▶ my ($text, $lead) = @{$CANON_REASON{$status->{cause}} // ["the cause is '$status->{cause}'."]};
Condition50%2/4
$CANON_REASON{$status->{"cause"}} // [qq[the cause is '$status->{"cause"}'.]]
l!l
781
$CANON_REASON{$status->{"cause"}} // [qq[the cause is '$status->{"cause"}'.]]
l!l
00
MC/DC100%1/1
$CANON_REASON{$status->{"cause"}} // [qq[the cause is '$status->{"cause"}'.]]
$CANON_REASON{$status->{"cause"}}
MC/DC0%0/1
$CANON_REASON{$status->{"cause"}} // [qq[the cause is '$status->{"cause"}'.]]
$CANON_REASON{$status->{"cause"}}
Truth table100%2/2
A
A= $CANON_REASON{$status->{"cause"}}
Aresult
11
00
Truth table0%0/2
A
A= $CANON_REASON{$status->{"cause"}}
Aresult
11
00
302 0 ▶ my $frame = $status->{by} // $status->{last_clear};
Condition50%3/6
$status->{"by"} // $status->{"last_clear"}
l!l&&r!l&&!r
3274
$status->{"by"} // $status->{"last_clear"}
l!l&&r!l&&!r
000
MC/DC100%2/2
$status->{"by"} // $status->{"last_clear"}
$status->{"by"} $status->{"last_clear"}
MC/DC0%0/2
$status->{"by"} // $status->{"last_clear"}
$status->{"by"} $status->{"last_clear"}
Truth table100%3/3
A // B
A= $status->{"by"}B= $status->{"last_clear"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $status->{"by"}B= $status->{"last_clear"}
ABresult
1X1
011
000
303 0 ▶ return "$text $lead $frame->[3] call in $frame->[1] at line $frame->[2]." if $lead && $frame;
Branch: 2 branches
BranchTrueFalseTotal
if $lead and $frame 5 74 79
if $lead and $frame 0 0 0
Condition50%3/6
$lead and $frame
!ll&&!rl&&r
23515
$lead and $frame
!ll&&!rl&&r
000
MC/DC100%2/2
$lead and $frame
$lead $frame
MC/DC0%0/2
$lead and $frame
$lead $frame
Truth table100%3/3
A and B
A= $leadB= $frame
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $leadB= $frame
ABresult
0X0
100
111
304 0 ▶ return "$text Construct the ORM with debug => 1 to record which call did it." if $lead && !$self->debug;
Branch: 2 branches
BranchTrueFalseTotal
if $lead and not $self->debug 49 25 74
if $lead and not $self->debug 0 0 0
Condition50%3/6
$lead and not $self->debug
!ll&&!rl&&r
23249
$lead and not $self->debug
!ll&&!rl&&r
000
MC/DC100%2/2
$lead and not $self->debug
$lead not $self->debug
MC/DC0%0/2
$lead and not $self->debug
$lead not $self->debug
Truth table100%3/3
A and not(B)
A= $leadB= not $self->debug
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $leadB= not $self->debug
ABresult
0X0
100
111
305 0 return $text;
306 }
307  
308 264 1;
309  
310 __END__
« blib/lib/DBIx/QuickORM/LookupTable/Identity.pm ↑ Summary blib/lib/DBIx/QuickORM/Command/GenerateSQL.pm »