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

filter: Statementstmt 73.4% 188 / 256 Branchbran 67.5% 77 / 114 Conditioncond 65.2% 30 / 46 MC/DCmcdc 59.2% 16 / 27 Subroutinesub 76.1% 32 / 42 Podpod 100% 30 / 30 totaltotal 72.4% 373 / 515 CC 114 SCAR 17.1
CC
114
Cov
71%
_fetch_missing
11.0
_fetch_missing
11.0
cas_update
10.5
CRAP 5.5
« blib/lib/DBIx/QuickORM/Source/Query.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Table.pm »
1 package DBIx::QuickORM::Role::Row;
2 257 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 257 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 257 our $VERSION = '0.000031';
6  
7 257 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 257 ▶use Hash::Util::FieldHash qw/fieldhash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 257 ▶use DBIx::QuickORM::LookupTable();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 257 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 257 requires qw/orm table row_data set_row_data/;
15  
16 =pod
17  
18 =encoding UTF-8
19  
20 =head1 NAME
21  
22 DBIx::QuickORM::Role::Row - The logic of a row object.
23  
24 =head1 DESCRIPTION
25  
26 A row object wraps one row's data (a hashref, as the ORM's CRUD operations
27 return it) with the ORM and the table it came from. C<raw_field> gives a value
28 as the database returned it. C<inflated_field>, and C<field>, give it
29 inflated through the field's type. A row in the database changes only through
30 C<update>, C<cas_update>, and C<delete>. There are no setters.
31  
32 A row need not be in the database: one built by the ORM's C<vivify>, one
33 deleted, or one whose insert was rolled back. C<is_in_db> and C<was_deleted>
34 tell. Such a row is changed with C<modify> and written with C<insert>.
35 C<update>, C<cas_update>, C<delete>, and C<refresh> croak on it.
36  
37 Inflated values are cached per field. When the row data is replaced (by
38 C<update>, C<refresh>, a field fetch that finds the row changed, or any call
39 of C<set_row_data>), cached values of fields whose raw value is unchanged are
40 kept and the rest are dropped.
41  
42 A read hands out the cached value itself, or a copy of it, as the type says
43 (see L<DBIx::QuickORM::Role::Type/"Cached inflated values">), so changing a
44 value read from a row never changes the row or the next read.
45  
46 A field missing from the row data was not fetched: it was omitted, or the row
47 came back from a write without C<RETURNING>. Reading it, in either form,
48 fetches every field asked for with one C<refresh_row>. When that shows the
49 row changed, the row takes the new row data, as C<refresh> would. Reading an
50 omitted field from each row in a loop can run one query per row.
51  
52 A row that is not canon (from a C<no_cache> handle, say) fetches its missing
53 fields without the row cache: they are added to its own row data, nothing the
54 ORM holds changes, and the row stays not canon.
55  
56 A row with no table (a row of a query, or a computed row given a row class)
57 returns the row data's values as they are from every field reader: no field
58 check, no inflation, and no fetch.
59  
60 C<update>, C<cas_update>, and C<refresh> call the ORM's CRUD operations and
61 take the row data they return. C<delete> calls C<delete_row>. Options mean
62 what L<DBIx::QuickORM/"CRUD operations"> says.
63  
64 Any class providing the required methods can consume the role. It keeps
65 nothing in the object. L<DBIx::QuickORM::Row> is the default row class.
66  
67 =head2 Overriding
68  
69 A row class changes what C<field> and C<fields> return by overriding them
70 (say, to give the raw form). It changes the cache policy by overriding
71 C<_inflation_cache> and C<_inflated_value>, the two supported override points.
72 Nothing else about the cache is.
73  
74 =head1 SYNOPSIS
75  
76 package My::Row;
77 use Object::HashBase qw{<orm <table row_data};
78 use Role::Tiny::With qw/with/;
79 with 'DBIx::QuickORM::Role::Row';
80  
81 # Raw values by default, but inflated_field and inflated_fields still inflate.
82 sub field { my $self = shift; $self->raw_field(@_) }
83 sub fields { my $self = shift; $self->raw_fields(@_) }
84  
85 package main;
86  
87 my $orm = DBIx::QuickORM->new(
88 credentials => \%credentials,
89 row_class => 'My::Row',
90 );
91  
92 my $ann = $orm->insert(
93 users => {
94 name => 'Ann',
95 prefs => {theme => 'dark'},
96 },
97 );
98  
99 my $raw = $ann->field('prefs'); # the JSON text
100 my $all = $ann->inflated_field('prefs'); # {theme => 'dark'}
101  
102 =head1 REQUIRED METHODS
103  
104 =over 4
105  
106 =item $orm = $row->orm
107  
108 The L<DBIx::QuickORM> the row came from.
109  
110 =item $table = $row->table
111  
112 The name of the row's table, or undef for a row with no table.
113  
114 =item \%data = $row->row_data
115  
116 The row data the row wraps.
117  
118 =item $row->set_row_data(\%data)
119  
120 Replaces the row data. The role calls it whenever the ORM returns new row
121 data for the row.
122  
123 =back
124  
125 =cut
126  
127 # Inside-out, keyed by row object: {data => the row data the values came from, values => {field => inflated}}.
128 257 fieldhash my %CACHE;
129  
130 =head1 PUBLIC METHODS
131  
132 =over 4
133  
134 =item $value = $row->field($name)
135  
136 =item @values = $row->fields(\@names)
137  
138 =item @values = $row->fields(@names)
139  
140 The inflated form: C<inflated_field> and C<inflated_fields>. Override these
141 to give a row class another default form.
142  
143 =back
144  
145 =cut
146  
147 250 ▶sub field { my $self = shift; $self->inflated_field(@_) }
Subroutine field: called CC=1 SCAR=0.0
Pod documented
148 8 ▶sub fields { my $self = shift; $self->inflated_fields(@_) }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
149  
150 =pod
151  
152 =over 4
153  
154 =item $value = $row->inflated_field($name)
155  
156 =item @values = $row->inflated_fields(\@names)
157  
158 =item @values = $row->inflated_fields(@names)
159  
160 Values inflated through each field's type: the cached value or a copy of it,
161 as the type says (see L</DESCRIPTION>). The raw value for an untyped field,
162 undef for NULL, and undef for an undef inflated value (a JSON C<null>).
163 Missing fields are fetched with one C<refresh_row> (one C<no_cache> read for a
164 row that is not canon). A row built by C<vivify>
165 returns undef for a field it was not given. So does a row whose insert
166 was rolled back.
167  
168 Croaks for a field the table does not have, and for a missing field that
169 cannot be fetched (no primary key, or the row is deleted or no longer in the
170 database).
171  
172 =back
173  
174 =cut
175  
176 # One field-record lookup. It calls _fetch_missing (which croaks for an unknown field) only when a field is unknown or missing.
177 236 ▶sub inflated_field {
Subroutine inflated_field: called CC=4 SCAR=17.9
Pod documented
178 0 ▶ my $self = shift;
Subroutine inflated_field: not called CC=4 SCAR=17.9
Pod documented
179 0 my ($name) = @_;
180  
181 0 my $table = $self->table;
182 0 ▶ return $self->row_data->{$name} unless defined $table;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $table 5 231 236
unless defined $table 0 0 0
183  
184 0 my $field = $self->orm->db_map->table($table)->{fields}->{$name};
185 0 ▶ $self->_fetch_missing($name) unless $field && exists $self->row_data->{$name};
Branch: 2 branches
BranchTrueFalseTotal
unless $field and exists $self->row_data->{$name} 11 220 231
unless $field and exists $self->row_data->{$name} 0 0 0
Condition50%3/6
$field and exists $self->row_data->{$name}
!ll&&!rl&&r
29220
$field and exists $self->row_data->{$name}
!ll&&!rl&&r
000
MC/DC100%2/2
$field and exists $self->row_data->{$name}
$field exists $self->row_data->{$name}
MC/DC0%0/2
$field and exists $self->row_data->{$name}
$field exists $self->row_data->{$name}
Truth table100%3/3
A and B
A= $fieldB= exists $self->row_data->{$name}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $fieldB= exists $self->row_data->{$name}
ABresult
0X0
100
111
186 0 return $self->_inflated_value($name, $field);
187 }
188  
189 10 ▶sub inflated_fields {
Subroutine inflated_fields: called CC=5 SCAR=21.5
Pod documented
190 0 ▶ my $self = shift;
Subroutine inflated_fields: not called CC=5 SCAR=21.5
Pod documented
191 0 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 8 10
ref $_[0] eq 'ARRAY' ? : 0 0 0
192  
193 0 my $table = $self->table;
194 0 ▶ return @{$self->row_data}{@names} unless defined $table;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $table 2 8 10
unless defined $table 0 0 0
195  
196 0 my $fields = $self->orm->db_map->table($table)->{fields};
197 0 my $data = $self->row_data;
198 0 ▶ $self->_fetch_missing(@names) if grep { !$fields->{$_} || !exists $data->{$_} } @names;
Branch: 2 branches
BranchTrueFalseTotal
if grep {not $fields->{$_} && exists $data->{$_};} @names 3 5 8
if grep {not $fields->{$_} && exists $data->{$_};} @names 0 0 0
Condition33%2/6
$fields->{$_} && exists $data->{$_}
!ll&&!rl&&r
0612
$fields->{$_} && exists $data->{$_}
!ll&&!rl&&r
000
MC/DC50%1/2
$fields->{$_} && exists $data->{$_}
$fields->{$_} exists $data->{$_}
MC/DC0%0/2
$fields->{$_} && exists $data->{$_}
$fields->{$_} exists $data->{$_}
Truth table66%2/3
A && B
A= $fields->{$_}B= exists $data->{$_}
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $fields->{$_}B= exists $data->{$_}
ABresult
0X0
100
111
199 0 return map { $self->_inflated_value($_, $fields->{$_}) } @names;
200 }
201  
202 =pod
203  
204 =over 4
205  
206 =item $raw = $row->raw_field($name)
207  
208 =item @raw = $row->raw_fields(\@names)
209  
210 =item @raw = $row->raw_fields(@names)
211  
212 Values as the database returned them, never inflated. Missing fields are
213 fetched and unknown ones croak, as for C<inflated_fields>.
214  
215 =back
216  
217 =cut
218  
219 15 ▶sub raw_field {
Subroutine raw_field: called CC=1 SCAR=1.2
Pod documented
220 0 ▶ my $self = shift;
Subroutine raw_field: not called CC=1 SCAR=1.2
Pod documented
221 0 my ($name) = @_;
222 0 return ($self->raw_fields($name))[0];
223 }
224  
225 16 ▶sub raw_fields {
Subroutine raw_fields: called CC=3 SCAR=14.2
Pod documented
226 0 ▶ my $self = shift;
Subroutine raw_fields: not called CC=3 SCAR=14.2
Pod documented
227 0 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 15 16
ref $_[0] eq 'ARRAY' ? : 0 0 0
228 0 ▶ $self->_fetch_missing(@names) if defined $self->table;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->table 15 1 16
if defined $self->table 0 0 0
229 0 return @{$self->row_data}{@names};
230 }
231  
232 =pod
233  
234 =over 4
235  
236 =item \%pk = $row->pk
237  
238 The primary key fields and their values from the row data. Undef when the row
239 has no table or the table has no primary key.
240  
241 =back
242  
243 =cut
244  
245 148 ▶sub pk {
Subroutine pk: called CC=3 SCAR=14.2
Pod documented
246 0 ▶ my $self = shift;
Subroutine pk: not called CC=3 SCAR=14.2
Pod documented
247 0 ▶ my $table = $self->table // return undef;
Condition50%2/4
$self->table // (return undef)
l!l
13513
$self->table // (return undef)
l!l
00
MC/DC100%1/1
$self->table // (return undef)
$self->table
MC/DC0%0/1
$self->table // (return undef)
$self->table
Truth table100%2/2
A
A= $self->table
Aresult
11
00
Truth table0%0/2
A
A= $self->table
Aresult
11
00
248 0 ▶ my $pk = $self->orm->db_map->pk($table) or return undef;
Branch: 2 branches
BranchTrueFalseTotal
my $pk = $self->orm->db_map->pk($table) or return undef 126 9 135
my $pk = $self->orm->db_map->pk($table) or return undef 0 0 0
249 0 my $data = $self->row_data;
250 0 return {map { $_ => $data->{$_} } @$pk};
251 }
252  
253 =pod
254  
255 =over 4
256  
257 =item $row = $row->update(\%changes, %update_row_options)
258  
259 Writes the changes with C<update_row> and takes the new row data. Returns the
260 row, or undef when no row matched (a compare-and-swap failed, or the row is
261 gone), leaving the row as it was.
262  
263 Each change is deflated through its field's type. The options are those of
264 C<update_row>: C<enforce_canon>, C<cas>, C<auto_cas>. A row that is not
265 canonical croaks under C<enforce_canon>. C<cas_update> writes one safely.
266  
267 Croaks when the changes are not a hashref, when given C<data> (it always
268 returns the row), when the row is not in the database, or when it has no
269 usable primary key.
270  
271 =back
272  
273 =cut
274  
275 33 ▶sub update {
Subroutine update: called CC=3 SCAR=14.2
Pod documented
276 0 ▶ my $self = shift;
Subroutine update: not called CC=3 SCAR=14.2
Pod documented
277 0 my ($changes, %params) = @_;
278 0 ▶ croak "update() needs a hashref of changes" unless ref($changes) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $changes eq "HASH" 1 32 33
unless ref $changes eq "HASH" 0 0 0
279 0 ▶ croak "update() always returns the row; use the ORM's update_row() for row data" if exists $params{data};
Branch: 2 branches
BranchTrueFalseTotal
if exists $params{"data"} 1 31 32
if exists $params{"data"} 0 0 0
280  
281 0 $self->_require_in_db('update');
282 0 my $table = $self->_require_pk('update');
283 0 my $orm = $self->orm;
284 0 return $orm->update_row($table, $self, $orm->connection->deflate($table, $changes), %params, raw => 1);
285 }
286  
287 =pod
288  
289 =over 4
290  
291 =item $row = $row->cas_update(\%changes)
292  
293 =item $row = $row->cas_update(\%changes, cas => \@fields)
294  
295 Writes the changes only if the database still holds the row's values for the
296 changed fields (or the C<cas> fields). Returns the row with the new data, or
297 undef on a conflict, leaving the row as it was.
298  
299 The row need not be canonical, even inside a transaction, so this is how
300 to write a row read before the transaction began. The changes are deflated as
301 C<update> deflates them.
302  
303 It compares the changed fields with C<=>, which does not suit floats,
304 PostgreSQL C<json>, or MySQL C<JSON>: pass C<cas> to compare other fields. On
305 MySQL and MariaDB a successful update leaves only the primary key in the row
306 data, so another C<cas_update> croaks until the row is refreshed (or the
307 compared fields are read again).
308  
309 Croaks when the changes are not a hashref of at least one change, on any
310 option but C<cas>, on an empty C<cas> list (an unconditional write), when the
311 row is not in the database, when it has no usable primary key, or when it does
312 not hold a field it compares.
313  
314 =back
315  
316 =cut
317  
318 15 ▶sub cas_update {
Subroutine cas_update: called CC=6 SCAR=23.5
Pod documented
319 0 ▶ my $self = shift;
Subroutine cas_update: not called CC=6 SCAR=23.5
Pod documented
320 0 my ($changes, %params) = @_;
321 0 ▶ croak "cas_update() needs a hashref of changes" unless ref($changes) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $changes eq "HASH" 1 14 15
unless ref $changes eq "HASH" 0 0 0
322 0 ▶ croak "cas_update() takes only the 'cas' option" if grep { $_ ne 'cas' } keys %params;
Branch: 2 branches
BranchTrueFalseTotal
if grep {$_ ne "cas";} keys %params 1 13 14
if grep {$_ ne "cas";} keys %params 0 0 0
323  
324 0 $self->_require_in_db('cas_update');
325 0 my $table = $self->_require_pk('cas_update');
326 0 my $orm = $self->orm;
327 0 ▶ croak "cas_update() needs a field to compare (an empty list would write unconditionally)" if ref($params{cas}) eq 'ARRAY' && !@{$params{cas}};
Branch: 2 branches
BranchTrueFalseTotal
if ref $params{"cas"} eq "ARRAY" and not @{$params{"cas"};} 1 10 11
if ref $params{"cas"} eq "ARRAY" and not @{$params{"cas"};} 0 0 0
Condition50%3/6
ref $params{"cas"} eq "ARRAY" and not @{$params{"cas"};}
!ll&&!rl&&r
641
ref $params{"cas"} eq "ARRAY" and not @{$params{"cas"};}
!ll&&!rl&&r
000
MC/DC100%2/2
ref $params{"cas"} eq "ARRAY" and not @{$params{"cas"};}
ref $params{"cas"} eq "ARRAY" not @{$params{"cas"};}
MC/DC0%0/2
ref $params{"cas"} eq "ARRAY" and not @{$params{"cas"};}
ref $params{"cas"} eq "ARRAY" not @{$params{"cas"};}
Truth table100%3/3
A and not(B)
A= ref $params{"cas"} eq "ARRAY"B= not @{$params{"cas"};}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= ref $params{"cas"} eq "ARRAY"B= not @{$params{"cas"};}
ABresult
0X0
100
111
328 0 ▶ my $cas = $params{cas} // [keys %$changes];
Condition50%2/4
$params{"cas"} // [keys %$changes]
l!l
46
$params{"cas"} // [keys %$changes]
l!l
00
MC/DC100%1/1
$params{"cas"} // [keys %$changes]
$params{"cas"}
MC/DC0%0/1
$params{"cas"} // [keys %$changes]
$params{"cas"}
Truth table100%2/2
A
A= $params{"cas"}
Aresult
11
00
Truth table0%0/2
A
A= $params{"cas"}
Aresult
11
00
329 0 return $orm->update_row($table, $self, $orm->connection->deflate($table, $changes), cas => $cas, enforce_canon => 0, raw => 1);
330 }
331  
332 =pod
333  
334 =over 4
335  
336 =item $count = $row->delete(%delete_row_options)
337  
338 Deletes the row with C<delete_row> and returns how many rows went. The row,
339 and every row holding the same row data, is then marked deleted.
340  
341 A delete that matches no row also marks it deleted (it is not in the
342 database) and returns 0.
343  
344 Croaks when the row is not in the database or has no usable primary key.
345  
346 =back
347  
348 =cut
349  
350 24 ▶sub delete {
Subroutine delete: called CC=1 SCAR=1.2
Pod documented
351 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=1.2
Pod documented
352 0 my (%params) = @_;
353 0 $self->_require_in_db('delete');
354 0 my $table = $self->_require_pk('delete');
355 0 return $self->orm->connection->delete_row($table, $self->row_data, %params, raw => 1);
356 }
357  
358 =pod
359  
360 =over 4
361  
362 =item $row = $row->refresh
363  
364 Reads the row from the database and takes the new row data. Takes no
365 arguments.
366  
367 Croaks when given any, when the row is not in the database (as this ORM last
368 saw it), or has no usable primary key. Croaks too when the row is no longer
369 there, marking it deleted.
370  
371 =back
372  
373 =cut
374  
375 32 ▶sub refresh {
Subroutine refresh: called CC=3 SCAR=14.2
Pod documented
376 0 ▶ my $self = shift;
Subroutine refresh: not called CC=3 SCAR=14.2
Pod documented
377 0 ▶ croak "refresh() takes no arguments" if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 1 31 32
if @_ 0 0 0
378  
379 0 $self->_require_in_db('refresh');
380 0 my $table = $self->_require_pk('refresh');
381 0 ▶ return $self->orm->refresh_row($table, $self, cache => 0, raw => 1) // croak "The row is no longer in the table '$table'";
Condition50%2/4
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1) // croak("The row is no longer in the table '${table}'")
l!l
193
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1) // croak("The row is no longer in the table '${table}'")
l!l
00
MC/DC0%0/1
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1) // croak("The row is no longer in the table '${table}'")
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1)
MC/DC0%0/1
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1) // croak("The row is no longer in the table '${table}'")
$self->orm->refresh_row($table, $self, "cache", 0, "raw", 1)
Truth table50%1/2
A
A= $self->orm->refresh_row($table, $self, "cache", 0, "raw", 1)
Aresult
11
00
Truth table0%0/2
A
A= $self->orm->refresh_row($table, $self, "cache", 0, "raw", 1)
Aresult
11
00
382 }
383  
384 =pod
385  
386 =over 4
387  
388 =item $bool = $row->is_canon
389  
390 True when the row data is the ORM's canonical copy of the row. False for a row
391 not in the database, even one without its key (the ORM's C<is_canon> croaks
392 for that). Croaks when a row in the database has no usable primary key.
393  
394 =back
395  
396 =cut
397  
398 34 ▶sub is_canon {
Subroutine is_canon: called CC=2 SCAR=9.2
Pod documented
399 0 ▶ my $self = shift;
Subroutine is_canon: not called CC=2 SCAR=9.2
Pod documented
400 0 ▶ return 0 unless $self->is_in_db;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->is_in_db 3 31 34
unless $self->is_in_db 0 0 0
401 0 my $table = $self->_require_pk('is_canon');
402 0 return $self->orm->connection->is_canon($table, $self->row_data);
403 }
404  
405 =over 4
406  
407 =item $bool = $row->is_in_db
408  
409 True unless the row was built by C<vivify> and not yet inserted, or was
410 marked deleted (see C<was_deleted>). This is what this ORM last saw, not a
411 check of the database.
412  
413 C<insert> sets it. A rollback of the inserting transaction or savepoint
414 clears it again, so the row can be inserted again.
415  
416 An update or refresh of that row in the same transaction carries this along.
417 A plain re-read that gives the row changed data does not: after a rollback
418 such a row may still report C<is_in_db>, so C<refresh> it to check.
419  
420 =item $bool = $row->was_deleted
421  
422 True when the row was marked deleted. Three things mark it: a row
423 C<delete>, a C<delete_row>, and a C<refresh> or C<find> that finds nothing in
424 the database.
425 A vivified row is neither in the database nor deleted.
426  
427 A bulk C<delete> on a handle marks nothing unless given
428 C<< mark_deleted => 1 >>. Otherwise rows already held keep C<is_in_db> true,
429 so refresh them to check.
430  
431 A delete inside a transaction or savepoint that rolls back is undone here too.
432  
433 =back
434  
435 =cut
436  
437 192 ▶sub is_in_db { DBIx::QuickORM::LookupTable->row_state($_[0]->row_data) ? 0 : 1 }
Subroutine is_in_db: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
'DBIx::QuickORM::LookupTable'->row_state($_[0]->row_data) ? : 42 150 192
438  
439 29 ▶sub was_deleted {
Subroutine was_deleted: called CC=3 SCAR=11.0
Pod documented
440 29 ▶ my $state = DBIx::QuickORM::LookupTable->row_state($_[0]->row_data) // '';
Condition100%2/2
"DBIx::QuickORM::LookupTable"->row_state($_[0]->row_data) // ""
l!l
236
MC/DC100%1/1
"DBIx::QuickORM::LookupTable"->row_state($_[0]->row_data) // ""
"DBIx::QuickORM::LookupTable"->row_state($_[0]->row_data)
Truth table100%2/2
A
A= "DBIx::QuickORM::LookupTable"->row_state($_[0]->row_data)
Aresult
11
00
441 29 ▶ return $state eq 'deleted' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$state eq 'deleted' ? : 20 9 29
442 }
443  
444 =pod
445  
446 =over 4
447  
448 =item $row = $row->insert
449  
450 Inserts a row that is not in the database, with the ORM's C<insert>, and takes
451 the new row data. The row is then in the database, and canonical when the
452 table has a primary key. Returns the row.
453  
454 Croaks when given arguments, for a row with no table, and for a row already
455 in the database.
456  
457 =back
458  
459 =cut
460  
461 6 ▶sub insert {
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
462 6 my $self = shift;
463 6 ▶ croak "insert() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
464 6 my $table = $self->_require_table('insert');
465 6 ▶ croak "Cannot insert a row that is already in the database (table '$table')" if $self->is_in_db;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_in_db 1 5 6
466 5 $self->set_row_data($self->orm->insert($table => $self->row_data, data => 1, raw => 1));
467 5 return $self;
468 }
469  
470 =pod
471  
472 =over 4
473  
474 =item $row = $row->modify(\%changes)
475  
476 Changes the fields of a row that is not in the database, before its
477 C<insert>. Nothing is written. Returns the row.
478  
479 The changes are deflated as C<update> deflates them. The row takes a changed
480 copy of its row data, so other rows holding the old row data are untouched.
481  
482 Croaks when the changes are not a hashref, for a row with no table, for a
483 field the table does not have, and for a row in the database: use C<update>.
484  
485 =back
486  
487 =cut
488  
489 8 ▶sub modify {
Subroutine modify: called CC=3 SCAR=11.0
Pod documented
490 8 my $self = shift;
491 8 my ($changes) = @_;
492 8 ▶ croak "modify() needs a hashref of changes" unless ref($changes) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $changes eq "HASH" 0 8 8
493 8 my $table = $self->_require_table('modify');
494 8 ▶ croak "Cannot modify a row that is in the database; use update()" if $self->is_in_db;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_in_db 1 7 8
495  
496 7 my $old = $self->row_data;
497 7 my $new = {%$old, %{$self->orm->connection->deflate($table, $changes)}};
498 6 DBIx::QuickORM::LookupTable->copy_row_state($old, $new);
499 6 $self->set_row_data($new);
500 6 return $self;
501 }
502  
503 =pod
504  
505 =over 4
506  
507 =item $h = $row->follow_fk($other_table, link => \%link)
508  
509 =item $h = $row->follow_fk($other_table, via => \%via)
510  
511 The ORM's C<follow_fk> from this row: a handle on the rows it links to.
512 Croaks for a row with no table.
513  
514 =back
515  
516 =cut
517  
518 26 ▶sub follow_fk {
Subroutine follow_fk: called CC=2 SCAR=6.9
Pod documented
519 26 my $self = shift;
520 26 my ($other, %params) = @_;
521 26 my $table = $self->table;
522 26 ▶ croak "Cannot follow a foreign key from a row with no table" unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 1 25 26
523 25 return $self->orm->_follow_fk([caller], $table, $self, $other, %params);
524 }
525  
526 =pod
527  
528 =over 4
529  
530 =item $new = $row->insert_fk($other_table, %opts, \%fields)
531  
532 =item \@new = $row->insert_many_fk($other_table, %opts, @fields)
533  
534 The ORM's C<insert_fk> and C<insert_many_fk> from this row: insert rows that
535 point at it.
536  
537 my $post = $alice->insert_fk('posts', {title => 'One'});
538 my $posts = $alice->insert_many_fk('posts', link => {id => 'user_id'}, {title => 'A'}, {title => 'B'});
539  
540 Croaks for a row with no table.
541  
542 =back
543  
544 =cut
545  
546 20 ▶sub insert_fk {
Subroutine insert_fk: called CC=2 SCAR=7.0
Pod documented
547 20 my $self = shift;
548 20 my $table = $self->table;
549 20 ▶ croak "Cannot insert rows linked to a row with no table" unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 20 20
550 20 return $self->orm->_insert_fk([caller], insert => $table, $self, @_);
551 }
552  
553 8 ▶sub insert_many_fk {
Subroutine insert_many_fk: called CC=2 SCAR=7.0
Pod documented
554 8 my $self = shift;
555 8 my $table = $self->table;
556 8 ▶ croak "Cannot insert rows linked to a row with no table" unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 8 8
557 8 return $self->orm->_insert_fk([caller], insert_many => $table, $self, @_);
558 }
559  
560 =pod
561  
562 =over 4
563  
564 =item $bool = $class->fast_bless
565  
566 True when the ORM may build rows of the class by blessing
567 C<< {orm => $orm, table => $table, row_data => \%data} >> directly, skipping
568 C<new>. False here. L<DBIx::QuickORM::Row> says when it is true.
569  
570 The ORM takes the answer once per row class per ORM, not once per row.
571  
572 =back
573  
574 =cut
575  
576 5 ▶sub fast_bless { 0 }
Subroutine fast_bless: called CC=1 SCAR=0.0
Pod documented
577  
578 =head1 PRIVATE METHODS
579  
580 =over 4
581  
582 =item $table = $row->_require_pk($action)
583  
584 The row's table. Croaks naming the action unless the table has a primary key
585 and the row data holds all of it.
586  
587 =back
588  
589 =cut
590  
591 134 ▶sub _require_pk {
Subroutine _require_pk: called CC=4 SCAR=13.9
592 134 my $self = shift;
593 134 my ($action) = @_;
594  
595 134 my $table = $self->table;
596 134 my $pk = $self->pk;
597 134 ▶ my $ok = $pk && !grep { !defined } values %$pk;
Condition100%3/3
$pk && !grep({not defined $_;} values %$pk)
!ll&&!rl&&r
172115
MC/DC100%2/2
$pk && !grep({not defined $_;} values %$pk)
$pk !grep({not defined $_;} values %$pk)
Truth table100%3/3
A && not(B)
A= $pkB= !grep({not defined $_;} values %$pk)
ABresult
0X0
100
111
598 134 ▶ croak "Cannot $action a row without a primary key" . (defined($table) ? " (table '$table')" : '') unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 19 115 134
defined $table ? : 10 9 19
599  
600 115 return $table;
601 }
602  
603 =pod
604  
605 =over 4
606  
607 =item $row->_require_in_db($action)
608  
609 Croaks naming the action unless the row is in the database.
610  
611 =item $table = $row->_require_table($action)
612  
613 The row's table. Croaks naming the action for a row with no table.
614  
615 =back
616  
617 =cut
618  
619 111 ▶sub _require_in_db {
Subroutine _require_in_db: called CC=2 SCAR=6.9
620 111 my $self = shift;
621 111 my ($action) = @_;
622 111 ▶ croak "Cannot $action a row that is not in the database; insert it first" unless $self->is_in_db;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_in_db 8 103 111
623 }
624  
625 14 ▶sub _require_table {
Subroutine _require_table: called CC=2 SCAR=7.0
626 14 my $self = shift;
627 14 my ($action) = @_;
628 14 my $table = $self->table;
629 14 ▶ croak "Cannot $action a row with no table" unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 14 14
630 14 return $table;
631 }
632  
633 =pod
634  
635 =over 4
636  
637 =item $row->_fetch_missing(@names)
638  
639 Fetches the missing fields, all of them with one C<refresh_row> that also
640 asks for the fields already present, and takes the row data it returns. A vivified row fetches nothing: its missing fields
641 read as undef. Croaks for a field the table does not have, and for a deleted
642 row.
643  
644 A row that is not canon reads just the missing fields through a C<no_cache>
645 handle and adds them to its own row data: the row cache is not touched, and
646 the row stays not canon.
647  
648 =back
649  
650 =cut
651  
652 29 ▶sub _fetch_missing {
Subroutine _fetch_missing: called CC=11 SCAR=24.0
653 29 my $self = shift;
654 29 my (@names) = @_;
655  
656 29 my $table = $self->table;
657 29 my $fields = $self->orm->db_map->table($table)->{fields};
658 29 for my $name (@names) {
659 39 ▶ croak "The table '$table' has no field '$name'" unless $fields->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{$name} 3 36 39
660 }
661  
662 26 my $data = $self->row_data;
663 26 ▶ return unless grep { !exists $data->{$_} } @names;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not exists $data->{$_};} @names 11 15 26
664 15 ▶ return if (DBIx::QuickORM::LookupTable->row_state($data) // '') eq 'vivified';
Branch: 1 branch
BranchTrueFalseTotal
if ("DBIx::QuickORM::LookupTable"->row_state($data) // "") eq "vivified" 3 12 15
Condition100%2/2
"DBIx::QuickORM::LookupTable"->row_state($data) // ""
l!l
411
MC/DC100%1/1
"DBIx::QuickORM::LookupTable"->row_state($data) // ""
"DBIx::QuickORM::LookupTable"->row_state($data)
Truth table100%2/2
A
A= "DBIx::QuickORM::LookupTable"->row_state($data)
Aresult
11
00
665 12 $self->_require_in_db('fetch missing fields of');
666  
667 11 $self->_require_pk('fetch missing fields of');
668  
669 # A row that is not canon reads around the row cache and keeps its own data.
670 10 ▶ unless ($self->is_canon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_canon) 6 4 10
671 6 my @missing = grep { !exists $data->{$_} } @names;
672 6 ▶ my $got = $self->orm->handle($table)->no_cache->fields(\@missing)->where($self->pk)->data_only->first or croak "Cannot fetch missing fields: the row is no longer in the table '$table'";
Branch: 1 branch
BranchTrueFalseTotal
my $got = $self->orm->handle($table)->no_cache->fields(\@missing)->where($self->pk)->data_only->first or croak("Cannot fetch missing fields: the row is no longer in the table '${table}'") 6 0 6
673 6 $data->{$_} = $got->{$_} for @missing;
674 6 return;
675 }
676  
677 # The present fields are asked for too, so a changed row's new data holds them as well.
678 4 my @want = ((grep { $fields->{$_} } keys %$data), @names);
679 4 ▶ my $new = $self->orm->connection->refresh_row($table, $data, fields => \@want, raw => 1) or croak "Cannot fetch missing fields: the row is no longer in the table '$table'";
Branch: 1 branch
BranchTrueFalseTotal
my $new = $self->orm->connection->refresh_row($table, $data, "fields", \@want, "raw", 1) or croak("Cannot fetch missing fields: the row is no longer in the table '${table}'") 3 1 4
680 3 ▶ $self->set_row_data($new) unless $new == $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $new == $data 1 2 3
681 }
682  
683 =pod
684  
685 =over 4
686  
687 =item \%values = $row->_inflation_cache
688  
689 The cache of inflated values (field name to value) for the current row data.
690 A supported override point.
691  
692 When the row data has been replaced since the cache was built, a new cache is
693 started, keeping the values of fields present in both whose raw values do not
694 differ by C<< DBIx::QuickORM::LookupTable->differ >>.
695  
696 =back
697  
698 =cut
699  
700 54 ▶sub _inflation_cache {
Subroutine _inflation_cache: called CC=7 SCAR=19.5
701 54 my $self = shift;
702 54 my $data = $self->row_data;
703 54 my $old = $CACHE{$self};
704 54 ▶ return $old->{values} if $old && $old->{data} == $data;
Branch: 1 branch
BranchTrueFalseTotal
if $old and $old->{"data"} == $data 24 30 54
Condition100%3/3
$old and $old->{"data"} == $data
!ll&&!rl&&r
23724
MC/DC100%2/2
$old and $old->{"data"} == $data
$old $old->{"data"} == $data
Truth table100%3/3
A and B
A= $oldB= $old->{"data"} == $data
ABresult
0X0
100
111
705  
706 30 my %keep;
707 30 ▶ if ($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 7 23 30
708 7 my $was = $old->{data};
709 7 for my $name (keys %{$old->{values}}) {
710 5 ▶ next unless exists $data->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$name} 0 5 5
711 5 ▶ next if DBIx::QuickORM::LookupTable->differ($was->{$name}, $data->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if "DBIx::QuickORM::LookupTable"->differ($was->{$name}, $data->{$name}) 3 2 5
712 2 $keep{$name} = $old->{values}->{$name};
713 }
714 }
715  
716 30 $CACHE{$self} = {data => $data, values => \%keep};
717 30 return \%keep;
718 }
719  
720 =pod
721  
722 =over 4
723  
724 =item $value = $row->_inflated_value($name, \%field)
725  
726 The value to hand out for a present field. A supported override point.
727  
728 The raw value when it is NULL, the field is untyped, or the field is
729 C<passthrough> (its type would return it unchanged). Otherwise the value
730 cached by C<_inflation_cache>, inflated on first read: as it is when the
731 type's C<inflated_is_immutable_for_cache> is true, or as C<clone_inflated>
732 copies it.
733  
734 =back
735  
736 =cut
737  
738 267 ▶sub _inflated_value {
Subroutine _inflated_value: called CC=6 SCAR=17.9
739 267 my $self = shift;
740 267 my ($name, $field) = @_;
741  
742 267 my $raw = $self->row_data->{$name};
743 267 my $type = $field->{orm_type};
744 267 ▶ return $raw unless defined($raw) && $type && !$field->{passthrough};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and $type and not $field->{"passthrough"} 213 54 267
Condition100%6/6
defined $raw and $type and not $field->{"passthrough"}
!ll&&!rl&&r
210354
defined $raw and $type
!ll&&!rl&&r
820257
MC/DC100%3/3
defined $raw and $type and not $field->{"passthrough"}
defined $raw $type not $field->{"passthrough"}
Truth table100%4/4
A and B and not(C)
A= defined $rawB= $typeC= not $field->{"passthrough"}
ABCresult
0XX0
10X0
1100
1111
745  
746 54 my $cache = $self->_inflation_cache;
747 54 ▶ $cache->{$name} = $type->inflate($raw, $field, $self->orm->connection) unless exists $cache->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cache->{$name} 31 23 54
748 54 ▶ return $type->inflated_is_immutable_for_cache ? $cache->{$name} : $type->clone_inflated($cache->{$name});
Branch: 1 branch
BranchTrueFalseTotal
$type->inflated_is_immutable_for_cache ? : 34 20 54
749 }
750  
751 257 1;
752  
753 __END__
« blib/lib/DBIx/QuickORM/Source/Query.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Table.pm »