Session/Store/DBI.pm

filter: Statementstmt 100% 166 / 166 Branchbran 91.3% 53 / 58 Conditioncond 73.4% 36 / 49 MC/DCmcdc 44.8% 13 / 29 Subroutinesub 100% 27 / 27 Podpod 0.0% 0 / 3 totaltotal 88.8% 295 / 332 CC 50 SCAR 14.6
CC
50
Cov
93%
_unchanged
8.0
_reusedHandle
6.0
_writePending
5.2
CRAP 4.3
« Session/Store/MariaDBJSON.pm ↑ Summary Session/Serialize/JSON.pm »
1 package Web::Session::Store::DBI;
2  
3 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Apache::Session::Store::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Web::Session::_common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 our @ISA = qw(Apache::Session::Store::DBI);
8 29 our $VERSION = '1.2.0';
9  
10 # Connection reuse (default): returns a handle cached by DBI->connect_cached()
11 # (which pings it and reconnects if needed), opened with the attributes $attr
12 # that the store uses without reuse. Returns undef if the store is already
13 # connected, if "noreuse" is set or if a Handle is given. Unless Commit is
14 # 0, the Postgres and Patroni stores use the connection pool of the Postgres
15 # store instead, unless KeepConnections is 0 (see Store/Postgres.pm).
16 #
17 # Why AutoCommit differs between stores: each store opens its connection
18 # with the attributes of the Apache::Session store it derives from.
19 # Apache::Session::Store::Postgres uses AutoCommit off and commits in DESTROY
20 # when Commit is set. Its materialize() locks the row with
21 # "SELECT ... FOR UPDATE": the Patroni store uses it (through _try()), so a
22 # Patroni session stays locked from the tie to the commit at untie, also with
23 # the connection pool (one transaction per tie, see Store/Patroni.pm). The
24 # Postgres store doesn't lock: its materialize() comes first from
25 # Apache::Session::Store::DBI. The SQLite store copies the AutoCommit off
26 # model. Apache::Session::Store::MySQL (MySQLJSON derives from it) was
27 # written for MyISAM, which has no transaction: AutoCommit on. The lock
28 # manager of all SQL backends is Apache::Session::Lock::Null: with AutoCommit
29 # off, the transaction only spans the reads and the write of one tie.
30 # Keeping each store's own attributes keeps its behaviour unchanged.
31 #
32 # Stores don't disconnect a handle they didn't open, so a transaction that
33 # the store doesn't commit (AutoCommit off, Commit not set or commit failure)
34 # would stay open, with its locks, on the shared handle: it is rolled back
35 # when the store is destroyed, after its own DESTROY (as disconnect did)
36 208 ▶sub _reusedHandle {
Subroutine _reusedHandle: called CC=6 SCAR=17.9
37 208 my ( $self, $args, $datasource, $username, $password, $attr ) = @_;
38 return undef
39 208 ▶ if ( defined $self->{dbh} or $args->{noreuse} or exists $args->{Handle} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} or $args->{"noreuse"} or exists $args->{"Handle"} 16 192 208
Condition100%6/6
defined $self->{"dbh"} or $args->{"noreuse"} or exists $args->{"Handle"}
l!l&&r!l&&!r
133192
defined $self->{"dbh"} or $args->{"noreuse"}
l!l&&r!l&&!r
211195
MC/DC100%3/3
defined $self->{"dbh"} or $args->{"noreuse"} or exists $args->{"Handle"}
defined $self->{"dbh"} $args->{"noreuse"} exists $args->{"Handle"}
Truth table100%4/4
A or B or C
A= defined $self->{"dbh"}B= $args->{"noreuse"}C= exists $args->{"Handle"}
ABCresult
1XX1
01X1
0011
0000
40  
41 # Fork safe, as the handles of the class methods (see _forkSafeAttr() in
42 # _common.pm): a child forked after the parent tied a session must not
43 # reuse the parent's handle, nor close its connection
44 192 ▶ my $dbh = DBI->connect_cached(
Condition50%1/2
'DBI'->connect_cached(Web::Session::_common::_pgDataSource($datasource), $username, $password, 'Web::Session::_common'->_forkSafeAttr($attr)) || die($DBI::errstr)
l!l
1920
MC/DC0%0/1
'DBI'->connect_cached(Web::Session::_common::_pgDataSource($datasource), $username, $password, 'Web::Session::_common'->_forkSafeAttr($attr)) || die($DBI::errstr)
'DBI'->connect_cached(Web::Session::_common::_pgDataSource($datasource), $username, $password, 'Web::Session::_common'->_forkSafeAttr($attr))
Truth table50%1/2
A
A= 'DBI'->connect_cached(Web::Session::_common::_pgDataSource($datasource), $username, $password, 'Web::Session::_common'->_forkSafeAttr($attr))
Aresult
11
00
45 Web::Session::_common::_pgDataSource($datasource),
46 $username, $password, Web::Session::_common->_forkSafeAttr($attr) )
47 || die $DBI::errstr;
48 $self->{reuse_rollback} =
49 bless \$dbh, 'Web::Session::Store::DBI::Rollback'
50 192 ▶ unless ( $dbh->{AutoCommit} );
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 173 19 192
51 192 return $dbh;
52 }
53  
54 # In a child created by fork(), a store connected by the parent still holds
55 # the parent's handle: connection() would return early and the child would
56 # use the parent's socket. Handles of the connection cache and of the
57 # PostgreSQL pool carry the pid of the process that opened them (see
58 # _forkSafeAttr() in _common.pm): such a handle is forgotten with its
59 # statements, neither closed (AutoInactiveDestroy) nor rolled back (see the
60 # Rollback guard below), so that connection() opens one for this process.
61 # Other handles (noreuse, Handle argument) are kept as is. Called by the
62 # connection() of every store, also by those not inheriting from this class,
63 # and by the DESTROY of the stores that commit there (the commit would go to
64 # the parent's transaction). Returns true if the handle was forgotten
65 953 ▶sub _forgetInheritedHandle {
Subroutine _forgetInheritedHandle: called CC=4 SCAR=13.9
66 953 my ($self) = @_;
67 953 ▶ my $dbh = $self->{dbh} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->{"dbh"} or return 0 452 501 953
68 452 my $pid = $dbh->{private_ws_pid};
69 452 ▶ return 0 unless ( defined $pid and $pid != $$ );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid != $$ 437 15 452
Condition100%3/3
defined $pid and $pid != $$
!ll&&!rl&&r
5638115
MC/DC100%2/2
defined $pid and $pid != $$
defined $pid $pid != $$
Truth table100%3/3
A and B
A= defined $pidB= $pid != $$
ABresult
0X0
100
111
70 15 delete @$self{ 'dbh', 'reuse_rollback', grep { /_sth\z/ } keys %$self };
71 15 return 1;
72 }
73  
74 # connection() of stores whose parent class $class (an Apache::Session::Store
75 # class) opens a connection with the attributes $attr and uses the Handle
76 # argument as is
77 33 ▶sub _connection {
Subroutine _connection: called CC=3 SCAR=11.0
78 33 my ( $self, $session, $class, $attr ) = @_;
79 33 _forgetInheritedHandle($self);
80 33 my $args = $session->{args};
81 33 my $connect = $class->can('connection');
82 33 my @credentials;
83 {
84 29 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 33 ▶ @credentials = map { $args->{$_} || ${"${class}::$_"} }
Condition66%2/3
$args->{$_} || ${"${class}::$_";}
l!l&&r!l&&!r
40059
MC/DC50%1/2
$args->{$_} || ${"${class}::$_";}
$args->{$_} ${"${class}::$_";}
Truth table66%2/3
A || B
A= $args->{$_}B= ${"${class}::$_";}
ABresult
1X1
011
000
86 qw(DataSource UserName Password);
87 }
88 33 ▶ if ( my $dbh = $self->_reusedHandle( $args, @credentials, $attr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->_reusedHandle($args, @credentials, $attr)) 24 9 33
89 24 local $args->{Handle} = $dbh;
90 24 return $self->$connect($session);
91 }
92 9 return $self->$connect($session);
93 }
94  
95 153 ▶sub insert {
Subroutine insert: called CC=4 SCAR=13.9
Pod undocumented
96 153 my ( $self, $session ) = @_;
97  
98 153 $self->connection($session);
99  
100 153 local $self->{dbh}->{RaiseError} = 1;
101  
102 my $index =
103 ref( $session->{args}->{Index} )
104 ? $session->{args}->{Index}
105 153 ▶ : [ split /\s+/, $session->{args}->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $session->{'args'}{'Index'} ? : 22 131 153
106  
107 $self->{insert_sth} //= $self->{dbh}->prepare_cached(
108 'INSERT INTO '
109 . $self->_quoteTable( $self->{table_name} ) . ' ('
110 . join( ',',
111 153 ▶ 'id', 'a_session', map { $self->_quoteColumn($_) } @$index )
Condition66%2/3
$self->{"insert_sth"} //= $self->{"dbh"}->prepare_cached('INSERT INTO ' . $self->_quoteTable($self->{"table_name"}) . ' (' . join(",", "id", "a_session", map({$self->_quoteColumn($_);} @$index)) . ') VALUES (' . join(",", ("?") x (2 + @$index)) . ')')
l!l&&r!l&&!r
21510
MC/DC0%0/2
$self->{"insert_sth"} //= $self->{"dbh"}->prepare_cached('INSERT INTO ' . $self->_quoteTable($self->{"table_name"}) . ' (' . join(",", "id", "a_session", map({$self->_quoteColumn($_);} @$index)) . ') VALUES (' . join(",", ("?") x (2 + @$index)) . ')')
$self->{"insert_sth"} $self->{"dbh"}->prepare_cached('INSERT INTO ' . $self->_quoteTable($self->{"table_name"}) . ' (' . join(",", "id", "a_session", map({$self->_quoteColumn($_);} @$index)) . ') VALUES (' . join(",", ("?") x (2 + @$index)) . ')')
Truth table66%2/3
A //= B
A= $self->{"insert_sth"}B= $self->{"dbh"}->prepare_cached('INSERT INTO ' . $self->_quoteTable($self->{"table_name"}) . ' (' . join(",", "id", "a_session", map({$self->_quoteColumn($_);} @$index)) . ') VALUES (' . join(",", ("?") x (2 + @$index)) . ')')
ABresult
1X1
011
000
112 . ') VALUES ('
113 . join( ',', ('?') x ( 2 + @$index ) ) . ')'
114 );
115  
116 150 $self->{insert_sth}->bind_param( 1, $session->{data}->{_session_id} );
117 150 $self->{insert_sth}->bind_param( 2, $session->{serialized} );
118 150 my $i = 3;
119 150 foreach my $f (@$index) {
120 307 $self->{insert_sth}->bind_param( $i, $session->{data}->{$f} );
121 307 $i++;
122 }
123  
124 150 $self->{insert_sth}->execute;
125  
126 145 $self->{insert_sth}->finish;
127 }
128  
129 138 ▶sub update {
Subroutine update: called CC=5 SCAR=16.1
Pod undocumented
130 138 my $self = shift;
131 138 my $session = shift;
132  
133 # Indexed columns are computed from the session data only: same data,
134 # same columns (unless Index lists a column added since the row was
135 # written: see RewriteUnchanged)
136 138 ▶ return 1 if ( _unchanged( $self, $session ) );
Branch: 1 branch
BranchTrueFalseTotal
if _unchanged($self, $session) 8 130 138
137  
138 130 $self->connection($session);
139  
140 130 local $self->{dbh}->{RaiseError} = 1;
141  
142 my $index =
143 ref( $session->{args}->{Index} )
144 ? $session->{args}->{Index}
145 130 ▶ : [ split /\s+/, $session->{args}->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $session->{'args'}{'Index'} ? : 1 129 130
146  
147 130 ▶ if ( !defined $self->{update_sth} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"update_sth"}) 128 2 130
148 $self->{update_sth} = $self->{dbh}->prepare_cached(
149 'UPDATE '
150 . $self->_quoteTable( $self->{table_name} ) . ' SET '
151 . join( ' = ?, ',
152 128 'a_session', map { $self->_quoteColumn($_) } @$index )
153 . ' = ? WHERE id = ?'
154 );
155 }
156  
157 130 $self->{update_sth}->bind_param( 1, $session->{serialized} );
158 130 my $i = 2;
159 130 foreach my $f (@$index) {
160 327 $self->{update_sth}->bind_param( $i, $session->{data}->{$f} );
161 327 $i++;
162 }
163 130 $self->{update_sth}->bind_param( $i, $session->{data}->{_session_id} );
164  
165 130 $self->{update_sth}->execute;
166  
167 # DBI doesn't guarantee rows() after finish()
168 129 my $rows = $self->{update_sth}->rows;
169 129 $self->{update_sth}->finish;
170 129 $self->_checkStored( $session, $rows );
171 127 _remember( $self, $session );
172 127 return 1;
173 }
174  
175 # Dies if the UPDATE just run matched no row: the session was deleted (logout,
176 # purge...) since it was read, and it is not written back. A wrapper that
177 # caches sessions must not keep it, and a die is the only error that every
178 # store reports. Some MySQL and MariaDB connections count the rows changed,
179 # not the rows found: the row is looked up before concluding. $rows defaults
180 # to rows() of the UPDATE (Patroni and MariaDBJSON, whose UPDATE is run, and
181 # finished, by Apache::Session: DBD::Pg, DBD::mysql and DBD::MariaDB still
182 # return it)
183 131 ▶sub _checkStored {
Subroutine _checkStored: called CC=5 SCAR=16.3
184 131 my ( $self, $session, $rows ) = @_;
185 131 ▶ $rows //= $self->{update_sth}->rows;
Condition66%2/3
$rows //= $self->{"update_sth"}->rows
l!l&&r!l&&!r
12830
MC/DC0%0/2
$rows //= $self->{"update_sth"}->rows
$rows $self->{"update_sth"}->rows
Truth table66%2/3
A //= B
A= $rowsB= $self->{"update_sth"}->rows
ABresult
1X1
011
000
186 131 ▶ return unless ( defined $rows and $rows == 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows and $rows == 0 129 2 131
Condition66%2/3
defined $rows and $rows == 0
!ll&&!rl&&r
01292
MC/DC50%1/2
defined $rows and $rows == 0
defined $rows $rows == 0
Truth table66%2/3
A and B
A= defined $rowsB= $rows == 0
ABresult
0X0
100
111
187 2 my $id = $session->{data}->{_session_id};
188 my $sth = $self->{dbh}->prepare_cached( 'SELECT id FROM '
189 . _quoteTable( $self, $self->{table_name} )
190 2 . ' WHERE id = ?' );
191 2 $sth->execute($id);
192 2 my $found = $sth->fetchrow_arrayref;
193 2 $sth->finish;
194 2 ▶ die "Session '$id' was deleted or expired, not stored again\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $found 2 0 2
195 unless ($found);
196 }
197  
198 # Unchanged sessions are not rewritten
199 #
200 # Apache::Session saves a session at untie as soon as a key was stored, even
201 # with the value it already had (Lemonldap::NG does that). An UPDATE that
202 # writes the same data is not free: PostgreSQL writes a new row version (and
203 # its WAL) that must be cleaned up later, plus a new entry in every index when
204 # the stored value of a_session changes (the indexes are expressions on it, so
205 # the update can't be HOT), for example when the keys of a json column come
206 # in another order; other databases lock and log the row. So the store keeps
207 # the serialized data it read (or last updated) and update() returns without
208 # writing when the new one is the same. insert() is unchanged: it doesn't
209 # keep what it wrote, so the first update() after it always writes.
210 #
211 # The functions below take the store as first argument, not as invocant:
212 # Web::Session::Store::MariaDBJSON and Web::Session::Store::Patroni don't
213 # inherit from this class and call them too.
214  
215 2 ▶sub materialize {
Subroutine materialize: called CC=1 SCAR=0.0
Pod undocumented
216 2 my ( $self, $session ) = @_;
217  
218 # Apache::Session::Store::DBI's one, as before this override (it comes
219 # first in the method resolution of the stores inheriting from this class)
220 2 $self->SUPER::materialize($session);
221 1 _remember( $self, $session );
222 }
223  
224 # Keep the serialized data that the database holds (after a read or an update)
225 208 ▶sub _remember {
Subroutine _remember: called CC=1 SCAR=0.0
226 208 my ( $self, $session ) = @_;
227 208 $self->{stored_serialized} = $session->{serialized};
228 }
229  
230 # Whether the serialized data to write ($session->{serialized}) is the one the
231 # database already holds
232 157 ▶sub _unchanged {
Subroutine _unchanged: called CC=8 SCAR=20.8
233 157 my ( $self, $session ) = @_;
234 157 my $old = $self->{stored_serialized};
235 157 my $new = $session->{serialized};
236 return 0
237 if ( !defined $old
238 or !defined $new
239 157 ▶ or $session->{args}->{RewriteUnchanged} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $old && defined $new or $session->{"args"}{"RewriteUnchanged"} 107 50 157
Condition83%5/6
not defined $old && defined $new or $session->{"args"}{"RewriteUnchanged"}
l!l&&r!l&&!r
105250
defined $old && defined $new
!ll&&!rl&&r
105052
MC/DC66%2/3
not defined $old && defined $new or $session->{"args"}{"RewriteUnchanged"}
defined $old defined $new $session->{"args"}{"RewriteUnchanged"}
Truth table75%3/4
not(A && B) or C
A= defined $oldB= defined $newC= $session->{"args"}{"RewriteUnchanged"}
ABCresult
0XX1
10X1
1111
1100
240  
241 # Same bytes. Strings are compared as the bytes exchanged with the
242 # database: the serializer produces characters (UTF-8 flag on), which DBD
243 # drivers send UTF-8 encoded, but a driver may return the stored value as
244 # undecoded bytes (MySQL json columns, a handle without the utf8 option).
245 # A plain "eq" would compare characters: it would wrongly find a Latin-1
246 # byte equal to the same character sent UTF-8 encoded, and wrongly find
247 # UTF-8 bytes different from the same characters
248 50 ▶ return 1 if ( _bytes($old) eq _bytes($new) );
Branch: 1 branch
BranchTrueFalseTotal
if _bytes($old) eq _bytes($new) 14 36 50
249  
250 # Same data. Columns of type jsonb (PostgreSQL) or json (MySQL) don't
251 # return the text written but a normalized one (keys sorted by length,
252 # spaces added), and sessions written by older versions have their keys in
253 # random order: the bytes never match. The data read is then serialized
254 # again, the way the session read it, and compared. When it matches,
255 # readers already get the data that would be written, so the write
256 # changes nothing they see. Only JSON objects: the session data is a
257 # hash, so a row holding another JSON value can't hold the same data, and
258 # a row in another format (Storable, written by old versions) is still
259 # rewritten as JSON.
260 # Costs one decode and one encode when the bytes differ (at each update
261 # with jsonb or MySQL json columns), much less than a write.
262 36 ▶ return 0 unless ( $old =~ /\A\s*\{/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $old =~ /\A\s*\{/ 1 35 36
263 35 local $@;
264 35 my $again = eval {
265 35 my $tmp = { serialized => $old };
266 35 $session->{unserialize}->($tmp);
267 32 $session->{serialize}->($tmp);
268 32 $tmp->{serialized};
269 };
270 35 ▶ return ( defined $again and _bytes($again) eq _bytes($new) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $again && _bytes($again) eq _bytes($new) ? : 4 31 35
Condition100%3/3
defined $again and _bytes($again) eq _bytes($new)
!ll&&!rl&&r
3284
MC/DC100%2/2
defined $again and _bytes($again) eq _bytes($new)
defined $again _bytes($again) eq _bytes($new)
Truth table100%3/3
A and B
A= defined $againB= _bytes($again) eq _bytes($new)
ABresult
0X0
100
111
271 }
272  
273 # The bytes a DBD driver exchanges for string $s: UTF-8 encoded when it holds
274 # characters (UTF-8 flag on), as is otherwise
275 164 ▶sub _bytes {
Subroutine _bytes: called CC=2 SCAR=6.9
276 164 my $s = shift;
277 164 ▶ utf8::encode($s) if ( utf8::is_utf8($s) );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($s) 143 21 164
278 164 return $s;
279 }
280  
281 # New sessions written once
282 #
283 # Apache::Session saves a new session when it is tied (an INSERT of its empty
284 # data), then at untie (an UPDATE with its data): two writes, two commits with
285 # AutoCommit on. Stores that call _deferInsert() from insert() write it once:
286 # update() calls _insertPending(), which inserts the session with its data;
287 # a session untied unmodified is inserted when the store is freed
288 # (_destroyPending(), from DESTROY) or, if it is still tied when the program
289 # ends, by the END block below: during the global destruction, DBI's objects
290 # may be freed before the store, in an order that Perl doesn't specify. This
291 # END block runs before DBI's, which this module loads first. Under mod_perl,
292 # END may also run at the end of a request: an INSERT then only comes
293 # earlier, the untie that follows updates the row. A child created by fork()
294 # leaves the pending INSERTs of its parent to it (the connection is the
295 # parent's), and a session removed in its own tie is never written
296 # (_removePending()). The stores define _insertNow(), their INSERT.
297 #
298 # The pending INSERT keeps the id and the indexed values that the session had
299 # at tie, copied, and the list of these fields: neither the session nor its
300 # arguments, as Lemonldap::NG's cache wrapper keeps the store in the
301 # arguments (object_store), a cycle that would keep both until the end of the
302 # program.
303  
304 29 my %PENDING; # refaddr => store with a pending INSERT (weak)
305 28 ▶END { _flushPending() }
Subroutine END: called CC=1 SCAR=0.0
306  
307 44 ▶sub _deferInsert {
Subroutine _deferInsert: called CC=5 SCAR=16.1
308 44 my ( $self, $session ) = @_;
309  
310 # A generated id is random, no row has it. An id given by the caller
311 # (setId, as Lemonldap::NG does) may be used: the tie dies then, as the
312 # INSERT at tie does, so that the caller doesn't take another session's
313 # row for its new session
314 44 ▶ if ( $session->{args}->{setId} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"args"}{"setId"}) 2 42 44
315 2 my $id = $session->{data}->{_session_id};
316 my $sth = $self->{dbh}->prepare_cached( 'SELECT 1 FROM '
317 . _quoteTable( $self, $self->{table_name} )
318 2 . ' WHERE id = ?' );
319 2 $sth->execute($id);
320 2 my $used = $sth->fetchrow_arrayref;
321 2 $sth->finish;
322 2 ▶ die "Web::Session: session $id already exists\n" if ($used);
Branch: 1 branch
BranchTrueFalseTotal
if $used 1 1 2
323 }
324 43 my $index = $session->{args}->{Index};
325 43 ▶ $index = [ ref($index) ? @$index : split( /\s+/, $index // '' ) ];
Branch: 1 branch
BranchTrueFalseTotal
ref $index ? : 0 43 43
Condition100%2/2
$index // ""
l!l
241
MC/DC100%1/1
$index // ""
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
326 $self->{pending_insert} = {
327 data => {
328 46 map { ( $_ => $session->{data}->{$_} ) } '_session_id',
329 @$index
330 },
331 serialized => $session->{serialized},
332 43 args => { Index => $index },
333 pid => $$,
334 };
335 43 Scalar::Util::weaken( $PENDING{ Scalar::Util::refaddr($self) } = $self );
336 43 return 1;
337 }
338  
339 32 ▶sub _flushPending {
Subroutine _flushPending: called CC=4 SCAR=14.3
340 32 foreach my $self ( grep { defined } values %PENDING ) {
341 2 ▶ my $pending = _forgetPending($self) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = _forgetPending($self) or next 2 0 2
342 2 ▶ _writePending( $self, $pending ) if ( $self->{dbh} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} 2 0 2
343 }
344 32 %PENDING = ();
345 }
346  
347 120 ▶sub _forgetPending {
Subroutine _forgetPending: called CC=1 SCAR=0.0
348 120 my ($self) = @_;
349 120 delete $PENDING{ Scalar::Util::refaddr($self) };
350 120 return delete $self->{pending_insert};
351 }
352  
353 # A failure is reported as a warning, like a failed update at untie. Not in a
354 # child created by fork(): the session and the connection are the parent's
355 25 ▶sub _writePending {
Subroutine _writePending: called CC=5 SCAR=16.4
356 25 my ( $self, $pending ) = @_;
357 return
358 if ( $pending->{pid} != $$
359 25 ▶ or ( $self->{dbh}->{private_ws_pid} // $$ ) != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $pending->{"pid"} != $$ or ($self->{"dbh"}{"private_ws_pid"} // $$) != $$ 3 22 25
Condition50%3/6
$pending->{"pid"} != $$ or ($self->{"dbh"}{"private_ws_pid"} // $$) != $$
l!l&&r!l&&!r
0322
$self->{"dbh"}{"private_ws_pid"} // $$
l!l&&r!l&&!r
2500
MC/DC0%0/3
$pending->{"pid"} != $$ or ($self->{"dbh"}{"private_ws_pid"} // $$) != $$
$pending->{"pid"} != $$ $self->{"dbh"}{"private_ws_pid"} $$
Truth table25%1/4
A or B // C
A= $pending->{"pid"} != $$B= $self->{"dbh"}{"private_ws_pid"}C= $$
ABCresult
1XX1
01X1
0011
0000
360 22 local $@;
361 22 ▶ eval { $self->_insertNow($pending); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_insertNow($pending); 1 } } or warn 'Web::Session: unable to store new session ' . qq[$pending->{"data"}{"_session_id"}: $@] 20 2 22
362 or warn "Web::Session: unable to store new session "
363 . "$pending->{data}->{_session_id}: $@";
364 }
365  
366 # Called by update(): inserts the pending session with its data. Returns
367 # false if there is none
368 26 ▶sub _insertPending {
Subroutine _insertPending: called CC=2 SCAR=6.9
369 26 my ( $self, $session ) = @_;
370 26 ▶ my $pending = $self->{pending_insert} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = $self->{"pending_insert"} or return 0 15 11 26
371  
372 # Inserted under the id the session had at tie (the one checked there
373 # when given by setId): Lemonldap::NG's hashed session store changes
374 # _session_id while the session is tied and puts it back before untie.
375 # If it didn't, an UPDATE would match no row; an INSERT under the
376 # current id would create a session under another id
377 15 my $id = $pending->{data}->{_session_id};
378  
379 # If the INSERT fails, the store tries it again when it is freed: with the
380 # data of this save, not those of the tie (an empty session)
381 15 $pending->{serialized} = $session->{serialized};
382 $pending->{data} = {
383 15 ( map { ( $_ => $session->{data}->{$_} ) } @{ $pending->{args}->{Index} } ),
384 _session_id => $id,
385 };
386 $self->_insertNow(
387 {
388 %$session,
389 15 data => { %{ $session->{data} }, _session_id => $id },
390 }
391 );
392  
393 # Forgotten once written only: after a failed INSERT, the row still
394 # doesn't exist, a new save() must insert it, not update another row.
395 # Remembered as written: saved again unchanged in this tie, the session
396 # is not rewritten (see _unchanged())
397 11 _forgetPending($self);
398 11 _remember( $self, $session );
399 11 return 1;
400 }
401  
402 # Called by remove(): created and removed in the same tie, nothing was
403 # written, so nothing is deleted. A row with this id (given by setId, inserted
404 # by another process since the tie) is another session's. Returns true then
405 7 ▶sub _removePending {
Subroutine _removePending: called CC=2 SCAR=6.9
406 7 ▶ return _forgetPending( $_[0] ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_forgetPending($_[0]) ? : 6 1 7
407 }
408  
409 # Called by DESTROY: a new session untied without a successful update is
410 # inserted now, as it was tied. During the global destruction, the END block
411 # already did it
412 95 ▶sub _destroyPending {
Subroutine _destroyPending: called CC=4 SCAR=14.9
413 95 my ($self) = @_;
414 95 ▶ my $pending = _forgetPending($self) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = _forgetPending($self) or return 23 72 95
415 _writePending( $self, $pending )
416 23 ▶ if ( $self->{dbh} and ${^GLOBAL_PHASE} ne 'DESTRUCT' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} and ${^GLOBAL_PHASE} ne "DESTRUCT" 23 0 23
Condition33%1/3
$self->{"dbh"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
!ll&&!rl&&r
0023
MC/DC0%0/2
$self->{"dbh"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
$self->{"dbh"} ${^GLOBAL_PHASE} ne "DESTRUCT"
Truth table33%1/3
A and B
A= $self->{"dbh"}B= ${^GLOBAL_PHASE} ne "DESTRUCT"
ABresult
0X0
100
111
417 }
418  
419 # INSERT of a new session, see above
420 32 ▶sub _insertNow {
Subroutine _insertNow: called CC=1 SCAR=0.0
421 32 my ( $self, $session ) = @_;
422 32 return insert( $self, $session );
423 }
424  
425 # Table name and columns of indexed fields in SQL queries, see
426 # Web::Session::_common::_quoteIdentifier(). Stores that need
427 # another quoting override them
428 348 ▶sub _quoteTable {
Subroutine _quoteTable: called CC=1 SCAR=0.0
429 348 my ( $self, $name ) = @_;
430 return Web::Session::_common->_quoteIdentifier( $self->{dbh},
431 348 $name );
432 }
433  
434 637 ▶sub _quoteColumn {
Subroutine _quoteColumn: called CC=1 SCAR=0.0
435 637 my ( $self, $field ) = @_;
436 return Web::Session::_common->_quoteIdentifier( $self->{dbh},
437 637 $field );
438 }
439  
440 package Web::Session::Store::DBI::Rollback;
441  
442 # Ends the transaction left by a store using a reused handle (no-op if the
443 # store committed it). Not in a child created by fork(): the transaction is
444 # the parent's, on the parent's socket
445 173 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.2
446 173 my $dbh = ${ $_[0] };
447 173 ▶ return if ( ( $dbh->{private_ws_pid} // $$ ) != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"private_ws_pid"} // $$) != $$ 9 164 173
Condition66%2/3
$dbh->{"private_ws_pid"} // $$
l!l&&r!l&&!r
17210
MC/DC0%0/2
$dbh->{"private_ws_pid"} // $$
$dbh->{"private_ws_pid"} $$
Truth table66%2/3
A // B
A= $dbh->{"private_ws_pid"}B= $$
ABresult
1X1
011
000
448 164 local $@;
449 164 ▶ eval { $dbh->rollback } if ( $dbh->{Active} and !$dbh->{AutoCommit} );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} and not $dbh->{"AutoCommit"} 163 1 164
Condition66%2/3
$dbh->{"Active"} and not $dbh->{"AutoCommit"}
!ll&&!rl&&r
10163
MC/DC50%1/2
$dbh->{"Active"} and not $dbh->{"AutoCommit"}
$dbh->{"Active"} not $dbh->{"AutoCommit"}
Truth table66%2/3
A and not(B)
A= $dbh->{"Active"}B= not $dbh->{"AutoCommit"}
ABresult
0X0
100
111
450 }
451  
452 29 1;
« Session/Store/MariaDBJSON.pm ↑ Summary Session/Serialize/JSON.pm »