Session/DBI.pm

filter: Statementstmt 91.9% 148 / 161 Branchbran 76.1% 67 / 88 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 90.0% 18 / 20 Podpod 0.0% 0 / 6 totaltotal 84.7% 244 / 288 CC 50 SCAR 24.3
CC
50
Cov
86%
deleteIfLowerThan
24.9
get_key_from_all_sessions
8.4
__ANON__
7.3
CRAP 11.4
« Session/LDAP.pm ↑ Summary Session/PgJSON.pm »
1 package Web::Session::DBI;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 21 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Web::Session::_common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 21 our $VERSION = '1.0.0';
10 21 our @ISA = qw(Apache::Session Web::Session::_common);
11  
12 45 ▶sub searchOn {
Subroutine searchOn: called CC=2 SCAR=6.9
Pod undocumented
13 45 my $class = shift;
14 45 my ( $args, $selectField, $value, @fields ) = @_;
15  
16 45 ▶ if ( $class->_fieldIsIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_fieldIsIndexed($args, $selectField)) { } 43 2 45
17 43 my $column =
18 $class->_quoteColumn( $class->_classDbh($args), $selectField );
19 43 return $class->_query( $args, $selectField, $value,
20 { query => "$column=?", values => [$value] }, @fields );
21 }
22 else {
23 2 return $class->SUPER::searchOn(@_);
24 }
25 }
26  
27 6 ▶sub searchOnExpr {
Subroutine searchOnExpr: called CC=2 SCAR=6.9
Pod undocumented
28 6 my $class = shift;
29 6 my ( $args, $selectField, $value, @fields ) = @_;
30  
31 6 ▶ if ( $class->_fieldIsIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_fieldIsIndexed($args, $selectField)) { } 4 2 6
32 4 $value =~ s/\*/%/g;
33 4 my $column =
34 $class->_quoteColumn( $class->_classDbh($args), $selectField );
35 4 return $class->_query( $args, $selectField, $value,
36 { query => "$column like ?", values => [$value] }, @fields );
37 }
38 else {
39 2 return $class->SUPER::searchOnExpr(@_);
40 }
41 }
42  
43 96 ▶sub searchLt {
Subroutine searchLt: called CC=1 SCAR=0.0
Pod undocumented
44 96 my $class = shift;
45 96 return $class->_searchCompare( '<', @_ );
46 }
47  
48 41 ▶sub searchGt {
Subroutine searchGt: called CC=1 SCAR=0.0
Pod undocumented
49 41 my $class = shift;
50 41 return $class->_searchCompare( '>', @_ );
51 }
52  
53 # Sessions without the field are skipped, also when it is compared in Perl
54 # (fields not listed in Index). $value is checked, then inserted like
55 # deleteIfLowerThan() thresholds: bound, it would take the type of the cast
56 # (bigint in PostgreSQL) and decimal values would fail
57 53 ▶sub _searchCompare {
Subroutine _searchCompare: called CC=3 SCAR=11.0
58 53 my ( $class, $op, $args, $selectField, $value, @fields ) = @_;
59 53 $value = $class->_checkSearchValue( $op, $value );
60 53 ▶ return {} unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 22 31 53
61  
62 18 ▶ my $scan = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 18 $class->_searchByTest( $args, $selectField,
64 $class->_compareTest( $op, $value ), @fields );
65 31 };
66 31 ▶ return $scan->() unless ( $class->_fieldIsIndexed( $args, $selectField ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_fieldIsIndexed($args, $selectField) 13 18 31
67 return $class->_searchQuery(
68 $op,
69 18 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 18 my $query =
71 $class->_buildCompareExpression(
72 $class->_quoteColumn( $class->_classDbh($args), $selectField ),
73 $op, $value );
74 18 $class->_query( $args, $selectField, $value,
75 { query => $query, values => [] }, @fields );
76 },
77 18 $scan
78 );
79 }
80  
81 59 ▶sub _query {
Subroutine _query: called CC=7 SCAR=19.5
82 59 my ( $class, $args, $selectField, $value, $query, @fields ) = @_;
83 59 my %res = ();
84 my $index =
85 ref( $args->{Index} )
86 ? $args->{Index}
87 59 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 6 53 59
88  
89 59 my $dbh = $class->_classDbh($args);
90 59 my $table_name = $class->_tableName( $dbh, $args );
91  
92 # Case 1: all requested fields are also indexed
93 59 my $indexed = $class->_tabInTab( \@fields, $index );
94 59 my $sth;
95 59 ▶ if ($indexed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexed) { } 6 53 59
96 my $fields =
97 6 join( ',', 'id', map { $class->_quoteColumn( $dbh, $_ ) } @fields );
98 6 $sth = $dbh->prepare(
99 "SELECT $fields from $table_name where $query->{query}");
100 6 $sth->execute( @{ $query->{values} } );
101 6 return $sth->fetchall_hashref('id');
102 }
103  
104 # Case 1: at least one field isn't indexed, decoding is needed
105 else {
106 53 $sth =
107 $dbh->prepare(
108 "SELECT id,a_session from $table_name where $query->{query}");
109 51 $sth->execute( @{ $query->{values} } );
110 51 my $sub = $class->_unserializer;
111 51 ▶ while ( my @row = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $sth->fetchrow_array) 64 51 115
112 64 eval {
113 64 my $tmp = &$sub( { serialized => $row[1] } );
114 64 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 18 46 64
115 18 $res{ $row[0] }->{$_} = $tmp->{$_} foreach (@fields);
116 }
117 else {
118 46 $res{ $row[0] } = $tmp;
119 }
120 };
121 64 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 64 64
122 0 print STDERR "Error in session $row[0]: $@\n";
123 0 delete $res{ $row[0] };
124 }
125 }
126 }
127 51 return \%res;
128 }
129  
130 47 ▶sub deleteIfLowerThan {
Subroutine deleteIfLowerThan: called CC=22 SCAR=32.1
Pod undocumented
131 47 my ( $class, $args, $rule ) = @_;
132 47 ▶ return wantarray ? ( 0, 0 ) : 0 unless ( $class->_checkRule($rule) );
Branch: 2 branches
BranchTrueFalseTotal
unless $class->_checkRule($rule) 13 34 47
wantarray ? : 0 13 13
133 34 my ( $query, %fields, @bind );
134 34 my $dbh = eval { $class->_classDbh($args) };
135 34 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 34 34
136 0 print STDERR "deleteIfLowerThan: $@\n";
137 0 ▶ return wantarray ? ( 0, 0 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
138 }
139 my $index =
140 ref( $args->{Index} )
141 ? $args->{Index}
142 34 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 4 30 34
143 34 ▶ return wantarray ? ( 0, 0 ) : 0 unless ( $class->_checkThresholds($rule) );
Branch: 2 branches
BranchTrueFalseTotal
unless $class->_checkThresholds($rule) 14 20 34
wantarray ? : 0 14 14
144 20 ▶ if ( $rule->{or} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'or'}) { } 16 4 20
elsif ($rule->{'and'}) { } 3 1 4
145 $query = join ' OR ', map {
146 18 $fields{$_}++;
147 $class->_buildLowerThanExpression( $class->_quoteColumn( $dbh, $_ ),
148 18 $rule->{or}->{$_} )
149 }
150 16 keys %{ $rule->{or} };
151 }
152 elsif ( $rule->{and} ) {
153 $query = join ' AND ', map {
154 4 $fields{$_}++;
155 $class->_buildLowerThanExpression( $class->_quoteColumn( $dbh, $_ ),
156 4 $rule->{and}->{$_} )
157 }
158 3 keys %{ $rule->{and} };
159 }
160 20 ▶ return wantarray ? ( 0, 0 ) : 0 unless ($query);
Branch: 2 branches
BranchTrueFalseTotal
unless $query 1 19 20
wantarray ? : 0 1 1
161 19 ▶ if ( $rule->{not} and keys %{ $rule->{not} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"not"} and keys %{$$rule{"not"};}) 6 13 19
Condition100%3/3
$rule->{"not"} and keys %{$$rule{"not"};}
!ll&&!rl&&r
1216
MC/DC100%2/2
$rule->{"not"} and keys %{$$rule{"not"};}
$rule->{"not"} keys %{$$rule{"not"};}
Truth table100%3/3
A and B
A= $rule->{"not"}B= keys %{$$rule{"not"};}
ABresult
0X0
100
111
162 $query = "($query) AND " . join(
163 ' AND ',
164 map {
165 6 $fields{$_}++;
166 6 push @bind, $rule->{not}->{$_};
167 6 my $c = $class->_quoteColumn( $dbh, $_ );
168 6 "($c IS NULL OR $c <> ?)"
169 }
170 6 keys %{ $rule->{not} }
171 );
172 }
173 19 ▶ return wantarray ? ( 0, 0 ) : 0
Branch: 2 branches
BranchTrueFalseTotal
unless $class->_tabInTab([keys %fields], $index) 0 19 19
wantarray ? : 0 0 0
174 unless ( $class->_tabInTab( [ keys %fields ], $index ) );
175 19 my $table_name = $class->_tableName( $dbh, $args );
176 my $rows =
177 19 eval { $dbh->do( "DELETE FROM $table_name WHERE $query", undef, @bind ) };
178 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 17 19
179 2 print STDERR "deleteIfLowerThan: $@\n";
180 2 ▶ return wantarray ? ( 0, 0 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
181 }
182 17 ▶ return wantarray ? ( 0, 0 ) : 0 unless defined $rows;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $rows 0 17 17
wantarray ? : 0 0 0
183  
184 17 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 7 10 17
185 7 ▶ $rows = 0 if $rows == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $rows == -1 0 7 7
186 7 return ( 1, $rows );
187 }
188 else {
189 10 return 1;
190 }
191 }
192  
193 # Overriding this only changes deleteIfLowerThan(): searchLt() and searchGt()
194 # use _buildCompareExpression()
195 34 ▶sub _buildLowerThanExpression {
Subroutine _buildLowerThanExpression: called CC=1 SCAR=0.0
196 34 my ( $class, $field, $value, @args ) = @_;
197 34 return $class->_buildCompareExpression( $field, '<', $value, @args );
198 }
199  
200 # Let specialized modules override this syntax if they need to. $op is "<" or
201 # ">", $value a number checked by the caller
202 44 ▶sub _buildCompareExpression {
Subroutine _buildCompareExpression: called CC=1 SCAR=0.0
203 44 my ( $class, $field, $op, $value ) = @_;
204 44 $class->_checkOp($op);
205 40 return "cast($field as integer) $op $value";
206 }
207  
208 94 ▶sub get_key_from_all_sessions {
Subroutine get_key_from_all_sessions: called CC=8 SCAR=21.3
Pod undocumented
209 94 my $class = shift;
210 94 my $args = shift;
211 94 my $data = shift;
212  
213 94 my $dbh = $class->_classDbh($args);
214 94 my $table_name = $class->_tableName( $dbh, $args );
215  
216 # Special case if all wanted fields are indexed
217 94 ▶ if ( $data and ref($data) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data ne "CODE") 22 72 94
Condition100%3/3
$data and ref $data ne "CODE"
!ll&&!rl&&r
403222
MC/DC100%2/2
$data and ref $data ne "CODE"
$data ref $data ne "CODE"
Truth table100%3/3
A and B
A= $dataB= ref $data ne "CODE"
ABresult
0X0
100
111
218 22 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 1 21 22
219 my $index =
220 ref( $args->{Index} )
221 ? $args->{Index}
222 22 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 3 19 22
223  
224 # Test if one field isn't indexed
225 22 my $indexed = $class->_tabInTab( $data, $index );
226  
227 # OK, all fields are indexed
228 22 ▶ if ($indexed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexed) 5 17 22
229 my $sth =
230 $dbh->prepare_cached( 'SELECT id,'
231 5 . join( ',', map { $class->_quoteColumn( $dbh, $_ ) } @$data )
232 . " from $table_name" );
233 5 $sth->execute;
234 5 return $sth->fetchall_hashref('id');
235 }
236 }
237 89 my %res;
238 my $next = (
239 $args->{DataSource} =~ /^sybase/i
240 0 ▶ ? sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 require Storable;
242 0 return Storable::thaw( pack( 'H*', $_[0] ) );
243 }
244 0 ▶ : $args->{DataSource} =~ /^mysql/i ? sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 require MIME::Base64;
246 0 require Storable;
247 0 return Storable::thaw( MIME::Base64::decode_base64( $_[0] ) );
248 }
249 : undef
250 89 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$args->{'DataSource'} =~ /^sybase/i ? : 0 89 89
$args->{'DataSource'} =~ /^mysql/i ? : 0 89 89
251 89 my $sub = $class->_unserializer;
252 $class->_forEachSession(
253 $dbh,
254 $table_name,
255 293 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
256 293 my @row = @_;
257 293 eval {
258 293 my $tmp = &$sub( { serialized => $row[1] }, $next );
259 293 ▶ if ( ref($data) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'CODE') { } 153 140 293
elsif ($data) { } 44 96 140
260 153 $tmp = &$data( $tmp, $row[0] );
261 153 ▶ $res{ $row[0] } = $tmp if ( defined($tmp) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 28 125 153
262 }
263 elsif ($data) {
264 44 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 44 44
265 44 $res{ $row[0] }->{$_} = $tmp->{$_} foreach (@$data);
266 }
267 else {
268 96 $res{ $row[0] } = $tmp;
269 }
270 };
271 293 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 293 293
272 0 print STDERR "Error in session $row[0]: $@\n";
273 0 delete $res{ $row[0] };
274 }
275 }
276 89 );
277 87 return \%res;
278 }
279  
280 244 ▶sub _classDbh {
Subroutine _classDbh: called CC=6 SCAR=19.2
281 244 my $class = shift;
282 244 my $args = shift;
283  
284 244 ▶ my $datasource = $args->{DataSource} or die "No datasource given !";
Branch: 1 branch
BranchTrueFalseTotal
my $datasource = $args->{"DataSource"} or die "No datasource given !" 244 0 244
285 244 my $username = $args->{UserName};
286 244 my $password = $args->{Password};
287 244 ▶ my $dbh =
Condition50%1/2
'DBI'->connect_cached($datasource, $username, $password, 'Web::Session::_common'->_forkSafeAttr({'RaiseError', 1, 'AutoCommit', 1})) || die($DBI::errstr)
l!l
2440
MC/DC0%0/1
'DBI'->connect_cached($datasource, $username, $password, 'Web::Session::_common'->_forkSafeAttr({'RaiseError', 1, 'AutoCommit', 1})) || die($DBI::errstr)
'DBI'->connect_cached($datasource, $username, $password, 'Web::Session::_common'->_forkSafeAttr({'RaiseError', 1, 'AutoCommit', 1}))
Truth table50%1/2
A
A= 'DBI'->connect_cached($datasource, $username, $password, 'Web::Session::_common'->_forkSafeAttr({'RaiseError', 1, 'AutoCommit', 1}))
Aresult
11
00
288 DBI->connect_cached( $datasource, $username, $password,
289 Web::Session::_common->_forkSafeAttr(
290 { RaiseError => 1, AutoCommit => 1 }
291 )
292 )
293 || die $DBI::errstr;
294 244 ▶ if ( $datasource =~ /^dbi:sqlite/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($datasource =~ /^dbi:sqlite/i) { } 242 2 244
elsif ($datasource =~ /^dbi:mysql/i) { } 0 2 2
elsif ($datasource =~ /^dbi:pg/i) { } 0 2 2
295 242 $dbh->{sqlite_unicode} = 1;
296 }
297 elsif ( $datasource =~ /^dbi:mysql/i ) {
298 0 $dbh->{mysql_enable_utf8} = 1;
299 }
300 elsif ( $datasource =~ /^dbi:pg/i ) {
301 0 $dbh->{pg_enable_utf8} = 1;
302 }
303 244 return $dbh;
304 }
305  
306 21 1;
307  
« Session/LDAP.pm ↑ Summary Session/PgJSON.pm »