Database/Driver.pm

filter: Statementstmt 79.0% 143 / 181 Branchbran 35.0% 14 / 40 Conditioncond 40.6% 13 / 32 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 64.1% 25 / 39 Podpod 80.0% 4 / 5 totaltotal 63.6% 200 / 314 CC 37 SCAR 21.5
CC
37
Cov
67%
_check_connection
30.0
handle
21.6
new
11.3
CRAP 8.6
« Database/Driver/Pg.pm ↑ Summary Database/Driver/SQLite.pm »
1 package Prancer::Session::Store::Database::Driver;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = '1.01';
8  
9 2 ▶use Plack::Session::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use parent qw(Plack::Session::Store);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Storable qw(nfreeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # even though this *should* work automatically, it was not
19 2 our @CARP_NOT = qw(Prancer Try::Tiny);
20  
21 3 ▶sub new {
Subroutine new: called CC=10 SCAR=24.2
Pod documented
22 3 my ($class, $config) = @_;
23  
24 # this is the only required field
25 3 ▶ unless ($config->{'database'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"database"}) 0 3 3
26 0 croak "could not initialize session handler: no database name configured";
27 }
28  
29 # initialize the serializer that will be used
30 3 ▶ my $self = bless($class->SUPER::new(%{$config || {}}), $class);
Condition50%1/2
$config || {}
l!l
30
MC/DC0%0/1
$config || {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
31 3 ▶ $self->{'_serializer'} = sub { encode_base64(nfreeze(shift)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 3 ▶ $self->{'_deserializer'} = sub { thaw(decode_base64(shift)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 3 $self->{'_database'} = $config->{'database'};
35 3 $self->{'_username'} = $config->{'username'};
36 3 $self->{'_password'} = $config->{'password'};
37 3 $self->{'_hostname'} = $config->{'hostname'};
38 3 $self->{'_port'} = $config->{'port'};
39 3 $self->{'_charset'} = $config->{'charset'};
40 3 ▶ $self->{'_check_threshold'} = $config->{'connection_check_threshold'} // 30;
Condition50%1/2
$config->{"connection_check_threshold"} // 30
l!l
03
MC/DC0%0/1
$config->{"connection_check_threshold"} // 30
$config->{"connection_check_threshold"}
Truth table50%1/2
A
A= $config->{"connection_check_threshold"}
Aresult
11
00
41 3 ▶ $self->{'_dsn_extra'} = $config->{'dsn_extra'} || {};
Condition50%1/2
$config->{'dsn_extra'} || {}
l!l
03
MC/DC0%0/1
$config->{'dsn_extra'} || {}
$config->{'dsn_extra'}
Truth table50%1/2
A
A= $config->{'dsn_extra'}
Aresult
11
00
42 3 ▶ $self->{'_on_connect'} = $config->{'on_connect'} || [];
Condition50%1/2
$config->{'on_connect'} || []
l!l
03
MC/DC0%0/1
$config->{'on_connect'} || []
$config->{'on_connect'}
Truth table50%1/2
A
A= $config->{'on_connect'}
Aresult
11
00
43 3 ▶ $self->{'_table'} = $config->{'table'} || "sessions";
Condition50%1/2
$config->{'table'} || 'sessions'
l!l
03
MC/DC0%0/1
$config->{'table'} || 'sessions'
$config->{'table'}
Truth table50%1/2
A
A= $config->{'table'}
Aresult
11
00
44 3 ▶ $self->{'_timeout'} = $config->{'expiration_timeout'} // 1800;
Condition50%1/2
$config->{"expiration_timeout"} // 1800
l!l
30
MC/DC0%0/1
$config->{"expiration_timeout"} // 1800
$config->{"expiration_timeout"}
Truth table50%1/2
A
A= $config->{"expiration_timeout"}
Aresult
11
00
45 3 ▶ $self->{'_autopurge'} = $config->{'autopurge'} // 1;
Condition50%1/2
$config->{"autopurge"} // 1
l!l
30
MC/DC0%0/1
$config->{"autopurge"} // 1
$config->{"autopurge"}
Truth table50%1/2
A
A= $config->{"autopurge"}
Aresult
11
00
46 3 ▶ $self->{'_autopurge_probability'} = $config->{'autopurge_probability'} || 0.1;
Condition50%1/2
$config->{'autopurge_probability'} || 0.1
l!l
30
MC/DC0%0/1
$config->{'autopurge_probability'} || 0.1
$config->{'autopurge_probability'}
Truth table50%1/2
A
A= $config->{'autopurge_probability'}
Aresult
11
00
47 3 $self->{'_application'} = $config->{'application'};
48  
49 # store a pool of database connection handles
50 3 $self->{'_handles'} = {};
51  
52 3 return $self;
53 }
54  
55 9 ▶sub handle {
Subroutine handle: called CC=10 SCAR=30.7
Pod undocumented
56 9 my $self = shift;
57  
58 # to be fork safe and thread safe, use a combination of the PID and TID
59 # (if running with use threads) to make sure no two processes/threads share
60 # a handle. implementation based on DBIx::Connector by David E. Wheeler
61 9 my $pid_tid = $$;
62 9 ▶ $pid_tid .= "_" . threads->tid() if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 9 9
63  
64 # see if we have a matching handle
65 9 ▶ my $handle = $self->{'_handles'}->{$pid_tid} || undef;
Condition100%2/2
$self->{'_handles'}{$pid_tid} || undef
l!l
63
MC/DC100%1/1
$self->{'_handles'}{$pid_tid} || undef
$self->{'_handles'}{$pid_tid}
Truth table100%2/2
A
A= $self->{'_handles'}{$pid_tid}
Aresult
11
00
66  
67 9 ▶ if ($handle->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{'dbh'}) { } 6 3 9
68 6 ▶ if ($handle->{'dbh'}{'Active'} && $self->{'_check_threshold'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{'dbh'}{'Active'} and $self->{'_check_threshold'} and time - $handle->{'last_connection_check'} < $self->{'_check_threshold'}) { } 6 0 6
Condition33%2/6
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"} and time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
!ll&&!rl&&r
006
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"}
!ll&&!rl&&r
006
MC/DC0%0/3
$handle->{"dbh"}{"Active"} and $self->{"_check_threshold"} and time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
$handle->{"dbh"}{"Active"} $self->{"_check_threshold"} time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
Truth table25%1/4
A and B and C
A= $handle->{"dbh"}{"Active"}B= $self->{"_check_threshold"}C= time - $handle->{"last_connection_check"} < $self->{"_check_threshold"}
ABCresult
0XX0
10X0
1100
1111
69 (time - $handle->{'last_connection_check'} < $self->{'_check_threshold'})) {
70  
71 # the handle has been checked recently so just return it
72 6 return $handle->{'dbh'};
73 } else {
74 0 ▶ if ($self->_check_connection($handle->{'dbh'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_connection($handle->{'dbh'})) { } 0 0 0
75 0 $handle->{'last_connection_check'} = time;
76 0 return $handle->{'dbh'};
77 } else {
78 # try to disconnect but don't care if it fails
79 0 ▶ if ($handle->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{"dbh"}) 0 0 0
80 0 ▶ try { $handle->{'dbh'}->disconnect() } catch {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 }
82  
83 # try to connect again and save the new handle
84 0 $handle->{'dbh'} = $self->_get_connection();
85 0 return $handle->{'dbh'};
86 }
87 }
88 } else {
89 3 $handle->{'dbh'} = $self->_get_connection();
90 2 ▶ if ($handle->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{"dbh"}) 2 0 2
91 2 $handle->{'last_connection_check'} = time;
92 2 $self->{'_handles'}->{$pid_tid} = $handle;
93 2 return $handle->{'dbh'};
94 }
95 }
96  
97 0 return;
98 }
99  
100 3 ▶sub _get_connection {
Subroutine _get_connection: called CC=1 SCAR=0.1
101 3 my $self = shift;
102 3 my $dbh = undef;
103  
104 3 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.7
105 0 ▶ $dbh = DBI->connect(@{$self->{'_dsn'}}) || die "${\$DBI::errstr}\n";
Condition50%1/2
'DBI'->connect(@{$$self{'_dsn'};}) || die("${\$DBI::errstr;}\n")
l!l
20
MC/DC0%0/1
'DBI'->connect(@{$$self{'_dsn'};}) || die("${\$DBI::errstr;}\n")
'DBI'->connect(@{$$self{'_dsn'};})
Truth table50%1/2
A
A= 'DBI'->connect(@{$$self{'_dsn'};})
Aresult
11
00
106  
107 # run any on_connect sql
108 2 $dbh->do($_) for (@{$self->{'_on_connect'}});
109 1 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=7.2
110 1 ▶ my $error = (defined($_) ? $_ : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 0 1
111 1 croak "could not initialize database connection: ${error}";
112 3 };
113  
114 2 return $dbh;
115 }
116  
117 # Check the connection is alive
118 0 ▶sub _check_connection {
Subroutine _check_connection: not called CC=5 SCAR=34.0
119 0 my $self = shift;
120 0 my $dbh = shift;
121 0 ▶ return 0 unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
122  
123 0 ▶ if ($dbh->{'Active'} && (my $result = $dbh->ping())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"Active"} and my $result = $dbh->ping) 0 0 0
Condition0%0/3
$dbh->{"Active"} and my $result = $dbh->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh->{"Active"} and my $result = $dbh->ping
$dbh->{"Active"} my $result = $dbh->ping
Truth table0%0/3
A and B
A= $dbh->{"Active"}B= my $result = $dbh->ping
ABresult
0X0
100
111
124 0 ▶ if (int($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $result) { } 0 0 0
125 # DB driver itself claims all is OK, trust it
126 0 return 1;
127 } else {
128 # it was "0 but true", meaning the DBD doesn't implement ping and
129 # instead we got the default DBI ping implementation. implement
130 # our own basic check, by performing a real simple query.
131 0 ▶ return try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 return $dbh->do("SELECT 1");
133 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 return 0;
135 0 };
136 }
137 }
138  
139 0 return 0;
140 }
141  
142 2 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.2
Pod documented
143 2 my ($self, $session_id) = @_;
144 2 my $dbh = $self->handle();
145 2 my $result = undef;
146  
147 2 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
148 2 my $now = time();
149 2 my $table = $self->{'_table'};
150 2 my $application = $self->{'_application'};
151  
152 2 my $sth = $dbh->prepare(qq|
153 SELECT data
154 FROM ${table}
155 WHERE id = ?
156 AND application = ?
157 AND timeout >= ?
158 |);
159 2 $sth->execute($session_id, $application, $now);
160 2 my ($data) = $sth->fetchrow();
161 2 $sth->finish();
162  
163 # deserialize the data if there is any
164 2 ▶ $result = (defined($data) ? $self->{'_deserializer'}->($data) : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 1 1 2
165  
166 # maybe we'll purge old sessions sometimes
167 2 $self->_purge();
168  
169 2 $dbh->commit();
170 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
171 0 ▶ try { $dbh->rollback(); } catch {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
172  
173 0 ▶ my $error = (defined($_) ? $_ : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
174 0 carp "error fetching from session: ${error}";
175 2 };
176  
177 2 return $result;
178 }
179  
180 1 ▶sub store {
Subroutine store: called CC=1 SCAR=0.2
Pod documented
181 1 my ($self, $session_id, $data) = @_;
182 1 my $dbh = $self->handle();
183  
184 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 1 my $now = time();
186 1 my $table = $self->{'_table'};
187 1 my $application = $self->{'_application'};
188 1 my $timeout = ($now + $self->{'_timeout'});
189 1 my $serialized = $self->{'_serializer'}->($data);
190  
191 1 my $insert_sth = $dbh->prepare(qq|
192 INSERT INTO ${table} (id, application, timeout, data)
193 SELECT ?, ?, ?, ?
194 WHERE NOT EXISTS (
195 SELECT 1
196 FROM ${table}
197 WHERE id = ?
198 AND application = ?
199 AND timeout >= ?
200 )
201 |);
202 1 $insert_sth->execute($session_id, $application, $timeout, $serialized, $session_id, $application, $now);
203 1 $insert_sth->finish();
204  
205 1 my $update_sth = $dbh->prepare(qq|
206 UPDATE ${table}
207 SET timeout = ?, data = ?
208 WHERE id = ?
209 AND application = ?
210 AND timeout >= ?
211 |);
212 1 $update_sth->execute($timeout, $serialized, $session_id, $application, $now);
213 1 $update_sth->finish();
214  
215 # maybe we'll purge old sessions sometimes
216 1 $self->_purge();
217  
218 1 $dbh->commit();
219 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
220 0 ▶ try { $dbh->rollback(); } catch {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
221  
222 0 ▶ my $error = (defined($_) ? $_ : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
223 0 carp "error fetching from session: ${error}";
224 1 };
225  
226 1 return;
227 }
228  
229 2 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.4
Pod documented
230 2 my ($self, $session_id) = @_;
231 2 my $dbh = $self->handle();
232  
233 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 2 my $table = $self->{'_table'};
235 2 my $application = $self->{'_application'};
236  
237 2 my $sth = $dbh->prepare(qq|
238 DELETE
239 FROM ${table}
240 WHERE id = ?
241 AND application = ?
242 |);
243 2 $sth->execute($session_id, $application);
244 2 $sth->finish();
245  
246 # maybe we'll purge old sessions sometimes
247 2 $self->_purge();
248  
249 2 $dbh->commit();
250 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
251 0 ▶ try { $dbh->rollback(); } catch {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
252  
253 0 ▶ my $error = (defined($_) ? $_ : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
254 0 carp "error fetching from session: ${error}";
255 2 };
256  
257 2 return;
258 }
259  
260 5 ▶sub _purge {
Subroutine _purge: called CC=3 SCAR=11.0
261 5 my $self = shift;
262  
263 # 10% of the time we will also purge old sessions
264 5 ▶ if ($self->{'_autopurge'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_autopurge"}) 1 4 5
265 1 my $chance = rand();
266 1 ▶ if ($chance <= $self->{'_autopurge_probability'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chance <= $self->{"_autopurge_probability"}) 1 0 1
267 1 my $now = time();
268 1 my $dbh = $self->handle();
269 1 my $table = $self->{'_table'};
270 1 my $application = $self->{'_application'};
271  
272 1 my $delete_sth = $dbh->prepare(qq|
273 DELETE
274 FROM ${table}
275 WHERE application = ?
276 AND timeout < ?
277 |);
278 1 $delete_sth->execute($application, $now);
279 1 $delete_sth->finish();
280 }
281 }
282  
283 5 return;
284 }
285  
286 # stolen from Hash::Merge::Simple
287 ## no critic (ProhibitUnusedPrivateSubroutines)
288 3 ▶sub _merge {
Subroutine _merge: called CC=6 SCAR=24.1
289 3 my ($self, $left, @right) = @_;
290  
291 3 ▶ return $left unless @right;
Branch: 1 branch
BranchTrueFalseTotal
unless @right 0 3 3
292 3 ▶ return $self->_merge($left, $self->_merge(@right)) if @right > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @right > 1 0 3 3
293  
294 3 my ($right) = @right;
295 3 my %merged = %{$left};
296  
297 3 for my $key (keys %{$right}) {
298 0 my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left;
299  
300 0 ▶ if ($hr and $hl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr and $hl) { } 0 0 0
Condition0%0/3
$hr and $hl
!ll&&!rl&&r
000
MC/DC0%0/2
$hr and $hl
$hr $hl
Truth table0%0/3
A and B
A= $hrB= $hl
ABresult
0X0
100
111
301 0 $merged{$key} = $self->_merge($left->{$key}, $right->{$key});
302 } else {
303 0 $merged{$key} = $right->{$key};
304 }
305 }
306  
307 3 return \%merged;
308 }
309  
310 2 1;
« Database/Driver/Pg.pm ↑ Summary Database/Driver/SQLite.pm »