blib/lib/DBIx/QuickORM/Connection.pm

filter: Statementstmt 88.1% 653 / 741 Branchbran 83.3% 295 / 354 Conditioncond 78.4% 229 / 292 MC/DCmcdc 61.9% 106 / 171 Subroutinesub 89.8% 71 / 79 Podpod 88.8% 16 / 18 totaltotal 82.7% 1370 / 1655 CC 574 SCAR 26.0
CC
574
Cov
85%
init
32.6
init
32.6
insert
30.6
CRAP 13.5
« blib/lib/DBIx/QuickORM/AggregateRow.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Join.pm »
1 package DBIx::QuickORM::Connection;
2 258 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 258 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 258 our $VERSION = '0.000031';
6  
7 258 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 258 ▶use Scalar::Util qw/blessed refaddr weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 258 ▶use DBIx::QuickORM::Dialect();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 258 ▶use DBIx::QuickORM::Exception::DB();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 258 ▶use DBIx::QuickORM::LookupTable();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 258 ▶use DBIx::QuickORM::Map();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 258 ▶use DBIx::QuickORM::Txn();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 258 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 <dbh
18 <db_map
19 <time_zone
20 <datetime_parsers
21 <retain_cache
22 <enforce_canon
23 <isolation
24 <dialect
25 <namespace
26 <pid
27 +lookup
28 <txns
29 +txn_count
30 +busy
31 +statements
32 258 };
33  
34 # Connections with an async query pending, so a forked child can release them at exit (see END).
35 258 my %PENDING;
36  
37 =pod
38  
39 =encoding UTF-8
40  
41 =head1 NAME
42  
43 DBIx::QuickORM::Connection - One database handle and the state tied to it.
44  
45 =head1 DESCRIPTION
46  
47 A Connection holds one database handle and everything that must be reset when
48 that handle is replaced: its dialect, its namespace, the pid that opened it,
49 the open transactions (L<DBIx::QuickORM::Txn>), and the lookup table of canon
50 rows (L<DBIx::QuickORM::LookupTable>). The ORM replaces the whole Connection
51 at once, as it does in a forked child. The new one starts with no transaction
52 and an empty lookup table.
53  
54 It implements the ORM's CRUD operations on plain row data and knows nothing of
55 the ORM: it returns hashrefs, which L<DBIx::QuickORM> wraps. A row object
56 given is read through its C<row_data>. The rules shared by every CRUD
57 operation (row data, C<raw>, value markers, what comes back with and without
58 C<RETURNING>) are in L<DBIx::QuickORM/"CRUD operations">. The entries here add
59 what the Connection itself does.
60  
61 Construction prepares the handle:
62  
63 =over 4
64  
65 =item *
66  
67 C<RaiseError> must be on. C<PrintError> is turned off and
68 C<AutoInactiveDestroy> on.
69  
70 =item *
71  
72 C<AutoCommit> must be on, with no transaction open: the ORM manages
73 transactions.
74  
75 =item *
76  
77 Every database error on the handle is thrown as a
78 L<DBIx::QuickORM::Exception::DB>, classified by the dialect.
79  
80 =item *
81  
82 The dialect sets up the session.
83  
84 =item *
85  
86 The connection must have a current schema or database, or construction
87 croaks.
88  
89 =back
90  
91 Rows read go through the lookup table's C<ingest>. Rows written replace its
92 entry.
93  
94 The single-row CRUD operations build their SQL once per table and set of fields
95 and run it through C<prepare_cached>. SQL holding an C<sql()> or C<binary()>
96 marker, C<insert_many>, and queries built elsewhere are prepared each time.
97  
98 In a forked child, a Connection that is freed, or still held at exit, lets go
99 of its inherited handle (C<release_inherited>), so the parent's session
100 survives.
101  
102 =head1 SYNOPSIS
103  
104 my $conn = $orm->connection; # the ORM's current Connection (a forked child gets a new one)
105  
106 my $dialect = $conn->dialect;
107 my $schema = $conn->namespace;
108 my $lookup = $conn->lookup;
109  
110 =head1 ATTRIBUTES
111  
112 =over 4
113  
114 =item $dbh = $conn->dbh
115  
116 The database handle. Required.
117  
118 =item $map = $conn->db_map
119  
120 The L<DBIx::QuickORM::Map>. When none is given the database is scanned into a
121 new one, which the ORM then adjusts in place.
122  
123 =item $tz = $conn->time_zone
124  
125 The session time zone, C<UTC> unless given.
126  
127 =item \@parsers = $conn->datetime_parsers
128  
129 The ORM's C<datetime_parsers>, for the DateTime type. Empty unless given.
130 Checked as the ORM's C<new> describes. C<set_datetime_parsers> changes it.
131  
132 =item $bool = $conn->retain_cache
133  
134 Passed to the lookup table as C<retain>. 0 unless given.
135  
136 =item $bool = $conn->enforce_canon
137  
138 The default C<enforce_canon> for C<update_row> and C<delete_row> outside a
139 transaction. 1 unless given. C<set_enforce_canon> changes it.
140  
141 =item $level = $conn->isolation
142  
143 The isolation level a root transaction opens at when C<txn> names none, in
144 lower case: a level C<txn> takes, or C<'server'>. Undef unless given: then
145 C<'read committed'> where the dialect C<supports_isolation>, otherwise
146 C<'server'>. An unknown level croaks.
147  
148 =item $dialect = $conn->dialect
149  
150 The L<DBIx::QuickORM::Dialect> object for the handle, holding the server
151 version.
152  
153 =item $ns = $conn->namespace
154  
155 The current schema or database, from the dialect.
156  
157 =item $pid = $conn->pid
158  
159 The process that opened the connection.
160  
161 =item \@txns = $conn->txns
162  
163 The open transactions, outermost first, as L<DBIx::QuickORM::Txn> objects
164 (held weakly). Empty outside a transaction.
165  
166 =back
167  
168 =cut
169  
170 350 ▶sub init {
Subroutine init: called CC=12 SCAR=34.9
Pod undocumented
171 0 ▶ my $self = shift;
Subroutine init: not called CC=12 SCAR=34.9
Pod undocumented
172  
173 0 my $dbh = $self->{+DBH};
174 0 ▶ croak "The connection did not produce a DBI database handle" unless blessed($dbh) && $dbh->isa('DBI::db');
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($dbh) and $dbh->isa("DBI::db") 2 348 350
unless builtin::blessed($dbh) and $dbh->isa("DBI::db") 0 0 0
Condition33%2/6
builtin::blessed($dbh) and $dbh->isa("DBI::db")
!ll&&!rl&&r
20348
builtin::blessed($dbh) and $dbh->isa("DBI::db")
!ll&&!rl&&r
000
MC/DC50%1/2
builtin::blessed($dbh) and $dbh->isa("DBI::db")
builtin::blessed($dbh) $dbh->isa("DBI::db")
MC/DC0%0/2
builtin::blessed($dbh) and $dbh->isa("DBI::db")
builtin::blessed($dbh) $dbh->isa("DBI::db")
Truth table66%2/3
A and B
A= builtin::blessed($dbh)B= $dbh->isa("DBI::db")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dbh)B= $dbh->isa("DBI::db")
ABresult
0X0
100
111
175 0 ▶ croak "The database handle must have RaiseError on" unless $dbh->{RaiseError};
Branch: 2 branches
BranchTrueFalseTotal
unless $dbh->{"RaiseError"} 16 332 348
unless $dbh->{"RaiseError"} 0 0 0
176 croak "The database handle must have AutoCommit on and no transaction open; the ORM manages transactions"
177 0 ▶ unless $dbh->{AutoCommit};
Branch: 2 branches
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 20 312 332
unless $dbh->{"AutoCommit"} 0 0 0
178  
179 0 ▶ $self->{+TIME_ZONE} //= 'UTC';
Condition50%2/4
$self->{"time_zone"} //= "UTC"
l!l
32280
$self->{"time_zone"} //= "UTC"
l!l
00
MC/DC100%1/1
$self->{"time_zone"} //= "UTC"
$self->{"time_zone"}
MC/DC0%0/1
$self->{"time_zone"} //= "UTC"
$self->{"time_zone"}
Truth table100%2/2
A
A= $self->{"time_zone"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"time_zone"}
Aresult
11
00
180 0 ▶ $self->{+DATETIME_PARSERS} = exists($self->{+DATETIME_PARSERS}) ? $self->_check_datetime_parsers($self->{+DATETIME_PARSERS}) : [];
Branch: 2 branches
BranchTrueFalseTotal
exists $self->{'datetime_parsers'} ? : 13 299 312
exists $self->{'datetime_parsers'} ? : 0 0 0
181 0 ▶ $self->{+RETAIN_CACHE} //= 0;
Condition50%2/4
$self->{"retain_cache"} //= 0
l!l
13294
$self->{"retain_cache"} //= 0
l!l
00
MC/DC100%1/1
$self->{"retain_cache"} //= 0
$self->{"retain_cache"}
MC/DC0%0/1
$self->{"retain_cache"} //= 0
$self->{"retain_cache"}
Truth table100%2/2
A
A= $self->{"retain_cache"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"retain_cache"}
Aresult
11
00
182 0 ▶ $self->{+ENFORCE_CANON} //= 1;
Condition50%2/4
$self->{"enforce_canon"} //= 1
l!l
6301
$self->{"enforce_canon"} //= 1
l!l
00
MC/DC100%1/1
$self->{"enforce_canon"} //= 1
$self->{"enforce_canon"}
MC/DC0%0/1
$self->{"enforce_canon"} //= 1
$self->{"enforce_canon"}
Truth table100%2/2
A
A= $self->{"enforce_canon"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"enforce_canon"}
Aresult
11
00
183 0 ▶ $self->{+ISOLATION} = $self->_isolation($self->{+ISOLATION}) if defined $self->{+ISOLATION};
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->{"isolation"} 4 303 307
if defined $self->{"isolation"} 0 0 0
184  
185 0 $dbh->{PrintError} = 0;
186 0 $dbh->{AutoInactiveDestroy} = 1;
187  
188 0 my $dialect = $self->{+DIALECT} = DBIx::QuickORM::Dialect->for_dbh($dbh);
189 0 $dbh->{HandleError} = $self->_error_handler($dialect, $self->{+TXNS} = []);
190 0 $dialect->init_session($dbh, $self->{+TIME_ZONE});
191  
192 0 my $ns = $self->{+NAMESPACE} = $dialect->namespace($dbh);
193 0 ▶ croak "The connection has no current schema or database; select one in the DSN" unless defined $ns;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $ns 1 304 305
unless defined $ns 0 0 0
194  
195 0 ▶ $self->{+DB_MAP} //= DBIx::QuickORM::Map->new(tables => $dialect->scan($dbh));
Condition33%2/6
$self->{"db_map"} //= "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
l!l&&r!l&&!r
192850
$self->{"db_map"} //= "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"db_map"} //= "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
$self->{"db_map"} "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
MC/DC0%0/2
$self->{"db_map"} //= "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
$self->{"db_map"} "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
Truth table66%2/3
A //= B
A= $self->{"db_map"}B= "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $self->{"db_map"}B= "DBIx::QuickORM::Map"->new("tables", $dialect->scan($dbh))
ABresult
1X1
011
000
196 0 $self->{+PID} = $$;
197 }
198  
199 =head1 PUBLIC METHODS
200  
201 =over 4
202  
203 =item $conn->set_enforce_canon($bool)
204  
205 Changes C<enforce_canon>. Each C<update_row> and C<delete_row> reads it when
206 it runs.
207  
208 =item $conn->set_datetime_parsers(\@parsers)
209  
210 Replaces C<datetime_parsers> with a copy of C<@parsers>, checked as the ORM's
211 C<new> checks them: it loads each class, and croaks on a bad parser.
212  
213 Values read after the call use the new parsers. A DateTime field read before
214 the call keeps the parsers it was read with, even while still unparsed.
215  
216 =back
217  
218 =cut
219  
220 2 ▶sub set_enforce_canon { $_[0]->{+ENFORCE_CANON} = $_[1] }
Subroutine set_enforce_canon: called CC=1 SCAR=0.0
Pod documented
221  
222 6 ▶sub set_datetime_parsers {
Subroutine set_datetime_parsers: called CC=1 SCAR=0.0
Pod documented
223 6 my $self = shift;
224 6 my ($parsers) = @_;
225 6 $self->{+DATETIME_PARSERS} = $self->_check_datetime_parsers($parsers);
226 }
227  
228 =over 4
229  
230 =item $lookup = $conn->lookup
231  
232 The Connection's L<DBIx::QuickORM::LookupTable>, built on first use.
233  
234 =back
235  
236 =cut
237  
238 1257 ▶sub lookup {
Subroutine lookup: called CC=2 SCAR=7.1
Pod documented
239 1257 my $self = shift;
240 1257 ▶ return $self->{+LOOKUP} //= DBIx::QuickORM::LookupTable->new(db_map => $self->{+DB_MAP}, connection => $self, retain => $self->{+RETAIN_CACHE});
Condition66%2/3
$self->{"lookup"} //= "DBIx::QuickORM::LookupTable"->new("db_map", $self->{"db_map"}, "connection", $self, "retain", $self->{"retain_cache"})
l!l&&r!l&&!r
1167900
MC/DC0%0/2
$self->{"lookup"} //= "DBIx::QuickORM::LookupTable"->new("db_map", $self->{"db_map"}, "connection", $self, "retain", $self->{"retain_cache"})
$self->{"lookup"} "DBIx::QuickORM::LookupTable"->new("db_map", $self->{"db_map"}, "connection", $self, "retain", $self->{"retain_cache"})
Truth table66%2/3
A //= B
A= $self->{"lookup"}B= "DBIx::QuickORM::LookupTable"->new("db_map", $self->{"db_map"}, "connection", $self, "retain", $self->{"retain_cache"})
ABresult
1X1
011
000
241 }
242  
243 =over 4
244  
245 =item $bool = $conn->is_canon($table, $row_or_data)
246  
247 True when the row data (or a row object's C<row_data>) is the lookup table's
248 entry for its primary key. Croaks when the primary key cannot be determined.
249  
250 =back
251  
252 =cut
253  
254 130 ▶sub is_canon {
Subroutine is_canon: called CC=1 SCAR=0.0
Pod documented
255 130 my $self = shift;
256 130 my ($table, $data) = @_;
257 130 return $self->lookup->is_canon($table, $self->_row_data($data));
258 }
259  
260 =over 4
261  
262 =item $data = $conn->insert($table, \%fields, raw => $bool, as_given => $bool, void => $bool)
263  
264 Inserts one row and returns its new row data, now the lookup table's entry.
265 Where the primary key comes from, C<as_given>, and the croaks, are as
266 L<DBIx::QuickORM/"CRUD operations"> describes for C<insert>.
267  
268 Primary key fields left out may get generated values first (see
269 C<_generate_keys>). C<as_given> includes them.
270  
271 A table without a primary key returns the C<RETURNING> row without storing it
272 in the lookup table.
273  
274 With C<as_given>, C<RETURNING> names only the primary key fields, and the
275 result is never stored.
276  
277 C<< void => 1 >> is what the ORM passes in void context: the row is inserted
278 without C<RETURNING>, nothing is stored or returned, and none of the primary
279 key croaks apply.
280  
281 =back
282  
283 =cut
284  
285 258 my %INSERT_ARGS = map { $_ => 1 } qw/raw void as_given/;
286  
287 284 ▶sub insert {
Subroutine insert: called CC=30 SCAR=34.2
Pod documented
288 284 my $self = shift;
289 284 my ($table, $fields, %params) = @_;
290  
291 284 $self->_check_params(insert => \%INSERT_ARGS, \%params);
292 283 my $t = $self->_table($table);
293 282 my $values = $self->_row_data($fields);
294 282 ▶ confess "insert() into '$table' needs at least one field" unless %$values;
Branch: 1 branch
BranchTrueFalseTotal
unless %$values 2 280 282
295 280 my $generated = $self->_generate_keys($t, $values);
296 280 $values = {%{$self->deflate($table, $values, raw => $params{raw})}, %{$self->deflate($table, $generated)}};
297 276 my @names = sort keys %$values;
298  
299 276 my $void = $params{void};
300 276 my $as_given = $params{as_given};
301 276 my $dialect = $self->{+DIALECT};
302 276 ▶ my $pk = $t->{pk} && @{$t->{pk}} ? $t->{pk} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$t->{'pk'} && @{$t->{'pk'};} ? : 272 4 276
Condition66%2/3
$t->{"pk"} and @{$t->{"pk"};}
!ll&&!rl&&r
40272
MC/DC50%1/2
$t->{"pk"} and @{$t->{"pk"};}
$t->{"pk"} @{$t->{"pk"};}
Truth table66%2/3
A and B
A= $t->{"pk"}B= @{$t->{"pk"};}
ABresult
0X0
100
111
303 276 ▶ my $returning = !$void && ($pk || !$as_given) && $dialect->supports_returning_insert;
Condition100%9/9
!$void && ($pk || !$as_given) && $dialect->supports_returning_insert
!ll&&!rl&&r
1371138
not $void and $pk || !$as_given
!ll&&!rl&&r
1361139
$pk or not $as_given
l!l&&r!l&&!r
13811
MC/DC100%4/4
!$void && ($pk || !$as_given) && $dialect->supports_returning_insert
not $void $pk not $as_given $dialect->supports_returning_insert
Truth table83%5/6
not(A) and B or not(C) && D
A= not $voidB= $pkC= not $as_givenD= $dialect->supports_returning_insert
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
304 276 ▶ my $given_pk = $pk && !grep { !defined($values->{$_}) || $self->_is_sql($values->{$_}) } @$pk;
Condition100%6/6
$pk && !grep({not defined $values->{$_} or $self->_is_sql($values->{$_});} @$pk)
!ll&&!rl&&r
435237
!defined($values->{$_}) || $self->_is_sql($values->{$_})
l!l&&r!l&&!r
341250
MC/DC100%2/2
$pk && !grep({not defined $values->{$_} or $self->_is_sql($values->{$_});} @$pk)
$pk !grep({not defined $values->{$_} or $self->_is_sql($values->{$_});} @$pk)
MC/DC100%2/2
!defined($values->{$_}) || $self->_is_sql($values->{$_})
!defined($values->{$_}) $self->_is_sql($values->{$_})
Truth table100%3/3
A && not(B)
A= $pkB= !grep({not defined $values->{$_} or $self->_is_sql($values->{$_});} @$pk)
ABresult
0X0
100
111
Truth table100%3/3
not(A) || B
A= !defined($values->{$_})B= $self->_is_sql($values->{$_})
ABresult
1X1
011
000
305 276 ▶ unless ($void || $returning) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($void or $returning) 2 274 276
Condition100%3/3
$void or $returning
l!l&&r!l&&!r
1361382
MC/DC100%2/2
$void or $returning
$void $returning
Truth table100%3/3
A or B
A= $voidB= $returning
ABresult
1X1
011
000
306 2 ▶ confess "insert() into '$table' has nothing to return: the table has no primary key and the database cannot return the row; call it in void context" unless $pk || $as_given;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk or $as_given 0 2 2
Condition66%2/3
$pk or $as_given
l!l&&r!l&&!r
110
MC/DC0%0/2
$pk or $as_given
$pk $as_given
Truth table66%2/3
A or B
A= $pkB= $as_given
ABresult
1X1
011
000
307 2 ▶ confess "insert() into '$table' needs every primary key field, since the database cannot return a compound key it generates" if $pk && !$given_pk && @$pk > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $pk and not $given_pk and @$pk > 1 0 2 2
Condition66%4/6
$pk and not $given_pk and @$pk > 1
!ll&&!rl&&r
110
$pk and not $given_pk
!ll&&!rl&&r
101
MC/DC0%0/3
$pk and not $given_pk and @$pk > 1
$pk not $given_pk @$pk > 1
Truth table50%2/4
A and not(B) and C
A= $pkB= not $given_pkC= @$pk > 1
ABCresult
0XX0
10X0
1100
1111
308 }
309  
310 276 my ($places, $binds, $marked) = $self->_value_sql($values, @names);
311 276 ▶ my $shape = $marked ? undef : join("\0" => 'insert', $table, ($returning ? ($as_given ? 'pk' : 'all') : ''), @names);
Branch: 3 branches
BranchTrueFalseTotal
$marked ? : 7 269 276
$returning ? : 136 133 269
$as_given ? : 6 130 136
312 my $sql = $self->_statement(
313 $shape,
314 143 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
315 143 my $sql = 'INSERT INTO ' . $dialect->quote_identifier($table) . ' (' . join(', ' => map { $dialect->quote_identifier($_) } @names) . ') VALUES (' . join(', ' => @$places) . ')';
316 143 ▶ $returning ? $sql . $self->_returning_sql($table, $as_given ? $pk : undef) : $sql;
Branch: 2 branches
BranchTrueFalseTotal
$returning ? : 78 65 143
$as_given ? : 6 72 78
317 }
318 276 );
319 276 my $sth = $self->_run($t->{fields}, $sql, $binds, undef, defined $shape);
320 261 ▶ return if $void;
Branch: 1 branch
BranchTrueFalseTotal
if $void 125 136 261
321  
322 136 ▶ my $data = $returning ? $self->_fetch($t->{fields}, $sth)->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$returning ? : 134 2 136
323 136 ▶ if ($pk && !$data && $given_pk) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pk and not $data and $given_pk) { } 0 136 136
elsif ($pk and not $data) { } 1 137 138
Condition83%5/6
$pk and not $data and $given_pk
!ll&&!rl&&r
13510
$pk and not $data
!ll&&!rl&&r
21331
MC/DC0%0/3
$pk and not $data and $given_pk
$pk not $data $given_pk
Truth table75%3/4
A and not(B) and C
A= $pkB= not $dataC= $given_pk
ABCresult
0XX0
10X0
1100
1111
324 0 $data = $self->_plain({map { $_ => $values->{$_} } @$pk});
325 }
326 ▶ elsif ($pk && !$data) {
Condition100%3/3
$pk and not $data
!ll&&!rl&&r
21331
MC/DC100%2/2
$pk and not $data
$pk not $data
Truth table100%3/3
A and not(B)
A= $pkB= not $data
ABresult
0X0
100
111
327 # Only MySQL gets here (no RETURNING). DBD::MariaDB gives 0 when the insert
328 # generated no key, and AUTO_INCREMENT never generates 0, so 0 is "none".
329 1 ▶ my $id = $dialect->last_insert_id($self->{+DBH}, $table, $pk->[0]) or confess "insert() into '$table' did not give a value for the primary key '$pk->[0]', and the database generated none";
Branch: 1 branch
BranchTrueFalseTotal
my $id = $dialect->last_insert_id($self->{"dbh"}, $table, $pk->[0]) or confess("insert() into '${table}' did not give a value for the primary key '$pk->[0]', and the database generated none") 1 0 1
330 1 $data = {$pk->[0] => $id};
331 }
332  
333 136 my $lookup = $self->lookup;
334 136 my $out;
335 136 ▶ if ($as_given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_given) { } 8 128 136
336 8 my %copy = map { $_ => $values->{$_} } grep { !$self->_is_sql($values->{$_}) } @names;
337 8 ▶ $out = {%{$self->_plain(\%copy)}, %{$data // {}}};
Condition100%2/2
$data // {}
l!l
71
MC/DC100%1/1
$data // {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
338 }
339 else {
340 128 ▶ $out = $pk ? $lookup->store($table, $data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
$pk ? : 127 1 128
341 }
342  
343 136 $lookup->mark_inserted($out);
344 136 return $out;
345 }
346  
347 =over 4
348  
349 =item \@data = $conn->insert_many($table, \@rows, raw => $bool)
350  
351 =item $count = $conn->insert_many($table, \@rows, count => 1, raw => $bool)
352  
353 =item $conn->insert_many($table, \@rows, void => 1, raw => $bool)
354  
355 Generates missing primary key values for each row as C<insert> does, then
356 inserts the rows in multi-row C<INSERT> statements, each holding as many rows
357 as the dialect's C<bind_limit> allows. The batching, the transaction around
358 it, and the croaks are as L<DBIx::QuickORM/"CRUD operations"> describes for
359 C<insert_many>.
360  
361 It returns an arrayref of the new rows' row data, now the lookup table's
362 entries (a table without a primary key bypasses it). Without C<RETURNING> each
363 holds the primary key taken from the given values.
364  
365 C<< count => 1 >> returns how many rows the database reports inserted
366 instead, and C<< void => 1 >> (what the ORM passes in void context) returns
367 nothing useful. Neither sends C<RETURNING> or stores anything.
368  
369 An empty C<\@rows> sends nothing and returns an empty arrayref, or 0 with
370 C<count>.
371  
372 The field list is checked once per call. Each value is deflated as
373 C<deflate> would, through C<_deflate_value>.
374  
375 =back
376  
377 =cut
378  
379 258 my %INSERT_MANY_ARGS = map { $_ => 1 } qw/raw count void/;
380  
381 45 ▶sub insert_many {
Subroutine insert_many: called CC=23 SCAR=32.6
Pod documented
382 45 my $self = shift;
383 45 my ($table, $rows, %params) = @_;
384  
385 45 $self->_check_params(insert_many => \%INSERT_MANY_ARGS, \%params);
386 41 my $t = $self->_table($table);
387 41 ▶ confess "insert_many() needs an arrayref of rows" unless ref($rows) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 1 40 41
388 40 ▶ my $returning = !$params{void} && !$params{count};
Condition100%3/3
$params{'void'} || $params{'count'}
l!l&&r!l&&!r
15718
MC/DC100%2/2
$params{'void'} || $params{'count'}
$params{'void'} $params{'count'}
Truth table100%3/3
A || B
A= $params{'void'}B= $params{'count'}
ABresult
1X1
011
000
389 40 ▶ return $returning ? [] : 0 unless @$rows;
Branch: 2 branches
BranchTrueFalseTotal
unless @$rows 6 34 40
$returning ? : 3 3 6
390 34 my @values = map { $self->_row_data($_) } @$rows;
391 34 my @names = sort keys %{$values[0]};
392 34 ▶ confess "insert_many() into '$table' needs at least one field in each row" unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 1 33 34
393  
394 33 for my $i (1 .. $#values) {
395 416 my $v = $values[$i];
396 416 ▶ confess "insert_many() into '$table' needs the same fields in every row, but row $i differs from the first" unless keys(%$v) == @names && !grep { !exists $v->{$_} } @names;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$v == @names and not grep({not exists $v->{$_};} @names) 2 414 416
Condition100%3/3
keys %$v == @names and not grep({not exists $v->{$_};} @names)
!ll&&!rl&&r
11414
MC/DC100%2/2
keys %$v == @names and not grep({not exists $v->{$_};} @names)
keys %$v == @names not grep({not exists $v->{$_};} @names)
Truth table100%3/3
A and not(B)
A= keys %$v == @namesB= not grep({not exists $v->{$_};} @names)
ABresult
0X0
100
111
397 }
398  
399 # The fields are checked once, and each value deflated as deflate() would, with its field record at hand.
400 31 $self->_check_fields($table, @names);
401 30 my ($fields, $raw) = ($t->{fields}, $params{raw});
402 @values = map {
403 30 my $v = $_;
404 444 my $generated = $self->_generate_keys($t, $v);
405 444 my %out = map { ($_ => $self->_deflate_value($table, $_, $fields->{$_}, $v->{$_}, $raw)) } @names;
406 444 $out{$_} = $self->_deflate_value($table, $_, $fields->{$_}, $generated->{$_}) for keys %$generated;
407 444 \%out;
408 } @values;
409 30 @names = sort keys %{$values[0]};
410  
411 30 my $dialect = $self->{+DIALECT};
412 30 ▶ my $pk = $t->{pk} && @{$t->{pk}} ? $t->{pk} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$t->{'pk'} && @{$t->{'pk'};} ? : 27 3 30
Condition66%2/3
$t->{"pk"} and @{$t->{"pk"};}
!ll&&!rl&&r
3027
MC/DC50%1/2
$t->{"pk"} and @{$t->{"pk"};}
$t->{"pk"} @{$t->{"pk"};}
Truth table66%2/3
A and B
A= $t->{"pk"}B= @{$t->{"pk"};}
ABresult
0X0
100
111
413 30 ▶ my $sql_ret = $returning && $dialect->supports_returning_insert;
Condition66%2/3
$returning && $dialect->supports_returning_insert
!ll&&!rl&&r
15015
MC/DC50%1/2
$returning && $dialect->supports_returning_insert
$returning $dialect->supports_returning_insert
Truth table66%2/3
A && B
A= $returningB= $dialect->supports_returning_insert
ABresult
0X0
100
111
414 30 ▶ if ($returning && !$sql_ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returning and not $sql_ret) 0 30 30
Condition66%2/3
$returning and not $sql_ret
!ll&&!rl&&r
15150
MC/DC0%0/2
$returning and not $sql_ret
$returning not $sql_ret
Truth table66%2/3
A and not(B)
A= $returningB= not $sql_ret
ABresult
0X0
100
111
415 0 ▶ confess "insert_many() into '$table' cannot return rows: the table has no primary key and the database cannot return them; pass count => 1 for how many went in" unless $pk;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk 0 0 0
416 my @short = grep {
417 0 my $v = $_;
418 0 ▶ grep { !defined($v->{$_}) || $self->_is_sql($v->{$_}) } @$pk
Condition0%0/3
!defined($v->{$_}) || $self->_is_sql($v->{$_})
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($v->{$_}) || $self->_is_sql($v->{$_})
!defined($v->{$_}) $self->_is_sql($v->{$_})
Truth table0%0/3
not(A) || B
A= !defined($v->{$_})B= $self->_is_sql($v->{$_})
ABresult
1X1
011
000
419 } @values;
420 0 ▶ confess "insert_many() into '$table' cannot return rows unless every row carries its whole primary key, since the database cannot return the keys it generates; pass count => 1 for how many went in" if @short;
Branch: 1 branch
BranchTrueFalseTotal
if @short 0 0 0
421 }
422  
423 30 my $into = 'INSERT INTO ' . $dialect->quote_identifier($table) . ' (' . join(', ' => map { $dialect->quote_identifier($_) } @names) . ') VALUES ';
424 30 my @batches = $self->_batches(\@values, \@names);
425 30 ▶ my $send = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
426 30 my ($count, @got) = (0);
427 30 for my $batch (@batches) {
428 38 my ($places, $binds) = @$batch;
429 38 ▶ my $sth = $self->_run($t->{fields}, $into . join(', ' => @$places) . ($sql_ret ? $self->_returning_sql($table) : ''), $binds);
Branch: 1 branch
BranchTrueFalseTotal
$sql_ret ? : 17 21 38
430 36 ▶ if ($sql_ret) { push @got => @{$self->_fetch($t->{fields}, $sth)} }
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_ret) { } 17 19 36
431 19 else { $count += $sth->rows }
432 }
433 28 ▶ return $sql_ret ? \@got : $count;
Branch: 1 branch
BranchTrueFalseTotal
$sql_ret ? : 15 13 28
434 30 };
435  
436 30 ▶ my $own_txn = @{$self->{+TXNS}} ? $dialect->supports_savepoints : @batches > 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'txns'};} ? : 3 27 30
437 30 ▶ my $out = $own_txn ? $self->txn($send) : $send->();
Branch: 1 branch
BranchTrueFalseTotal
$own_txn ? : 6 24 30
438 28 ▶ return $out unless $returning;
Branch: 1 branch
BranchTrueFalseTotal
unless $returning 13 15 28
439  
440 # Without RETURNING each row is its primary key, taken from the given values.
441 my @got = $sql_ret ? @$out : map {
442 0 ▶ my $v = $_;
Branch: 1 branch
BranchTrueFalseTotal
$sql_ret ? : 15 0 15
443 0 $self->_plain({map { ($_ => $v->{$_}) } @$pk})
444 } @values;
445  
446 15 my $lookup = $self->lookup;
447 15 ▶ @got = map { $lookup->store($table, $_) } @got if $pk;
Branch: 1 branch
BranchTrueFalseTotal
if $pk 14 1 15
448 15 $lookup->mark_inserted(@got);
449 15 return \@got;
450 }
451  
452 =over 4
453  
454 =item $data = $conn->update_row($table, \%data, \%changes, %opts)
455  
456 Updates one row, found by the primary key in the row data, and returns its new
457 row data, or undef when no row matched.
458  
459 The new row data replaces the old in the lookup table, re-keyed when the
460 primary key changed. Undef leaves the lookup table alone. At least one change
461 is required. Setting a primary key field with C<sql()> croaks on a database
462 without C<RETURNING> for C<UPDATE>, since the new key could not be known.
463  
464 The options C<enforce_canon>, C<cas>, and C<auto_cas> are as
465 L<DBIx::QuickORM/"CRUD operations"> describes for C<update_row>. Each C<cas>
466 field must be in the row data.
467  
468 =back
469  
470 =cut
471  
472 258 my %UPDATE_ARGS = map { $_ => 1 } qw/enforce_canon cas auto_cas raw/;
473  
474 107 ▶sub update_row {
Subroutine update_row: called CC=11 SCAR=24.0
Pod documented
475 107 my $self = shift;
476 107 my ($table, $data, $changes, %params) = @_;
477  
478 107 $self->_check_params(update_row => \%UPDATE_ARGS, \%params);
479 106 my $t = $self->_table($table);
480 105 $data = $self->_row_data($data);
481 104 ▶ confess "update_row() needs a hashref of at least one change" unless ref($changes) eq 'HASH' && %$changes;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $changes eq "HASH" and %$changes 2 102 104
Condition66%2/3
ref $changes eq "HASH" and %$changes
!ll&&!rl&&r
02102
MC/DC50%1/2
ref $changes eq "HASH" and %$changes
ref $changes eq "HASH" %$changes
Truth table66%2/3
A and B
A= ref $changes eq "HASH"B= %$changes
ABresult
0X0
100
111
482 102 my @set = sort keys %$changes;
483  
484 102 my $pk = $self->_pk_values($table, $data, $params{raw});
485 99 $self->_enforce_canon($table, $data, $params{enforce_canon});
486 86 my $where = {%{$self->deflate($table, {$self->_cas_values($table, $data, $changes, \%params)}, raw => $params{raw})}, %$pk};
487 83 $changes = $self->deflate($table, $changes, raw => $params{raw});
488  
489 82 my $dialect = $self->{+DIALECT};
490 82 my $returning = $dialect->supports_returning_update;
491 82 my @sql_pk = grep { $self->_is_sql($changes->{$_}) } keys %$pk;
492 82 ▶ confess "update_row() cannot set the primary key of '$table' with sql() on a database without RETURNING for UPDATE, since the new key could not be known" if @sql_pk && !$returning;
Branch: 1 branch
BranchTrueFalseTotal
if @sql_pk and not $returning 1 81 82
Condition66%2/3
@sql_pk and not $returning
!ll&&!rl&&r
8101
MC/DC50%1/2
@sql_pk and not $returning
@sql_pk not $returning
Truth table66%2/3
A and not(B)
A= @sql_pkB= not $returning
ABresult
0X0
100
111
493  
494 81 my ($where_sql, $where_binds, $where_marked) = $self->_where_sql($where);
495 79 my ($places, $set_binds, $marked) = $self->_value_sql($changes, @set);
496 79 ▶ my $shape = $marked || $where_marked ? undef : join("\0" => 'update', $table, @set, $where_sql);
Branch: 1 branch
BranchTrueFalseTotal
$marked || $where_marked ? : 5 74 79
Condition66%2/3
$marked or $where_marked
l!l&&r!l&&!r
5074
MC/DC50%1/2
$marked or $where_marked
$marked $where_marked
Truth table66%2/3
A or B
A= $markedB= $where_marked
ABresult
1X1
011
000
497 my $sql = $self->_statement(
498 $shape,
499 45 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
500 45 my $sql = 'UPDATE ' . $dialect->quote_identifier($table) . ' SET ' . join(', ' => map { $dialect->quote_identifier($set[$_]) . " = $places->[$_]" } 0 .. $#set) . " WHERE $where_sql";
501 45 ▶ $returning ? $sql . $self->_returning_sql($table) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
$returning ? : 44 1 45
502 }
503 79 );
504 79 my $sth = $self->_run($t->{fields}, $sql, [@$set_binds, @$where_binds], undef, defined $shape);
505  
506 77 my $new;
507 77 ▶ if ($returning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returning) { } 76 1 77
508 76 ▶ $new = $self->_fetch($t->{fields}, $sth)->[0] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$new = $self->_fetch($t->{"fields"}, $sth)->[0] or return undef 66 10 76
509 }
510 else {
511 1 ▶ return undef unless $sth->rows > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->rows > 0 0 1 1
512 1 ▶ $new = $self->_plain({map { $_ => exists($changes->{$_}) ? $changes->{$_} : $pk->{$_} } keys %$pk});
Branch: 1 branch
BranchTrueFalseTotal
exists $changes->{$_} ? : 1 0 1
513 }
514  
515 67 my $lookup = $self->lookup;
516 67 $lookup->forget($table, $self->_plain($pk));
517 67 $lookup->carry_inserted($data, $new);
518 67 return $lookup->store($table, $new);
519 }
520  
521 =over 4
522  
523 =item $count = $conn->delete_row($table, \%data, enforce_canon => $bool, raw => $bool)
524  
525 Deletes one row, found by the primary key in the row data, and returns how
526 many rows were deleted (0 or 1). Its lookup table entry is removed, and the
527 row data and that entry are marked deleted (see L<DBIx::QuickORM::LookupTable/Row state>).
528 C<enforce_canon> is as for C<update_row>.
529  
530 =back
531  
532 =cut
533  
534 258 my %DELETE_ARGS = map { $_ => 1 } qw/enforce_canon raw/;
535  
536 38 ▶sub delete_row {
Subroutine delete_row: called CC=2 SCAR=6.9
Pod documented
537 38 my $self = shift;
538 38 my ($table, $data, %params) = @_;
539  
540 38 $self->_check_params(delete_row => \%DELETE_ARGS, \%params);
541 37 my $t = $self->_table($table);
542 37 $data = $self->_row_data($data);
543 37 my $pk = $self->_pk_values($table, $data, $params{raw});
544 37 $self->_enforce_canon($table, $data, $params{enforce_canon});
545  
546 33 my ($where_sql, $where_binds, $marked) = $self->_where_sql($pk);
547 33 ▶ my $sql = $self->_statement($marked ? undef : join("\0" => 'delete', $table, $where_sql), sub { 'DELETE FROM ' . $self->{+DIALECT}->quote_identifier($table) . " WHERE $where_sql" });
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$marked ? : 1 32 33
548 33 my $sth = $self->_run($t->{fields}, $sql, $where_binds, undef, !$marked);
549  
550 32 $self->lookup->gone($table, $self->_plain($pk), $data);
551 32 return $sth->rows;
552 }
553  
554 =over 4
555  
556 =item $data = $conn->refresh_row($table, $pk, cache => $bool, db => $bool, fields => \@fields, raw => $bool)
557  
558 The canon row data for the primary key, or undef when there is no such row.
559 C<$pk> is the key in any form C<_pk_values> takes, so row data works and its
560 other fields are ignored. This is also C<find>.
561  
562 C<fields> must be fields of the table. C<cache>, C<db>, and C<fields> are as
563 L<DBIx::QuickORM/"CRUD operations"> describes for C<refresh_row>: the lookup
564 table's entry when it holds every field asked for, otherwise those fields read
565 from the database and ingested.
566  
567 A row the database no longer has is removed from the lookup table, and the
568 row data given and that entry are marked deleted.
569  
570 =back
571  
572 =cut
573  
574 258 my %REFRESH_ARGS = map { $_ => 1 } qw/cache db fields raw/;
575  
576 232 ▶sub refresh_row {
Subroutine refresh_row: called CC=20 SCAR=30.0
Pod documented
577 232 my $self = shift;
578 232 my ($table, $data, %params) = @_;
579  
580 232 $self->_check_params(refresh_row => \%REFRESH_ARGS, \%params);
581 232 my $t = $self->_table($table);
582 232 my $pk = $self->_pk_values($table, $data, $params{raw});
583 225 my $key = $self->_plain($pk);
584 225 ▶ my $cache = $params{cache} // 1;
Condition100%2/2
$params{"cache"} // 1
l!l
51174
MC/DC100%1/1
$params{"cache"} // 1
$params{"cache"}
Truth table100%2/2
A
A= $params{"cache"}
Aresult
11
00
585 225 ▶ my $db = $params{db} // 1;
Condition100%2/2
$params{"db"} // 1
l!l
32193
MC/DC100%1/1
$params{"db"} // 1
$params{"db"}
Truth table100%2/2
A
A= $params{"db"}
Aresult
11
00
586 225 ▶ confess "refresh_row() needs 'cache' or 'db' to be true" unless $cache || $db;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache or $db 1 224 225
Condition100%3/3
$cache or $db
l!l&&r!l&&!r
174501
MC/DC100%2/2
$cache or $db
$cache $db
Truth table100%3/3
A or B
A= $cacheB= $db
ABresult
1X1
011
000
587  
588 224 my $fields = $params{fields};
589 224 ▶ confess "'fields' must be an arrayref of field names" if defined($fields) && ref($fields) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields and ref $fields ne "ARRAY" 1 223 224
Condition100%3/3
defined $fields and ref $fields ne "ARRAY"
!ll&&!rl&&r
211121
MC/DC100%2/2
defined $fields and ref $fields ne "ARRAY"
defined $fields ref $fields ne "ARRAY"
Truth table100%3/3
A and B
A= defined $fieldsB= ref $fields ne "ARRAY"
ABresult
0X0
100
111
590 223 ▶ $self->_check_fields($table, @$fields) if $fields;
Branch: 1 branch
BranchTrueFalseTotal
if $fields 12 211 223
591  
592 222 my %seen;
593 222 ▶ my @select = $fields ? grep { !$seen{$_}++ } sort(@$fields, @{$t->{pk}}) : @{$self->_default_fields($table)};
Branch: 1 branch
BranchTrueFalseTotal
$fields ? : 11 211 222
594  
595 222 my $lookup = $self->lookup;
596 222 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 172 50 222
597 172 my $canon = $lookup->lookup($table, $key);
598 172 ▶ return $canon if $canon && (!$db || !grep { !exists $canon->{$_} } @select);
Branch: 1 branch
BranchTrueFalseTotal
if $canon and not $db && grep({not exists $canon->{$_};} @select) 57 115 172
Condition100%6/6
$canon and not $db && grep({not exists $canon->{$_};} @select)
!ll&&!rl&&r
109657
$db && grep({not exists $canon->{$_};} @select)
!ll&&!rl&&r
21366
MC/DC100%3/3
$canon and not $db && grep({not exists $canon->{$_};} @select)
$canon $db grep({not exists $canon->{$_};} @select)
Truth table100%4/4
A and not(B && C)
A= $canonB= $dbC= grep({not exists $canon->{$_};} @select)
ABCresult
0XX0
1110
10X1
1101
599 }
600 165 ▶ return undef unless $db;
Branch: 1 branch
BranchTrueFalseTotal
unless $db 10 155 165
601  
602 155 my $dialect = $self->{+DIALECT};
603 155 my ($where_sql, $where_binds, $marked) = $self->_where_sql($pk);
604 my $sql = $self->_statement(
605 $marked ? undef : join("\0" => 'select', $table, @select, $where_sql),
606 64 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
607 64 'SELECT ' . join(', ' => map { $dialect->quote_identifier($_) } @select) . ' FROM ' . $dialect->quote_identifier($table) . " WHERE $where_sql";
608 }
609 155 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$marked ? : 2 153 155
610  
611 155 my $row = $self->_fetch($t->{fields}, $self->_run($t->{fields}, $sql, $where_binds, undef, !$marked))->[0];
612 154 ▶ my $given = blessed($data) && $data->can('row_data') ? $data->row_data : ref($data) eq 'HASH' ? $data : undef;
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($data) && $data->can('row_data') ? : 24 130 154
ref $data eq 'HASH' ? : 50 80 130
Condition100%3/3
builtin::blessed($data) and $data->can("row_data")
!ll&&!rl&&r
123724
MC/DC100%2/2
builtin::blessed($data) and $data->can("row_data")
builtin::blessed($data) $data->can("row_data")
Truth table100%3/3
A and B
A= builtin::blessed($data)B= $data->can("row_data")
ABresult
0X0
100
111
613 154 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 134 20 154
614 134 my $canon = $lookup->ingest($table, $row);
615 134 ▶ $lookup->carry_inserted($given, $canon) if $given;
Branch: 1 branch
BranchTrueFalseTotal
if $given 65 69 134
616 134 return $canon;
617 }
618  
619 20 $lookup->gone($table, $key, $given);
620 20 return undef;
621 }
622  
623 =over 4
624  
625 =item \@rows = $conn->select($source, $sql, \@binds)
626  
627 =item \%pending = $conn->select($source, $sql, \@binds, async => 1)
628  
629 Runs SQL built elsewhere for a source (a L<DBIx::QuickORM::Role::Source>) and
630 returns every row as fetched.
631  
632 Binds are C<[$name, $value]> pairs. The name (undef for none) is one of the
633 source's C<field_records>, which also decide how fetched columns are read.
634 Nothing is ingested here. The source's C<results> ingests the rows that
635 belong to a table.
636  
637 With C<async>, on a dialect that C<supports_async>, the query is started
638 without waiting for it and a hash of three code references is returned:
639 C<ready> (true once the result has arrived), C<collect> (waits for the result
640 and returns the rows), and C<cancel> (gives up on the query). Call C<collect>
641 or C<cancel> once. A dialect without async queries croaks.
642  
643 While the query is pending the Connection is busy: every query, CRUD operation,
644 and transaction statement that would send SQL croaks (a C<find> answered from
645 the cache does not), including an explicit C<rollback>.
646  
647 A rollback forced by a block's exception, or by a L<DBIx::QuickORM::Txn>
648 going out of scope, cancels the query first. After that C<collect> throws and
649 C<cancel> does nothing.
650  
651 =back
652  
653 =cut
654  
655 258 my %SELECT_ARGS = (async => 1);
656  
657 236 ▶sub select {
Subroutine select: called CC=7 SCAR=32.5
Pod documented
658 236 my $self = shift;
659 236 my ($source, $sql, $binds, %params) = @_;
660  
661 236 $self->_check_params(select => \%SELECT_ARGS, \%params);
662 235 ▶ confess "select() needs a source (a DBIx::QuickORM::Role::Source)" unless blessed($source) && $source->DOES('DBIx::QuickORM::Role::Source');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->DOES("DBIx::QuickORM::Role::Source") 1 234 235
Condition66%2/3
builtin::blessed($source) and $source->DOES("DBIx::QuickORM::Role::Source")
!ll&&!rl&&r
10234
MC/DC50%1/2
builtin::blessed($source) and $source->DOES("DBIx::QuickORM::Role::Source")
builtin::blessed($source) $source->DOES("DBIx::QuickORM::Role::Source")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->DOES("DBIx::QuickORM::Role::Source")
ABresult
0X0
100
111
663 234 ▶ confess "'binds' must be an arrayref" if defined($binds) && ref($binds) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $binds and ref $binds ne "ARRAY" 1 233 234
Condition66%2/3
defined $binds and ref $binds ne "ARRAY"
!ll&&!rl&&r
02331
MC/DC50%1/2
defined $binds and ref $binds ne "ARRAY"
defined $binds ref $binds ne "ARRAY"
Truth table66%2/3
A and B
A= defined $bindsB= ref $binds ne "ARRAY"
ABresult
0X0
100
111
664  
665 233 my $fields = $source->field_records;
666 233 $binds = $self->_resolve_binds($source, $binds);
667 233 ▶ return $self->_fetch($fields, $self->_run($fields, $sql, $binds)) unless $params{async};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"async"} 233 0 233
668  
669 0 my ($dbh, $dialect) = @{$self}{+DBH, +DIALECT};
670 0 ▶ confess "The " . $dialect->name . " dialect has no async queries" unless $dialect->supports_async;
Branch: 1 branch
BranchTrueFalseTotal
unless $dialect->supports_async 0 0 0
671 0 my $sth = $self->_run($fields, $sql, $binds, $dialect->async_prepare_attrs);
672 0 weaken($PENDING{refaddr($self)} = $self);
673  
674 # The statement is freed as soon as the query is over, before another can start beside it.
675 0 ▶ my $done = sub { undef $sth; delete $self->{+BUSY}; delete $PENDING{refaddr($self)} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
676 my $pending = {
677 0 ▶ ready => sub { $sth ? $dialect->async_ready($dbh, $sth) : 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$sth ? : 0 0 0
678 0 ▶ collect => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
679 0 ▶ die "This async query was cancelled when the transaction it ran in was rolled back\n" unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
680 0 my $rows;
681 0 my $ok = eval { $dialect->async_result($dbh, $sth); $rows = $self->_fetch($fields, $sth); 1 };
682 0 my $err = $@;
683 0 $done->();
684 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
685 0 return $rows;
686 },
687 0 ▶ cancel => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
688 0 ▶ return unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
689 0 my $ok = eval { $dialect->cancel_async($dbh, $sth); 1 };
690 0 my $err = $@;
691 0 $done->();
692 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
693 },
694 0 };
695 0 $self->{+BUSY} = {sth => $sth, cancel => $pending->{cancel}};
696 0 return $pending;
697 }
698  
699 =over 4
700  
701 =item $count = $conn->bulk_write($source, $sql, \@binds)
702  
703 =item $count = $conn->bulk_write($source, $sql, \@binds, mark_deleted => 1, keys => [$select_sql, \@select_binds])
704  
705 Runs an C<UPDATE> or C<DELETE> built elsewhere for a table source and returns
706 the number of rows the database reports. Binds are as for C<select>.
707  
708 It cannot tell which rows changed, so it clears the table from the lookup
709 table's top level and, inside a transaction, from its outside level. A source
710 without a table croaks.
711  
712 With C<mark_deleted>, C<$sql> must be a C<DELETE>. The deleted rows' primary
713 keys are also learned, and each is passed to the lookup table's C<gone>
714 before the table is cleared: held rows for those keys are marked deleted (a
715 rollback unmarks them).
716  
717 The count is still the rows the database reports deleted. With C<RETURNING>
718 that equals the keys returned. On MySQL it can exceed them (see below).
719  
720 =over 4
721  
722 =item *
723  
724 Where the dialect C<supports_returning_delete>, C<$sql> gets C<RETURNING>
725 and the key columns.
726  
727 =item *
728  
729 Otherwise (MySQL) C<keys> is required: a C<SELECT> of the key columns with
730 the same C<where>. It runs with C<FOR UPDATE>, locking those rows, then
731 C<$sql> runs. Both run in a transaction of their own when none is open.
732 Nothing is marked unless the C<DELETE> succeeds. At C<READ COMMITTED> a row
733 another session commits between the two is deleted and counted, but not
734 marked.
735  
736 =back
737  
738 =back
739  
740 =cut
741  
742 258 my %BULK_ARGS = map { $_ => 1 } qw/mark_deleted keys/;
743  
744 15 ▶sub bulk_write {
Subroutine bulk_write: called CC=11 SCAR=29.8
Pod documented
745 15 my $self = shift;
746 15 my ($source, $sql, $binds, %params) = @_;
747  
748 15 $self->_check_params(bulk_write => \%BULK_ARGS, \%params);
749 15 ▶ confess "bulk_write() needs a source (a DBIx::QuickORM::Role::Source)" unless blessed($source) && $source->DOES('DBIx::QuickORM::Role::Source');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->DOES("DBIx::QuickORM::Role::Source") 0 15 15
Condition33%1/3
builtin::blessed($source) and $source->DOES("DBIx::QuickORM::Role::Source")
!ll&&!rl&&r
0015
MC/DC0%0/2
builtin::blessed($source) and $source->DOES("DBIx::QuickORM::Role::Source")
builtin::blessed($source) $source->DOES("DBIx::QuickORM::Role::Source")
Truth table33%1/3
A and B
A= builtin::blessed($source)B= $source->DOES("DBIx::QuickORM::Role::Source")
ABresult
0X0
100
111
750 15 my $table = $source->table;
751 15 ▶ confess "bulk_write() needs a source with a table" unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 15 15
752 15 ▶ confess "'binds' must be an arrayref" if defined($binds) && ref($binds) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $binds and ref $binds ne "ARRAY" 0 15 15
Condition33%1/3
defined $binds and ref $binds ne "ARRAY"
!ll&&!rl&&r
0150
MC/DC0%0/2
defined $binds and ref $binds ne "ARRAY"
defined $binds ref $binds ne "ARRAY"
Truth table33%1/3
A and B
A= defined $bindsB= ref $binds ne "ARRAY"
ABresult
0X0
100
111
753  
754 15 my $fields = $source->field_records;
755 15 $binds = $self->_resolve_binds($source, $binds);
756 15 my $lookup = $self->lookup;
757  
758 15 ▶ unless ($params{mark_deleted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"mark_deleted"}) 9 6 15
759 9 my $sth = $self->_run($fields, $sql, $binds);
760 9 $lookup->forget($table);
761 9 return $sth->rows;
762 }
763  
764 6 my $dialect = $self->{+DIALECT};
765 6 ▶ if ($dialect->supports_returning_delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dialect->supports_returning_delete) 6 0 6
766 6 my $returning = join ', ' => map { $dialect->quote_identifier($_) } @{$self->_table($table)->{pk}};
767 6 my $keys = $self->_fetch($fields, $self->_run($fields, "$sql RETURNING $returning", $binds));
768 6 $lookup->gone($table, $_) for @$keys;
769 6 $lookup->forget($table);
770 6 return scalar @$keys;
771 }
772  
773 0 my $select = $params{keys};
774 0 ▶ confess "bulk_write() needs 'keys' to mark deleted rows: the " . $dialect->name . " dialect has no RETURNING on DELETE" unless ref($select) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $select eq "ARRAY" 0 0 0
775 0 ▶ my $run = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
776 0 my $keys = $self->_fetch($fields, $self->_run($fields, "$select->[0] FOR UPDATE", $self->_resolve_binds($source, $select->[1])));
777 0 my $sth = $self->_run($fields, $sql, $binds);
778 0 $lookup->gone($table, $_) for @$keys;
779 0 $lookup->forget($table);
780 0 return $sth->rows;
781 0 };
782 0 ▶ return @{$self->{+TXNS}} ? $run->() : $self->txn($run);
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'txns'};} ? : 0 0 0
783 }
784  
785 =pod
786  
787 =over 4
788  
789 =item $txn = $conn->txn(caller => \@frame)
790  
791 =item $txn = $conn->txn(isolation => $level, caller => \@frame)
792  
793 =item @result = $conn->txn(sub { my $txn = shift; ... }, retry => $n, isolation => $level, caller => \@frame)
794  
795 Opens a transaction, or a savepoint when one is already open, and returns its
796 L<DBIx::QuickORM::Txn>. Given a block, runs it in the transaction and returns
797 what the block returns.
798  
799 The transaction statements are sent as SQL, never through DBI's
800 C<begin_work>. Nesting croaks on a dialect without savepoints (DuckDB).
801 C<caller> is where it was opened, for warnings and errors. It is the caller of
802 this method unless given.
803  
804 Opening one begins a lookup table level (see L<DBIx::QuickORM::LookupTable>).
805  
806 The block is called in list context when C<txn> is, otherwise in scalar
807 context. A normal return commits,
808 unless the block already committed or rolled back itself. An exception rolls
809 back and is rethrown.
810  
811 With C<retry>, the block of a root transaction runs again in a new
812 transaction, up to C<$n> more times, while the exception is a
813 L<DBIx::QuickORM::Exception::DB> whose C<retriable> is true. A
814 C<retry> above 0 croaks inside another transaction or without a block. One
815 that is not a count croaks.
816  
817 C<isolation> is the root transaction's level: C<read uncommitted>, C<read
818 committed>, C<repeatable read>, or C<serializable>, in any case, or
819 C<'server'> for the database's own default. Without it the Connection's
820 C<isolation> applies. Every retry begins at the same level. It croaks inside
821 another transaction and for any other value. A level on a database without
822 settable levels (SQLite, DuckDB) croaks when the transaction opens.
823  
824 =back
825  
826 =cut
827  
828 258 my %TXN_ARGS = map { $_ => 1 } qw/retry isolation caller/;
829 258 my @ISOLATION = ('read committed', 'read uncommitted', 'repeatable read', 'serializable', 'server');
830 258 my %ISOLATION = map { $_ => 1 } @ISOLATION;
831  
832 142 ▶sub txn {
Subroutine txn: called CC=16 SCAR=27.7
Pod documented
833 142 my $self = shift;
834 142 ▶ my $code = ref($_[0]) eq 'CODE' ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 86 56 142
835 142 my %params = @_;
836  
837 142 $self->_check_params(txn => \%TXN_ARGS, \%params);
838 141 ▶ my $caller = $params{caller} // [caller];
Condition100%2/2
$params{"caller"} // [caller]
l!l
1356
MC/DC100%1/1
$params{"caller"} // [caller]
$params{"caller"}
Truth table100%2/2
A
A= $params{"caller"}
Aresult
11
00
839 141 ▶ my $retry = $params{retry} // 0;
Condition100%2/2
$params{"retry"} // 0
l!l
10131
MC/DC100%1/1
$params{"retry"} // 0
$params{"retry"}
Truth table100%2/2
A
A= $params{"retry"}
Aresult
11
00
840 141 ▶ confess "'retry' must be a count of retries" unless $retry =~ m/^\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $retry =~ /^\d+\z/ 1 140 141
841 140 ▶ confess "'retry' needs a block: txn(sub { ... }, retry => \$n)" if $retry && !$code;
Branch: 1 branch
BranchTrueFalseTotal
if $retry and not $code 1 139 140
Condition100%3/3
$retry and not $code
!ll&&!rl&&r
13181
MC/DC100%2/2
$retry and not $code
$retry not $code
Truth table100%3/3
A and not(B)
A= $retryB= not $code
ABresult
0X0
100
111
842 139 ▶ confess "'retry' is only for a root transaction, and a transaction is already open" if $retry && @{$self->{+TXNS}};
Branch: 1 branch
BranchTrueFalseTotal
if $retry and @{$self->{"txns"};} 1 138 139
Condition100%3/3
$retry and @{$self->{"txns"};}
!ll&&!rl&&r
13171
MC/DC100%2/2
$retry and @{$self->{"txns"};}
$retry @{$self->{"txns"};}
Truth table100%3/3
A and B
A= $retryB= @{$self->{"txns"};}
ABresult
0X0
100
111
843  
844 138 my $level = $params{isolation};
845 138 ▶ if (defined $level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level) 6 132 138
846 6 $level = $self->_isolation($level);
847 5 ▶ confess "'isolation' is only for a root transaction, and a transaction is already open" if @{$self->{+TXNS}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"txns"};} 1 4 5
848 }
849 136 ▶ return $self->_begin($caller, $level) unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 53 83 136
850  
851 83 my $list = wantarray;
852 83 for (my $tries = 0;; $tries++) {
853 89 my ($ok, $err, @out) = $self->_run_block($code, $caller, $list, $level);
854 89 ▶ return $list ? @out : $out[0] if $ok;
Branch: 2 branches
BranchTrueFalseTotal
if $ok 63 26 89
$list ? : 2 61 63
855 26 ▶ die $err unless $tries < $retry && $self->_retriable($err);
Branch: 1 branch
BranchTrueFalseTotal
unless $tries < $retry and $self->_retriable($err) 20 6 26
Condition100%3/3
$tries < $retry and $self->_retriable($err)
!ll&&!rl&&r
1736
MC/DC100%2/2
$tries < $retry and $self->_retriable($err)
$tries < $retry $self->_retriable($err)
Truth table100%3/3
A and B
A= $tries < $retryB= $self->_retriable($err)
ABresult
0X0
100
111
856 }
857 }
858  
859 =pod
860  
861 =over 4
862  
863 =item $conn->commit_txn($txn)
864  
865 =item $conn->rollback_txn($txn)
866  
867 What C<< $txn->commit >> and C<< $txn->rollback >> do (see
868 L<DBIx::QuickORM::Txn>).
869  
870 Either croaks unless the transaction is open and innermost, this is the
871 process that opened it, and no async query is pending.
872  
873 Committing a transaction marked failed rolls it back instead, then croaks
874 (see C<_run>).
875  
876 Ending a transaction ends its lookup table level, as
877 L<DBIx::QuickORM::LookupTable> describes. A failed commit or rollback is
878 handled as L<DBIx::QuickORM::Txn> describes.
879  
880 =back
881  
882 =cut
883  
884 68 ▶sub commit_txn {
Subroutine commit_txn: called CC=5 SCAR=16.1
Pod documented
885 68 my $self = shift;
886 68 my ($txn) = @_;
887  
888 68 $self->_check_open($txn, 'commit');
889 64 $self->_check_idle; # Before the commit, whose failure would roll back.
890 64 ▶ if (defined(my $failed = $txn->failed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $failed = $txn->failed)) 2 62 64
891 2 $self->_unwind($txn);
892 2 confess "The transaction opened at " . $txn->opened_at . " was rolled back instead of committed, since a statement in it failed.\nThe failure: $failed\n";
893 }
894 62 ▶ my $sql = $txn->depth == 1 ? 'COMMIT' : 'RELEASE SAVEPOINT ' . $txn->name;
Branch: 1 branch
BranchTrueFalseTotal
$txn->depth == 1 ? : 49 13 62
895 62 ▶ unless (eval { $self->_do($sql); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_do($sql); 1 } }) 3 59 62
896 3 my $err = $@;
897 3 ▶ die $err if eval { $self->_unwind($txn); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->_unwind($txn); 1 } } 1 2 3
898 2 my $rollback_err = $@;
899 2 $self->_abandon($txn);
900 2 chomp($err, $rollback_err);
901 2 die "The outcome of the transaction opened at " . $txn->opened_at . " is unknown: committing it failed, and so did rolling it back.\nCommit error: $err\nRollback error: $rollback_err\n";
902 }
903  
904 59 pop @{$self->{+TXNS}};
905 59 $self->lookup->commit_level;
906 59 return 1;
907 }
908  
909 48 ▶sub rollback_txn {
Subroutine rollback_txn: called CC=1 SCAR=0.0
Pod documented
910 48 my $self = shift;
911 48 my ($txn) = @_;
912 48 $self->_check_open($txn, 'roll back');
913 46 $self->_check_idle;
914 46 $self->_unwind($txn);
915 45 return 1;
916 }
917  
918 =pod
919  
920 =over 4
921  
922 =item \%values = $conn->deflate($table, \%values, raw => $bool)
923  
924 The values in a new hashref (under C<raw> with no marker, the one given),
925 deflated as L<DBIx::QuickORM/"CRUD operations"> describes. The CRUD operations
926 use it on every value they send. Row objects use it for the changes they send
927 raw.
928  
929 Value markers (L<DBIx::QuickORM::Value>) are handled even under C<raw>: a
930 C<raw> marker is unwrapped, an C<inflated> one is deflated through the type
931 whatever C<is_inflated> says, and C<binary> and C<sql> markers are kept for
932 the SQL. With C<raw> nothing else changes, and values holding no marker are
933 returned as they are.
934  
935 Croaks for a table not in the map, or a field the table does not have, before
936 deflating anything.
937  
938 =back
939  
940 =cut
941  
942 1180 ▶sub deflate {
Subroutine deflate: called CC=3 SCAR=11.0
Pod documented
943 1180 my $self = shift;
944 1180 my ($table, $values, %params) = @_;
945 1180 my $fields = $self->_table($table)->{fields};
946 1179 $self->_check_fields($table, sort keys %$values);
947  
948 1171 my $raw = $params{raw};
949 1171 ▶ return $values if $raw && !grep { $self->_is_marker($_) } values %$values;
Branch: 1 branch
BranchTrueFalseTotal
if $raw and not grep({$self->_is_marker($_);} values %$values) 145 1026 1171
Condition100%3/3
$raw and not grep({$self->_is_marker($_);} values %$values)
!ll&&!rl&&r
10251145
MC/DC100%2/2
$raw and not grep({$self->_is_marker($_);} values %$values)
$raw not grep({$self->_is_marker($_);} values %$values)
Truth table100%3/3
A and not(B)
A= $rawB= not grep({$self->_is_marker($_);} values %$values)
ABresult
0X0
100
111
950  
951 1026 return {map { $_ => $self->_deflate_value($table, $_, $fields->{$_}, $values->{$_}, $raw) } keys %$values};
952 }
953  
954 =over 4
955  
956 =item $conn->release_inherited
957  
958 In a forked child, lets go of the handle inherited from the parent without
959 touching the parent's session.
960  
961 An async query the parent left pending is cleared too, so nothing the child
962 does later, or at exit, waits on it. It does nothing in the process that
963 opened the Connection, and calling it again is harmless.
964  
965 =back
966  
967 =cut
968  
969 7 ▶sub release_inherited {
Subroutine release_inherited: called CC=4 SCAR=22.5
Pod documented
970 7 my $self = shift;
971 7 ▶ return if $self->{+PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} == $$ 0 7 7
972 7 $self->{+DIALECT}->release_inherited($self->{+DBH});
973  
974 7 ▶ my $busy = delete $self->{+BUSY} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $busy = delete $self->{"busy"} or return 0 7 7
975 0 my $sth = $busy->{sth};
976 0 delete $PENDING{refaddr($self)};
977 0 ▶ @{$sth}{qw/InactiveDestroy RaiseError PrintError PrintWarn HandleError/} = (1, 0, 0, 0, sub { 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
978 0 local $@;
979 0 ▶ eval { $sth->finish; 1 } or warn $@; # Against the released socket: it reads nothing of the parent's.
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth->finish; 1 } } or warn $@ 0 0 0
980 0 return;
981 }
982  
983 =head1 PRIVATE METHODS
984  
985 =over 4
986  
987 =item $code = $conn->_error_handler($dialect, \@txns)
988  
989 The C<HandleError> callback: dies with a L<DBIx::QuickORM::Exception::DB>
990 built from the failing handle. First it marks the innermost open transaction
991 failed, with the error (see C<_run>). It holds the dialect and the
992 transaction stack, not the Connection.
993  
994 =back
995  
996 =cut
997  
998 305 ▶sub _error_handler {
Subroutine _error_handler: called CC=1 SCAR=0.3
999 305 my $self = shift;
1000 305 my ($dialect, $txns) = @_;
1001  
1002 30 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=20.5
1003 30 my ($msg, $h) = @_;
1004 30 my ($native, $state, $errstr) = ($h->err, $h->state, $h->errstr);
1005 30 ▶ $state = undef unless defined($state) && length($state);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and length $state 0 30 30
Condition33%1/3
defined $state and length $state
!ll&&!rl&&r
0030
MC/DC0%0/2
defined $state and length $state
defined $state length $state
Truth table33%1/3
A and B
A= defined $stateB= length $state
ABresult
0X0
100
111
1006  
1007 30 my $txn = $txns->[-1];
1008 30 ▶ $txn->{failed} //= $errstr // $msg if $txn;
Branch: 1 branch
BranchTrueFalseTotal
if $txn 12 18 30
Condition33%2/6
$txn->{"failed"} //= $errstr // $msg
l!l&&r!l&&!r
0120
$errstr // $msg
l!l&&r!l&&!r
1200
MC/DC0%0/3
$txn->{"failed"} //= $errstr // $msg
$txn->{"failed"} $errstr $msg
Truth table0%0/4
A //= B // C
A= $txn->{"failed"}B= $errstrC= $msg
ABCresult
1XX1
01X1
0011
0000
1009  
1010 die DBIx::QuickORM::Exception::DB->new(
1011 kind => $dialect->error_kind($native, $state, $errstr),
1012 state => $state,
1013 native => $native,
1014 errstr => $errstr,
1015 statement => $h->{Statement},
1016 30 message => $msg,
1017 );
1018 305 };
1019 }
1020  
1021 =pod
1022  
1023 =over 4
1024  
1025 =item \%generated = $conn->_generate_keys(\%table, \%values)
1026  
1027 New values for the table's primary key fields missing from C<%values>, from
1028 each field's type's C<generate_key_value>. A field's C<generate> hint
1029 decides: C<none> never, C<prefer_orm> always, and C<prefer_db> (the default)
1030 unless the column has C<db_fills> and the dialect can return the inserted
1031 key (C<supports_returning_insert>). Fields without a type, or whose
1032 type cannot generate, are left out. The values are inflated. The caller
1033 deflates them, even for C<raw> inserts.
1034  
1035 =back
1036  
1037 =cut
1038  
1039 724 ▶sub _generate_keys {
Subroutine _generate_keys: called CC=11 SCAR=24.0
1040 724 my $self = shift;
1041 724 my ($t, $values) = @_;
1042  
1043 724 my %generated;
1044 724 ▶ for my $name (@{$t->{pk} // []}) {
Condition100%2/2
$t->{"pk"} // []
l!l
7159
MC/DC100%1/1
$t->{"pk"} // []
$t->{"pk"}
Truth table100%2/2
A
A= $t->{"pk"}
Aresult
11
00
1045 736 ▶ next if exists $values->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $values->{$name} 680 56 736
1046 56 my $field = $t->{fields}->{$name};
1047 56 my $type = $field->{orm_type};
1048 56 ▶ next unless $type && $type->can('generate_key_value');
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $type->can("generate_key_value") 44 12 56
Condition66%2/3
$type and $type->can("generate_key_value")
!ll&&!rl&&r
44012
MC/DC50%1/2
$type and $type->can("generate_key_value")
$type $type->can("generate_key_value")
Truth table66%2/3
A and B
A= $typeB= $type->can("generate_key_value")
ABresult
0X0
100
111
1049  
1050 12 ▶ my $mode = $field->{generate} // 'prefer_db';
Condition100%2/2
$field->{"generate"} // "prefer_db"
l!l
39
MC/DC100%1/1
$field->{"generate"} // "prefer_db"
$field->{"generate"}
Truth table100%2/2
A
A= $field->{"generate"}
Aresult
11
00
1051 12 ▶ next if $mode eq 'none' || ($mode eq 'prefer_db' && $field->{db_fills} && $self->{+DIALECT}->supports_returning_insert);
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "none" or $mode eq "prefer_db" and $field->{"db_fills"} and $self->{"dialect"}->supports_returning_insert 3 9 12
Condition88%8/9
$mode eq "none" or $mode eq "prefer_db" and $field->{"db_fills"} and $self->{"dialect"}->supports_returning_insert
l!l&&r!l&&!r
219
$mode eq "prefer_db" and $field->{"db_fills"} and $self->{"dialect"}->supports_returning_insert
!ll&&!rl&&r
901
$mode eq "prefer_db" and $field->{"db_fills"}
!ll&&!rl&&r
181
MC/DC75%3/4
$mode eq "none" or $mode eq "prefer_db" and $field->{"db_fills"} and $self->{"dialect"}->supports_returning_insert
$mode eq "none" $mode eq "prefer_db" $field->{"db_fills"} $self->{"dialect"}->supports_returning_insert
Truth table80%4/5
A or B and C and D
A= $mode eq "none"B= $mode eq "prefer_db"C= $field->{"db_fills"}D= $self->{"dialect"}->supports_returning_insert
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1052 9 $generated{$name} = $type->generate_key_value($field, $self);
1053 }
1054  
1055 724 return \%generated;
1056 }
1057  
1058 =pod
1059  
1060 =over 4
1061  
1062 =item \%data = $conn->_row_data($row_or_data)
1063  
1064 The row data: a hashref as given, or a row object's C<row_data>. Croaks on
1065 anything else.
1066  
1067 =back
1068  
1069 =cut
1070  
1071 1004 ▶sub _row_data {
Subroutine _row_data: called CC=5 SCAR=16.2
1072 1004 my $self = shift;
1073 1004 my ($data) = @_;
1074 1004 ▶ $data = $data->row_data if blessed($data) && $data->can('row_data');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) and $data->can("row_data") 58 946 1004
Condition66%2/3
builtin::blessed($data) and $data->can("row_data")
!ll&&!rl&&r
946058
MC/DC50%1/2
builtin::blessed($data) and $data->can("row_data")
builtin::blessed($data) $data->can("row_data")
Truth table66%2/3
A and B
A= builtin::blessed($data)B= $data->can("row_data")
ABresult
0X0
100
111
1075 1004 ▶ confess "Expected row data (a hashref) or a row object, got " . ($data // 'undef') unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 1002 1004
Condition50%1/2
$data // "undef"
l!l
20
MC/DC0%0/1
$data // "undef"
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
1076 1002 return $data;
1077 }
1078  
1079 =pod
1080  
1081 =over 4
1082  
1083 =item $value = $conn->_deflate_value($table, $name, \%field, $value, $raw)
1084  
1085 One value deflated as C<deflate> describes. C<\%field> is undef for a name
1086 that is not a field.
1087  
1088 A value the type deflated to plain text then goes through the field's
1089 C<format>, if it has one and is not binary.
1090  
1091 =back
1092  
1093 =cut
1094  
1095 9261 ▶sub _deflate_value {
Subroutine _deflate_value: called CC=21 SCAR=30.5
1096 9261 my $self = shift;
1097 9261 my ($table, $name, $field, $value, $raw) = @_;
1098 9261 ▶ my $type = $field ? $field->{orm_type} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 9239 22 9261
1099  
1100 9261 ▶ if (ref($value) && $self->_is_marker($value)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value and $self->_is_marker($value)) { } 61 9200 9261
elsif ($raw or not defined $value) { } 191 9009 9200
elsif ($type and not ref $value) { } 94 17772 17866
Condition100%3/3
ref $value and $self->_is_marker($value)
!ll&&!rl&&r
91396161
MC/DC100%2/2
ref $value and $self->_is_marker($value)
ref $value $self->_is_marker($value)
Truth table100%3/3
A and B
A= ref $valueB= $self->_is_marker($value)
ABresult
0X0
100
111
1101 61 my $kind = $value->kind;
1102 61 ▶ return $value if $kind eq 'binary' || $kind eq 'sql';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "binary" or $kind eq "sql" 24 37 61
Condition100%3/3
$kind eq "binary" or $kind eq "sql"
l!l&&r!l&&!r
91537
MC/DC100%2/2
$kind eq "binary" or $kind eq "sql"
$kind eq "binary" $kind eq "sql"
Truth table100%3/3
A or B
A= $kind eq "binary"B= $kind eq "sql"
ABresult
1X1
011
000
1103 37 $value = $value->value;
1104 37 ▶ return $value if $kind eq 'raw' || !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "raw" or not defined $value 22 15 37
Condition66%2/3
$kind eq "raw" or not defined $value
l!l&&r!l&&!r
22015
MC/DC50%1/2
$kind eq "raw" or not defined $value
$kind eq "raw" not defined $value
Truth table66%2/3
A or not(B)
A= $kind eq "raw"B= not defined $value
ABresult
1X1
011
000
1105 }
1106 ▶ elsif ($raw || !defined($value)) {
Condition100%3/3
$raw or not defined $value
l!l&&r!l&&!r
176159009
MC/DC100%2/2
$raw or not defined $value
$raw not defined $value
Truth table100%3/3
A or not(B)
A= $rawB= not defined $value
ABresult
1X1
011
000
1107 191 return $value;
1108 }
1109 ▶ elsif ($type && !ref($value)) {
Condition100%3/3
$type and not ref $value
!ll&&!rl&&r
88575894
MC/DC100%2/2
$type and not ref $value
$type not ref $value
Truth table100%3/3
A and not(B)
A= $typeB= not ref $value
ABresult
0X0
100
111
1110 94 ▶ return $value unless $type->can('is_inflated') && $type->is_inflated($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $type->can("is_inflated") and $type->is_inflated($value) 40 54 94
Condition66%2/3
$type->can("is_inflated") and $type->is_inflated($value)
!ll&&!rl&&r
04054
MC/DC50%1/2
$type->can("is_inflated") and $type->is_inflated($value)
$type->can("is_inflated") $type->is_inflated($value)
Truth table66%2/3
A and B
A= $type->can("is_inflated")B= $type->is_inflated($value)
ABresult
0X0
100
111
1111 }
1112  
1113 8984 ▶ unless ($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 8860 124 8984
1114 8860 ▶ confess "The field '$name' of '$table' has no type to deflate a reference with; deflate it yourself and pass raw => 1" if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 8856 8860
1115 8856 return $value;
1116 }
1117  
1118 124 my $out = $type->deflate($value, $field, $self);
1119 124 my $format = $field->{format};
1120 124 ▶ return $out unless $format && defined($out) && !ref($out) && ($field->{affinity} // '') ne 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless $format and defined $out and not ref $out and ($field->{"affinity"} // "") ne "binary" 117 7 124
Condition72%8/11
$format and defined $out and not ref $out and ($field->{"affinity"} // "") ne "binary"
!ll&&!rl&&r
11617
$format and defined $out and not ref $out
!ll&&!rl&&r
11608
$format and defined $out
!ll&&!rl&&r
11608
$field->{"affinity"} // ""
l!l
80
MC/DC25%1/4
$format and defined $out and not ref $out and ($field->{"affinity"} // "") ne "binary"
$format defined $out not ref $out $field->{"affinity"}
Truth table40%2/5
A and B and not(C) and D
A= $formatB= defined $outC= not ref $outD= $field->{"affinity"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1121 7 return $format->($out);
1122 }
1123  
1124 =pod
1125  
1126 =over 4
1127  
1128 =item $bool = $conn->_is_marker($value)
1129  
1130 =item $bool = $conn->_is_sql($value)
1131  
1132 True for a value marker, or an C<sql> marker.
1133  
1134 =back
1135  
1136 =cut
1137  
1138 1710 ▶sub _is_marker { blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') ? 1 : 0 }
Subroutine _is_marker: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') ? : 120 1590 1710
Condition100%3/3
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value")
!ll&&!rl&&r
156426120
MC/DC100%2/2
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value")
builtin::blessed($_[1]) $_[1]->isa("DBIx::QuickORM::Value")
Truth table100%3/3
A and B
A= builtin::blessed($_[1])B= $_[1]->isa("DBIx::QuickORM::Value")
ABresult
0X0
100
111
1139 1054 ▶sub _is_sql { $_[0]->_is_marker($_[1]) && $_[1]->kind eq 'sql' ? 1 : 0 }
Subroutine _is_sql: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_is_marker($_[1]) && $_[1]->kind eq 'sql' ? : 16 1038 1054
Condition100%3/3
$_[0]->_is_marker($_[1]) and $_[1]->kind eq "sql"
!ll&&!rl&&r
10271116
MC/DC100%2/2
$_[0]->_is_marker($_[1]) and $_[1]->kind eq "sql"
$_[0]->_is_marker($_[1]) $_[1]->kind eq "sql"
Truth table100%3/3
A and B
A= $_[0]->_is_marker($_[1])B= $_[1]->kind eq "sql"
ABresult
0X0
100
111
1140  
1141 =pod
1142  
1143 =over 4
1144  
1145 =item \%values = $conn->_plain(\%values)
1146  
1147 A copy with each C<binary> marker replaced by its value, for lookup keys and
1148 row data.
1149  
1150 =back
1151  
1152 =cut
1153  
1154 349 ▶sub _plain {
Subroutine _plain: called CC=2 SCAR=6.9
1155 349 my $self = shift;
1156 349 my ($values) = @_;
1157 349 ▶ return {map { my $v = $values->{$_}; ($_ => $self->_is_marker($v) ? $v->value : $v) } keys %$values};
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_marker($v) ? : 4 360 364
1158 }
1159  
1160 =pod
1161  
1162 =over 4
1163  
1164 =item (\@places, \@binds, $marked) = $conn->_value_sql(\%values, @names)
1165  
1166 The SQL for each named value, C<?> or an C<sql> marker's SQL, and the binds,
1167 in order. C<$marked> is true when any value was a marker: its SQL or bind
1168 attributes make the statement one of a kind, never cached.
1169  
1170 =back
1171  
1172 =cut
1173  
1174 799 ▶sub _value_sql {
Subroutine _value_sql: called CC=6 SCAR=17.9
1175 799 my $self = shift;
1176 799 my ($values, @names) = @_;
1177  
1178 799 my (@places, @binds, $marked);
1179 799 for my $name (@names) {
1180 8716 my $value = $values->{$name};
1181 8716 ▶ $marked = 1 if ref($value) && $self->_is_marker($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $self->_is_marker($value) 14 8702 8716
Condition66%2/3
ref $value and $self->_is_marker($value)
!ll&&!rl&&r
8702014
MC/DC50%1/2
ref $value and $self->_is_marker($value)
ref $value $self->_is_marker($value)
Truth table66%2/3
A and B
A= ref $valueB= $self->_is_marker($value)
ABresult
0X0
100
111
1182 8716 ▶ if (ref($value) && $self->_is_sql($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $self->_is_sql($value)) 10 8706 8716
Condition100%3/3
ref $value and $self->_is_sql($value)
!ll&&!rl&&r
8702410
MC/DC100%2/2
ref $value and $self->_is_sql($value)
ref $value $self->_is_sql($value)
Truth table100%3/3
A and B
A= ref $valueB= $self->_is_sql($value)
ABresult
0X0
100
111
1183 10 push @places => $value->value;
1184 10 push @binds => map { [undef, $_] } @{$value->binds};
1185 10 next;
1186 }
1187 8706 push @places => '?';
1188 8706 push @binds => [$name => $value];
1189 }
1190  
1191 799 return (\@places, \@binds, $marked);
1192 }
1193  
1194 =pod
1195  
1196 =over 4
1197  
1198 =item @batches = $conn->_batches(\@values, \@names)
1199  
1200 The rows of a multi-row insert, as C<[\@places, \@binds]> pairs: each place is
1201 one row's C<(...)>, and each pair holds as many rows as fit in the dialect's
1202 C<bind_limit>, at least one.
1203  
1204 =back
1205  
1206 =cut
1207  
1208 30 ▶sub _batches {
Subroutine _batches: called CC=5 SCAR=16.1
1209 30 my $self = shift;
1210 30 my ($values, $names) = @_;
1211  
1212 30 my $limit = $self->{+DIALECT}->bind_limit;
1213 30 my $plain = '(' . join(', ' => ('?') x @$names) . ')';
1214 30 my @batches = ([[], []]);
1215 30 for my $row (@$values) {
1216 444 my ($places, $binds, $marked) = $self->_value_sql($row, @$names);
1217 444 ▶ push @batches => [[], []] if @{$batches[-1]->[0]} && @{$batches[-1]->[1]} + @$binds > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @{$batches[-1][0];} and @{$batches[-1][1];} + @$binds > $limit 8 436 444
Condition100%3/3
@{$batches[-1][0];} and @{$batches[-1][1];} + @$binds > $limit
!ll&&!rl&&r
304068
MC/DC100%2/2
@{$batches[-1][0];} and @{$batches[-1][1];} + @$binds > $limit
@{$batches[-1][0];} @{$batches[-1][1];} + @$binds > $limit
Truth table100%3/3
A and B
A= @{$batches[-1][0];}B= @{$batches[-1][1];} + @$binds > $limit
ABresult
0X0
100
111
1218 444 ▶ push @{$batches[-1]->[0]} => $marked ? '(' . join(', ' => @$places) . ')' : $plain;
Branch: 1 branch
BranchTrueFalseTotal
$marked ? : 1 443 444
1219 444 push @{$batches[-1]->[1]} => @$binds;
1220 }
1221  
1222 30 return @batches;
1223 }
1224  
1225 =pod
1226  
1227 =over 4
1228  
1229 =item \@binds = $conn->_resolve_binds($source, \@binds)
1230  
1231 Binds built elsewhere with their C<raw> and C<inflated> markers resolved, as
1232 C<deflate> with C<raw> resolves them, by the source's field each bind names.
1233  
1234 =back
1235  
1236 =cut
1237  
1238 248 ▶sub _resolve_binds {
Subroutine _resolve_binds: called CC=5 SCAR=16.1
1239 248 my $self = shift;
1240 248 my ($source, $binds) = @_;
1241  
1242 248 ▶ my $table = $source->table // '';
Condition100%2/2
$source->table // ""
l!l
21533
MC/DC100%1/1
$source->table // ""
$source->table
Truth table100%2/2
A
A= $source->table
Aresult
11
00
1243 248 my $fields = $source->field_records;
1244 248 ▶ return [map { my ($name, $value) = @$_; [$name, $self->_deflate_value($table, $name // '', defined($name) ? $fields->{$name} : undef, $value, 1)] } @{$binds // []}];
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 164 19 183
Condition75%3/4
$name // ""
l!l
16419
$binds // []
l!l
2480
MC/DC100%1/1
$name // ""
$name
MC/DC0%0/1
$binds // []
$binds
Truth table100%2/2
A
A= $name
Aresult
11
00
Truth table50%1/2
A
A= $binds
Aresult
11
00
1245 }
1246  
1247 =pod
1248  
1249 =over 4
1250  
1251 =item $conn->_check_params($name, \%known, \%params)
1252  
1253 Croaks naming every key of C<%params> not in C<%known>.
1254  
1255 =back
1256  
1257 =cut
1258  
1259 1099 ▶sub _check_params {
Subroutine _check_params: called CC=2 SCAR=6.9
1260 1099 my $self = shift;
1261 1099 my ($name, $known, $params) = @_;
1262 1099 my @unknown = sort grep { !$known->{$_} } keys %$params;
1263 1099 ▶ confess "Unknown argument(s) to $name(): " . join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 9 1090 1099
1264 }
1265  
1266 =pod
1267  
1268 =over 4
1269  
1270 =item \%table = $conn->_table($table)
1271  
1272 The map's record for the table. Croaks when there is none.
1273  
1274 =back
1275  
1276 =cut
1277  
1278 1892 ▶sub _table {
Subroutine _table: called CC=4 SCAR=13.9
1279 1892 my $self = shift;
1280 1892 my ($table) = @_;
1281 1892 ▶ confess "A table name is required" unless defined($table) && length($table);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table and length $table 1 1891 1892
Condition66%2/3
defined $table and length $table
!ll&&!rl&&r
101891
MC/DC50%1/2
defined $table and length $table
defined $table length $table
Truth table66%2/3
A and B
A= defined $tableB= length $table
ABresult
0X0
100
111
1282 1891 ▶ return $self->_map->table($table) // confess "The table '$table' is not in the database map";
Condition100%2/2
$self->_map->table($table) // confess("The table '${table}' is not in the database map")
l!l
18892
MC/DC100%1/1
$self->_map->table($table) // confess("The table '${table}' is not in the database map")
$self->_map->table($table)
Truth table100%2/2
A
A= $self->_map->table($table)
Aresult
11
00
1283 }
1284  
1285 =pod
1286  
1287 =over 4
1288  
1289 =item $conn->_check_fields($table, @fields)
1290  
1291 Croaks for the first field not in the table.
1292  
1293 =back
1294  
1295 =cut
1296  
1297 1329 ▶sub _check_fields {
Subroutine _check_fields: called CC=3 SCAR=11.0
1298 1329 my $self = shift;
1299 1329 my ($table, @fields) = @_;
1300 1329 my $known = $self->_map->table($table)->{fields};
1301 1329 for my $field (@fields) {
1302 1440 ▶ confess "The table '$table' has no field '$field'" unless $known->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $known->{$field} 12 1428 1440
1303 }
1304 }
1305  
1306 =pod
1307  
1308 =over 4
1309  
1310 =item \%pk = $conn->_pk_values($table, $pk)
1311  
1312 =item \%pk = $conn->_pk_values($table, $pk, $raw)
1313  
1314 The key-field hash from a primary key in any form C<normalize_pk> in
1315 L<DBIx::QuickORM::Map> takes, each value put through C<deflate> (with C<raw>
1316 when C<$raw> is true). Every key argument takes this path. A C<binary> marker
1317 is kept for binding. C<_plain> of the hash is the raw key the lookup table
1318 takes. Croaks as C<normalize_pk> does, and on an C<sql> marker.
1319  
1320 =back
1321  
1322 =cut
1323  
1324 390 ▶sub _pk_values {
Subroutine _pk_values: called CC=3 SCAR=11.0
1325 390 my $self = shift;
1326 390 my ($table, $pk, $raw) = @_;
1327  
1328 390 my $map = $self->_map;
1329 390 my $values = $map->normalize_pk($table, $pk);
1330 379 my $fields = $map->pk($table);
1331 379 my $out = $self->deflate($table, {map { $fields->[$_] => $values->[$_] } 0 .. $#$fields}, raw => $raw);
1332 378 for my $field (@$fields) {
1333 390 ▶ confess "sql() cannot be used in a primary key (the field '$field')" if $self->_is_sql($out->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_sql($out->{$field}) 1 389 390
1334 }
1335 377 return $out;
1336 }
1337  
1338 =pod
1339  
1340 =over 4
1341  
1342 =item \@copy = $conn->_check_datetime_parsers(\@parsers)
1343  
1344 A copy of C<@parsers> once each is a code reference, an object with
1345 C<parse_datetime>, or a class name. Loads each class not yet loaded. Croaks
1346 when the shape is wrong, or a class cannot be loaded or has no
1347 C<parse_datetime>.
1348  
1349 =back
1350  
1351 =cut
1352  
1353 19 ▶sub _check_datetime_parsers {
Subroutine _check_datetime_parsers: called CC=10 SCAR=23.0
1354 19 my $self = shift;
1355 19 my ($parsers) = @_;
1356  
1357 croak "'datetime_parsers' must be an arrayref of parsers: classes or objects with parse_datetime, or code references"
1358 19 ▶ unless ref($parsers) eq 'ARRAY' && !grep { ref($_) ne 'CODE' && !(blessed($_) ? $_->can('parse_datetime') : defined($_) && !ref($_) && m/^\w+(?:::\w+)*\z/) } @$parsers;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $parsers eq "ARRAY" and not grep({ref $_ ne "CODE" and not builtin::blessed($_) ? $_->can("parse_datetime") : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/;} @$parsers) 4 15 19
builtin::blessed($_) ? : 1 9 10
Condition91%11/12
ref $parsers eq "ARRAY" and not grep({ref $_ ne "CODE" and not builtin::blessed($_) ? $_->can("parse_datetime") : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/;} @$parsers)
!ll&&!rl&&r
2215
ref $_ ne 'CODE' && !(builtin::blessed($_) ? $_->can('parse_datetime') : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/)
!ll&&!rl&&r
1182
defined $_ and not ref $_ and /^\w+(?:::\w+)*\z/
!ll&&!rl&&r
117
defined $_ and not ref $_
!ll&&!rl&&r
018
MC/DC100%2/2
ref $parsers eq "ARRAY" and not grep({ref $_ ne "CODE" and not builtin::blessed($_) ? $_->can("parse_datetime") : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/;} @$parsers)
ref $parsers eq "ARRAY" not grep({ref $_ ne "CODE" and not builtin::blessed($_) ? $_->can("parse_datetime") : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/;} @$parsers)
MC/DC100%2/2
ref $_ ne 'CODE' && !(builtin::blessed($_) ? $_->can('parse_datetime') : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/)
ref $_ ne 'CODE' !(builtin::blessed($_) ? $_->can('parse_datetime') : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/)
MC/DC66%2/3
defined $_ and not ref $_ and /^\w+(?:::\w+)*\z/
defined $_ not ref $_ /^\w+(?:::\w+)*\z/
Truth table100%3/3
A and not(B)
A= ref $parsers eq "ARRAY"B= not grep({ref $_ ne "CODE" and not builtin::blessed($_) ? $_->can("parse_datetime") : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/;} @$parsers)
ABresult
0X0
100
111
Truth table100%3/3
A && not(B)
A= ref $_ ne 'CODE'B= !(builtin::blessed($_) ? $_->can('parse_datetime') : defined $_ && !ref($_) && /^\w+(?:::\w+)*\z/)
ABresult
0X0
100
111
Truth table75%3/4
A and not(B) and C
A= defined $_B= not ref $_C= /^\w+(?:::\w+)*\z/
ABCresult
0XX0
10X0
1100
1111
1359  
1360 15 for my $parser (grep { !ref($_) } @$parsers) {
1361 7 ▶ next if $parser->can('parse_datetime');
Branch: 1 branch
BranchTrueFalseTotal
if $parser->can("parse_datetime") 1 6 7
1362 6 require DBIx::QuickORM;
1363 6 DBIx::QuickORM->load_class("'datetime_parsers' names the class '$parser', which", $parser);
1364 4 ▶ croak "'datetime_parsers' names the class '$parser', which has no parse_datetime" unless $parser->can('parse_datetime');
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->can("parse_datetime") 2 2 4
1365 }
1366  
1367 11 return [@$parsers];
1368 }
1369  
1370 =pod
1371  
1372 =over 4
1373  
1374 =item $conn->_enforce_canon($table, \%data, $flag)
1375  
1376 Croaks unless the data is canon, when C<$flag> (or its default) says to check.
1377  
1378 =back
1379  
1380 =cut
1381  
1382 136 ▶sub _enforce_canon {
Subroutine _enforce_canon: called CC=5 SCAR=16.1
1383 136 my $self = shift;
1384 136 my ($table, $data, $flag) = @_;
1385  
1386 136 my $lookup = $self->lookup;
1387 136 ▶ $flag //= $lookup->in_txn ? 1 : $self->{+ENFORCE_CANON};
Branch: 1 branch
BranchTrueFalseTotal
$lookup->in_txn ? : 21 57 78
Condition66%2/3
$flag //= $lookup->in_txn ? 1 : $self->{"enforce_canon"}
l!l&&r!l&&!r
58780
MC/DC0%0/2
$flag //= $lookup->in_txn ? 1 : $self->{"enforce_canon"}
$flag $lookup->in_txn ? 1 : $self->{"enforce_canon"}
Truth table66%2/3
A //= B
A= $flagB= $lookup->in_txn ? 1 : $self->{"enforce_canon"}
ABresult
1X1
011
000
1388 136 ▶ return unless $flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag 61 75 136
1389 75 ▶ return if $lookup->is_canon($table, $data);
Branch: 1 branch
BranchTrueFalseTotal
if $lookup->is_canon($table, $data) 58 17 75
1390 17 confess "The row data for '$table' is not canon (not the ORM's current copy of that row); refresh the row, or pass enforce_canon => 0; for an update, a row's cas_update() writes only while the database still holds what the row read";
1391 }
1392  
1393 =pod
1394  
1395 =over 4
1396  
1397 =item %cas = $conn->_cas_values($table, \%data, \%changes, \%params)
1398  
1399 The compare-and-swap fields and their values from the row data.
1400  
1401 =back
1402  
1403 =cut
1404  
1405 86 ▶sub _cas_values {
Subroutine _cas_values: called CC=6 SCAR=17.9
1406 86 my $self = shift;
1407 86 my ($table, $data, $changes, $params) = @_;
1408  
1409 86 ▶ my $cas = $params->{cas} // [];
Condition100%2/2
$params->{"cas"} // []
l!l
2561
MC/DC100%1/1
$params->{"cas"} // []
$params->{"cas"}
Truth table100%2/2
A
A= $params->{"cas"}
Aresult
11
00
1410 86 ▶ confess "'cas' must be an arrayref of field names" unless ref($cas) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cas eq "ARRAY" 1 85 86
1411 85 my @fields = @$cas;
1412 85 ▶ push @fields => grep { exists $data->{$_} } keys %$changes if $params->{auto_cas};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"auto_cas"} 2 83 85
1413 85 $self->_check_fields($table, @fields);
1414  
1415 85 for my $field (@fields) {
1416 26 ▶ confess "The 'cas' field '$field' is not in the row data" unless exists $data->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$field} 2 24 26
1417 }
1418  
1419 83 return map { $_ => $data->{$_} } @fields;
1420 }
1421  
1422 =pod
1423  
1424 =over 4
1425  
1426 =item $map = $conn->_map
1427  
1428 The map.
1429  
1430 =back
1431  
1432 =cut
1433  
1434 3956 ▶sub _map { $_[0]->{+DB_MAP} }
Subroutine _map: called CC=1 SCAR=0.0
1435  
1436 =pod
1437  
1438 =over 4
1439  
1440 =item \@fields = $conn->_default_fields($table)
1441  
1442 The fields a table's queries select: C<default_fields>, or every field.
1443  
1444 =back
1445  
1446 =cut
1447  
1448 346 ▶sub _default_fields {
Subroutine _default_fields: called CC=2 SCAR=6.9
1449 346 my $self = shift;
1450 346 my ($table) = @_;
1451 346 my $t = $self->_map->table($table);
1452 346 ▶ return $t->{default_fields} // [sort keys %{$t->{fields}}];
Condition100%2/2
$t->{"default_fields"} // [sort(keys %{$$t{"fields"};})]
l!l
36310
MC/DC100%1/1
$t->{"default_fields"} // [sort(keys %{$$t{"fields"};})]
$t->{"default_fields"}
Truth table100%2/2
A
A= $t->{"default_fields"}
Aresult
11
00
1453 }
1454  
1455 =pod
1456  
1457 =over 4
1458  
1459 =item $sql = $conn->_returning_sql($table)
1460  
1461 =item $sql = $conn->_returning_sql($table, \@fields)
1462  
1463 A C<RETURNING> clause, with a leading space, naming the fields, or the
1464 table's default fields, quoted.
1465  
1466 =back
1467  
1468 =cut
1469  
1470 139 ▶sub _returning_sql {
Subroutine _returning_sql: called CC=2 SCAR=7.0
1471 139 my $self = shift;
1472 139 my ($table, $fields) = @_;
1473 139 my $dialect = $self->{+DIALECT};
1474 139 ▶ return ' RETURNING ' . join(', ' => map { $dialect->quote_identifier($_) } @{$fields // $self->_default_fields($table)});
Condition66%2/3
$fields // $self->_default_fields($table)
l!l&&r!l&&!r
61330
MC/DC0%0/2
$fields // $self->_default_fields($table)
$fields $self->_default_fields($table)
Truth table66%2/3
A // B
A= $fieldsB= $self->_default_fields($table)
ABresult
1X1
011
000
1475 }
1476  
1477 =pod
1478  
1479 =over 4
1480  
1481 =item ($sql, \@binds, $marked) = $conn->_where_sql(\%pairs)
1482  
1483 A C<WHERE> body comparing each field to its value, in field order. An undef
1484 value becomes C<IS NULL>. Binds are C<[$field, $value]> pairs. An C<sql>
1485 marker croaks. C<$marked> is true when a value is another marker, as for
1486 C<_value_sql>. The SQL is built once per set of fields and null values (see
1487 C<_statement>).
1488  
1489 =back
1490  
1491 =cut
1492  
1493 270 ▶sub _where_sql {
Subroutine _where_sql: called CC=6 SCAR=17.9
1494 270 my $self = shift;
1495 270 my ($pairs) = @_;
1496  
1497 270 my (@shape, @binds, $marked);
1498 270 for my $field (sort keys %$pairs) {
1499 304 my $value = $pairs->{$field};
1500 304 ▶ confess "sql() cannot be used in a primary key or compare-and-swap value (the field '$field')" if $self->_is_sql($value);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_sql($value) 2 302 304
1501 302 ▶ $marked = 1 if ref($value) && $self->_is_marker($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $self->_is_marker($value) 3 299 302
Condition66%2/3
ref $value and $self->_is_marker($value)
!ll&&!rl&&r
29903
MC/DC50%1/2
ref $value and $self->_is_marker($value)
ref $value $self->_is_marker($value)
Truth table66%2/3
A and B
A= ref $valueB= $self->_is_marker($value)
ABresult
0X0
100
111
1502 302 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 299 3 302
1503 299 push @shape => $field;
1504 299 push @binds => [$field => $value];
1505 }
1506 else {
1507 3 push @shape => "\x01$field";
1508 }
1509 }
1510  
1511 268 my $dialect = $self->{+DIALECT};
1512 my $sql = $self->_statement(
1513 join("\0" => 'where', @shape),
1514 65 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1515 65 ▶ join ' AND ' => map { m/^\x01(.*)\z/s ? $dialect->quote_identifier($1) . ' IS NULL' : $dialect->quote_identifier($_) . ' = ?' } @shape;
Branch: 1 branch
BranchTrueFalseTotal
/^\x01(.*)\z/s ? : 2 78 80
1516 }
1517 268 );
1518  
1519 268 return ($sql, \@binds, $marked);
1520 }
1521  
1522 =pod
1523  
1524 =over 4
1525  
1526 =item $sql = $conn->_statement($shape, \&build)
1527  
1528 The SQL C<build> returns, kept by C<$shape> so it is built once per shape: a
1529 key naming the statement kind, the table, and its fields in order. An undef
1530 shape (a statement with a marker, see C<_value_sql>) builds it every time.
1531  
1532 =back
1533  
1534 =cut
1535  
1536 811 ▶sub _statement {
Subroutine _statement: called CC=3 SCAR=11.0
1537 811 my $self = shift;
1538 811 my ($shape, $build) = @_;
1539 811 ▶ return $build->() unless defined $shape;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shape 15 796 811
1540 796 ▶ return $self->{+STATEMENTS}->{$shape} //= $build->();
Condition66%2/3
$self->{"statements"}{$shape} //= &$build()
l!l&&r!l&&!r
4753210
MC/DC0%0/2
$self->{"statements"}{$shape} //= &$build()
$self->{"statements"}{$shape} &$build()
Truth table66%2/3
A //= B
A= $self->{"statements"}{$shape}B= &$build()
ABresult
1X1
011
000
1541 }
1542  
1543 =pod
1544  
1545 =over 4
1546  
1547 =item $sth = $conn->_run(\%fields, $sql, \@binds)
1548  
1549 =item $sth = $conn->_run(\%fields, $sql, \@binds, \%prepare_attrs, $cached)
1550  
1551 Prepares and executes the SQL, the one place the CRUD operations bind values.
1552 With C<$cached> the statement comes from C<prepare_cached>, for the
1553 single-row CRUD operations' fixed SQL only.
1554  
1555 Every statement the Connection sends comes through here, so here it croaks
1556 while an async query is pending (see C<select>), and while the innermost
1557 transaction is marked failed, unless the statement rolls it back. Each bind is
1558 C<[$name, $value]>. The field record C<$fields{$name}> (none for an undef
1559 name) decides the bind attributes, except that a C<binary> marker is bound as
1560 binary. The SQL and every value not going to a binary field pass through the
1561 dialect's C<text_to_db>.
1562  
1563 =back
1564  
1565 =cut
1566  
1567 1120 ▶sub _run {
Subroutine _run: called CC=15 SCAR=27.1
1568 1120 my $self = shift;
1569 1120 my ($fields, $sql, $binds, $attrs, $cached) = @_;
1570  
1571 1120 $self->_check_idle;
1572 1120 ▶ if (my $txn = $self->{+TXNS}->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $txn = $self->{"txns"}[-1]) 327 793 1120
1573 # RELEASE SAVEPOINT reaches a failed level only after ROLLBACK TO it: commit_txn rolls a failed level back first.
1574 327 ▶ confess "A statement in the transaction opened at " . $txn->opened_at . " failed, so the transaction can only be rolled back; to recover from a failure and go on, run the work that may fail in a nested transaction.\nThe failure: " . $txn->failed . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $txn->failed and not $sql =~ /^(?:ROLLBACK|RELEASE SAVEPOINT)\b/ 5 322 327
Condition100%3/3
defined $txn->failed and not $sql =~ /^(?:ROLLBACK|RELEASE SAVEPOINT)\b/
!ll&&!rl&&r
306165
MC/DC100%2/2
defined $txn->failed and not $sql =~ /^(?:ROLLBACK|RELEASE SAVEPOINT)\b/
defined $txn->failed not $sql =~ /^(?:ROLLBACK|RELEASE SAVEPOINT)\b/
Truth table100%3/3
A and not(B)
A= defined $txn->failedB= not $sql =~ /^(?:ROLLBACK|RELEASE SAVEPOINT)\b/
ABresult
0X0
100
111
1575 if defined($txn->failed) && $sql !~ m/^(?:ROLLBACK|RELEASE SAVEPOINT)\b/;
1576 }
1577 1115 my $dialect = $self->{+DIALECT};
1578  
1579 1115 my (@values, @attrs, @text, %attrs_of);
1580 1115 for my $bind (@$binds) {
1581 9187 my ($name, $value) = @$bind;
1582 9187 ▶ my $field = defined($name) ? $fields->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 9160 27 9187
1583 9187 ▶ my $attrs = $field ? ($attrs_of{$field} //= [$dialect->bind_attrs($field)])->[0] : undef; # once per field record
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 9157 30 9187
Condition100%2/2
$attrs_of{$field} //= [$dialect->bind_attrs($field)]
l!l
77441413
MC/DC100%1/1
$attrs_of{$field} //= [$dialect->bind_attrs($field)]
$attrs_of{$field}
Truth table100%2/2
A
A= $attrs_of{$field}
Aresult
11
00
1584 9187 ▶ if (ref($value) && $self->_is_marker($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $self->_is_marker($value)) 9 9178 9187
Condition66%2/3
ref $value and $self->_is_marker($value)
!ll&&!rl&&r
917809
MC/DC50%1/2
ref $value and $self->_is_marker($value)
ref $value $self->_is_marker($value)
Truth table66%2/3
A and B
A= ref $valueB= $self->_is_marker($value)
ABresult
0X0
100
111
1585 9 ▶ confess "A " . $value->kind . "() marker cannot be bound here" unless $value->kind eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless $value->kind eq "binary" 0 9 9
1586 9 ($field, $value) = ({affinity => 'binary'}, $value->value);
1587 9 $attrs = $dialect->bind_attrs($field);
1588 }
1589 9187 ▶ push @text => scalar(@values) unless $field && $field->{affinity} eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless $field and $field->{"affinity"} eq "binary" 9139 48 9187
Condition100%3/3
$field and $field->{"affinity"} eq "binary"
!ll&&!rl&&r
29911048
MC/DC100%2/2
$field and $field->{"affinity"} eq "binary"
$field $field->{"affinity"} eq "binary"
Truth table100%3/3
A and B
A= $fieldB= $field->{"affinity"} eq "binary"
ABresult
0X0
100
111
1590 9187 push @values => $value;
1591 9187 push @attrs => $attrs;
1592 }
1593 1115 $dialect->text_to_db($sql, @values[@text]);
1594  
1595 # 3: a cached statement still active is left alone and a new one prepared.
1596 1115 ▶ my $sth = $cached ? $self->{+DBH}->prepare_cached($sql, $attrs, 3) : $self->{+DBH}->prepare($sql, $attrs);
Branch: 1 branch
BranchTrueFalseTotal
$cached ? : 525 590 1115
1597 1102 $sth->bind_param($_ + 1, $values[$_], $attrs[$_]) for 0 .. $#values;
1598 1102 $sth->execute;
1599  
1600 1086 return $sth;
1601 }
1602  
1603 =pod
1604  
1605 =over 4
1606  
1607 =item $conn->_check_idle
1608  
1609 Croaks while an async query is pending.
1610  
1611 =back
1612  
1613 =cut
1614  
1615 1230 ▶sub _check_idle {
Subroutine _check_idle: called CC=2 SCAR=7.2
1616 1230 my $self = shift;
1617 1230 ▶ confess "An async query is still pending on this connection; collect its iterator (ready, next, or all) first" if $self->{+BUSY};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"busy"} 0 1230 1230
1618 }
1619  
1620 =pod
1621  
1622 =over 4
1623  
1624 =item $conn->_do($sql)
1625  
1626 Runs a transaction statement through C<_run>.
1627  
1628 =back
1629  
1630 =cut
1631  
1632 291 ▶sub _do { $_[0]->_run({}, $_[1], []) }
Subroutine _do: called CC=1 SCAR=0.0
1633  
1634 =pod
1635  
1636 =over 4
1637  
1638 =item \@rows = $conn->_fetch(\%fields, $sth)
1639  
1640 Every remaining row of an executed statement, as hashes keyed by column name.
1641 It is the one place the CRUD operations fetch rows. The names and every value
1642 not from a binary field (by C<$fields{$name}>) pass through the dialect's
1643 C<text_from_db>.
1644  
1645 =back
1646  
1647 =cut
1648  
1649 618 ▶sub _fetch {
Subroutine _fetch: called CC=4 SCAR=13.9
1650 618 my $self = shift;
1651 618 my ($fields, $sth) = @_;
1652  
1653 618 my $dialect = $self->{+DIALECT};
1654  
1655 618 my @names = @{$sth->{NAME}};
1656 618 $dialect->text_from_db(@names);
1657 618 ▶ my @text = grep { (($fields->{$names[$_]} // {})->{affinity} // '') ne 'binary' } 0 .. $#names;
Condition100%4/4
+($$fields{$names[$_]} // {})->{"affinity"} // ""
l!l
167177
$$fields{$names[$_]} // {}
l!l
167177
MC/DC100%1/1
+($$fields{$names[$_]} // {})->{"affinity"} // ""
$$fields{$names[$_]}
Truth table100%2/2
A
A= $$fields{$names[$_]}
Aresult
11
00
1658  
1659 618 my @rows;
1660 618 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 817 618 1435
1661 817 my @values = @$row;
1662 817 $dialect->text_from_db(@values[@text]);
1663 817 push @rows => {map { $names[$_] => $values[$_] } 0 .. $#names};
1664 }
1665  
1666 618 return \@rows;
1667 }
1668  
1669 =pod
1670  
1671 =over 4
1672  
1673 =item $level = $conn->_isolation($level)
1674  
1675 The level in lower case. Confesses unless it is one C<txn> takes or
1676 C<'server'>.
1677  
1678 =back
1679  
1680 =cut
1681  
1682 10 ▶sub _isolation {
Subroutine _isolation: called CC=4 SCAR=14.3
1683 10 my $self = shift;
1684 10 my ($level) = @_;
1685 10 ▶ return lc $level if defined($level) && !ref($level) && $ISOLATION{lc $level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $level and not ref $level and $ISOLATION{lc $level} 8 2 10
Condition50%3/6
defined $level and not ref $level and $ISOLATION{lc $level}
!ll&&!rl&&r
028
defined $level and not ref $level
!ll&&!rl&&r
0010
MC/DC33%1/3
defined $level and not ref $level and $ISOLATION{lc $level}
defined $level not ref $level $ISOLATION{lc $level}
Truth table50%2/4
A and not(B) and C
A= defined $levelB= not ref $levelC= $ISOLATION{lc $level}
ABCresult
0XX0
10X0
1100
1111
1686 2 confess "'isolation' must be one of: " . join(', ' => map { "'$_'" } @ISOLATION);
1687 }
1688  
1689 =pod
1690  
1691 =over 4
1692  
1693 =item $txn = $conn->_begin(\@caller, $level)
1694  
1695 Opens a transaction or savepoint and pushes it onto the stack. C<$level> is
1696 the root transaction's isolation level from C<txn>, or undef.
1697  
1698 A root transaction always states its level: C<$level>, else the Connection's
1699 C<isolation>, else C<'read committed'> where the dialect
1700 C<supports_isolation>. C<'server'> sends the dialect's plain C<begin_sql>.
1701  
1702 =back
1703  
1704 =cut
1705  
1706 142 ▶sub _begin {
Subroutine _begin: called CC=8 SCAR=20.8
1707 142 my $self = shift;
1708 142 my ($caller, $level) = @_;
1709  
1710 142 my $txns = $self->{+TXNS};
1711 142 my $dialect = $self->{+DIALECT};
1712 142 my $name = 'qorm_txn_' . ++$self->{+TXN_COUNT};
1713 142 ▶ if (@$txns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$txns) { } 32 110 142
1714 32 ▶ confess "The " . $dialect->name . " dialect has no savepoints, so transactions cannot be nested" unless $dialect->supports_savepoints;
Branch: 1 branch
BranchTrueFalseTotal
unless $dialect->supports_savepoints 0 32 32
1715 32 $self->_do("SAVEPOINT $name");
1716 31 $self->lookup->push_level;
1717 }
1718 else {
1719 110 ▶ $level //= $self->{+ISOLATION} // ($dialect->supports_isolation ? 'read committed' : 'server');
Branch: 1 branch
BranchTrueFalseTotal
$dialect->supports_isolation ? : 0 105 105
Condition100%4/4
$level //= $self->{"isolation"} // ($dialect->supports_isolation ? "read committed" : "server")
l!l
4106
$self->{"isolation"} // ($dialect->supports_isolation ? "read committed" : "server")
l!l
1105
MC/DC100%1/1
$level //= $self->{"isolation"} // ($dialect->supports_isolation ? "read committed" : "server")
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
1720 110 ▶ $self->_do($_) for $dialect->begin_sql($level eq 'server' ? undef : uc $level);
Branch: 1 branch
BranchTrueFalseTotal
$level eq 'server' ? : 107 3 110
1721 106 $self->lookup->begin_root;
1722 }
1723  
1724 137 my $txn = DBIx::QuickORM::Txn->new(connection => $self, name => $name, depth => @$txns + 1, caller => $caller);
1725 137 weaken($txns->[@$txns] = $txn);
1726 137 return $txn;
1727 }
1728  
1729 =pod
1730  
1731 =over 4
1732  
1733 =item ($ok, $err, @out) = $conn->_run_block($code, \@caller, $list, $level)
1734  
1735 Runs one attempt of a transaction block, as C<txn> describes, and reports how
1736 it ended, including a failure to open the transaction. A failed rollback after
1737 the block threw is a warning, so the block's exception is what propagates. The
1738 transaction is then abandoned (C<_abandon>).
1739  
1740 =back
1741  
1742 =cut
1743  
1744 89 ▶sub _run_block {
Subroutine _run_block: called CC=7 SCAR=19.5
1745 89 my $self = shift;
1746 89 my ($code, $caller, $list, $level) = @_;
1747  
1748 89 ▶ my $txn = eval { $self->_begin($caller, $level) } or return (0, $@);
Branch: 1 branch
BranchTrueFalseTotal
my $txn = eval { do { $self->_begin($caller, $level) } } or return 0, $@ 85 4 89
1749 85 my @out;
1750 85 my $ok = eval {
1751 85 ▶ if ($list) { @out = $code->($txn) }
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 4 81 85
1752 81 else { $out[0] = $code->($txn) }
1753 65 ▶ $self->commit_txn($txn) if $self->_is_open($txn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_open($txn) 46 19 65
1754 63 1;
1755 };
1756 85 ▶ return (1, undef, @out) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 63 22 85
1757  
1758 22 my $err = $@;
1759 22 ▶ if ($self->_is_open($txn) && !eval { $self->_unwind($txn); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_open($txn) and not eval { do { $self->_unwind($txn); 1 } }) 2 20 22
Condition100%3/3
$self->_is_open($txn) and not eval { do { $self->_unwind($txn); 1 } }
!ll&&!rl&&r
1192
MC/DC100%2/2
$self->_is_open($txn) and not eval { do { $self->_unwind($txn); 1 } }
$self->_is_open($txn) not eval { do { $self->_unwind($txn); 1 } }
Truth table100%3/3
A and not(B)
A= $self->_is_open($txn)B= not eval { do { $self->_unwind($txn); 1 } }
ABresult
0X0
100
111
1760 2 warn "Rolling back the transaction opened at " . $txn->opened_at . " after its block threw an exception failed: $@";
1761 2 $self->_abandon($txn);
1762 }
1763 22 return (0, $err);
1764 }
1765  
1766 =pod
1767  
1768 =over 4
1769  
1770 =item $bool = $conn->_retriable($err)
1771  
1772 True for a L<DBIx::QuickORM::Exception::DB> whose C<retriable> is true.
1773  
1774 =back
1775  
1776 =cut
1777  
1778 9 ▶sub _retriable {
Subroutine _retriable: called CC=4 SCAR=13.9
1779 9 my $self = shift;
1780 9 my ($err) = @_;
1781 9 ▶ return 0 unless blessed($err) && $err->isa('DBIx::QuickORM::Exception::DB');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($err) and $err->isa("DBIx::QuickORM::Exception::DB") 1 8 9
Condition66%2/3
builtin::blessed($err) and $err->isa("DBIx::QuickORM::Exception::DB")
!ll&&!rl&&r
108
MC/DC50%1/2
builtin::blessed($err) and $err->isa("DBIx::QuickORM::Exception::DB")
builtin::blessed($err) $err->isa("DBIx::QuickORM::Exception::DB")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa("DBIx::QuickORM::Exception::DB")
ABresult
0X0
100
111
1782 8 ▶ return $err->retriable ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$err->retriable ? : 6 2 8
1783 }
1784  
1785 =pod
1786  
1787 =over 4
1788  
1789 =item $bool = $conn->_is_open($txn)
1790  
1791 True while the transaction is on the stack.
1792  
1793 =back
1794  
1795 =cut
1796  
1797 340 ▶sub _is_open {
Subroutine _is_open: called CC=3 SCAR=11.0
1798 340 my $self = shift;
1799 340 my ($txn) = @_;
1800 340 my $open = $self->{+TXNS}->[$txn->depth - 1];
1801 340 ▶ return $open && $open == $txn ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$open && $open == $txn ? : 185 155 340
Condition100%3/3
$open and $open == $txn
!ll&&!rl&&r
1478185
MC/DC100%2/2
$open and $open == $txn
$open $open == $txn
Truth table100%3/3
A and B
A= $openB= $open == $txn
ABresult
0X0
100
111
1802 }
1803  
1804 =pod
1805  
1806 =over 4
1807  
1808 =item $conn->_check_open($txn, $action)
1809  
1810 Croaks unless the transaction is open and innermost, and this is the process
1811 that opened it.
1812  
1813 =back
1814  
1815 =cut
1816  
1817 116 ▶sub _check_open {
Subroutine _check_open: called CC=4 SCAR=13.9
1818 116 my $self = shift;
1819 116 my ($txn, $action) = @_;
1820  
1821 116 my $where = $txn->opened_at;
1822 116 ▶ confess "Cannot $action the transaction opened at $where from process $$; process $self->{+PID} opened it" unless $self->{+PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} == $$ 0 116 116
1823 116 ▶ confess "Cannot $action the transaction opened at $where: it is already finished" unless $self->_is_open($txn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_open($txn) 3 113 116
1824 113 ▶ confess "Cannot $action the transaction opened at $where: a transaction opened inside it is still open" unless $txn->depth == @{$self->{+TXNS}};
Branch: 1 branch
BranchTrueFalseTotal
unless $txn->depth == @{$self->{"txns"};} 3 110 113
1825 }
1826  
1827 =pod
1828  
1829 =over 4
1830  
1831 =item $conn->_unwind($txn)
1832  
1833 Rolls the transaction back, with every transaction open inside it, then drops
1834 them from the stack and the lookup table. An async query still pending is
1835 cancelled first, warning if that fails. Nothing is dropped when the database
1836 refuses the rollback.
1837  
1838 =back
1839  
1840 =cut
1841  
1842 77 ▶sub _unwind {
Subroutine _unwind: called CC=4 SCAR=16.0
1843 77 my $self = shift;
1844 77 my ($txn) = @_;
1845  
1846 # A query left pending inside it cannot be collected any more. On PostgreSQL the cancel aborts the transaction.
1847 77 ▶ if (my $busy = $self->{+BUSY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $busy = $self->{"busy"}) 0 77 77
1848 0 local $@;
1849 0 ▶ eval { $busy->{cancel}->(); 1 } or warn "Cancelling the async query pending in the transaction opened at " . $txn->opened_at . " failed: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $busy->{"cancel"}->(); 1 } } or warn 'Cancelling the async query pending in the transaction opened at ' . $txn->opened_at . " failed: $@" 0 0 0
1850 }
1851  
1852 77 ▶ if ($txn->depth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->depth == 1) { } 60 17 77
1853 60 $self->_do('ROLLBACK');
1854 }
1855 else {
1856 17 $self->_do('ROLLBACK TO SAVEPOINT ' . $txn->name);
1857 14 $self->_do('RELEASE SAVEPOINT ' . $txn->name);
1858 }
1859  
1860 70 $self->_drop_levels($txn);
1861 }
1862  
1863 =pod
1864  
1865 =over 4
1866  
1867 =item $conn->_drop_levels($txn)
1868  
1869 Drops the transaction, and every transaction open inside it, from the stack
1870 and the lookup table, without SQL.
1871  
1872 =back
1873  
1874 =cut
1875  
1876 76 ▶sub _drop_levels {
Subroutine _drop_levels: called CC=2 SCAR=6.9
1877 76 my $self = shift;
1878 76 my ($txn) = @_;
1879  
1880 76 my $txns = $self->{+TXNS};
1881 76 my $lookup = $self->lookup;
1882 76 ▶ while (@$txns >= $txn->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$txns >= $txn->depth) 78 76 154
1883 78 pop @$txns;
1884 78 $lookup->rollback_level;
1885 }
1886 }
1887  
1888 =pod
1889  
1890 =over 4
1891  
1892 =item $conn->_abandon($txn)
1893  
1894 For a transaction whose outcome is unknown: drops it as C<_drop_levels> does,
1895 then empties every remaining transaction lookup level, so no row held from
1896 inside the transaction is canon. The outside level is kept.
1897  
1898 =back
1899  
1900 =cut
1901  
1902 6 ▶sub _abandon {
Subroutine _abandon: called CC=1 SCAR=0.0
1903 6 my $self = shift;
1904 6 my ($txn) = @_;
1905 6 $self->_drop_levels($txn);
1906 6 $self->lookup->clear_txn_levels;
1907 }
1908  
1909 336 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=13.9
1910 336 my $self = shift;
1911 336 ▶ return if !$self->{+PID} || $self->{+PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"pid"} or $self->{"pid"} == $$ 332 4 336
Condition100%3/3
not $self->{"pid"} or $self->{"pid"} == $$
l!l&&r!l&&!r
462864
MC/DC100%2/2
not $self->{"pid"} or $self->{"pid"} == $$
not $self->{"pid"} $self->{"pid"} == $$
Truth table100%3/3
not(A) or B
A= not $self->{"pid"}B= $self->{"pid"} == $$
ABresult
1X1
011
000
1912  
1913 # By now DBI has torn its handles down. Any still alive at exit were released before that.
1914 4 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 4 4
1915  
1916 4 $self->release_inherited;
1917 }
1918  
1919 # A child that exits with the parent's async query still pending would otherwise free the handle and the
1920 # statement in any order during global destruction. DBD::Pg 3.21.2, freeing the statement after its handle,
1921 # reads freed memory (https://github.com/bucardo/dbdpg/issues/218) and can wait on the query, taking the
1922 # parent's result off the shared socket.
1923 258 ▶END {
Subroutine END: called CC=2 SCAR=11.6
1924 0 ▶ local ($?, $@, $!);
Subroutine END: not called CC=2 SCAR=11.6
1925 0 $_->release_inherited for grep { $_ } values %PENDING;
1926 }
1927  
1928 258 1;
1929  
1930 __END__
« blib/lib/DBIx/QuickORM/AggregateRow.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Join.pm »