blib/lib/DBIx/QuickORM/Handle.pm

filter: Statementstmt 92.7% 470 / 507 Branchbran 86.3% 240 / 278 Conditioncond 72.6% 151 / 208 MC/DCmcdc 50.8% 62 / 122 Subroutinesub 94.5% 103 / 109 Podpod 96.0% 49 / 51 totaltotal 84.3% 1075 / 1275 CC 409 SCAR 20.9
CC
409
Cov
87%
_column
25.0
_column
25.0
_modify
15.0
CRAP 8.1
« blib/lib/DBIx/QuickORM/DSL.pm ↑ Summary blib/lib/DBIx/QuickORM/LookupTable.pm »
1 package DBIx::QuickORM::Handle;
2 258 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 258 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 258 our $VERSION = '0.000033';
6  
7 258 ▶use Atomic::Pipe();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 258 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 258 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 258 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 258 ▶use Storable qw/freeze thaw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 258 ▶use DBIx::QuickORM::AggregateRow();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 258 ▶use DBIx::QuickORM::Iterator();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 258 ▶use DBIx::QuickORM::Source::Join();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 258 ▶use DBIx::QuickORM::Value();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 258 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
19 <orm
20 <source
21 <state
22 +where
23 +order_by
24 +limit
25 +offset
26 +fields
27 +row_class
28 +iterator
29 +group_by
30 +having
31 <mode
32 +columns
33 +no_cache
34 +where_builders
35 +temp_table
36 +temp_view
37 258 };
38  
39 =pod
40  
41 =encoding UTF-8
42  
43 =head1 NAME
44  
45 DBIx::QuickORM::Handle - Build a query and fetch its rows.
46  
47 =head1 DESCRIPTION
48  
49 A handle is a query over a source (L<DBIx::QuickORM::Role::Source>): a table,
50 an SQL query, or tables joined together. Modifiers such as C<where> and
51 C<order_by> each return a new handle, so a handle can be refined and reused
52 freely. The fetching methods run the query and hand out rows. The writing
53 methods update or delete what it matches.
54  
55 A handle is stateless until its query is sent. Then the results are cached
56 on it until C<clear>. Every reading method and every C<iter> shares that one
57 run of the query. A modifier's new handle starts without it.
58  
59 Running the query fetches every row at once, ingests the rows of a table with
60 a primary key into the lookup table (see L<DBIx::QuickORM>) unless the handle
61 is C<no_cache> (or C<bare>), and hands them out through an iterator
62 (L<DBIx::QuickORM::Iterator>).
63  
64 The SQL comes from the ORM's C<sql_builder>, whose documentation describes the
65 syntax of C<where> and C<order_by>. The default is
66 L<DBIx::QuickORM::SQLBuilder::SQLAbstract>, using L<SQL::Abstract>'s syntax.
67  
68 Rows are built with the handle's C<row_class> when one was set (even to undef,
69 for row data), otherwise as the ORM resolves them for the table. Rows of a
70 query use the ORM's C<row_class>. Rows of a join are L<DBIx::QuickORM::JoinRow>
71 objects (see L</Joins>), and computed rows are L<DBIx::QuickORM::AggregateRow>
72 objects (see L</Computed columns>).
73  
74 =head1 SYNOPSIS
75  
76 my $h = $orm->handle('users')
77 ->where({name => {-like => 'A%'}})
78 ->order_by('name')
79 ->limit(10);
80  
81 my $n = $h->count;
82 my @all = $h->all;
83  
84 my $user = $orm->handle('users')->find($id);
85 $orm->handle('posts')->where({views => 0})->delete;
86  
87 =head1 EXAMPLES
88  
89 =head2 Filter, sort, and page
90  
91 my @page = $orm->handle('users')
92 ->where({
93 name => {-like => 'A%'},
94 email => {'!=' => undef},
95 })
96 ->order_by([{-desc => 'created'}, 'name'])
97 ->limit(20)
98 ->offset(40)
99 ->all;
100  
101 =head2 Narrow or widen a condition
102  
103 my $alice = $orm->handle('users')->where({name => 'Alice'});
104 my $emailed = $alice->and({email => {'!=' => undef}}); # both conditions
105 my $either = $alice->or({name => 'Bob'}); # either condition
106 my $bob = $alice->where({name => 'Bob'}); # where replaces the condition
107  
108 =head2 Compare with SQL
109  
110 use DBIx::QuickORM::Value qw/sql/;
111  
112 my @old = $orm->handle('users')
113 ->where({created => {'<' => sql("now() - interval '30 days'")}})
114 ->all;
115  
116 =head2 Read only some fields
117  
118 my @users = $orm->handle('users')->fields(['name'])->all; # name, plus the primary key
119 # a field not fetched loads on first read
120 my $email = $users[0]->field('email');
121  
122 =head2 Get plain hashrefs instead of row objects
123  
124 my @data = $orm->handle('users')->data_only->all;
125 my $name = $data[0]->{name};
126  
127 # hashrefs the row cache never sees
128 my @bare = $orm->handle('users')->bare->all;
129  
130 =head2 Query with your own SQL
131  
132 my $dupes = $orm->handle(
133 \['SELECT name, COUNT(*) AS n FROM users GROUP BY name HAVING COUNT(*) > ?', 1],
134 );
135 my @dupes = $dupes->all;
136 my @named = $dupes->where({name => 'Alice'})->all; # the query becomes a subquery
137  
138 =head2 Insert many rows and get them back
139  
140 my $posts = $orm->handle('posts')->insert_many(
141 [
142 map {
143 +{
144 user_id => $id,
145 title => "Post $_",
146 }
147 } 1 .. 3
148 ],
149 ); # an arrayref of the new rows
150  
151 =head2 Loop over the same rows twice at once
152  
153 Each C<iter> call returns a new iterator with its own position, so each loop
154 has its own. They share the handle's one run of the query.
155  
156 my $h = $orm->handle('posts')->order_by('id');
157  
158 my @pairs;
159 my $outer = $h->iter;
160 while (my $left = $outer->next) {
161 my $inner = $h->iter; # the same rows, no second query
162 while (my $right = $inner->next) {
163 push @pairs => [$left->field('id'), $right->field('id')];
164 }
165 }
166  
167 =head1 ATTRIBUTES
168  
169 =over 4
170  
171 =item $orm = $h->orm
172  
173 =item $source = $h->source
174  
175 Both required.
176  
177 =item $state = $h->state
178  
179 The iterator state (see L<DBIx::QuickORM::Iterator>), undef unless given. A
180 new C<where> (also through C<and> or C<or>) or a join drops it.
181  
182 =item $mode = $h->mode
183  
184 C<async>, C<aside>, or C<forked>, or undef for an ordinary handle.
185  
186 =back
187  
188 =cut
189  
190 390 ▶sub init {
Subroutine init: called CC=4 SCAR=20.1
Pod undocumented
191 0 ▶ my $self = shift;
Subroutine init: not called CC=4 SCAR=20.1
Pod undocumented
192 0 ▶ croak "An 'orm' is required" unless $self->{+ORM};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"orm"} 0 390 390
unless $self->{"orm"} 0 0 0
193 0 ▶ croak "A 'source' is required (a DBIx::QuickORM::Role::Source)" unless blessed($self->{+SOURCE}) && $self->{+SOURCE}->DOES('DBIx::QuickORM::Role::Source');
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($self->{"source"}) and $self->{"source"}->DOES("DBIx::QuickORM::Role::Source") 1 389 390
unless builtin::blessed($self->{"source"}) and $self->{"source"}->DOES("DBIx::QuickORM::Role::Source") 0 0 0
Condition33%2/6
builtin::blessed($self->{"source"}) and $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
!ll&&!rl&&r
10389
builtin::blessed($self->{"source"}) and $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
!ll&&!rl&&r
000
MC/DC50%1/2
builtin::blessed($self->{"source"}) and $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
builtin::blessed($self->{"source"}) $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
MC/DC0%0/2
builtin::blessed($self->{"source"}) and $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
builtin::blessed($self->{"source"}) $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
Truth table66%2/3
A and B
A= builtin::blessed($self->{"source"})B= $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($self->{"source"})B= $self->{"source"}->DOES("DBIx::QuickORM::Role::Source")
ABresult
0X0
100
111
194 }
195  
196 =head1 PUBLIC METHODS
197  
198 =head2 Modifiers
199  
200 my $users = $orm->handle('users');
201 my $recent = $users->order_by({-desc => 'created'})->limit(5); # $users is unchanged
202 # 5: without an argument, the setting
203 my $limit = $recent->limit;
204  
205 Each returns a new handle with the one setting replaced and no cached rows, and
206 leaves the handle it was called on unchanged. So a modifier called in void
207 context would do nothing, and croaks instead. One that takes a value returns
208 the current setting when called without one. An argument of the wrong shape croaks, at the
209 latest when the query runs.
210  
211 =over 4
212  
213 =item $h2 = $h->where($where)
214  
215 The condition: a hashref or arrayref in the builder's syntax, an C<sql()>
216 marker, or a plain string of SQL.
217  
218 use DBIx::QuickORM::Value qw/sql/;
219  
220 my $users = $orm->handle('users');
221 my $alice = $users->where({name => 'Alice'});
222 my $either = $users->where([{name => 'Alice'}, {name => 'Bob'}]); # an arrayref is OR
223 my $lower = $users->where(sql('lower(name) = ?', 'alice')); # SQL with binds
224 my $silent = $users->where('email IS NULL'); # SQL used as written
225 my $where = $alice->where; # {name => 'Alice'}
226  
227 The where builders render it (see C<where_builders>).
228  
229 A plain string is raw SQL, used as written. Never interpolate untrusted input
230 into it. Pass values as binds with C<sql('... ?', $value)>.
231  
232 A new C<where> replaces the old one entirely, including one C<follow_fk> set.
233 Use C<and> to narrow a handle's condition instead.
234  
235 =item $h2 = $h->and($where)
236  
237 =item $h2 = $h->or($where)
238  
239 The handle with its C<where> combined with C<$where>: rows must meet both
240 (C<and>) or either (C<or>). See L</Narrow or widen a condition> for the
241 common use.
242  
243 # the string goes in parentheses, so its OR stays grouped:
244 # (name = 'Amy' OR name = 'Bob') AND email IS NOT NULL
245 my $emailed = $orm->handle('users')
246 ->where(q{name = 'Amy' OR name = 'Bob'})
247 ->and({email => {'!=' => undef}});
248  
249 Both need a condition to add to: on a handle without a C<where>, or with an
250 empty one (C<{}>, C<[]>, or C<''>), they croak. Use C<where> there. Value
251 markers work in either part. Neither returns the current setting, but
252 C<where> does.
253  
254 A side that is wholly a string, C<sql()>, or C<< \[...] >> is put in
255 parentheses when combined, so an C<OR> in it stays grouped. A literal you nest
256 yourself inside C<-and> or C<-or> is yours to parenthesize.
257  
258 =item $h2 = $h->order_by($order)
259  
260 The order of the rows, in the builder's syntax.
261  
262 =item $h2 = $h->limit($n)
263  
264 =item $h2 = $h->offset($n)
265  
266 The most rows to return, and how many rows to skip before them.
267  
268 =item $h2 = $h->fields(\@fields)
269  
270 Selects only these fields. The primary key is always added. Without it the
271 table's default fields are selected (every column for a query).
272  
273 =item $h2 = $h->fields({$name => $spec, ...})
274  
275 Computed columns, each selected as C<$name>. See L</Computed columns>. The
276 rows are computed rows.
277  
278 =item $h2 = $h->group_by(@fields)
279  
280 =item $h2 = $h->having($condition)
281  
282 Group the rows by these fields, and keep the groups matching the condition.
283 The rows are computed rows.
284  
285 use DBIx::QuickORM::Value qw/sql/;
286  
287 # one row per user with more than two posts, holding user_id
288 my @busy = $orm->handle('posts')
289 ->group_by('user_id')
290 ->having(sql('COUNT(*) > ?', 2))
291 ->all;
292  
293 # a computed field's name works in having
294 my @read = $orm->handle('posts')
295 ->group_by('user_id')
296 ->fields({
297 user_id => 'user_id',
298 total_views => [sum => 'views'],
299 })
300 ->having({total_views => {'>' => 100}})
301 ->all;
302  
303 # several fields are a list, not an arrayref
304 my @pairs = $orm->handle('posts')
305 ->group_by('user_id', 'views')
306 ->all; # one row per user and view count
307  
308 C<having> is in the builder's syntax, as for C<where>. It can name grouped
309 fields and computed fields, or take an C<sql()> marker such as
310 C<< sql('COUNT(*) > ?', 2) >>. A computed field's name goes to MySQL and
311 MariaDB as written. Elsewhere the builder writes the field's SQL in its place
312 (PostgreSQL rejects a select alias in C<HAVING>).
313  
314 Without computed fields the handle selects the grouped fields, or the
315 C<fields> given as a list. A handle with C<having> but neither croaks when
316 run.
317  
318 SQLite sends a bind in an C<sql()> marker as text, which an aggregate's number
319 never matches. Cast the bind there:
320 C<< sql('COUNT(*) > CAST(? AS INTEGER)', $n) >>.
321  
322 =item $h2 = $h->row_class($class_or_undef)
323  
324 The class rows from this handle are built with. Undef means row data.
325  
326 package My::User;
327 use parent 'DBIx::QuickORM::Row';
328  
329 sub greeting { 'Hello, ' . $_[0]->field('name') }
330  
331 package main;
332  
333 my $user = $orm->handle('users')->row_class('My::User')->find($id);
334 my $hello = $user->greeting;
335  
336 my $data = $orm->handle('users')->row_class(undef)->find($id); # a hashref
337  
338 A class without a C<new> is loaded now, as the ORM loads its row classes. One
339 that cannot be loaded croaks.
340  
341 =item $h2 = $h->data_only
342  
343 Shorthand for C<row_class(undef)>: rows come back as row data, as the
344 CRUD operations' C<< data => 1 >> gives. A row object given to C<update_row> or
345 C<refresh_row> is still returned as itself. Pass C<< data => 1 >> for its row
346 data.
347  
348 Table rows still go through the lookup table. For a table with a primary key
349 each hashref is the cache's own copy, so never change it. Use C<bare> for
350 hashrefs the cache never sees.
351  
352 It takes no argument. C<row_class($class)> undoes it.
353  
354 =item $h2 = $h->no_cache
355  
356 Its reads skip the lookup table: rows come back as fetched, and nothing the
357 ORM holds changes. C<find> and C<refresh_row> are not such reads. They use the
358 cache as their own options say.
359  
360 my $fresh = $orm->handle('users')->no_cache;
361 my @seen = $fresh->all; # as fetched, not canonical
362 my $user = $fresh->find($id); # find still gives the canonical row
363 my @canon = $fresh->cache->all; # back through the lookup table
364  
365 It takes no argument. Undo it with C<cache>.
366  
367 The rows, row objects or hashrefs, are not canonical. Writing with them
368 croaks under the default C<enforce_canon>. C<find> or C<refresh_row> gives a
369 canonical row.
370  
371 =item $h2 = $h->cache
372  
373 Undoes C<no_cache>: reads go through the lookup table again and their rows are
374 canonical. It takes no argument.
375  
376 =item $h2 = $h->bare
377  
378 Shorthand for C<data_only> and C<no_cache> together: rows come back as
379 hashrefs, as fetched, and the lookup table never sees them. Changing one
380 changes nothing the ORM holds, though the handle's other reads hand out the
381 same hashref.
382  
383 They are not canonical, so writing with them croaks under the default
384 C<enforce_canon>. It takes no argument. C<row_class($class)> brings back row
385 objects, and C<cache> brings back the lookup table.
386  
387 =item $h2 = $h->where_builders(\@builders)
388  
389 The where builders this handle's C<where> is rendered through, replacing the
390 ORM's C<where_builders> (see L<DBIx::QuickORM>).
391  
392 # this handle's where always goes to SQL::Abstract
393 my $h = $orm->handle('users')
394 ->where_builders(['DBIx::QuickORM::WhereBuilder::SQLAbstract'])
395 ->where({name => 'Bob'});
396 my $builders = $h->where_builders; # the handle's own list
397  
398 Each class is loaded as the ORM loads them. Without an argument it returns
399 the handle's own setting, undef unless set.
400  
401 =back
402  
403 Computed rows are keyed by the selected names, which the order may name.
404 They belong to no table: no primary key is added and they never enter the
405 lookup table.
406  
407 They are L<DBIx::QuickORM::AggregateRow> objects, which inflate and follow
408 foreign keys by each column's spec. When the row class resolved for the source
409 (the table's, or the ORM's) is undef they are hashrefs. A handle C<row_class>
410 replaces either: undef for hashrefs, or a class built as for a query's rows.
411  
412 =head2 Computed columns
413  
414 use DBIx::QuickORM::Value qw/sql/;
415  
416 my @per_user = $orm->handle('posts')
417 ->group_by('user_id')
418 ->order_by('user_id')
419 ->fields({
420 # the field itself, with its type and foreign key
421 user_id => 'user_id',
422 # COUNT(*)
423 posts => [count => '*'],
424 # an SQL expression: no type, no foreign key
425 titles => sql('COUNT(DISTINCT title)'),
426 # SUM(views)
427 views => [sum => 'views'],
428 # MAX(id), followed to that post
429 latest => {
430 agg => [max => 'id'],
431 fk => 'posts',
432 },
433 })
434 ->all;
435  
436 my $count = $per_user[0]->field('posts');
437 my $user = $per_user[0]->follow_fk('users')->only;
438 my $latest = $per_user[0]->follow_fk('posts')->only;
439  
440 C<< fields({$name => $spec}) >> takes one of these per column:
441  
442 =over 4
443  
444 =item $name => 'field'
445  
446 The field itself (C<alias.field> on a join), with the facts C<like> gives
447 below: its type and foreign keys.
448  
449 =item $name => sql(...)
450  
451 =item $name => \'SQL'
452  
453 An SQL expression (see L<DBIx::QuickORM::Value>), with no type and no
454 foreign key. C<\'SQL'> is short for C<sql('SQL')>.
455  
456 =item $name => [$func, 'field']
457  
458 C<FUNC(field)>, the field quoted for the database. C<$func> must be a word
459 (it is written into the SQL), and the field must exist. When C<$func> is
460 C<min> or C<max>, or one of the ORM's C<like_aggregates> (any case), the
461 result is one of the field's values, so the column takes the field's type
462 and foreign keys. Any other function's result has neither.
463  
464 The field C<*> gives C<FUNC(*)>: C<< [count => '*'] >> counts rows. Such a
465 column has no type or foreign key. A function that does not take C<*> fails
466 when the query runs, with the database's error.
467  
468 =item $name => {sql => sql(...) | agg => [$func, 'field'], type => $type, db_type => $db_type, fk => $fk, like => 'field'}
469  
470 A column with its facts adjusted.
471  
472 use DBIx::QuickORM::Value qw/sql/;
473  
474 my $stats = $orm->handle('users')
475 ->fields({
476 # \'SQL' is short for sql('SQL')
477 users => \'COUNT(*)',
478 # the created field's type (a lazy DateTime), declared as a date
479 newest => {
480 sql => sql('CAST(MAX(created) AS date)'),
481 like => 'created',
482 db_type => 'date',
483 },
484 # a foreign key to a field: [$table, $field] or {$table => $field}
485 referrer => {
486 sql => sql('MIN(referred_by)'),
487 fk => ['users', 'id'],
488 },
489 # a type of your choosing: an arrayref from JSON
490 names => {
491 sql => sql('json_agg(name ORDER BY name)'),
492 type => 'JSON',
493 },
494 })
495 ->only;
496  
497 my $newest = $stats->field('newest');
498 my $day = $newest->datetime_ref->ymd; # datetime_ref gives the DateTime
499 my $names = $stats->field('names'); # ['Alice', 'Amy', ...]
500 my $first = $stats->follow_fk('users')->only; # the referrer's row
501  
502 Over no rows the aggregates give NULL, so those columns are undef then.
503  
504 Exactly one of C<sql> or C<agg>, which work as above. The other keys adjust
505 the column:
506  
507 =over 4
508  
509 =item like => 'field'
510  
511 The column takes the field's facts: its type, its declared type (and what
512 follows from it, such as affinity), and its single-field foreign keys. With
513 C<like>, nothing comes from C<agg>'s field.
514  
515 =item type => $type
516  
517 The type, named as in the ORM's C<types>. Replaces what C<like> or C<agg>
518 gave.
519  
520 =item db_type => $db_type
521  
522 The declared type. Replaces what C<like> or C<agg> gave, but keeps the type
523 they implied. Give C<type> too to change it.
524  
525 =item fk => $fk
526  
527 A single-field foreign key, replacing what C<like> or C<agg> gave: a table
528 name (that table's primary key, which must be one field), C<[$table, $field]>,
529 or C<< {$table => $field} >>.
530  
531 =back
532  
533 =back
534  
535 Croaks on an unknown key, an unknown field, a function name that is not a
536 word, or an C<fk> naming a table or field not in the map.
537  
538 =head2 Running elsewhere
539  
540 # the query starts, and iter() returns at once
541 my $it = $orm->handle('posts')->async->iter;
542 do_something_else() until $it->ready;
543 my @posts = $it->all;
544  
545 my $aside = $orm->handle('posts')->aside->iter; # on a second connection
546 my $forked = $orm->handle('posts')->forked->iter; # in a child process
547 $orm->insert(users => {name => 'Busy'}); # the ORM's own connection stays free
548 my @all = ($aside->all, $forked->all);
549  
550 =over 4
551  
552 =item $h2 = $h->async
553  
554 The handle's query starts without waiting on its first read: C<iter> returns a
555 pending iterator (see L<DBIx::QuickORM::Iterator>), whose C<ready> checks without
556 waiting and whose reading methods wait for the rows. Every iterator of the
557 handle shares that one query.
558  
559 The query runs on the ORM's own connection, so its rows are canonical like any
560 other's, inside a transaction or not. Until it is collected the connection is
561 busy, and every other query, write, and transaction statement on it croaks.
562  
563 A query not yet collected is cancelled once nothing can read it: the handle
564 is freed or cleared, and so is every iterator over it. A transaction rolled
565 back because its block threw or it went out of scope cancels it too. Its
566 iterators then throw when read. On PostgreSQL, cancelling inside a
567 transaction aborts that transaction. MySQL and MariaDB cannot cancel, so they
568 wait for the result and discard it.
569  
570 Needs a database with async queries (PostgreSQL, MySQL, MariaDB). Croaks on
571 any other. On some platforms a child forked while a PostgreSQL async query is
572 pending may crash on exit (L<https://github.com/bucardo/dbdpg/issues/218>).
573  
574 =item $h2 = $h->aside
575  
576 Like C<async>, but each query runs on a new connection from the ORM's
577 connector, so the ORM's own connection stays free.
578  
579 The connection is closed once the rows are collected, or when the query is
580 cancelled before that. It sees only committed data. Its rows are ingested as
581 usual.
582  
583 Starting the query, or reading its rows (C<ready> included), croaks while a
584 transaction is open. A query started before a transaction may run during it,
585 so read it after the transaction ends. Its rows may then be stale if the
586 transaction changed the same rows.
587  
588 Needs a database with async queries that a second connection can reach (not
589 an in-memory database). On an ORM given a C<dbh>, C<aside> itself croaks.
590  
591 =item $h2 = $h->forked
592  
593 Each query runs in a forked child process, on a new connection from the
594 connector, and the child sends the rows back and exits.
595  
596 The iterator is pending as for C<async>. Rows are ingested, and transactions
597 are refused, as for C<aside>. An error in the child is thrown on
598 collection as C<The forked query failed: ...> with the child's message.
599 Cancelling the query kills and reaps the child.
600  
601 The child never touches the parent's connection. A forked query can race
602 changes the parent makes meanwhile to the same rows, as any other reader can.
603  
604 Needs a database a second connection can reach (not DuckDB or an in-memory
605 SQLite database). On an ORM given a C<dbh>, C<forked> itself croaks.
606  
607 =item $bool = $h->can_async
608  
609 =item $bool = $h->can_aside
610  
611 =item $bool = $h->can_forked
612  
613 True when the mode can run here, false where it would croak.
614  
615 =item $h2 = $h->maybe_async
616  
617 =item $h2 = $h->maybe_aside
618  
619 =item $h2 = $h->maybe_forked
620  
621 The mode when it can run, otherwise no mode change.
622  
623 my $posts = $orm->handle('posts');
624  
625 # in a child process where this database and ORM allow one
626 my $it = $posts->maybe_forked->iter;
627 my @all = $it->all;
628  
629 # the same choice by hand
630 my $h = $posts->can_forked ? $posts->forked : $posts->sync;
631  
632 # an ORM given a dbh has no second connection
633 my $given = DBIx::QuickORM->new(dbh => $dbh);
634 my $async = $given->handle('posts')->can_async; # true: its own connection
635 my $aside = $given->handle('posts')->can_aside; # false
636  
637 The check covers the database, the ORM, and temporary tables, not
638 transactions. Inside one, C<aside> and C<forked> still croak when read.
639  
640 Either way a new handle without cached rows, as for any modifier. Void
641 context croaks.
642  
643 On an ordinary handle, falling back runs the query normally on the ORM's
644 connection. A handle already in another mode keeps it. Inside a
645 transaction that query sees the transaction's own uncommitted writes, while
646 C<aside> and C<forked> see committed data only.
647  
648 Everything else (C<iter>, C<ready>, C<all>, C<count>) works the same either
649 way.
650  
651 =item $h2 = $h->sync
652  
653 A new handle with the same query and no background mode: its reads run on the
654 ORM's connection when asked for.
655  
656 my $aside = $orm->handle('posts')->aside;
657 my $now = $aside->sync; # $now->mode is undef
658  
659 # a temporary table needs an ordinary handle
660 my $t = $orm->temp_table($aside->sync);
661  
662 Like any modifier, it keeps no cached rows, and void context croaks.
663  
664 =back
665  
666 =head2 Joins
667  
668 my @rows = $orm->handle('posts')
669 ->join('users', as => 'author')
670 ->where({
671 'author.name' => 'Alice',
672 'posts.views' => {'>' => 10},
673 })
674 ->order_by('posts.id')
675 ->all;
676  
677 for my $row (@rows) {
678 my $post = $row->row('posts');
679 my $author = $row->row('author');
680 my $title = $row->data->{'posts.title'};
681 }
682  
683 # Every user and their posts, keeping users without any.
684 my @users = $orm->handle('users')
685 ->left_join('posts', link => {id => 'user_id'})
686 ->order_by('users.name')
687 ->all;
688  
689 =over 4
690  
691 =item $h2 = $h->join($table, as => $name, from => $name, link => \%link)
692  
693 =item $h2 = $h->join($table, as => $name, from => $name, via => \%via)
694  
695 =item $h2 = $h->inner_join($table, ...)
696  
697 =item $h2 = $h->left_join($table, ...)
698  
699 Join another table to a table handle, or to a join handle (see
700 L<DBIx::QuickORM::Source::Join>). C<join> and C<inner_join> are inner joins,
701 C<left_join> a left join.
702  
703 # Posts, their author, and whoever referred the author.
704 my @rows = $orm->handle('posts')
705 ->join('users', as => 'author')
706 ->left_join(
707 'users',
708 as => 'referrer',
709 from => 'author',
710 link => {referred_by => 'id'},
711 )
712 ->all;
713 my $referrer = $rows[0]->row('referrer'); # undef when nobody referred the author
714  
715 # Different users sharing a name. No key links the names, so via names the fields.
716 my @same = $orm->handle('users')
717 ->inner_join(
718 'users',
719 as => 'twin',
720 via => {name => 'name'},
721 )
722 ->where('users.id <> twin.id')
723 ->data_only
724 ->all;
725 my $twin = $same[0]->{'twin.name'}; # row data, keyed component.field
726  
727 Options (an unknown one croaks):
728  
729 =over 4
730  
731 =item as => $name
732  
733 The component's name, the table name by default. A table can be joined more
734 than once, even to itself, under different names. A name already taken croaks.
735  
736 =item from => $name
737  
738 The component the link starts at, the base table by default.
739  
740 =item link => \%link
741  
742 The foreign key to join on, C<< {from_field => field} >>, as C<follow_fk>
743 takes it (see L<DBIx::QuickORM/Following foreign keys>). Without it there
744 must be exactly one foreign key between the two tables.
745  
746 =item via => \%via
747  
748 The fields to join on when the map knows no key between the tables, in the
749 same form: C<< via => {editor_id => 'id'} >>. Every field must exist. Not with
750 C<link>.
751  
752 =back
753  
754 On a join handle C<where>, C<order_by>, C<fields>, C<group_by>, and C<having>
755 name fields as C<component.field>. C<fields> croaks for any other name.
756  
757 The rows are L<DBIx::QuickORM::JoinRow> objects. With C<row_class(undef)> they
758 are each row's raw data instead, a hashref keyed C<component.field>. Any other
759 C<row_class> croaks. A join cannot be written through: write through its
760 component rows.
761  
762 =back
763  
764 =cut
765  
766 214 ▶sub where { shift->_modify(where => WHERE(), @_) }
Subroutine where: called CC=1 SCAR=0.0
Pod documented
767 54 ▶sub order_by { shift->_modify(order_by => ORDER_BY(), @_) }
Subroutine order_by: called CC=1 SCAR=0.0
Pod documented
768 39 ▶sub limit { shift->_modify(limit => LIMIT(), @_) }
Subroutine limit: called CC=1 SCAR=0.0
Pod documented
769 5 ▶sub offset { shift->_modify(offset => OFFSET(), @_) }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
770 142 ▶sub fields { shift->_modify(fields => FIELDS(), @_) }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
771 17 ▶sub having { shift->_modify(having => HAVING(), @_) }
Subroutine having: called CC=1 SCAR=0.0
Pod documented
772  
773 # Perl keywords, but fine as method names: they are only ever called as $h->and(...) and $h->or(...).
774 20 ▶sub and { shift->_combine_where(and => @_) }
Subroutine and: called CC=1 SCAR=0.0
Pod documented
775 15 ▶sub or { shift->_combine_where(or => @_) }
Subroutine or: called CC=1 SCAR=0.0
Pod documented
776  
777 3 ▶sub async { $_[0]->_run_mode('async') }
Subroutine async: called CC=1 SCAR=0.0
Pod documented
778 3 ▶sub aside { $_[0]->_run_mode('aside') }
Subroutine aside: called CC=1 SCAR=0.0
Pod documented
779 22 ▶sub forked { $_[0]->_run_mode('forked') }
Subroutine forked: called CC=1 SCAR=0.0
Pod documented
780  
781 5 ▶sub can_async { $_[0]->_cannot_run('async') ? 0 : 1 }
Subroutine can_async: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_cannot_run('async') ? : 5 0 5
782 3 ▶sub can_aside { $_[0]->_cannot_run('aside') ? 0 : 1 }
Subroutine can_aside: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_cannot_run('aside') ? : 3 0 3
783 4 ▶sub can_forked { $_[0]->_cannot_run('forked') ? 0 : 1 }
Subroutine can_forked: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_cannot_run('forked') ? : 3 1 4
784  
785 6 ▶sub maybe_async { $_[0]->_run_mode('async', 1) }
Subroutine maybe_async: called CC=1 SCAR=0.0
Pod documented
786 5 ▶sub maybe_aside { $_[0]->_run_mode('aside', 1) }
Subroutine maybe_aside: called CC=1 SCAR=0.0
Pod documented
787 6 ▶sub maybe_forked { $_[0]->_run_mode('forked', 1) }
Subroutine maybe_forked: called CC=1 SCAR=0.0
Pod documented
788  
789 1 ▶sub sync {
Subroutine sync: called CC=2 SCAR=7.1
Pod documented
790 1 my $self = shift;
791 1 ▶ croak "sync() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
792 1 return $self->_modify(sync => MODE(), undef);
793 }
794  
795 41 ▶sub join { shift->_join([caller], join => @_) }
Subroutine join: called CC=1 SCAR=0.0
Pod documented
796 3 ▶sub inner_join { shift->_join([caller], inner_join => @_) }
Subroutine inner_join: called CC=1 SCAR=0.0
Pod documented
797 6 ▶sub left_join { shift->_join([caller], left_join => @_) }
Subroutine left_join: called CC=1 SCAR=0.0
Pod documented
798  
799 28 ▶sub row_class {
Subroutine row_class: called CC=4 SCAR=13.9
Pod documented
800 28 my $self = shift;
801 28 ▶ $self->{+ORM}->load_row_class($_[0]) if @_ == 1 && defined($_[0]) && !ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and defined $_[0] and not ref $_[0] 18 10 28
Condition100%6/6
@_ == 1 and defined $_[0] and not ref $_[0]
!ll&&!rl&&r
9118
@_ == 1 and defined $_[0]
!ll&&!rl&&r
2719
MC/DC100%3/3
@_ == 1 and defined $_[0] and not ref $_[0]
@_ == 1 defined $_[0] not ref $_[0]
Truth table100%4/4
A and B and not(C)
A= @_ == 1B= defined $_[0]C= not ref $_[0]
ABCresult
0XX0
10X0
1100
1111
802 26 return $self->_modify(row_class => ROW_CLASS(), @_);
803 }
804  
805 47 ▶sub data_only {
Subroutine data_only: called CC=2 SCAR=6.9
Pod documented
806 47 my $self = shift;
807 47 ▶ croak "data_only() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 46 47
808 46 return $self->_modify(data_only => ROW_CLASS(), undef);
809 }
810  
811 25 ▶sub no_cache {
Subroutine no_cache: called CC=2 SCAR=6.9
Pod documented
812 25 my $self = shift;
813 25 ▶ croak "no_cache() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 24 25
814 24 return $self->_modify(no_cache => NO_CACHE(), 1);
815 }
816  
817 5 ▶sub cache {
Subroutine cache: called CC=2 SCAR=6.9
Pod documented
818 5 my $self = shift;
819 5 ▶ croak "cache() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 4 5
820 4 return $self->_modify(cache => NO_CACHE(), 0);
821 }
822  
823 9 ▶sub bare {
Subroutine bare: called CC=2 SCAR=6.9
Pod documented
824 9 my $self = shift;
825 9 ▶ croak "bare() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 8 9
826 8 return $self->_modify(bare => ROW_CLASS(), undef)->_modify(bare => NO_CACHE(), 1);
827 }
828  
829 6 ▶sub where_builders {
Subroutine where_builders: called CC=2 SCAR=6.9
Pod documented
830 6 my $self = shift;
831 6 ▶ my @args = @_ == 1 ? ($self->{+ORM}->_where_builders('where_builders()', $_[0])) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4 2 6
832 5 return $self->_modify(where_builders => WHERE_BUILDERS(), @args);
833 }
834  
835 41 ▶sub group_by {
Subroutine group_by: called CC=5 SCAR=16.2
Pod documented
836 41 my $self = shift;
837 41 ▶ return $self->{+GROUP_BY} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 40 41
838 40 ▶ croak "group_by() takes field names" if grep { !defined($_) || ref($_) || !length($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_ or not length $_;} @_ 1 39 40
Condition66%4/6
!defined($_) || ref $_ || !length($_)
l!l&&r!l&&!r
1039
not defined $_ or ref $_
l!l&&r!l&&!r
0139
MC/DC33%1/3
!defined($_) || ref $_ || !length($_)
not defined $_ ref $_ !length($_)
Truth table50%2/4
not(A) or B || not(C)
A= not defined $_B= ref $_C= !length($_)
ABCresult
1XX1
01X1
0011
0000
839 39 return $self->_modify(group_by => GROUP_BY(), [@_]);
840 }
841  
842 =head2 Fetching
843  
844 my $h = $orm->handle('users')->order_by('name');
845  
846 my $first = $h->first; # runs the query
847 my $last = $h->last; # same rows, no second query
848 my @all = $h->all;
849  
850 my $bob = $h->where({email => 'bob@example.com'})->only; # one row or undef, croaks on more
851  
852 my @names;
853 while (my $user = $h->next) { # the handle's own position
854 push @names => $user->field('name');
855 }
856 $h->reset;
857 my $again = $h->next; # the first row again
858  
859 my $it = $h->iter; # a new position over the same rows
860 $h->clear; # forget the rows, so the next read runs the query again
861  
862 =over 4
863  
864 =item $row = $h->first
865  
866 =item $row = $h->last
867  
868 =item @rows = $h->all
869  
870 =item $row = $h->only
871  
872 =item $bool = $h->ready
873  
874 =item $row = $h->next
875  
876 =item $h->reset
877  
878 The iterator methods (see L<DBIx::QuickORM::Iterator>), on the handle's own
879 iterator over the handle's one run of the query.
880  
881 The query runs on the first read (these or C<iter>), and the rows stay on the
882 handle until C<clear>. A modifier's new handle does not share them.
883  
884 =back
885  
886 =cut
887  
888 45 ▶sub first { $_[0]->_read('first') }
Subroutine first: called CC=1 SCAR=0.0
Pod documented
889 3 ▶sub last { $_[0]->_read('last') }
Subroutine last: called CC=1 SCAR=0.0
Pod documented
890 184 ▶sub all { $_[0]->_read('all') }
Subroutine all: called CC=1 SCAR=0.0
Pod documented
891 82 ▶sub only { $_[0]->_read('only') }
Subroutine only: called CC=1 SCAR=0.0
Pod documented
892 8 ▶sub ready { $_[0]->_read('ready') }
Subroutine ready: called CC=1 SCAR=0.0
Pod documented
893 7 ▶sub next { $_[0]->_read('next') }
Subroutine next: called CC=1 SCAR=0.0
Pod documented
894 2 ▶sub reset { $_[0]->_iter->reset }
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
895  
896 =pod
897  
898 =over 4
899  
900 =item $it = $h->iter
901  
902 A new iterator, every call, with its own position over the handle's one run
903 of the query. The first read sends the query. Later calls reuse its rows.
904  
905 In C<async>, C<aside>, or C<forked> mode there is one background query per
906 handle, however many iterators read it. Collecting through any of them
907 collects for all.
908  
909 =back
910  
911 =cut
912  
913 29 ▶sub iter {
Subroutine iter: called CC=1 SCAR=0.0
Pod documented
914 29 my $self = shift;
915 29 return DBIx::QuickORM::Iterator->new(query => $self->_iter->query, state => $self->{+STATE});
916 }
917  
918 =pod
919  
920 =over 4
921  
922 =item $h->clear
923  
924 Forgets the handle's cached rows and its own position, in place, so the next
925 read sends the query again. Returns nothing.
926  
927 Iterators already returned by C<iter> keep the old rows. The ORM's lookup
928 table is untouched. C<clear_cache> on the ORM clears that.
929  
930 =back
931  
932 =cut
933  
934 38 ▶sub clear { delete $_[0]->{+ITERATOR}; return }
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
935  
936 =pod
937  
938 =over 4
939  
940 =item $temp = $h->temp_table(name => $name, pk => \@fields)
941  
942 =item $temp = $h->temp_table
943  
944 =item $temp = $h->temp_table(rebuild => 1)
945  
946 =item $temp = $h->temp_view(...)
947  
948 A temporary table (or view) made from the handle's query, as
949 C<< $orm->temp_table($h, ...) >> and C<< $orm->temp_view($h, ...) >> make one,
950 kept on this handle. C<name> and C<pk> are optional.
951  
952 my $h = $orm->handle('posts')->where({views => 0});
953  
954 my $t = $h->temp_table(
955 name => 'unread',
956 pk => ['id'],
957 );
958 my $kept = $h->temp_table; # the same table, not a new one
959 my @rows = $t->handle->all;
960  
961 $t->drop;
962 my $live = $h->temp_table->is_live ? $h->temp_table : $h->temp_table(rebuild => 1);
963  
964 my $v = $h->temp_view; # a view over the same query, kept beside the table
965  
966 =over 4
967  
968 =item *
969  
970 The first call creates it. A call without arguments returns the one kept,
971 even once it no longer exists, so the caller always knows which table it
972 holds.
973  
974 =item *
975  
976 A call with C<name> or C<pk> croaks while the kept one exists. Once it no
977 longer does (dropped, rolled back, or its connection replaced), such a call
978 creates and keeps a new one.
979  
980 =item *
981  
982 C<< rebuild => 1 >> creates the kept one again, with the options that created
983 it. It croaks while the kept one still exists, on a handle that kept
984 none, and with C<name> or C<pk>.
985  
986 =back
987  
988 A new handle from a modifier does not keep this handle's table or view.
989 C<clear> leaves it kept.
990  
991 =back
992  
993 =cut
994  
995 15 ▶sub temp_table { my $self = shift; $self->_temp(table => @_) }
Subroutine temp_table: called CC=1 SCAR=0.0
Pod documented
996 4 ▶sub temp_view { my $self = shift; $self->_temp(view => @_) }
Subroutine temp_view: called CC=1 SCAR=0.0
Pod documented
997  
998 =pod
999  
1000 =over 4
1001  
1002 =item $n = $h->count
1003  
1004 How many rows match the C<where>, counted by the database (C<SELECT
1005 COUNT(*)>).
1006  
1007 The order, limit, and offset are ignored, and nothing is ingested. It runs at
1008 once on the ORM's connection, whatever the handle's mode. Croaks with
1009 a C<group_by> or C<having>. Count groups with computed fields instead,
1010 C<< fields({n => [count => '*']}) >>.
1011  
1012 =back
1013  
1014 =cut
1015  
1016 21 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
1017 21 my $self = shift;
1018  
1019 21 my @set = grep { defined $self->{$_} } GROUP_BY(), HAVING();
1020 21 ▶ croak "count() cannot count groups (the handle has a " . CORE::join(', ' => @set) . "); select fields({n => [count => '*']}) instead" if @set;
Branch: 1 branch
BranchTrueFalseTotal
if @set 3 18 21
1021  
1022 18 my ($orm, $source) = @{$self}{+ORM, +SOURCE};
1023 18 my $conn = $orm->connection;
1024 18 my $row = $conn->select($source, $orm->sql_builder->count($source, $conn->dialect, $self->_where_request))->[0];
1025 18 return (values %$row)[0];
1026 }
1027  
1028 =head2 Writing
1029  
1030 my $posts = $orm->handle('posts');
1031  
1032 my $post = $posts->insert({
1033 user_id => $id,
1034 title => 'Draft',
1035 });
1036 $posts->update_row($post, {title => 'Final'});
1037 $posts->delete_row($post);
1038  
1039 my $changed = $posts->where({user_id => $id})->update({views => 0}); # how many rows changed
1040 my $deleted = $posts->where({views => 0})->delete; # how many rows went
1041  
1042 These need a table handle. On a query or join handle they croak.
1043  
1044 =over 4
1045  
1046 =item $count = $h->update(\%changes)
1047  
1048 =item $count = $h->delete
1049  
1050 =item $count = $h->delete(mark_deleted => 1)
1051  
1052 Updates or deletes every row matching the C<where> (every row of the table
1053 without one) in one statement, and returns the number of rows the database
1054 reports.
1055  
1056 Changes are deflated as for C<update_row>, and a field the table does not have
1057 croaks. C<update> takes exactly one hashref. C<delete> takes only
1058 C<mark_deleted>. Both croak when the handle has an order, limit, offset,
1059 C<group_by>, or C<having>.
1060  
1061 The ORM then drops every canonical copy of the table's rows, since it cannot
1062 tell which rows changed. Inside a transaction that includes the table's rows
1063 cached from before the transaction began: they are dropped, and rolling the
1064 outermost transaction back does not bring them back.
1065  
1066 C<< mark_deleted => 1 >> makes C<delete> also learn which rows it deleted.
1067 Held rows for them say C<was_deleted>, as after a row C<delete>. The table's
1068 other rows are dropped as usual. A rollback undoes the marks. A table
1069 without a primary key croaks.
1070  
1071 my $stale = $posts->find($post_id);
1072 $posts->where({views => 0})->delete(mark_deleted => 1);
1073 my $gone = $stale->was_deleted; # true if that post had no views
1074  
1075 The cost: the database sends back the primary key of every deleted row, and
1076 the ORM holds them all at once, so a huge delete uses memory in proportion.
1077  
1078 MySQL has no C<RETURNING> on C<DELETE> (MariaDB has), so there it is slower:
1079 a C<SELECT ... FOR UPDATE> of the keys runs first, locking the rows until the
1080 transaction ends, then the C<DELETE>. Outside a transaction both run in one
1081 of their own. A row another session inserts and commits between the two
1082 statements may be deleted without being marked (MySQL and Percona at
1083 C<READ COMMITTED>, not MariaDB, which uses C<RETURNING>).
1084  
1085 =back
1086  
1087 =cut
1088  
1089 14 ▶sub update {
Subroutine update: called CC=5 SCAR=16.5
Pod documented
1090 14 my $self = shift;
1091 14 my ($changes) = @_;
1092 14 my $debug = $self->{+ORM}->debug;
1093 14 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 14 14
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1094  
1095 14 ▶ croak "update() takes one hashref of changes" unless @_ == 1 && ref($changes) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and ref $changes eq "HASH" 1 13 14
Condition66%2/3
@_ == 1 and ref $changes eq "HASH"
!ll&&!rl&&r
0113
MC/DC50%1/2
@_ == 1 and ref $changes eq "HASH"
@_ == 1 ref $changes eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $changes eq "HASH"
ABresult
0X0
100
111
1096  
1097 13 my $table = $self->_bulk_table('update');
1098 11 my ($orm, $source) = @{$self}{+ORM, +SOURCE};
1099 11 my $conn = $orm->connection;
1100 11 return $conn->bulk_write($source, $orm->sql_builder->update($source, $conn->dialect, $conn->deflate($table, $changes), $self->_where_request));
1101 }
1102  
1103 23 ▶sub delete {
Subroutine delete: called CC=10 SCAR=23.2
Pod documented
1104 23 my $self = shift;
1105 23 my $debug = $self->{+ORM}->debug;
1106 23 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 23 23
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1107 23 ▶ croak "delete() takes only 'mark_deleted => \$bool'" unless !@_ || (@_ == 2 && $_[0] eq 'mark_deleted');
Branch: 1 branch
BranchTrueFalseTotal
unless not @_ or @_ == 2 and $_[0] eq "mark_deleted" 3 20 23
Condition100%6/6
not @_ or @_ == 2 and $_[0] eq "mark_deleted"
l!l&&r!l&&!r
1283
@_ == 2 and $_[0] eq "mark_deleted"
!ll&&!rl&&r
218
MC/DC100%3/3
not @_ or @_ == 2 and $_[0] eq "mark_deleted"
not @_ @_ == 2 $_[0] eq "mark_deleted"
Truth table100%4/4
not(A) or B and C
A= not @_B= @_ == 2C= $_[0] eq "mark_deleted"
ABCresult
1XX1
0111
00X0
0100
1108 20 my %params = @_;
1109  
1110 20 my $table = $self->_bulk_table('delete');
1111 17 my ($orm, $source) = @{$self}{+ORM, +SOURCE};
1112 17 my $conn = $orm->connection;
1113 17 my $dialect = $conn->dialect;
1114 17 my $builder = $orm->sql_builder;
1115 17 my @where = $self->_where_request;
1116 17 my ($sql, $binds) = $builder->delete($source, $dialect, @where);
1117 17 ▶ return $conn->bulk_write($source, $sql, $binds) unless $params{mark_deleted};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"mark_deleted"} 10 7 17
1118  
1119 7 my $pk = $conn->_table($table)->{pk};
1120 7 ▶ croak "delete(mark_deleted => 1) needs a primary key, and the table '$table' has none" unless $pk && @$pk;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk and @$pk 1 6 7
Condition66%2/3
$pk and @$pk
!ll&&!rl&&r
106
MC/DC50%1/2
$pk and @$pk
$pk @$pk
Truth table66%2/3
A and B
A= $pkB= @$pk
ABresult
0X0
100
111
1121 6 ▶ my @keys = $dialect->supports_returning_delete ? () : (keys => [$builder->select($source, $dialect, fields => $pk, @where)]);
Branch: 1 branch
BranchTrueFalseTotal
$dialect->supports_returning_delete ? : 6 0 6
1122 6 return $conn->bulk_write($source, $sql, $binds, mark_deleted => 1, @keys);
1123 }
1124  
1125 =pod
1126  
1127 =over 4
1128  
1129 =item $row = $h->insert(\%fields, %opts)
1130  
1131 =item \@rows = $h->insert_many(\@rows, %opts)
1132  
1133 =item $row = $h->find($pk, %opts)
1134  
1135 =item $row = $h->update_row($row_or_data, \%changes, %opts)
1136  
1137 =item $count = $h->delete_row($row_or_data, %opts)
1138  
1139 =item $row = $h->refresh_row($row_or_data, %opts)
1140  
1141 The ORM's CRUD operations (see L<DBIx::QuickORM/"CRUD operations">) on the
1142 handle's table, with rows built as the handle builds them. L</Writing> shows
1143 C<insert>, C<update_row>, and C<delete_row>.
1144 L</Insert many rows and get them back> shows C<insert_many>.
1145  
1146 my $users = $orm->handle('users');
1147 my $user = $users->find($id);
1148 $user = $users->refresh_row($user, cache => 0); # read again from the database
1149  
1150 my $data = $users->data_only->find($id); # data_only makes rows plain hashrefs
1151  
1152 They follow the ORM's return rule, building rows with the handle's row class.
1153  
1154 C<refresh_row> takes row data, a row, or a primary key. C<find> is
1155 C<refresh_row> given a primary key. A key comes in any form:
1156  
1157 my $users = $orm->handle('users');
1158 my $user = $users->find({id => $id}); # a hashref of the key fields (row data works)
1159 $user = $users->find([$id]); # an arrayref of the values
1160 $user = $users->find($id); # a single value, for a single-field key
1161  
1162 The arrayref holds every key field's value, such as C<[$org_id, $user_id]>,
1163 in the primary key's declared order, not the table's column order. A row
1164 object works as its row data. Key values are deflated like any value sent,
1165 so a C<DateTime> or a value marker works.
1166  
1167 A typed key finds the cached row in any form: a C<DateTime> in any zone or
1168 the database's text, a UUID in upper case or as 16 bytes.
1169  
1170 The database gets the key as you pass it, deflated, while the cache matches
1171 canonical values. On a text column, which compares exact text, pass the form
1172 it stores, or give the field a C<format> (see
1173 L<DBIx::QuickORM::Role::Type/Field hints>).
1174  
1175 C<normalize_pk> in L<DBIx::QuickORM::Map> lists what croaks.
1176  
1177 Each croaks when the handle has a C<where>, order, limit, offset, C<group_by>,
1178 or C<having>.
1179  
1180 =item $row = $h->find_or_insert(\%fields, by => \@fields, %opts)
1181  
1182 The ORM's C<find_or_insert> (see L<DBIx::QuickORM/Finding or inserting>) on
1183 the handle's table, building the row with the handle's row class.
1184  
1185 my $users = $orm->handle('users');
1186 my $gil = $users->find_or_insert(
1187 {
1188 name => 'Gil',
1189 email => 'gil@example.com',
1190 },
1191 by => ['email'],
1192 );
1193 my $same = $users->data_only->find_or_insert(
1194 {email => 'gil@example.com'},
1195 by => ['email'],
1196 ); # finds Gil's row as row data: no insert, so no name needed
1197  
1198 It croaks as the CRUD operations above do, and on a join or query handle.
1199  
1200 =back
1201  
1202 =cut
1203  
1204 70 ▶sub insert {
Subroutine insert: called CC=3 SCAR=12.0
Pod documented
1205 70 my $self = shift;
1206 70 my $debug = $self->{+ORM}->debug;
1207 70 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 70 70
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
7000
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1208 70 $self->_primitive(insert => @_);
1209 }
1210  
1211 4 ▶sub insert_many {
Subroutine insert_many: called CC=3 SCAR=12.0
Pod documented
1212 4 my $self = shift;
1213 4 my $debug = $self->{+ORM}->debug;
1214 4 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 4 4
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
400
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1215 4 $self->_primitive(insert_many => @_);
1216 }
1217  
1218 12 ▶sub update_row {
Subroutine update_row: called CC=3 SCAR=12.0
Pod documented
1219 12 my $self = shift;
1220 12 my $debug = $self->{+ORM}->debug;
1221 12 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 12 12
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1222 12 $self->_primitive(update_row => @_);
1223 }
1224  
1225 5 ▶sub refresh_row {
Subroutine refresh_row: called CC=3 SCAR=12.0
Pod documented
1226 5 my $self = shift;
1227 5 my $debug = $self->{+ORM}->debug;
1228 5 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 5 5
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
500
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1229 5 $self->_primitive(refresh_row => @_);
1230 }
1231  
1232 91 ▶sub find {
Subroutine find: called CC=3 SCAR=11.8
Pod documented
1233 91 my $self = shift;
1234 91 my $debug = $self->{+ORM}->debug;
1235 91 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 91 91
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
9100
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1236 91 my ($pk, @args) = @_;
1237 91 return $self->{+ORM}->find($self->_row_table('find'), $pk, $self->_row_class_arg, @args);
1238 }
1239  
1240 11 ▶sub find_or_insert {
Subroutine find_or_insert: called CC=2 SCAR=7.1
Pod documented
1241 11 my $self = shift;
1242 11 my ($fields, @args) = @_;
1243 11 ▶ return $self->{+ORM}->_find_or_insert($self->{+ORM}->debug ? [(caller(0))[0 .. 3]] : [caller], $self->_row_table('find_or_insert'), $fields, $self->_row_class_arg, @args);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'orm'}->debug ? : 0 11 11
1244 }
1245  
1246 5 ▶sub delete_row {
Subroutine delete_row: called CC=3 SCAR=11.8
Pod documented
1247 5 my $self = shift;
1248 5 my $debug = $self->{+ORM}->debug;
1249 5 ▶ local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 5 5
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
500
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1250 5 my $table = $self->_row_table('delete_row');
1251 2 return $self->{+ORM}->connection->delete_row($table, @_);
1252 }
1253  
1254 =head1 PRIVATE METHODS
1255  
1256 =over 4
1257  
1258 =item $it = $h->_iter
1259  
1260 =item $it = $h->_iter(\@frame)
1261  
1262 The handle's own iterator, made on the first read by sending the query. It
1263 holds the query every C<iter> shares, so a pending query lives as long as the
1264 handle or any of its iterators. C<@frame>, or its caller's (a public
1265 reader's), goes to C<_send>.
1266  
1267 =back
1268  
1269 =cut
1270  
1271 344 ▶sub _iter {
Subroutine _iter: called CC=4 SCAR=13.9
1272 344 my $self = shift;
1273 344 my ($frame) = @_;
1274 344 ▶ return $self->{+ITERATOR} if $self->{+ITERATOR};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"iterator"} 21 323 344
1275  
1276 # The public reader that called this, one frame up, is the call the rows arrive under (recorded only with debug).
1277 323 ▶ $frame //= [(caller(1))[0 .. 3]] if $self->{+ORM}->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"orm"}->debug 4 319 323
Condition50%1/2
$frame //= [(caller 1)[0..3]]
l!l
40
MC/DC0%0/1
$frame //= [(caller 1)[0..3]]
$frame
Truth table50%1/2
A
A= $frame
Aresult
11
00
1278 323 return $self->{+ITERATOR} = DBIx::QuickORM::Iterator->new(query => $self->_send($frame), state => $self->{+STATE});
1279 }
1280  
1281 =pod
1282  
1283 =over 4
1284  
1285 =item @out = $h->_read($method)
1286  
1287 The handle's iterator's C<$method>, for the reader of that name. While the
1288 query is pending, the reader's frame goes in the query's C<frame> for the
1289 collection (see L<DBIx::QuickORM::Iterator>), so the rows it brings arrive
1290 under that frame. A collected query is read as it is.
1291  
1292 =back
1293  
1294 =cut
1295  
1296 329 ▶sub _read {
Subroutine _read: called CC=5 SCAR=16.1
1297 329 my $self = shift;
1298 329 my ($method) = @_;
1299  
1300 329 ▶ my $it = $self->{+ITERATOR} // $self->_iter($self->{+ORM}->debug ? [(caller(1))[0 .. 3]] : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'orm'}->debug ? : 4 303 307
Condition100%3/3
$self->{"iterator"} // $self->_iter($self->{"orm"}->debug ? [(caller 1)[0..3]] : undef)
l!l&&r!l&&!r
223034
MC/DC0%0/2
$self->{"iterator"} // $self->_iter($self->{"orm"}->debug ? [(caller 1)[0..3]] : undef)
$self->{"iterator"} $self->_iter($self->{"orm"}->debug ? [(caller 1)[0..3]] : undef)
Truth table66%2/3
A // B
A= $self->{"iterator"}B= $self->_iter($self->{"orm"}->debug ? [(caller 1)[0..3]] : undef)
ABresult
1X1
011
000
1301 316 my $query = $it->query;
1302 316 ▶ return $it->$method unless $query->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"pending"} 310 6 316
1303  
1304 6 ▶ local $query->{frame} = [(caller(1))[0 .. 3]] if $query->{debug}; # the iterator hands it to the collection, after its process check
Branch: 1 branch
BranchTrueFalseTotal
if $query->{"debug"} 0 6 6
1305 6 return $it->$method;
1306 }
1307  
1308 =pod
1309  
1310 =over 4
1311  
1312 =item \%query = $h->_send(\@frame)
1313  
1314 Sends the handle's query and returns the query hashref its iterators share
1315 (see L<DBIx::QuickORM::Iterator>): the rows, or the pending query in a
1316 background mode. Rows read at once arrive under C<@frame> in the Debug
1317 object's C<caller> slot, unless it is set. C<aside> and C<forked> croak while a transaction is open,
1318 and give the pending query a C<blocked> that refuses reads while one is.
1319  
1320 =back
1321  
1322 =cut
1323  
1324 323 ▶sub _send {
Subroutine _send: called CC=9 SCAR=22.3
1325 323 my $self = shift;
1326 323 my ($frame) = @_;
1327  
1328 323 my ($orm, $source, $mode) = @{$self}{+ORM, +SOURCE, +MODE};
1329 323 my $conn = $orm->connection;
1330 323 my @query = ($source, $orm->sql_builder->select($source, $conn->dialect, $self->_select_request));
1331 316 my $results = $self->_results;
1332  
1333 316 ▶ unless ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode) 300 16 316
1334 300 my $debug = $orm->debug;
1335 300 ▶ local $debug->{caller} = $frame if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 4 296 300
Condition66%2/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
29604
MC/DC50%1/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table66%2/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1336 300 my ($rows, $build) = $results->($conn->select(@query));
1337 295 return {rows => $rows, build => $build};
1338 }
1339  
1340 16 my $async = $mode eq 'async';
1341 16 ▶ croak "$mode() cannot start a query while a transaction is open; start it before the transaction, or after it ends" if !$async && @{$conn->txns};
Branch: 1 branch
BranchTrueFalseTotal
if not $async and @{$conn->txns;} 2 14 16
Condition66%2/3
not $async and @{$conn->txns;}
!ll&&!rl&&r
0142
MC/DC50%1/2
not $async and @{$conn->txns;}
not $async @{$conn->txns;}
Truth table66%2/3
not(A) and B
A= not $asyncB= @{$conn->txns;}
ABresult
0X0
100
111
1342  
1343 14 ▶ my $pending =
Branch: 2 branches
BranchTrueFalseTotal
$async ? : 0 14 14
$mode eq 'aside' ? : 0 14 14
1344 $async ? $conn->select(@query, async => 1)
1345 : $mode eq 'aside' ? $self->_aside($conn, @query)
1346 : $self->_forked($conn, @query);
1347 14 my $collect = $pending->{collect};
1348 14 ▶ my $blocked = $async ? undef : sub { @{$orm->connection->txns} ? "This $mode() query cannot be read while a transaction is open; read it after the transaction ends" : undef };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
$async ? : 0 14 14
@{$orm->connection->txns;} ? : 7 17 24
1349 # The iterator passes its reader's frame, so the rows the result brings are attributed to that call.
1350 13 ▶ my $take = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
1351 13 my $debug = $orm->debug; # $orm, not the handle, so the handle's pending query makes no cycle
1352 13 ▶ local $debug->{caller} = $_[0] if $debug && !$debug->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $debug and not $debug->{"caller"} 0 13 13
Condition33%1/3
$debug and not $debug->{"caller"}
!ll&&!rl&&r
1300
MC/DC0%0/2
$debug and not $debug->{"caller"}
$debug not $debug->{"caller"}
Truth table33%1/3
A and not(B)
A= $debugB= not $debug->{"caller"}
ABresult
0X0
100
111
1353 13 return $results->($collect->());
1354 14 };
1355 14 return {pending => {%$pending, collect => $take, blocked => $blocked}, debug => $orm->debug};
1356 }
1357  
1358 =pod
1359  
1360 =over 4
1361  
1362 =item $out = $h->_primitive($method, @args)
1363  
1364 The ORM's CRUD operation on the handle's table, with rows built as the handle
1365 builds them.
1366  
1367 =back
1368  
1369 =cut
1370  
1371 91 ▶sub _primitive {
Subroutine _primitive: called CC=2 SCAR=6.9
1372 91 my $self = shift;
1373 91 my ($method, $arg, @args) = @_;
1374 91 ▶ my @changes = $method eq 'update_row' ? (shift @args) : ();
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'update_row' ? : 12 79 91
1375 91 return $self->{+ORM}->_primitive($method, $self->_row_table($method), $arg, @changes, $self->_row_class_arg, @args);
1376 }
1377  
1378 =pod
1379  
1380 =over 4
1381  
1382 =item $value = $h->_modify($method, $key)
1383  
1384 =item $h2 = $h->_modify($method, $key, $value)
1385  
1386 The setting, or a copy of the handle with it replaced, without the iterator
1387 (and without the state when the C<where> or the source changes). C<$method>
1388 is the public modifier, for messages. Croaks on more than one argument, a bad
1389 C<fields> or C<row_class> value, or void context, where the copy would be
1390 lost.
1391  
1392 =back
1393  
1394 =cut
1395  
1396 738 ▶sub _modify {
Subroutine _modify: called CC=15 SCAR=27.1
1397 738 my $self = shift;
1398 738 my ($method, $key, @args) = @_;
1399 738 ▶ return $self->{$key} unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 18 720 738
1400 720 ▶ croak "$method() takes one argument" if @args > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 1 719 720
1401  
1402 719 my ($value) = @args;
1403 719 ▶ croak "fields() takes an arrayref of field names or a hashref of computed columns" if $key eq FIELDS() && !$self->_fields_ok($value);
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "fields" and not $self->_fields_ok($value) 2 717 719
Condition100%3/3
$key eq "fields" and not $self->_fields_ok($value)
!ll&&!rl&&r
5821352
MC/DC100%2/2
$key eq "fields" and not $self->_fields_ok($value)
$key eq "fields" not $self->_fields_ok($value)
Truth table100%3/3
A and not(B)
A= $key eq "fields"B= not $self->_fields_ok($value)
ABresult
0X0
100
111
1404 717 ▶ croak "row_class() takes a class name or undef" if $key eq ROW_CLASS() && ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "row_class" and ref $value 1 716 717
Condition100%3/3
$key eq "row_class" and ref $value
!ll&&!rl&&r
639771
MC/DC100%2/2
$key eq "row_class" and ref $value
$key eq "row_class" ref $value
Truth table100%3/3
A and B
A= $key eq "row_class"B= ref $value
ABresult
0X0
100
111
1405  
1406 716 my %clone = (%$self, $key => $value);
1407 716 delete @clone{+ITERATOR, +TEMP_TABLE, +TEMP_VIEW};
1408 716 ▶ delete $clone{+STATE} if $key eq WHERE() || $key eq SOURCE();
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "where" or $key eq "source" 274 442 716
Condition100%3/3
$key eq "where" or $key eq "source"
l!l&&r!l&&!r
22846442
MC/DC100%2/2
$key eq "where" or $key eq "source"
$key eq "where" $key eq "source"
Truth table100%3/3
A or B
A= $key eq "where"B= $key eq "source"
ABresult
1X1
011
000
1409 716 ▶ delete $clone{+COLUMNS} if $key eq FIELDS() || $key eq SOURCE() || $key eq GROUP_BY();
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "fields" or $key eq "source" or $key eq "group_by" 220 496 716
Condition100%6/6
$key eq "fields" or $key eq "source" or $key eq "group_by"
l!l&&r!l&&!r
18139496
$key eq "fields" or $key eq "source"
l!l&&r!l&&!r
13546535
MC/DC100%3/3
$key eq "fields" or $key eq "source" or $key eq "group_by"
$key eq "fields" $key eq "source" $key eq "group_by"
Truth table100%4/4
A or B or C
A= $key eq "fields"B= $key eq "source"C= $key eq "group_by"
ABCresult
1XX1
01X1
0011
0000
1410  
1411 716 my $clone = bless(\%clone, ref($self));
1412 716 ▶ $clone->_columns if $key eq FIELDS() && ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "fields" and ref $value eq "HASH" 82 634 716
Condition100%3/3
$key eq "fields" and ref $value eq "HASH"
!ll&&!rl&&r
5815382
MC/DC100%2/2
$key eq "fields" and ref $value eq "HASH"
$key eq "fields" ref $value eq "HASH"
Truth table100%3/3
A and B
A= $key eq "fields"B= ref $value eq "HASH"
ABresult
0X0
100
111
1413 700 ▶ croak "$method() returns a new handle and leaves this one unchanged; calling it in void context does nothing" unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 25 675 700
1414 675 return $clone;
1415 }
1416  
1417 =pod
1418  
1419 =over 4
1420  
1421 =item $h2 = $h->_combine_where($op, $where)
1422  
1423 The handle with its C<where> combined with C<$where> by the builder's
1424 C<combine_where>. Croaks, pointing to C<where>, when the handle has no
1425 C<where> or an empty one.
1426  
1427 =back
1428  
1429 =cut
1430  
1431 35 ▶sub _combine_where {
Subroutine _combine_where: called CC=9 SCAR=22.0
1432 35 my $self = shift;
1433 35 my ($op, @args) = @_;
1434 35 ▶ croak "$op() takes one condition" unless @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 2 33 35
1435  
1436 33 my $old = $self->{+WHERE};
1437 33 my $type = ref($old);
1438 33 ▶ croak "$op() needs a where to combine with; this handle has none, use where() instead"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $old or $type eq "HASH" and not %$old or $type eq "ARRAY" and not @$old or $type eq "" and $old eq "" 9 24 33
Condition94%17/18
not defined $old or $type eq "HASH" and not %$old or $type eq "ARRAY" and not @$old or $type eq "" and $old eq ""
l!l&&r!l&&!r
6324
not defined $old or $type eq "HASH" and not %$old or $type eq "ARRAY" and not @$old
l!l&&r!l&&!r
4227
not defined $old or $type eq "HASH" and not %$old
l!l&&r!l&&!r
2229
$type eq "HASH" and not %$old
!ll&&!rl&&r
12172
$type eq "ARRAY" and not @$old
!ll&&!rl&&r
2702
$type eq "" and $old eq ""
!ll&&!rl&&r
2043
MC/DC85%6/7
not defined $old or $type eq "HASH" and not %$old or $type eq "ARRAY" and not @$old or $type eq "" and $old eq ""
not defined $old $type eq "HASH" not %$old $type eq "ARRAY" not @$old $type eq "" $old eq ""
Truth table43%7/16
not(A) or B and not(C) or D and not(E) or F and G
A= not defined $oldB= $type eq "HASH"C= not %$oldD= $type eq "ARRAY"E= not @$oldF= $type eq ""G= $old eq ""
ABCDEFGresult
1XXXXXX1
011XXXX1
00X11XX1
01011XX1
00X0X111
00X10111
0100X111
01010111
00X0X0X0
00X0X100
00X100X0
00X10100
0100X0X0
0100X100
010100X0
01010100
1439 if !defined($old) || ($type eq 'HASH' && !%$old) || ($type eq 'ARRAY' && !@$old) || ($type eq '' && $old eq '');
1440  
1441 24 return $self->_modify($op, WHERE(), $self->{+ORM}->sql_builder->combine_where(op => $op, left => $old, right => $args[0]));
1442 }
1443  
1444 =pod
1445  
1446 =over 4
1447  
1448 =item $h2 = $h->_join(\@frame, $method, $table, %opts)
1449  
1450 The handle over its source with one more joined table, making a join from a
1451 table source first. Link errors report at C<@frame>, the public method's
1452 C<caller> frame.
1453  
1454 =back
1455  
1456 =cut
1457  
1458 50 ▶sub _join {
Subroutine _join: called CC=7 SCAR=19.7
1459 50 my $self = shift;
1460 50 my ($frame, $method, $table, %opts) = @_;
1461 50 ▶ my $type = $method eq 'left_join' ? 'left' : 'inner';
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'left_join' ? : 6 44 50
1462  
1463 50 my @unknown = sort grep { !m/^(?:as|from|link|via)\z/ } keys %opts;
1464 50 ▶ croak "Unknown argument(s) to a join: " . CORE::join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 50 50
1465 50 ▶ croak "A join needs the table to join" unless defined($table) && length($table);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table and length $table 0 50 50
Condition33%1/3
defined $table and length $table
!ll&&!rl&&r
0050
MC/DC0%0/2
defined $table and length $table
defined $table length $table
Truth table33%1/3
A and B
A= defined $tableB= length $table
ABresult
0X0
100
111
1466  
1467 50 my $orm = $self->{+ORM};
1468 50 my $map = $orm->db_map;
1469 50 my $source = $self->{+SOURCE};
1470 50 ▶ unless ($source->isa('DBIx::QuickORM::Source::Join')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($source->isa("DBIx::QuickORM::Source::Join")) 47 3 50
1471 47 ▶ croak "Joins start from a table handle" unless $source->isa('DBIx::QuickORM::Source::Table');
Branch: 1 branch
BranchTrueFalseTotal
unless $source->isa("DBIx::QuickORM::Source::Table") 1 46 47
1472 46 $source = DBIx::QuickORM::Source::Join->new(db_map => $map, components => [{as => $source->table, table => $source->table}]);
1473 }
1474  
1475 49 my $link = $orm->_link_for($frame, $map, $source->component_table($opts{from}), $table, {link => delete $opts{link}, via => delete $opts{via}});
1476 47 return $self->_modify($method, SOURCE(), $source->extend($type, $table, %opts, link => $link));
1477 }
1478  
1479 =pod
1480  
1481 =over 4
1482  
1483 =item $h2 = $h->_run_mode($mode)
1484  
1485 =item $h2 = $h->_run_mode($mode, $maybe)
1486  
1487 The handle with the mode set. Without C<$maybe> it croaks with
1488 C<_cannot_run>'s reason. With it, a mode that cannot run leaves the mode as
1489 it was, but the handle is still a new one.
1490  
1491 =item $reason = $h->_cannot_run($mode)
1492  
1493 Why the mode cannot run, or undef when it can: the database has no async
1494 queries, or the mode needs a second connection and none can reach the
1495 database or the ORM was given a C<dbh>.
1496  
1497 =back
1498  
1499 =cut
1500  
1501 45 ▶sub _run_mode {
Subroutine _run_mode: called CC=5 SCAR=16.1
1502 45 my $self = shift;
1503 45 my ($mode, $maybe) = @_;
1504  
1505 45 my $reason = $self->_cannot_run($mode);
1506 45 ▶ croak $reason if $reason && !$maybe;
Branch: 1 branch
BranchTrueFalseTotal
if $reason and not $maybe 9 36 45
Condition100%3/3
$reason and not $maybe
!ll&&!rl&&r
21159
MC/DC100%2/2
$reason and not $maybe
$reason not $maybe
Truth table100%3/3
A and not(B)
A= $reasonB= not $maybe
ABresult
0X0
100
111
1507 36 ▶ return $self->_modify(($maybe ? "maybe_$mode" : $mode), MODE(), $reason ? $self->{+MODE} : $mode);
Branch: 2 branches
BranchTrueFalseTotal
$maybe ? : 17 19 36
$reason ? : 15 21 36
1508 }
1509  
1510 57 ▶sub _cannot_run {
Subroutine _cannot_run: called CC=9 SCAR=22.2
1511 57 my $self = shift;
1512 57 my ($mode) = @_;
1513  
1514 57 my $conn = $self->{+ORM}->connection;
1515 57 my $dialect = $conn->dialect;
1516 57 my $name = $dialect->name;
1517 57 ▶ return "The $name dialect has no async queries, so $mode() cannot run" if $mode ne 'forked' && !$dialect->supports_async;
Branch: 1 branch
BranchTrueFalseTotal
if $mode ne "forked" and not $dialect->supports_async 25 32 57
Condition66%2/3
$mode ne "forked" and not $dialect->supports_async
!ll&&!rl&&r
32025
MC/DC50%1/2
$mode ne "forked" and not $dialect->supports_async
$mode ne "forked" not $dialect->supports_async
Truth table66%2/3
A and not(B)
A= $mode ne "forked"B= not $dialect->supports_async
ABresult
0X0
100
111
1518 32 ▶ return "No second connection can reach this $name database, so $mode() cannot run" if $mode ne 'async' && !$dialect->supports_second_connection($conn->dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $mode ne "async" and not $dialect->supports_second_connection($conn->dbh) 3 29 32
Condition66%2/3
$mode ne "async" and not $dialect->supports_second_connection($conn->dbh)
!ll&&!rl&&r
0293
MC/DC50%1/2
$mode ne "async" and not $dialect->supports_second_connection($conn->dbh)
$mode ne "async" not $dialect->supports_second_connection($conn->dbh)
Truth table66%2/3
A and not(B)
A= $mode ne "async"B= not $dialect->supports_second_connection($conn->dbh)
ABresult
0X0
100
111
1519 29 ▶ return "This ORM was given a 'dbh', so it cannot open the second connection $mode() needs" if $mode ne 'async' && !$self->{+ORM}->_can_connect;
Branch: 1 branch
BranchTrueFalseTotal
if $mode ne "async" and not $self->{"orm"}->_can_connect 6 23 29
Condition66%2/3
$mode ne "async" and not $self->{"orm"}->_can_connect
!ll&&!rl&&r
0236
MC/DC50%1/2
$mode ne "async" and not $self->{"orm"}->_can_connect
$mode ne "async" not $self->{"orm"}->_can_connect
Truth table66%2/3
A and not(B)
A= $mode ne "async"B= not $self->{"orm"}->_can_connect
ABresult
0X0
100
111
1520 23 ▶ if ($mode ne 'async' && (my $temp = $self->_temp_source)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "async" and my $temp = $self->_temp_source) 1 22 23
Condition66%2/3
$mode ne "async" and my $temp = $self->_temp_source
!ll&&!rl&&r
0221
MC/DC50%1/2
$mode ne "async" and my $temp = $self->_temp_source
$mode ne "async" my $temp = $self->_temp_source
Truth table66%2/3
A and B
A= $mode ne "async"B= my $temp = $self->_temp_source
ABresult
0X0
100
111
1521 1 return "The temporary table or view '$temp' exists only on this connection, so the second connection $mode() needs cannot read it";
1522 }
1523 22 return undef;
1524 }
1525  
1526 =pod
1527  
1528 =over 4
1529  
1530 =item $temp = $h->_temp($kind, %opts)
1531  
1532 C<temp_table> and C<temp_view>, for C<$kind> C<table> or C<view>. The kept
1533 one and its first call's options sit in the handle's C<temp_table> or
1534 C<temp_view> slot as C<[$temp, \%opts]>.
1535  
1536 =item %row_class = $h->_temp_row_class
1537  
1538 C<< (row_class => $class_or_undef) >> when the handle set a row class itself
1539 (C<row_class>, C<data_only>, C<bare>), else an empty list.
1540  
1541 =back
1542  
1543 =cut
1544  
1545 258 my %TEMP_ARGS = map { $_ => 1 } qw/name pk rebuild/;
1546  
1547 19 ▶sub _temp {
Subroutine _temp: called CC=11 SCAR=24.0
1548 19 my $self = shift;
1549 19 my ($kind, %opts) = @_;
1550 19 my $method = "temp_$kind";
1551 19 my @bad = sort grep { !$TEMP_ARGS{$_} } keys %opts;
1552 19 ▶ croak "$method() got unknown option(s): " . CORE::join(', ' => @bad) if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 19 19
1553  
1554 19 ▶ my $slot = $kind eq 'table' ? TEMP_TABLE() : TEMP_VIEW();
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'table' ? : 15 4 19
1555 19 my $kept = $self->{$slot};
1556 19 ▶ my $live = $kept && $kept->[0]->is_live;
Condition100%3/3
$kept && $kept->[0]->is_live
!ll&&!rl&&r
469
MC/DC100%2/2
$kept && $kept->[0]->is_live
$kept $kept->[0]->is_live
Truth table100%3/3
A && B
A= $keptB= $kept->[0]->is_live
ABresult
0X0
100
111
1557  
1558 19 ▶ if (delete $opts{rebuild}) {
Branch: 3 branches
BranchTrueFalseTotal
if (delete $opts{'rebuild'}) { } 5 14 19
elsif (not %opts) { } 9 5 14
elsif ($live) { } 2 3 5
1559 5 ▶ croak "$method(rebuild => 1) takes no 'name' or 'pk': it builds the kept temporary $kind again as it was" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 4 5
1560 4 ▶ croak "$method(rebuild => 1): this handle has no temporary $kind to rebuild" unless $kept;
Branch: 1 branch
BranchTrueFalseTotal
unless $kept 1 3 4
1561 3 ▶ croak "$method(rebuild => 1): the temporary $kind '" . $kept->[0]->name . "' still exists" if $live;
Branch: 1 branch
BranchTrueFalseTotal
if $live 1 2 3
1562 2 %opts = %{$kept->[1]};
1563 }
1564 elsif (!%opts) {
1565 9 ▶ return $kept->[0] if $kept;
Branch: 1 branch
BranchTrueFalseTotal
if $kept 8 1 9
1566 }
1567 elsif ($live) {
1568 2 croak "$method(): this handle already keeps the temporary $kind '" . $kept->[0]->name . "', which still exists";
1569 }
1570  
1571 6 my $internal = "_$method";
1572 6 my $temp = $self->{+ORM}->$internal(1, $self, %opts);
1573 6 $self->{$slot} = [$temp, {%opts}];
1574 6 return $temp;
1575 }
1576  
1577 32 ▶sub _temp_row_class { exists($_[0]->{+ROW_CLASS}) ? (row_class => $_[0]->{+ROW_CLASS}) : () }
Subroutine _temp_row_class: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'row_class'} ? : 1 31 32
1578  
1579 =pod
1580  
1581 =over 4
1582  
1583 =item $name = $h->_temp_source
1584  
1585 The first temporary table or view the handle's source reads (its table, or a
1586 join's components), or undef. A query source's SQL is not looked into.
1587  
1588 =back
1589  
1590 =cut
1591  
1592 23 ▶sub _temp_source {
Subroutine _temp_source: called CC=5 SCAR=16.2
1593 23 my $self = shift;
1594 23 my $source = $self->{+SOURCE};
1595 23 my $map = $self->{+ORM}->db_map;
1596 23 ▶ my @tables = $source->isa('DBIx::QuickORM::Source::Join') ? $source->tables : ($source->table);
Branch: 1 branch
BranchTrueFalseTotal
$source->isa('DBIx::QuickORM::Source::Join') ? : 0 23 23
1597 23 for my $table (grep { defined } @tables) {
1598 22 my $t = $map->table($table);
1599 22 ▶ return $table if $t && $t->{temp};
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t->{"temp"} 1 21 22
Condition66%2/3
$t and $t->{"temp"}
!ll&&!rl&&r
0211
MC/DC50%1/2
$t and $t->{"temp"}
$t $t->{"temp"}
Truth table66%2/3
A and B
A= $tB= $t->{"temp"}
ABresult
0X0
100
111
1600 }
1601 22 return undef;
1602 }
1603  
1604 =pod
1605  
1606 =over 4
1607  
1608 =item \%pending = $h->_aside($conn, $source, $sql, \@binds)
1609  
1610 Starts the query as an async one on a new Connection like C<$conn>, and
1611 returns its pending code references, closing that connection once it is
1612 collected or cancelled. The code references hold the ORM, not the handle.
1613  
1614 =back
1615  
1616 =cut
1617  
1618 0 ▶sub _aside {
Subroutine _aside: not called CC=1 SCAR=6.9
1619 0 my $self = shift;
1620 0 my ($conn, @query) = @_;
1621  
1622 0 my $orm = $self->{+ORM};
1623 0 my $aside = $orm->_connect_like($conn);
1624 0 my $pending = $aside->select(@query, async => 1);
1625 0 ▶ my $close = sub { $aside->dbh->disconnect };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1626 return {
1627 ready => $pending->{ready},
1628 0 ▶ collect => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1629 0 my $rows;
1630 0 my $ok = eval { $rows = $pending->{collect}->(); 1 };
1631 0 my $err = $@;
1632 0 $close->();
1633 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
1634 0 return $rows;
1635 },
1636 0 ▶ cancel => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1637 0 ▶ eval { $pending->{cancel}->(); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pending->{"cancel"}->(); 1 } } or warn $@ 0 0 0
1638 0 $close->();
1639 },
1640 0 };
1641 }
1642  
1643 =pod
1644  
1645 =over 4
1646  
1647 =item \%pending = $h->_forked($conn, $source, $sql, \@binds)
1648  
1649 Forks a child that runs the query on a new Connection like C<$conn>, writes
1650 one message, and leaves through C<POSIX::_exit> on every path, so nothing it
1651 inherited is destroyed there.
1652  
1653 The message is a L<Storable> C<freeze> of C<< {rows => \@rows} >> or
1654 C<< {error => $message} >>. It is not C<nfreeze>, which keeps no more than 15
1655 significant digits of a float.
1656 C<ready> reads without blocking, C<collect> blocks, and C<cancel> kills and
1657 reaps the child.
1658  
1659 =back
1660  
1661 =cut
1662  
1663 14 ▶sub _forked {
Subroutine _forked: called CC=5 SCAR=18.4
1664 14 my $self = shift;
1665 14 my ($conn, @query) = @_;
1666  
1667 14 my $orm = $self->{+ORM};
1668  
1669 14 my ($reader, $writer) = Atomic::Pipe->pair;
1670 14 ▶ my $pid = fork // die "Could not fork: $!";
Condition50%1/2
fork // die("Could not fork: $!")
l!l
140
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
1671 14 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 14 14
1672 0 ▶ my $out = eval {
Condition0%0/2
eval { do { $reader->close; my $child = $orm->_connect_like($conn); my $rows = $child->select(@query); $child->dbh->disconnect; +{'rows', $rows} } } || {'error', "$@"}
l!l
00
MC/DC0%0/1
eval { do { $reader->close; my $child = $orm->_connect_like($conn); my $rows = $child->select(@query); $child->dbh->disconnect; +{'rows', $rows} } } || {'error', "$@"}
eval { do { $reader->close; my $child = $orm->_connect_like($conn); my $rows = $child->select(@query); $child->dbh->disconnect; +{'rows', $rows} } }
Truth table0%0/2
A
A= eval { do { $reader->close; my $child = $orm->_connect_like($conn); my $rows = $child->select(@query); $child->dbh->disconnect; +{'rows', $rows} } }
Aresult
11
00
1673 0 $reader->close;
1674 0 my $child = $orm->_connect_like($conn);
1675 0 my $rows = $child->select(@query);
1676 0 $child->dbh->disconnect;
1677 0 +{rows => $rows};
1678 } || +{error => "$@"};
1679 0 ▶ eval { $writer->write_message(freeze($out)); $writer->close; 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $writer->write_message(freeze($out)); $writer->close; 1 } } or warn $@ 0 0 0
1680 0 POSIX::_exit(0);
1681 }
1682 14 $writer->close;
1683  
1684 14 my $msg;
1685 14 ▶ my $reap = sub { local $?; waitpid($pid, 0) };
Subroutine __ANON__: called CC=1 SCAR=0.0
1686 return {
1687 4 ▶ ready => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
1688 4 $reader->blocking(0);
1689 4 ▶ $msg //= $reader->read_message;
Condition66%2/3
$msg //= $reader->read_message
l!l&&r!l&&!r
022
MC/DC50%1/2
$msg //= $reader->read_message
$msg $reader->read_message
Truth table66%2/3
A //= B
A= $msgB= $reader->read_message
ABresult
1X1
011
000
1690 4 ▶ my $ready = defined($msg) || $reader->eof; # eof reads too, so it must not block either
Condition66%2/3
defined $msg || $reader->eof
l!l&&r!l&&!r
202
MC/DC50%1/2
defined $msg || $reader->eof
defined $msg $reader->eof
Truth table66%2/3
A || B
A= defined $msgB= $reader->eof
ABresult
1X1
011
000
1691 4 $reader->blocking(1);
1692 4 return $ready;
1693 },
1694 13 ▶ collect => sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
1695 13 ▶ $msg //= $reader->read_message;
Condition66%2/3
$msg //= $reader->read_message
l!l&&r!l&&!r
2110
MC/DC0%0/2
$msg //= $reader->read_message
$msg $reader->read_message
Truth table66%2/3
A //= B
A= $msgB= $reader->read_message
ABresult
1X1
011
000
1696 13 $reap->();
1697 13 ▶ die "The forked query ended without sending its result\n" unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 13 13
1698 13 my $out = thaw($msg);
1699 13 ▶ die "The forked query failed: $out->{error}" . ($out->{error} =~ m/\n\z/ ? '' : "\n") if defined $out->{error};
Branch: 2 branches
BranchTrueFalseTotal
if defined $out->{"error"} 1 12 13
$out->{'error'} =~ /\n\z/ ? : 0 1 1
1700 12 return $out->{rows};
1701 },
1702 1 ▶ cancel => sub { kill('KILL', $pid); $reap->() },
Subroutine __ANON__: called CC=1 SCAR=0.0
1703 14 };
1704 }
1705  
1706 =pod
1707  
1708 =over 4
1709  
1710 =item $code = $h->_results
1711  
1712 A code reference turning fetched rows into C<(\@rows, \&build)> for an
1713 iterator, given the rows: through the source's C<results>, ingesting them
1714 unless the handle is C<no_cache>, or as computed rows. It holds
1715 the ORM and the source, not the handle, so a pending iterator kept in the
1716 handle makes no cycle.
1717  
1718 =back
1719  
1720 =cut
1721  
1722 316 ▶sub _results {
Subroutine _results: called CC=5 SCAR=16.1
1723 316 my $self = shift;
1724 316 my ($orm, $source) = @{$self}{+ORM, +SOURCE};
1725 316 my @build = $self->_row_class_arg;
1726 316 my $cached = !$self->{+NO_CACHE};
1727 263 ▶ return sub { $source->results($orm, $_[0], $cached, @build) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1728 316 ▶ unless $self->_computed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_computed 270 46 316
1729  
1730 4 ▶ return sub { ($_[0], $orm->_row_builder(undef, @build)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1731 46 ▶ if exists $self->{+ROW_CLASS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"row_class"} 4 42 46
1732 25 ▶ return sub { ($_[0], undef) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1733 42 ▶ unless defined $orm->_row_class($source->table);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $orm->_row_class($source->table) 26 16 42
1734  
1735 # Blessed directly while new, init, and the init hooks are as AggregateRow ships them: init only checks arguments known good here.
1736 16 my $columns = $self->_columns;
1737 16 my $class = 'DBIx::QuickORM::AggregateRow';
1738 3 ▶ return sub { ($_[0], sub { $class->new(orm => $orm, source => $source, columns => $columns, row_data => $_[0]) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1739 16 ▶ unless $class->_bless_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_bless_ok 3 13 16
1740 13 ▶ return sub { ($_[0], sub { bless({orm => $orm, source => $source, columns => $columns, row_data => $_[0]}, $class) }) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1741 }
1742  
1743 =pod
1744  
1745 =over 4
1746  
1747 =item %request = $h->_select_request
1748  
1749 The builder's C<select> request: the handle's settings (the C<where> through
1750 C<_where_request>), with the primary key
1751 added to C<fields>, or the source's default fields. For computed rows, each
1752 column's expression under its name.
1753  
1754 =back
1755  
1756 =cut
1757  
1758 370 ▶sub _select_request {
Subroutine _select_request: called CC=3 SCAR=11.0
1759 370 my $self = shift;
1760 370 my $source = $self->{+SOURCE};
1761  
1762 370 my $given = $self->{+FIELDS};
1763 370 ▶ if ($self->_computed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_computed) 61 309 370
1764 61 my $columns = $self->_columns;
1765 59 $given = {map { ($_ => $columns->{$_}->{expr}) } keys %$columns};
1766 }
1767  
1768 368 my %request = (fields => $source->select_fields($given), map { ($_ => $self->{$_}) } ORDER_BY(), LIMIT(), OFFSET(), GROUP_BY(), HAVING());
1769 365 ▶ return ($self->_where_request, map { defined($request{$_}) ? ($_ => $request{$_}) : () } keys %request);
Branch: 1 branch
BranchTrueFalseTotal
defined $request{$_} ? : 525 1665 2190
1770 }
1771  
1772 =pod
1773  
1774 =over 4
1775  
1776 =item %request = $h->_where_request
1777  
1778 The C<where> and C<where_builders> for a builder request: the handle's where
1779 builders, or the ORM's. Empty without a C<where>.
1780  
1781 =back
1782  
1783 =cut
1784  
1785 410 ▶sub _where_request {
Subroutine _where_request: called CC=3 SCAR=11.0
1786 410 my $self = shift;
1787 410 my $where = $self->{+WHERE};
1788 410 ▶ return () unless defined $where;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where 149 261 410
1789 261 ▶ return (where => $where, where_builders => $self->{+WHERE_BUILDERS} // $self->{+ORM}->where_builders);
Condition66%2/3
$self->{"where_builders"} // $self->{"orm"}->where_builders
l!l&&r!l&&!r
22590
MC/DC0%0/2
$self->{"where_builders"} // $self->{"orm"}->where_builders
$self->{"where_builders"} $self->{"orm"}->where_builders
Truth table66%2/3
A // B
A= $self->{"where_builders"}B= $self->{"orm"}->where_builders
ABresult
1X1
011
000
1790 }
1791  
1792 =pod
1793  
1794 =over 4
1795  
1796 =item $bool = $h->_computed
1797  
1798 True when the handle yields computed rows.
1799  
1800 =back
1801  
1802 =cut
1803  
1804 686 ▶sub _computed {
Subroutine _computed: called CC=4 SCAR=13.9
1805 686 my $self = shift;
1806 686 ▶ return 1 if ref($self->{+FIELDS}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"fields"} eq "HASH" 86 600 686
1807 600 ▶ return defined($self->{+GROUP_BY}) || defined($self->{+HAVING}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'group_by'} || defined $self->{'having'} ? : 21 579 600
Condition100%3/3
defined $self->{"group_by"} or defined $self->{"having"}
l!l&&r!l&&!r
201579
MC/DC100%2/2
defined $self->{"group_by"} or defined $self->{"having"}
defined $self->{"group_by"} defined $self->{"having"}
Truth table100%3/3
A or B
A= defined $self->{"group_by"}B= defined $self->{"having"}
ABresult
1X1
011
000
1808 }
1809  
1810 =pod
1811  
1812 =over 4
1813  
1814 =item $bool = $h->_fields_ok($value)
1815  
1816 True for what C<fields> takes: an arrayref of field names, or a non-empty
1817 hashref (its columns are checked by C<_columns>).
1818  
1819 =back
1820  
1821 =cut
1822  
1823 137 ▶sub _fields_ok {
Subroutine _fields_ok: called CC=4 SCAR=13.9
1824 137 my $self = shift;
1825 137 my ($value) = @_;
1826 137 ▶ return !grep { !$self->_is_name($_) } @$value if ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 53 84 137
1827 84 ▶ return ref($value) eq 'HASH' && %$value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' && %$value ? : 82 2 84
Condition100%3/3
ref $value eq "HASH" and %$value
!ll&&!rl&&r
1182
MC/DC100%2/2
ref $value eq "HASH" and %$value
ref $value eq "HASH" %$value
Truth table100%3/3
A and B
A= ref $value eq "HASH"B= %$value
ABresult
0X0
100
111
1828 }
1829  
1830 =pod
1831  
1832 =over 4
1833  
1834 =item \%columns = $h->_columns
1835  
1836 The computed columns, built once per handle: each name mapped to its field
1837 record (as L<DBIx::QuickORM::AggregateRow> takes C<columns>) plus C<expr>, a
1838 field name, an C<sql()> marker, or C<[$FUNC, $field]> (C<$field> may be C<*>). A record's C<name> is
1839 the column's, and its C<table> the source's table, absent when it has none.
1840 Without a C<fields> hashref, the grouped or listed fields each name
1841 themselves. Croaks when C<having> has neither.
1842  
1843 =back
1844  
1845 =cut
1846  
1847 185 ▶sub _columns {
Subroutine _columns: called CC=9 SCAR=22.0
1848 185 my $self = shift;
1849 185 ▶ return $self->{+COLUMNS} if $self->{+COLUMNS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"columns"} 90 95 185
1850  
1851 95 my $given = $self->{+FIELDS};
1852 95 ▶ if (ref($given) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $given ne "HASH") 12 83 95
1853 12 ▶ my $names = $given // $self->{+GROUP_BY} // [];
Condition100%5/5
($given // $self->{"group_by"}) // []
l!l
111
$given // $self->{"group_by"}
l!l&&r!l&&!r
291
MC/DC100%2/2
($given // $self->{"group_by"}) // []
$given $self->{"group_by"}
Truth table100%3/3
A // B
A= $givenB= $self->{"group_by"}
ABresult
1X1
011
000
1854 12 ▶ croak "A handle with having() needs group_by() or fields to select" unless @$names;
Branch: 1 branch
BranchTrueFalseTotal
unless @$names 1 11 12
1855 11 $given = {map { $_ => $_ } @$names};
1856 }
1857  
1858 94 ▶ my %like = map { (lc($_) => 1) } qw/min max/, @{$self->{+ORM}->like_aggregates // []};
Condition100%2/2
$self->{"orm"}->like_aggregates // []
l!l
5242
MC/DC100%1/1
$self->{"orm"}->like_aggregates // []
$self->{"orm"}->like_aggregates
Truth table100%2/2
A
A= $self->{"orm"}->like_aggregates
Aresult
11
00
1859 94 my $table = $self->{+SOURCE}->table;
1860 94 my %columns;
1861 94 for my $name (keys %$given) {
1862 # A column is named for itself, not for the field whose facts it took.
1863 139 my $column = $columns{$name} = $self->_column($name, $given->{$name}, \%like);
1864 122 $column->{name} = $name;
1865 122 delete $column->{table};
1866 122 ▶ $column->{table} = $table if defined $table;
Branch: 1 branch
BranchTrueFalseTotal
if defined $table 103 19 122
1867 }
1868  
1869 77 return $self->{+COLUMNS} = \%columns;
1870 }
1871  
1872 258 my %COLUMN_KEYS = map { $_ => 1 } qw/sql agg type db_type fk like/;
1873  
1874 =pod
1875  
1876 =over 4
1877  
1878 =item \%column = $h->_column($name, $spec, \%like)
1879  
1880 One column of C<_columns>. C<%like> holds the lowercased names of the
1881 aggregate functions that keep the field's facts.
1882  
1883 =back
1884  
1885 =cut
1886  
1887 139 ▶sub _column {
Subroutine _column: called CC=25 SCAR=32.2
1888 139 my $self = shift;
1889 139 my ($name, $spec, $like) = @_;
1890  
1891 139 ▶ return {expr => $spec, $self->_field_facts($name, $spec)} if $self->_is_name($spec);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_name($spec) 52 87 139
1892 87 ▶ $spec = DBIx::QuickORM::Value::sql($$spec) if ref($spec) eq 'SCALAR'; # \"SQL" is sql("SQL")
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec eq "SCALAR" 9 78 87
1893 86 ▶ $spec = $self->_is_sql($spec) ? {sql => $spec} : ref($spec) eq 'ARRAY' ? {agg => $spec} : $spec;
Branch: 2 branches
BranchTrueFalseTotal
$self->_is_sql($spec) ? : 21 65 86
ref $spec eq 'ARRAY' ? : 34 31 65
1894 86 ▶ croak "Column '$name' must be a field name, an sql() marker, \\'SQL', [func, field], or a hashref" unless ref($spec) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 1 85 86
1895  
1896 85 my @unknown = sort grep { !$COLUMN_KEYS{$_} } keys %$spec;
1897 85 ▶ croak "Unknown key(s) in column '$name': " . CORE::join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 84 85
1898 84 ▶ croak "Column '$name' needs exactly one of 'sql' or 'agg'" unless exists($spec->{sql}) xor exists($spec->{agg});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"sql"} xor exists $spec->{"agg"} 2 82 84
Condition100%4/4
exists $spec->{"sql"} xor exists $spec->{"agg"}
l&&rl&&!r!l&&r!l&&!r
137451
MC/DC100%2/2
exists $spec->{"sql"} xor exists $spec->{"agg"}
exists $spec->{"sql"} exists $spec->{"agg"}
Truth table100%4/4
A xor B
A= exists $spec->{"sql"}B= exists $spec->{"agg"}
ABresult
110
101
011
000
1899  
1900 82 my %column;
1901 82 ▶ if (exists $spec->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{'sql'}) { } 37 45 82
1902 37 ▶ croak "'sql' in column '$name' must be an sql() marker" unless $self->_is_sql($spec->{sql});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_sql($spec->{"sql"}) 1 36 37
1903 36 $column{expr} = $spec->{sql};
1904 }
1905 else {
1906 45 my $agg = $spec->{agg};
1907 45 ▶ my ($func, $field) = ref($agg) eq 'ARRAY' && @$agg == 2 ? @$agg : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $agg eq 'ARRAY' && @$agg == 2 ? : 44 1 45
Condition66%2/3
ref $agg eq "ARRAY" and @$agg == 2
!ll&&!rl&&r
0144
MC/DC50%1/2
ref $agg eq "ARRAY" and @$agg == 2
ref $agg eq "ARRAY" @$agg == 2
Truth table66%2/3
A and B
A= ref $agg eq "ARRAY"B= @$agg == 2
ABresult
0X0
100
111
1908 45 ▶ croak "'agg' in column '$name' must be [func, field]" unless $self->_is_name($func) && $self->_is_name($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_name($func) and $self->_is_name($field) 1 44 45
Condition66%2/3
$self->_is_name($func) and $self->_is_name($field)
!ll&&!rl&&r
1044
MC/DC50%1/2
$self->_is_name($func) and $self->_is_name($field)
$self->_is_name($func) $self->_is_name($field)
Truth table66%2/3
A and B
A= $self->_is_name($func)B= $self->_is_name($field)
ABresult
0X0
100
111
1909 44 ▶ croak "Column '$name' names the function '$func', which is not a word (/^\\w+\$/)" unless $func =~ m/^\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $func =~ /^\w+\z/ 1 43 44
1910 # _field_facts also checks the field exists. '*' names no field, so has no facts.
1911 43 ▶ my %facts = $field eq '*' ? () : $self->_field_facts($name, $field);
Branch: 1 branch
BranchTrueFalseTotal
$field eq '*' ? : 10 33 43
1912 42 ▶ %column = (expr => [uc($func), $field], $like->{lc $func} && !defined($spec->{like}) ? %facts : ());
Branch: 1 branch
BranchTrueFalseTotal
$$like{lc $func} && !defined($spec->{'like'}) ? : 20 22 42
Condition100%3/3
$$like{lc $func} and not defined $spec->{"like"}
!ll&&!rl&&r
19320
MC/DC100%2/2
$$like{lc $func} and not defined $spec->{"like"}
$$like{lc $func} not defined $spec->{"like"}
Truth table100%3/3
A and not(B)
A= $$like{lc $func}B= not defined $spec->{"like"}
ABresult
0X0
100
111
1913 }
1914  
1915 78 ▶ %column = (%column, $self->_field_facts($name, $spec->{like})) if defined $spec->{like};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->{"like"} 7 71 78
1916  
1917 78 ▶ if (defined $spec->{db_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{"db_type"}) 2 76 78
1918 2 delete $column{size}; # the dialect derives it from the db_type, or there is none
1919 2 %column = (%column, %{$self->{+ORM}->connection->dialect->field_facts($spec->{db_type})});
1920 }
1921  
1922 78 ▶ $column{fk} = $self->_column_fk($name, $spec->{fk}) if exists $spec->{fk};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"fk"} 13 65 78
1923 74 ▶ if (defined $spec->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{"type"}) 7 67 74
1924 # Recompute passthrough for the new type, as the map build does.
1925 7 my $type = $column{orm_type} = $self->{+ORM}->require_type($spec->{type}, 'type');
1926 6 delete $column{passthrough};
1927 6 ▶ $column{passthrough} = 1 if $type->can('inflate_is_identity') && $type->inflate_is_identity(\%column);
Branch: 1 branch
BranchTrueFalseTotal
if $type->can("inflate_is_identity") and $type->inflate_is_identity(\%column) 2 4 6
Condition66%2/3
$type->can("inflate_is_identity") and $type->inflate_is_identity(\%column)
!ll&&!rl&&r
042
MC/DC50%1/2
$type->can("inflate_is_identity") and $type->inflate_is_identity(\%column)
$type->can("inflate_is_identity") $type->inflate_is_identity(\%column)
Truth table66%2/3
A and B
A= $type->can("inflate_is_identity")B= $type->inflate_is_identity(\%column)
ABresult
0X0
100
111
1928 }
1929  
1930 73 return \%column;
1931 }
1932  
1933 =pod
1934  
1935 =over 4
1936  
1937 =item %facts = $h->_field_facts($name, $field)
1938  
1939 The field's record from the map (C<orm_type>, C<db_type>, C<type>, C<size>,
1940 C<affinity>, as it has them) and its single-field foreign keys as C<fk>, for
1941 the column C<$name>. Croaks when the field is not a field of the source. A
1942 query source knows no fields, so a name passes unchecked with no facts.
1943  
1944 =back
1945  
1946 =cut
1947  
1948 92 ▶sub _field_facts {
Subroutine _field_facts: called CC=10 SCAR=23.2
1949 92 my $self = shift;
1950 92 my ($name, $field) = @_;
1951  
1952 92 ▶ croak "Column '$name' must name a field" unless $self->_is_name($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_name($field) 0 92 92
1953  
1954 92 my $source = $self->{+SOURCE};
1955 92 my $records = $source->field_records;
1956 92 ▶ return () unless %$records;
Branch: 1 branch
BranchTrueFalseTotal
unless %$records 1 91 92
1957 91 ▶ my $record = $records->{$field} or croak "Column '$name' names '$field', which is not a field of the handle's source";
Branch: 1 branch
BranchTrueFalseTotal
my $record = $records->{$field} or croak("Column '${name}' names '${field}', which is not a field of the handle's source") 87 4 91
1958  
1959 87 my ($alias, $short) = $field =~ m/^(?:(.+)\.)?([^.]+)\z/;
1960 87 ▶ my $table = $source->table // $source->component_table($alias);
Condition66%2/3
$source->table // $source->component_table($alias)
l!l&&r!l&&!r
74130
MC/DC0%0/2
$source->table // $source->component_table($alias)
$source->table $source->component_table($alias)
Truth table66%2/3
A // B
A= $source->tableB= $source->component_table($alias)
ABresult
1X1
011
000
1961 87 ▶ my $fks = $self->{+ORM}->db_map->table($table)->{fk} // {};
Condition50%1/2
$self->{"orm"}->db_map->table($table)->{"fk"} // {}
l!l
870
MC/DC0%0/1
$self->{"orm"}->db_map->table($table)->{"fk"} // {}
$self->{"orm"}->db_map->table($table)->{"fk"}
Truth table50%1/2
A
A= $self->{"orm"}->db_map->table($table)->{"fk"}
Aresult
11
00
1962  
1963 87 my %fk;
1964 87 for my $other (keys %$fks) {
1965 61 ▶ my @to = map { $_->{$short} } grep { keys(%$_) == 1 && exists $_->{$short} } @{$fks->{$other}};
Condition66%2/3
keys %$_ == 1 && exists $_->{$short}
!ll&&!rl&&r
06428
MC/DC50%1/2
keys %$_ == 1 && exists $_->{$short}
keys %$_ == 1 exists $_->{$short}
Truth table66%2/3
A && B
A= keys %$_ == 1B= exists $_->{$short}
ABresult
0X0
100
111
1966 61 ▶ $fk{$other} = [map { +{$name => $_} } @to] if @to;
Branch: 1 branch
BranchTrueFalseTotal
if @to 28 33 61
1967 }
1968  
1969 87 ▶ return (%$record, %fk ? (fk => \%fk) : ());
Branch: 1 branch
BranchTrueFalseTotal
%fk ? : 28 59 87
1970 }
1971  
1972 =pod
1973  
1974 =over 4
1975  
1976 =item \%fk = $h->_column_fk($name, $fk)
1977  
1978 An C<fk> given for column C<$name> (a table, C<[$table, $field]>, or
1979 C<< {$table => $field} >>) as C<< {$table => [{$name => $field}]} >>. Croaks
1980 when its table or field is not in the map, or a bare table's primary key is
1981 not one field.
1982  
1983 =back
1984  
1985 =cut
1986  
1987 13 ▶sub _column_fk {
Subroutine _column_fk: called CC=14 SCAR=26.6
1988 13 my $self = shift;
1989 13 my ($name, $fk) = @_;
1990  
1991 13 my $type = ref($fk);
1992 13 ▶ my @parts = $type eq 'ARRAY' ? @$fk : $type eq 'HASH' ? %$fk : $type ? () : ($fk);
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 4 9 13
$type eq 'HASH' ? : 3 6 9
$type ? : 0 6 6
1993 13 ▶ my $ok = (@parts == 1 && !$type) || (@parts == 2 && $type);
Condition77%7/9
@parts == 1 && !$type || @parts == 2 && $type
l!l&&r!l&&!r
661
@parts == 1 and not $type
!ll&&!rl&&r
706
@parts == 2 and $type
!ll&&!rl&&r
106
MC/DC50%2/4
@parts == 1 && !$type || @parts == 2 && $type
@parts == 1 not $type @parts == 2 $type
Truth table42%3/7
A and not(B) || C and D
A= @parts == 1B= not $typeC= @parts == 2D= $type
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1994 13 ▶ croak "'fk' in column '$name' must be a table, [table, field], or {table => field}" unless $ok && !grep { !$self->_is_name($_) } @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok and not grep({not $self->_is_name($_);} @parts) 1 12 13
Condition66%2/3
$ok and not grep({not $self->_is_name($_);} @parts)
!ll&&!rl&&r
1012
MC/DC50%1/2
$ok and not grep({not $self->_is_name($_);} @parts)
$ok not grep({not $self->_is_name($_);} @parts)
Truth table66%2/3
A and not(B)
A= $okB= not grep({not $self->_is_name($_);} @parts)
ABresult
0X0
100
111
1995  
1996 12 my ($table, $field) = @parts;
1997 12 ▶ my $t = $self->{+ORM}->db_map->table($table) or croak "'fk' in column '$name' names the table '$table', which is not in the database map";
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"orm"}->db_map->table($table) or croak("'fk' in column '${name}' names the table '${table}', which is not in the database map") 11 1 12
1998 11 ▶ unless (defined $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field) 5 6 11
1999 5 my $pk = $t->{pk};
2000 5 ▶ croak "'fk' in column '$name' names the table '$table', whose primary key is not a single field" unless $pk && @$pk == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk and @$pk == 1 1 4 5
Condition66%2/3
$pk and @$pk == 1
!ll&&!rl&&r
014
MC/DC50%1/2
$pk and @$pk == 1
$pk @$pk == 1
Truth table66%2/3
A and B
A= $pkB= @$pk == 1
ABresult
0X0
100
111
2001 4 $field = $pk->[0];
2002 }
2003 10 ▶ croak "'fk' in column '$name' names '$table.$field', which is not a field" unless $t->{fields}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"fields"}{$field} 1 9 10
2004  
2005 9 return {$table => [{$name => $field}]};
2006 }
2007  
2008 =pod
2009  
2010 =over 4
2011  
2012 =item $bool = $h->_is_name($value)
2013  
2014 True for a defined, non-empty plain string.
2015  
2016 =item $bool = $h->_is_sql($value)
2017  
2018 True for an C<sql()> marker.
2019  
2020 =back
2021  
2022 =cut
2023  
2024 404 ▶sub _is_name { defined($_[1]) && !ref($_[1]) && length($_[1]) ? 1 : 0 }
Subroutine _is_name: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] && !ref($_[1]) && length $_[1] ? : 316 88 404
Condition83%5/6
defined $_[1] and not ref $_[1] and length $_[1]
!ll&&!rl&&r
880316
defined $_[1] and not ref $_[1]
!ll&&!rl&&r
187316
MC/DC66%2/3
defined $_[1] and not ref $_[1] and length $_[1]
defined $_[1] not ref $_[1] length $_[1]
Truth table75%3/4
A and not(B) and C
A= defined $_[1]B= not ref $_[1]C= length $_[1]
ABCresult
0XX0
10X0
1100
1111
2025 123 ▶sub _is_sql { blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') && $_[1]->kind eq 'sql' ? 1 : 0 }
Subroutine _is_sql: called CC=4 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') && $_[1]->kind eq 'sql' ? : 57 66 123
Condition66%4/6
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
!ll&&!rl&&r
66057
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value")
!ll&&!rl&&r
66057
MC/DC33%1/3
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
builtin::blessed($_[1]) $_[1]->isa("DBIx::QuickORM::Value") $_[1]->kind eq "sql"
Truth table50%2/4
A and B and C
A= builtin::blessed($_[1])B= $_[1]->isa("DBIx::QuickORM::Value")C= $_[1]->kind eq "sql"
ABCresult
0XX0
10X0
1100
1111
2026  
2027 =pod
2028  
2029 =over 4
2030  
2031 =item @args = $h->_row_class_arg
2032  
2033 The handle's row class as named arguments, C<< (row_class => $class) >>,
2034 when one was set (even to undef), or an empty list when none was. The ORM's
2035 C<_row>, C<_row_builder>, CRUD calls, and a source's C<results> take it as
2036 their C<row_class> option.
2037  
2038 =back
2039  
2040 =cut
2041  
2042 486 ▶sub _row_class_arg { exists($_[0]->{+ROW_CLASS}) ? (row_class => $_[0]->{+ROW_CLASS}) : () }
Subroutine _row_class_arg: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'row_class'} ? : 82 404 486
2043  
2044 =pod
2045  
2046 =over 4
2047  
2048 =item $row = $h->_row(\%data)
2049  
2050 The row data built into a row as the handle builds rows, or undef for undef.
2051  
2052 =back
2053  
2054 =cut
2055  
2056 0 ▶sub _row {
Subroutine _row: not called CC=1 SCAR=6.9
2057 0 my $self = shift;
2058 0 my ($data) = @_;
2059 0 return $self->{+ORM}->_row($self->{+SOURCE}->table, $data, $self->_row_class_arg);
2060 }
2061  
2062 =pod
2063  
2064 =over 4
2065  
2066 =item $table = $h->_table($action)
2067  
2068 The source's table. Croaks naming the action for a source without one.
2069  
2070 =back
2071  
2072 =cut
2073  
2074 231 ▶sub _table {
Subroutine _table: called CC=2 SCAR=6.9
2075 231 my $self = shift;
2076 231 my ($action) = @_;
2077 231 my $table = $self->{+SOURCE}->table;
2078 231 ▶ croak "$action() needs a handle on a table, not a query or join" unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 13 218 231
2079 218 return $table;
2080 }
2081  
2082 =pod
2083  
2084 =over 4
2085  
2086 =item $table = $h->_bulk_table($action)
2087  
2088 =item $table = $h->_row_table($action)
2089  
2090 The table, after croaking for the settings the action cannot honor.
2091  
2092 =back
2093  
2094 =cut
2095  
2096 33 ▶sub _bulk_table {
Subroutine _bulk_table: called CC=2 SCAR=6.9
2097 33 my $self = shift;
2098 33 my ($action) = @_;
2099 33 my $table = $self->_table($action);
2100 31 my @set = grep { defined $self->{$_} } ORDER_BY(), LIMIT(), OFFSET(), GROUP_BY(), HAVING();
2101 31 ▶ croak "$action() cannot honor the handle's " . CORE::join(', ' => @set) if @set;
Branch: 1 branch
BranchTrueFalseTotal
if @set 3 28 31
2102 28 return $table;
2103 }
2104  
2105 198 ▶sub _row_table {
Subroutine _row_table: called CC=2 SCAR=6.9
2106 198 my $self = shift;
2107 198 my ($action) = @_;
2108 198 my $table = $self->_table($action);
2109 187 my @set = grep { defined $self->{$_} } WHERE(), ORDER_BY(), LIMIT(), OFFSET(), GROUP_BY(), HAVING();
2110 187 ▶ croak "$action() works on the whole table, but the handle has a " . CORE::join(', ' => @set) if @set;
Branch: 1 branch
BranchTrueFalseTotal
if @set 15 172 187
2111 172 return $table;
2112 }
2113  
2114 258 1;
2115  
2116 __END__
« blib/lib/DBIx/QuickORM/DSL.pm ↑ Summary blib/lib/DBIx/QuickORM/LookupTable.pm »