blib/lib/DBIx/QuickORM/Handle.pm

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