Browseable/DBI.pm

filter: Statementstmt 72.2% 104 / 144 Branchbran 50.0% 31 / 62 Conditioncond 35.2% 6 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 4 totaltotal 60.8% 154 / 253 CC 40 SCAR 35.6
CC
40
Cov
63%
deleteIfLowerThan
132.0
get_key_from_all_sessions
18.8
_classDbh
8.8
CRAP 35.0
« Browseable/PgHstore.pm ↑ Summary Browseable/Store/Cassandra.pm »
1 package Apache::Session::Browseable::DBI;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Apache::Session::Browseable::_common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '1.3.9';
10 4 our @ISA = qw(Apache::Session Apache::Session::Browseable::_common);
11  
12 6 ▶sub searchOn {
Subroutine searchOn: called CC=2 SCAR=6.9
Pod undocumented
13 6 my $class = shift;
14 6 my ( $args, $selectField, $value, @fields ) = @_;
15  
16 # Escape quotes
17 6 $selectField =~ s/'/''/g;
18 6 ▶ if ( $class->_fieldIsIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_fieldIsIndexed($args, $selectField)) { } 4 2 6
19 4 return $class->_query( $args, $selectField, $value,
20 { query => "$selectField=?", values => [$value] }, @fields );
21 }
22 else {
23 2 return $class->SUPER::searchOn(@_);
24 }
25 }
26  
27 2 ▶sub searchOnExpr {
Subroutine searchOnExpr: called CC=2 SCAR=6.9
Pod undocumented
28 2 my $class = shift;
29 2 my ( $args, $selectField, $value, @fields ) = @_;
30  
31 # Escape quotes
32 2 $value =~ s/'/''/g;
33 2 $selectField =~ s/'/''/g;
34 2 ▶ if ( $class->_fieldIsIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_fieldIsIndexed($args, $selectField)) { } 1 1 2
35 1 $value =~ s/\*/%/g;
36 1 return $class->_query( $args, $selectField, $value,
37 { query => "$selectField like ?", values => [$value] }, @fields );
38 }
39 else {
40 1 return $class->SUPER::searchOnExpr(@_);
41 }
42 }
43  
44 5 ▶sub _query {
Subroutine _query: called CC=8 SCAR=21.0
45 5 my ( $class, $args, $selectField, $value, $query, @fields ) = @_;
46 5 my %res = ();
47 my $index =
48 ref( $args->{Index} )
49 ? $args->{Index}
50 5 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 5 5
51  
52 5 my $dbh = $class->_classDbh($args);
53 my $table_name = $args->{TableName}
54 5 ▶ || $Apache::Session::Store::DBI::TableName;
Condition33%1/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
050
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table33%1/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
55  
56 # Case 1: all requested fields are also indexed
57 5 my $indexed = $class->_tabInTab( \@fields, $index );
58 5 my $sth;
59 5 ▶ if ($indexed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexed) { } 1 4 5
60 1 my $fields = join( ',', 'id', map { s/'//g; $_ } @fields );
61 1 $sth = $dbh->prepare(
62 "SELECT $fields from $table_name where $query->{query}");
63 1 $sth->execute( @{ $query->{values} } );
64 1 return $sth->fetchall_hashref('id');
65 }
66  
67 # Case 1: at least one field isn't indexed, decoding is needed
68 else {
69 4 $sth =
70 $dbh->prepare(
71 "SELECT id,a_session from $table_name where $query->{query}");
72 4 $sth->execute( @{ $query->{values} } );
73 4 ▶ while ( my @row = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $sth->fetchrow_array) 7 4 11
74 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 7 my $self = eval "&${class}::populate();";
76 7 my $sub = $self->{unserialize};
77 7 eval {
78 7 my $tmp = &$sub( { serialized => $row[1] } );
79 7 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 2 5 7
80 2 $res{ $row[0] }->{$_} = $tmp->{$_} foreach (@fields);
81 }
82 else {
83 5 $res{ $row[0] } = $tmp;
84 }
85 };
86 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
87 0 print STDERR "Error in session $row[0]: $@\n";
88 0 delete $res{ $row[0] };
89 }
90 }
91 }
92 4 return \%res;
93 }
94  
95 0 ▶sub deleteIfLowerThan {
Subroutine deleteIfLowerThan: not called CC=11 SCAR=48.8
Pod undocumented
96 0 my ( $class, $args, $rule ) = @_;
97 0 my ( $query, %fields );
98 my $index =
99 ref( $args->{Index} )
100 ? $args->{Index}
101 0 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 0 0
102 0 ▶ if ( $rule->{or} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'or'}) { } 0 0 0
elsif ($rule->{'and'}) { } 0 0 0
103 $query = join ' OR ', map {
104 0 $fields{$_}++;
105 0 $class->_buildLowerThanExpression( $_, $rule->{or}->{$_} )
106 }
107 0 keys %{ $rule->{or} };
108 }
109 elsif ( $rule->{and} ) {
110 $query = join ' AND ', map {
111 0 $fields{$_}++;
112 0 $class->_buildLowerThanExpression( $_, $rule->{and}->{$_} )
113 }
114 0 keys %{ $rule->{and} };
115 }
116 0 ▶ if ( $rule->{not} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"not"}) 0 0 0
117 $query = "($query) AND " . join(
118 ' AND ',
119 map {
120 0 $rule->{not}->{$_} =~ s/'/''/g;
121 0 $fields{$_}++;
122 0 "$_ <> '$rule->{not}->{$_}'"
123 }
124 0 keys %{ $rule->{not} }
125 );
126 }
127 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $query and $class->_tabInTab([keys %fields], $index) 0 0 0
Condition0%0/3
$query and $class->_tabInTab([keys %fields], $index)
!ll&&!rl&&r
000
MC/DC0%0/2
$query and $class->_tabInTab([keys %fields], $index)
$query $class->_tabInTab([keys %fields], $index)
Truth table0%0/3
A and B
A= $queryB= $class->_tabInTab([keys %fields], $index)
ABresult
0X0
100
111
128 unless ( $query and $class->_tabInTab( [ keys %fields ], $index ) );
129 0 my $dbh = $class->_classDbh($args);
130 my $table_name = $args->{TableName}
131 0 ▶ || $Apache::Session::Store::DBI::TableName;
Condition0%0/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table0%0/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
132 0 my $rows = $dbh->do("DELETE FROM $table_name WHERE $query");
133 0 ▶ return 0 unless defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows 0 0 0
134  
135 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
136 0 ▶ $rows = 0 if $rows == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $rows == -1 0 0 0
137 0 return ( 1, $rows );
138 }
139 else {
140 0 return 1;
141 }
142 }
143  
144 # Let specialized modules override this syntax if they need to
145 0 ▶sub _buildLowerThanExpression {
Subroutine _buildLowerThanExpression: not called CC=1 SCAR=6.9
146 0 my ( $class, $field, $value ) = @_;
147 0 return "cast($field as integer) < $value";
148 }
149  
150 6 ▶sub get_key_from_all_sessions {
Subroutine get_key_from_all_sessions: called CC=16 SCAR=29.3
Pod undocumented
151 6 my $class = shift;
152 6 my $args = shift;
153 6 my $data = shift;
154  
155 my $table_name = $args->{TableName}
156 6 ▶ || $Apache::Session::Store::DBI::TableName;
Condition33%1/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
060
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table33%1/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
157 6 my $dbh = $class->_classDbh($args);
158  
159 # Special case if all wanted fields are indexed
160 6 ▶ if ( $data and ref($data) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data ne "CODE") 2 4 6
Condition100%3/3
$data and ref $data ne "CODE"
!ll&&!rl&&r
132
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
161 2 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 2 2
162 my $index =
163 ref( $args->{Index} )
164 ? $args->{Index}
165 2 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 2 2
166  
167 # Test if one field isn't indexed
168 2 my $indexed = $class->_tabInTab( $data, $index );
169  
170 # OK, all fields are indexed
171 2 ▶ if ($indexed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexed) 1 1 2
172 my $sth =
173 $dbh->prepare_cached( 'SELECT id,'
174 1 . join( ',', map { s/'/''/g; $_ } @$data )
175 . " from $table_name" );
176 1 $sth->execute;
177 1 return $sth->fetchall_hashref('id');
178 }
179 }
180 5 my $sth = $dbh->prepare_cached("SELECT id,a_session from $table_name");
181 5 $sth->execute;
182 5 my %res;
183 my $next = (
184 $args->{DataSource} =~ /^sybase/i
185 0 ▶ ? sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 0 require Storable;
187 0 return Storable::thaw( pack( 'H*', $_[0] ) );
188 }
189 0 ▶ : $args->{DataSource} =~ /^mysql/i ? sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 require MIME::Base64;
191 0 require Storable;
192 0 return Storable::thaw( MIME::Base64::decode_base64( $_[0] ) );
193 }
194 : undef
195 5 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$args->{'DataSource'} =~ /^sybase/i ? : 0 5 5
$args->{'DataSource'} =~ /^mysql/i ? : 0 5 5
196 5 ▶ while ( my @row = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $sth->fetchrow_array) 140 5 145
197 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
198 140 my $self = eval "&${class}::populate();";
199 140 eval {
200 140 my $sub = $self->{unserialize};
201 140 my $tmp = &$sub( { serialized => $row[1] }, $next );
202 140 ▶ if ( ref($data) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'CODE') { } 84 56 140
elsif ($data) { } 28 28 56
203 84 $tmp = &$data( $tmp, $row[0] );
204 84 ▶ $res{ $row[0] } = $tmp if ( defined($tmp) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 0 84 84
205 }
206 elsif ($data) {
207 28 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 28 28
208 28 $res{ $row[0] }->{$_} = $tmp->{$_} foreach (@$data);
209 }
210 else {
211 28 $res{ $row[0] } = $tmp;
212 }
213 };
214 140 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 140 140
215 0 print STDERR "Error in session $row[0]: $@\n";
216 0 delete $res{ $row[0] };
217 }
218 }
219 5 return \%res;
220 }
221  
222 11 ▶sub _classDbh {
Subroutine _classDbh: called CC=6 SCAR=21.8
223 11 my $class = shift;
224 11 my $args = shift;
225  
226 11 ▶ my $datasource = $args->{DataSource} or die "No datasource given !";
Branch: 1 branch
BranchTrueFalseTotal
my $datasource = $args->{"DataSource"} or die "No datasource given !" 11 0 11
227 11 my $username = $args->{UserName};
228 11 my $password = $args->{Password};
229 11 ▶ my $dbh =
Condition50%1/2
'DBI'->connect_cached($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1}) || die($DBI::errstr)
l!l
110
MC/DC0%0/1
'DBI'->connect_cached($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1}) || die($DBI::errstr)
'DBI'->connect_cached($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1})
Truth table50%1/2
A
A= 'DBI'->connect_cached($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1})
Aresult
11
00
230 DBI->connect_cached( $datasource, $username, $password,
231 { RaiseError => 1, AutoCommit => 1 } )
232 || die $DBI::errstr;
233 11 ▶ if ( $datasource =~ /^dbi:sqlite/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($datasource =~ /^dbi:sqlite/i) { } 11 0 11
elsif ($datasource =~ /^dbi:mysql/i) { } 0 0 0
elsif ($datasource =~ /^dbi:pg/i) { } 0 0 0
234 11 $dbh->{sqlite_unicode} = 1;
235 }
236 elsif ( $datasource =~ /^dbi:mysql/i ) {
237 0 $dbh->{mysql_enable_utf8} = 1;
238 }
239 elsif ( $datasource =~ /^dbi:pg/i ) {
240 0 $dbh->{pg_enable_utf8} = 1;
241 }
242 11 return $dbh;
243 }
244  
245 4 1;
246  
« Browseable/PgHstore.pm ↑ Summary Browseable/Store/Cassandra.pm »