Session/_common.pm

filter: Statementstmt 96.9% 189 / 195 Branchbran 91.3% 95 / 104 Conditioncond 91.6% 44 / 48 MC/DCmcdc 82.7% 24 / 29 Subroutinesub 100% 31 / 31 Podpod 0.0% 0 / 2 totaltotal 93.6% 383 / 409 CC 78 SCAR 15.8
CC
78
Cov
95%
_forEachSession
9.3
_pgDataSource
8.0
_quietSocket
7.0
CRAP 4.8
« Session/Store/SQLite.pm ↑ Summary Session/Store/MariaDBJSON.pm »
1 package Web::Session::_common;
2  
3 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use AutoLoader 'AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 43 our $VERSION = '1.2.0';
7  
8 # Number of sessions read per query by _forEachSession(). Values outside
9 # 1..1_000_000 fall back to the default (a huge value would break LIMIT)
10 43 our $BatchSize = 1000;
11  
12 # Seconds during which a connection used is taken as alive if its socket has
13 # nothing to read, see _lazyPing() (and _usable() in Store/Postgres.pm)
14 43 our $PING_AFTER = 30;
15 43 our %CALLBACKS = ( ping => \&_lazyPing );
16  
17 # Attributes of the handles used by class methods (searchOn(), ...), made fork
18 # safe. These handles are shared through DBI->connect_cached(): in a pre-fork
19 # server, if the parent called a class method before forking, every child
20 # would get the parent's handle, so its socket: concurrent use corrupts the
21 # protocol and a child exiting closes the parent's server connection.
22 # - private_ws_pid: connect_cached() uses the attributes as part of its cache
23 # key, so each process gets (and caches) its own handle
24 # - AutoInactiveDestroy: a handle inherited from the parent is not closed on
25 # the server when the child destroys it
26 # Threads (ithreads) share the pid, but each one has its own interpreter, so
27 # its own connect_cached() cache: a handle is never shared between threads.
28 # A store connected before a fork is handled by _forgetInheritedHandle() in
29 # Store/DBI.pm
30 # - Callbacks, HandleError: see _lazyPing()
31 457 ▶sub _forkSafeAttr {
Subroutine _forkSafeAttr: called CC=1 SCAR=0.0
32 457 my ( $class, $attr ) = @_;
33 return {
34 457 HandleError => \&_pingNext,
35 %$attr,
36 AutoInactiveDestroy => 1,
37 private_ws_pid => $$,
38 Callbacks => \%CALLBACKS,
39 };
40 }
41  
42 # connect_cached() pings a cached handle each time it returns it: a round trip
43 # at each tie, as long as the query itself. A handle returned less than
44 # $PING_AFTER seconds ago whose socket has nothing to read is alive: a
45 # connection closed by the server (restart, wait_timeout, KILL) has an end of
46 # file or a reset to read. An older handle (a NAT or a load balancer may have
47 # dropped the connection without a word) and a driver without socket (SQLite)
48 # are pinged. Callbacks of DBI: when the callback undefines $_, ping() is not
49 # called and the callback returns its result. The hash is the same for every
50 # handle, as connect_cached() keys its cache with the attributes.
51 # A connection lost without a word (NAT, firewall, frozen server) keeps a
52 # quiet socket: the query fails. The HandleError of the handle (inherited by
53 # its statement handles) then makes the next reuse ping it, so that
54 # connect_cached() replaces it at once
55 422 ▶sub _lazyPing {
Subroutine _lazyPing: called CC=4 SCAR=13.9
56 422 my ($dbh) = @_;
57 422 my $last = $dbh->{private_ws_seen};
58 422 my $now = time;
59 422 $dbh->{private_ws_seen} = $now;
60 return
61 422 ▶ unless ( defined $last
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last and $now - $last < $Web::Session::_common::PING_AFTER and _quietSocket($dbh) 416 6 422
Condition100%6/6
defined $last and $now - $last < $Web::Session::_common::PING_AFTER and _quietSocket($dbh)
!ll&&!rl&&r
393776
defined $last and $now - $last < $Web::Session::_common::PING_AFTER
!ll&&!rl&&r
381383
MC/DC100%3/3
defined $last and $now - $last < $Web::Session::_common::PING_AFTER and _quietSocket($dbh)
defined $last $now - $last < $Web::Session::_common::PING_AFTER _quietSocket($dbh)
Truth table100%4/4
A and B and C
A= defined $lastB= $now - $last < $Web::Session::_common::PING_AFTERC= _quietSocket($dbh)
ABCresult
0XX0
10X0
1100
1111
62 and $now - $last < $PING_AFTER
63 and _quietSocket($dbh) );
64 6 undef $_;
65 6 return 1;
66 }
67  
68 # HandleError, see _lazyPing(). Returns false: RaiseError still applies
69 13 ▶sub _pingNext {
Subroutine _pingNext: called CC=4 SCAR=13.9
70 13 my ( undef, $h ) = @_;
71 13 ▶ my $dbh = ( $h->{Type} // '' ) eq 'st' ? $h->{Database} : $h;
Branch: 1 branch
BranchTrueFalseTotal
($h->{'Type'} // '') eq 'st' ? : 1 12 13
Condition100%2/2
$h->{"Type"} // ""
l!l
121
MC/DC100%1/1
$h->{"Type"} // ""
$h->{"Type"}
Truth table100%2/2
A
A= $h->{"Type"}
Aresult
11
00
72 13 ▶ $dbh->{private_ws_seen} = undef if ($dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 13 0 13
73 13 return 0;
74 }
75  
76 # Options added to the DataSource of a PostgreSQL connection that doesn't set
77 # them (see "TIMEOUTS" in Web::Session): without them, a request waits for the
78 # TCP timeout of the system (about 15 minutes) when the server stops
79 # answering, a network failure or a frozen primary for example. The mocked
80 # tests that compare DataSources empty this hash
81 43 our %PG_TIMEOUTS = ( connect_timeout => 5, tcp_user_timeout => 10_000 );
82  
83 554 ▶sub _pgDataSource {
Subroutine _pgDataSource: called CC=8 SCAR=20.8
84 554 my ($dsn) = @_;
85 554 ▶ return $dsn unless ( defined $dsn and $dsn =~ /^dbi:Pg:/i );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsn and $dsn =~ /^dbi:Pg:/i 448 106 554
Condition100%3/3
defined $dsn and $dsn =~ /^dbi:Pg:/i
!ll&&!rl&&r
1447106
MC/DC100%2/2
defined $dsn and $dsn =~ /^dbi:Pg:/i
defined $dsn $dsn =~ /^dbi:Pg:/i
Truth table100%3/3
A and B
A= defined $dsnB= $dsn =~ /^dbi:Pg:/i
ABresult
0X0
100
111
86 106 foreach my $k ( sort keys %PG_TIMEOUTS ) {
87 18 ▶ next if ( $dsn =~ /[:;\s]\Q$k\E\s*=/i );
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ /[:;\s]\Q$k\E\s*=/i 2 16 18
88 16 ▶ next if ( $k eq 'tcp_user_timeout' and !_tcpUserTimeout() );
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "tcp_user_timeout" and not _tcpUserTimeout() 1 15 16
Condition100%3/3
$k eq "tcp_user_timeout" and not _tcpUserTimeout()
!ll&&!rl&&r
871
MC/DC100%2/2
$k eq "tcp_user_timeout" and not _tcpUserTimeout()
$k eq "tcp_user_timeout" not _tcpUserTimeout()
Truth table100%3/3
A and not(B)
A= $k eq "tcp_user_timeout"B= not _tcpUserTimeout()
ABresult
0X0
100
111
89 15 ▶ $dsn .= ( $dsn =~ /[:;]\z/ ? '' : ';' ) . "$k=$PG_TIMEOUTS{$k}";
Branch: 1 branch
BranchTrueFalseTotal
$dsn =~ /[:;]\z/ ? : 1 14 15
90 }
91 106 return $dsn;
92 }
93  
94 # Whether libpq knows tcp_user_timeout (libpq >= 12): it rejects an option it
95 # doesn't know, before connecting. Checked once, on a socket path that
96 # doesn't exist: no network involved
97 43 my $tcpUserTimeout;
98  
99 4 ▶sub _tcpUserTimeout {
Subroutine _tcpUserTimeout: called CC=4 SCAR=14.0
100 4 ▶ return $tcpUserTimeout if ( defined $tcpUserTimeout );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tcpUserTimeout 3 1 4
101 1 require DBI;
102 1 local $@;
103 1 eval {
104 1 DBI->connect( 'dbi:Pg:host=/nonexistent;tcp_user_timeout=1',
105 '', '', { PrintError => 0, RaiseError => 0 } );
106 };
107 1 ▶ return $tcpUserTimeout =
Branch: 1 branch
BranchTrueFalseTotal
($DBI::errstr // '') =~ /tcp_user_timeout/ ? : 0 1 1
Condition50%1/2
$DBI::errstr // ""
l!l
01
MC/DC0%0/1
$DBI::errstr // ""
$DBI::errstr
Truth table50%1/2
A
A= $DBI::errstr
Aresult
11
00
108 ( ( $DBI::errstr // '' ) =~ /tcp_user_timeout/ ) ? 0 : 1;
109 }
110  
111 # Socket of a connection to a database server, per driver
112 0 my %SOCKET = (
113 Pg => 'pg_socket',
114 mysql => 'mysql_sockfd',
115 MariaDB => 'mariadb_sockfd',
116 );
117  
118 # Whether $dbh is active and its socket has nothing to read (no end of file,
119 # reset or unexpected data), without a round trip. False for a driver without
120 # socket
121 410 ▶sub _quietSocket {
Subroutine _quietSocket: called CC=7 SCAR=19.5
122 410 my ($dbh) = @_;
123 410 ▶ return 0 unless ( $dbh->{Active} );
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Active"} 1 409 410
124 409 ▶ my $attr = $SOCKET{ $dbh->{Driver}->{Name} } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $attr = $SOCKET{$dbh->{"Driver"}{"Name"}} or return 0 32 377 409
125 32 my $fd = eval { $dbh->{$attr} };
126 32 ▶ return 0 unless ( defined $fd and $fd >= 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd >= 0 22 10 32
Condition100%3/3
defined $fd and $fd >= 0
!ll&&!rl&&r
21110
MC/DC100%2/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table100%3/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
127 10 my $rin = '';
128 10 vec( $rin, $fd, 1 ) = 1;
129  
130 # The descriptor number only: no Perl handle opened, none closed
131 10 my $ready = select( my $rout = $rin, undef, undef, 0 );
132 10 ▶ return ( defined $ready and $ready == 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ready && $ready == 0 ? : 7 3 10
Condition66%2/3
defined $ready and $ready == 0
!ll&&!rl&&r
037
MC/DC50%1/2
defined $ready and $ready == 0
defined $ready $ready == 0
Truth table66%2/3
A and B
A= defined $readyB= $ready == 0
ABresult
0X0
100
111
133 }
134  
135 102 ▶sub _tabInTab {
Subroutine _tabInTab: called CC=5 SCAR=16.1
136 102 my ( $class, $t1, $t2 ) = @_;
137  
138 # if no fields are required, return 0
139 102 ▶ return 0 unless ( @$t1 and @$t2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @$t1 and @$t2 36 66 102
Condition66%2/3
@$t1 and @$t2
!ll&&!rl&&r
36066
MC/DC50%1/2
@$t1 and @$t2
@$t1 @$t2
Truth table66%2/3
A and B
A= @$t1B= @$t2
ABresult
0X0
100
111
140 66 foreach my $f (@$t1) {
141 89 ▶ unless ( grep { $_ eq $f } @$t2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $f;} @$t2) 35 54 89
142 35 return 0;
143 }
144 }
145 31 return 1;
146 }
147  
148 160 ▶sub _fieldIsIndexed {
Subroutine _fieldIsIndexed: called CC=2 SCAR=6.9
149 160 my ( $class, $args, $field ) = @_;
150 my $index =
151 160 ▶ ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 6 154 160
152 160 return ( grep { $_ eq $field } @$index );
153 }
154  
155 # Check the structure of a deleteIfLowerThan() rule
156 78 ▶sub _checkRule {
Subroutine _checkRule: called CC=5 SCAR=16.1
157 78 my ( $class, $rule ) = @_;
158 78 ▶ unless ( ref($rule) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rule eq "HASH") 6 72 78
159 6 print STDERR "deleteIfLowerThan: rule must be a hash reference\n";
160 6 return 0;
161 }
162 72 foreach my $type (qw(or and not)) {
163 203 ▶ next unless defined $rule->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule->{$type} 102 101 203
164 101 ▶ unless ( ref( $rule->{$type} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rule->{$type} eq "HASH") 13 88 101
165 13 print STDERR "deleteIfLowerThan: $type must be a hash reference\n";
166 13 return 0;
167 }
168 }
169 59 return 1;
170 }
171  
172 # deleteIfLowerThan() thresholds are inserted into SQL queries, so they must
173 # be numbers
174 59 ▶sub _checkThresholds {
Subroutine _checkThresholds: called CC=6 SCAR=17.9
175 59 my ( $class, $rule ) = @_;
176 59 ▶ return 1 unless ( ref($rule) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" 0 59 59
177 59 ▶ my $thresholds = $rule->{or} || $rule->{and};
Condition100%3/3
$rule->{'or'} || $rule->{'and'}
l!l&&r!l&&!r
5351
MC/DC100%2/2
$rule->{'or'} || $rule->{'and'}
$rule->{'or'} $rule->{'and'}
Truth table100%3/3
A || B
A= $rule->{'or'}B= $rule->{'and'}
ABresult
1X1
011
000
178 59 ▶ return 1 unless ( ref($thresholds) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thresholds eq "HASH" 1 58 59
179 58 foreach ( values %$thresholds ) {
180 61 ▶ unless ( $class->_isNumber($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_isNumber($_)) 16 45 61
181 16 print STDERR "deleteIfLowerThan: threshold must be a number\n";
182 16 return 0;
183 }
184 }
185 42 return 1;
186 }
187  
188 # Identifier (table or column name) inserted into an SQL query. It is quoted
189 # with the driver's quote_identifier() when the driver supports it the
190 # standard way. PostgreSQL folds unquoted names to lower case, so tables and
191 # columns created without quotes have lower case names: the name is
192 # lowercased before being quoted, which keeps the previous behaviour for
193 # names in mixed case (TableName => 'Sessions' still finds "sessions"). A
194 # "schema.table" name is quoted part by part, and a name that already
195 # contains quotes is left unchanged. Other drivers get the name as before:
196 # quoting could change its case or meaning there
197 1345 ▶sub _quoteIdentifier {
Subroutine _quoteIdentifier: called CC=4 SCAR=13.9
198 1345 my ( $class, $dbh, $name ) = @_;
199 1345 my $driver = $dbh->{Driver}->{Name};
200 1345 ▶ unless ( $driver =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/) 10 1335 1345
201 10 $name =~ s/'/''/g;
202 10 return $name;
203 }
204 1335 ▶ return $name if ( $name =~ /["`]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /["`]/ 7 1328 1335
205 1328 ▶ $name = lc($name) if ( $driver eq 'Pg' );
Branch: 1 branch
BranchTrueFalseTotal
if $driver eq "Pg" 107 1221 1328
206 1328 return join '.', map { $dbh->quote_identifier($_) } split /\./, $name, -1;
207 }
208  
209 # Column of an indexed field in SQL queries. Callers check Index with the
210 # field name, then insert the result of this method: backends that need
211 # another quoting override it
212 111 ▶sub _quoteColumn {
Subroutine _quoteColumn: called CC=1 SCAR=0.0
213 111 my ( $class, $dbh, $field ) = @_;
214 111 return $class->_quoteIdentifier( $dbh, $field );
215 }
216  
217 # Quoted table name, see _quoteIdentifier()
218 233 ▶sub _tableName {
Subroutine _tableName: called CC=2 SCAR=7.1
219 233 my ( $class, $dbh, $args ) = @_;
220 return $class->_quoteIdentifier( $dbh,
221 233 ▶ $args->{TableName} || $Apache::Session::Store::DBI::TableName );
Condition66%2/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
661670
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table66%2/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
222 }
223  
224 # Digits are bounded: MySQL rejects out-of-range literals
225 252 ▶sub _isNumber {
Subroutine _isNumber: called CC=2 SCAR=6.9
226 252 my ( $class, $value ) = @_;
227 252 ▶ return ( defined($value)
Condition100%3/3
defined $value && $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
!ll&&!rl&&r
1498140
MC/DC100%2/2
defined $value && $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
defined $value $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
Truth table100%3/3
A && B
A= defined $valueB= $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
ABresult
0X0
100
111
228 and $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/ );
229 }
230  
231 # The operator of a comparison is inserted into SQL queries
232 200 ▶sub _checkOp {
Subroutine _checkOp: called CC=5 SCAR=16.1
233 200 my ( $class, $op ) = @_;
234 200 ▶ die "_buildCompareExpression: invalid operator '"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $op and $op eq "<" || $op eq ">" 32 168 200
defined $op ? : 24 8 32
Condition100%6/6
defined $op and $op eq "<" || $op eq ">"
!ll&&!rl&&r
824168
$op eq "<" or $op eq ">"
l!l&&r!l&&!r
1135524
MC/DC100%3/3
defined $op and $op eq "<" || $op eq ">"
defined $op $op eq "<" $op eq ">"
Truth table100%4/4
A and B or C
A= defined $opB= $op eq "<"C= $op eq ">"
ABCresult
0XX0
1000
11X1
1011
235 . ( defined $op ? $op : 'undef' ) . "'\n"
236 unless ( defined $op and ( $op eq '<' or $op eq '>' ) );
237 168 return $op;
238 }
239  
240 # searchLt() and searchGt() values are also inserted into SQL queries. Return
241 # the value without surrounding spaces (kept by the Lemonldap::NG CLI), or
242 # undef if it isn't a number
243 191 ▶sub _checkSearchValue {
Subroutine _checkSearchValue: called CC=4 SCAR=13.9
244 191 my ( $class, $op, $value ) = @_;
245 191 ▶ $value =~ s/^\s+|\s+\z//g if ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 179 12 191
246 191 ▶ unless ( $class->_isNumber($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_isNumber($value)) 96 95 191
247 96 ▶ print STDERR 'search'
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 88 8 96
248 . ( $op eq '<' ? 'Lt' : 'Gt' )
249 . ": value must be a number\n";
250 96 return undef;
251 }
252 95 return $value;
253 }
254  
255 # Run the SQL query $sub of searchLt() and searchGt(). The database may
256 # reject the comparison: a cast fails as soon as one session has a value that
257 # isn't an integer. The error is printed, then $scan compares the sessions in
258 # Perl, as the generic search of Lemonldap::NG does. {} if the scan fails
259 # too: these methods don't die (see "ERRORS" in Web::Session)
260 76 ▶sub _searchQuery {
Subroutine _searchQuery: called CC=5 SCAR=16.1
261 76 my ( $class, $op, $sub, $scan ) = @_;
262 76 ▶ my $name = 'search' . ( $op eq '<' ? 'Lt' : 'Gt' );
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 38 38 76
263 76 my $res = eval { $sub->() };
264 76 ▶ return $res unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 44 32 76
265 32 my $err = $@;
266 32 chomp $err;
267 32 print STDERR "$name: $err\n";
268 32 ▶ return {} unless ($scan);
Branch: 1 branch
BranchTrueFalseTotal
unless $scan 0 32 32
269 32 print STDERR "$name: comparing all sessions in Perl instead\n";
270 32 $res = eval { $scan->() };
271 32 ▶ return $res unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 20 12 32
272 12 $err = $@;
273 12 chomp $err;
274 12 print STDERR "$name: $err\n";
275 12 return {};
276 }
277  
278 # Perl test of searchLt() and searchGt(), for _searchByTest(). Sessions
279 # without the field are skipped, as by the SQL comparison
280 45 ▶sub _compareTest {
Subroutine _compareTest: called CC=2 SCAR=6.9
281 45 my ( $class, $op, $value ) = @_;
282 return $op eq '<'
283 72 ▶ ? sub { defined( $_[0] ) and $_[0] < $value }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] < $value 58 14 72
284 45 ▶ : sub { defined( $_[0] ) and $_[0] > $value };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$op eq '<' ? : 27 18 45
defined $_[0] and $_[0] > $value 60 14 74
285 }
286  
287 # Perl version of searchLt() and searchGt(): read all sessions and keep those
288 # for which $test->( value of $selectField ) is true. As in SQL, a value that
289 # doesn't start with a number is 0: Perl would read "inf" or "nan" as
290 # Infinity and NaN
291 51 ▶sub _searchByTest {
Subroutine _searchByTest: called CC=1 SCAR=0.0
292 51 my ( $class, $args, $selectField, $test, @fields ) = @_;
293 51 my %res;
294 $class->get_key_from_all_sessions(
295 $args,
296 174 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
297 174 my ( $entry, $id ) = @_;
298 174 my $v = $entry->{$selectField};
299 174 ▶ $v = 0 if ( defined $v and $v !~ /^\s*-?[0-9]/ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and not $v =~ /^\s*-?[0-9]/ 62 112 174
Condition100%3/3
defined $v and not $v =~ /^\s*-?[0-9]/
!ll&&!rl&&r
308262
MC/DC100%2/2
defined $v and not $v =~ /^\s*-?[0-9]/
defined $v not $v =~ /^\s*-?[0-9]/
Truth table100%3/3
A and not(B)
A= defined $vB= not $v =~ /^\s*-?[0-9]/
ABresult
0X0
100
111
300 174 ▶ return undef unless ( $test->($v) );
Branch: 1 branch
BranchTrueFalseTotal
unless &$test($v) 96 78 174
301 78 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 7 71 78
302 7 $res{$id}->{$_} = $entry->{$_} foreach (@fields);
303 }
304 else {
305 71 $res{$id} = $entry;
306 }
307 78 undef;
308 }
309 51 );
310 39 return \%res;
311 }
312  
313 # Get the unserialize sub of a class (populate() may be inherited)
314 181 ▶sub _unserializer {
Subroutine _unserializer: called CC=2 SCAR=7.1
315 181 my ($class) = @_;
316 181 ▶ my $p = $class->can('populate') or die "$class has no populate()\n";
Branch: 1 branch
BranchTrueFalseTotal
my $p = $class->can("populate") or die "$class has no populate()\n" 181 0 181
317 181 return $p->()->{unserialize};
318 }
319  
320 # Batch size (LIMIT) used by _forEachSession() and by the batched purge of
321 # PgJSON: $BatchSize if it is an integer in 1..1_000_000, else 1000
322 121 ▶sub _batchSize {
Subroutine _batchSize: called CC=4 SCAR=13.9
323 121 ▶ my ($limit) = ( $BatchSize // '' ) =~ /^\s*([1-9][0-9]*)\s*\z/;
Condition100%2/2
$Web::Session::_common::BatchSize // ""
l!l
1201
MC/DC100%1/1
$Web::Session::_common::BatchSize // ""
$Web::Session::_common::BatchSize
Truth table100%2/2
A
A= $Web::Session::_common::BatchSize
Aresult
11
00
324 121 ▶ return ( !$limit or $limit > 1_000_000 ) ? 1000 : $limit;
Branch: 1 branch
BranchTrueFalseTotal
!$limit || $limit > 1000000 ? : 7 114 121
Condition100%3/3
not $limit or $limit > 1000000
l!l&&r!l&&!r
52114
MC/DC100%2/2
not $limit or $limit > 1000000
not $limit $limit > 1000000
Truth table100%3/3
not(A) or B
A= not $limitB= $limit > 1000000
ABresult
1X1
011
000
325 }
326  
327 # Call $sub->( $id, $serialized ) for each session. If the driver supports
328 # LIMIT, sessions are read by batches to avoid loading the whole table in
329 # memory. No server-side cursor here: $sub may reuse the database handle.
330 105 ▶sub _forEachSession {
Subroutine _forEachSession: called CC=8 SCAR=22.3
331 105 my ( $class, $dbh, $table_name, $sub ) = @_;
332 105 my $limit = $class->_batchSize;
333 105 my $sql = "SELECT id,a_session FROM $table_name";
334 105 ▶ unless ( $dbh->{Driver}->{Name} =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"Driver"}{"Name"} =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/) 0 105 105
335 0 my $sth = $dbh->prepare_cached($sql);
336 0 $sth->execute;
337 0 ▶ while ( my @row = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $sth->fetchrow_array) 0 0 0
338 0 $sub->(@row);
339 }
340 0 return;
341 }
342  
343 # Not a single snapshot: rows inserted during the iteration with an id
344 # lower than the current position are not visited (harmless for purge
345 # and sessions explorer)
346 105 my ( $last, $rows );
347 105 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@$rows >= $limit and do { do { my $sth = $dbh->prepare_cached($sql . (defined $last ? " WHERE id > ?" : "") . " ORDER BY id LIMIT $limit"); $sth->execute(defined $last ? $last : ()); $rows = $sth->fetchall_arrayref; &$sub(@$_) foreach (@$rows); $last = $rows->[-1][0] if @$rows } } 8 102 110
348 113 ▶ my $sth =
Branch: 1 branch
BranchTrueFalseTotal
defined $last ? : 8 105 113
349 $dbh->prepare_cached( $sql
350 . ( defined($last) ? ' WHERE id > ?' : '' )
351 . " ORDER BY id LIMIT $limit" );
352 111 ▶ $sth->execute( defined($last) ? ($last) : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $last ? : 8 103 111
353 110 $rows = $sth->fetchall_arrayref;
354 110 $sub->(@$_) foreach (@$rows);
355 110 ▶ $last = $rows->[-1]->[0] if (@$rows);
Branch: 1 branch
BranchTrueFalseTotal
if @$rows 108 2 110
356 } while ( @$rows >= $limit );
357 102 return;
358 }
359  
360 43 1;
361 __END__
« Session/Store/SQLite.pm ↑ Summary Session/Store/MariaDBJSON.pm »