blib/lib/DBIx/QuickORM/Handle.pm

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