Session/MySQLJSON.pm

filter: Statementstmt 42.3% 80 / 189 Branchbran 22.3% 17 / 76 Conditioncond 34.6% 9 / 26 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 69.2% 18 / 26 Podpod 0.0% 0 / 5 totaltotal 37.6% 127 / 337 CC 52 SCAR 45.4
CC
52
Cov
36%
deleteIfLowerThan
342.0
_query
56.0
__ANON__
56.0
CRAP 93.8
« Session/Redis.pm ↑ Summary Session/Store/Redis.pm »
1 package Web::Session::MySQLJSON;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Apache::Session::Lock::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Web::Session::Store::MySQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Web::Session::Generate::SHA256;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Web::Session::Serialize::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Web::Session::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Web::Session::_common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 our $VERSION = '1.0.0';
15 9 our @ISA = qw(Web::Session::DBI Apache::Session);
16  
17 9 ▶sub populate {
Subroutine populate: called CC=6 SCAR=17.9
Pod undocumented
18 9 my $self = shift;
19  
20 # Any field can be searched from a_session: setting Index would make the
21 # store write the indexed fields into columns of the same name, which
22 # don't exist in a JSON table
23 9 ▶ if ( $self and $self->{args} and defined $self->{args}->{Index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $self->{"args"} and defined $self->{"args"}{"Index"}) 4 5 9
Condition83%5/6
$self and $self->{"args"} and defined $self->{"args"}{"Index"}
!ll&&!rl&&r
414
$self and $self->{"args"}
!ll&&!rl&&r
405
MC/DC66%2/3
$self and $self->{"args"} and defined $self->{"args"}{"Index"}
$self $self->{"args"} defined $self->{"args"}{"Index"}
Truth table75%3/4
A and B and C
A= $selfB= $self->{"args"}C= defined $self->{"args"}{"Index"}
ABCresult
0XX0
10X0
1100
1111
24 my $index =
25 ref( $self->{args}->{Index} )
26 ? $self->{args}->{Index}
27 4 ▶ : [ split /\s+/, $self->{args}->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'args'}{'Index'} ? : 1 3 4
28 4 ▶ warn "Web::Session::MySQLJSON: Index should not be set,"
Branch: 1 branch
BranchTrueFalseTotal
if @$index 2 2 4
29 . " the store would write the fields as columns\n"
30 if (@$index);
31 }
32  
33 9 $self->{object_store} = new Web::Session::Store::MySQL $self;
34 9 $self->{lock_manager} = new Apache::Session::Lock::Null $self;
35 9 $self->{generate} = \&Web::Session::Generate::SHA256::generate;
36 9 $self->{validate} = \&Web::Session::Generate::SHA256::validate;
37 9 $self->{serialize} = \&Web::Session::Serialize::JSON::serialize;
38 9 $self->{unserialize} = \&Web::Session::Serialize::JSON::unserialize;
39  
40 9 return $self;
41 }
42  
43 0 ▶sub searchOn {
Subroutine searchOn: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my ( $class, $args, $selectField, $value, @fields ) = @_;
45 0 my $dbh = $class->_classDbh($args);
46 0 my $query = {
47 query => $class->_sqlField( $dbh, $selectField, $args ) . ' =?',
48 values => [$value]
49 };
50 0 return $class->_query( $args, $query, @fields );
51 }
52  
53 0 ▶sub searchOnExpr {
Subroutine searchOnExpr: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my ( $class, $args, $selectField, $value, @fields ) = @_;
55 0 my $dbh = $class->_classDbh($args);
56 0 $value =~ s/\*/%/g;
57 0 my $query = {
58 query => $class->_sqlField( $dbh, $selectField, $args ) . ' like ?',
59 values => [$value]
60 };
61 0 return $class->_query( $args, $query, @fields );
62 }
63  
64 # searchLt() and searchGt() are inherited from DBI.pm: any field can be
65 # compared in SQL here
66 84 ▶sub _searchCompare {
Subroutine _searchCompare: called CC=2 SCAR=6.9
67 84 my ( $class, $op, $args, $selectField, $value, @fields ) = @_;
68 84 $value = $class->_checkSearchValue( $op, $value );
69 84 ▶ return {} unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 48 36 84
70 return $class->_searchQuery(
71 $op,
72 36 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 36 my $query = {
74 query => $class->_buildCompareExpression(
75 $selectField, $op, $value, $class->_classDbh($args), $args
76 ),
77 values => []
78 };
79 36 $class->_query( $args, $query, @fields );
80 },
81 18 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 18 $class->_searchByTest( $args, $selectField,
83 $class->_compareTest( $op, $value ), @fields );
84 }
85 36 );
86 }
87  
88 0 ▶sub _query {
Subroutine _query: not called CC=7 SCAR=40.3
89 0 my ( $class, $args, $query, @fields ) = @_;
90 0 my %res = ();
91 my $index =
92 ref( $args->{Index} )
93 ? $args->{Index}
94 0 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 0 0
95  
96 0 my $dbh = $class->_classDbh($args);
97 0 my $table_name = $class->_tableName( $dbh, $args );
98  
99 0 my $sth;
100 0 my ( $select, $aliases ) = $class->_sqlSelect( $dbh, \@fields, $args );
101 0 ▶ my $fields = @fields ? join( ',', 'id', @$select ) : 'id,a_session';
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 0 0 0
102 0 $sth =
103 $dbh->prepare("SELECT $fields from $table_name where $query->{query}");
104 0 $sth->execute( $class->_utf8( @{ $query->{values} } ) );
105  
106 0 ▶ my $res = $sth->fetchall_hashref('id') or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $res = $sth->fetchall_hashref("id") or return {} 0 0 0
107 0 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
108 0 $class->_renameAliases( $res, $aliases );
109 }
110 else {
111 0 my $sub = $class->_unserializer;
112 0 foreach my $s ( keys %$res ) {
113 0 eval {
114 0 my $tmp = &$sub( { serialized => $res->{$s}->{a_session} } );
115 0 $res->{$s} = $tmp;
116 };
117 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
118 0 print STDERR "Error in session $s: $@\n";
119 0 delete $res->{$s};
120 }
121 }
122 }
123 0 return $res;
124 }
125  
126 0 ▶sub deleteIfLowerThan {
Subroutine deleteIfLowerThan: not called CC=18 SCAR=58.3
Pod undocumented
127 0 my ( $class, $args, $rule ) = @_;
128 0 ▶ return wantarray ? ( 0, 0 ) : 0
Branch: 2 branches
BranchTrueFalseTotal
unless $class->_checkRule($rule) and $class->_checkThresholds($rule) 0 0 0
wantarray ? : 0 0 0
Condition0%0/3
$class->_checkRule($rule) and $class->_checkThresholds($rule)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->_checkRule($rule) and $class->_checkThresholds($rule)
$class->_checkRule($rule) $class->_checkThresholds($rule)
Truth table0%0/3
A and B
A= $class->_checkRule($rule)B= $class->_checkThresholds($rule)
ABresult
0X0
100
111
129 unless ( $class->_checkRule($rule) and $class->_checkThresholds($rule) );
130 0 my ( $query, @bind );
131 0 my $dbh = eval { $class->_classDbh($args) };
132 0 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
133 0 print STDERR "deleteIfLowerThan: $@\n";
134 0 ▶ return wantarray ? ( 0, 0 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
135 }
136 0 ▶ if ( $rule->{or} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'or'}) { } 0 0 0
elsif ($rule->{'and'}) { } 0 0 0
137 $query = join ' OR ', map {
138 0 $class->_buildLowerThanExpression( $_, $rule->{or}->{$_}, $dbh, $args )
139 }
140 0 keys %{ $rule->{or} };
141 }
142 elsif ( $rule->{and} ) {
143 $query = join ' AND ', map {
144 0 $class->_buildLowerThanExpression( $_, $rule->{and}->{$_}, $dbh, $args )
145 }
146 0 keys %{ $rule->{and} };
147 }
148 0 ▶ return wantarray ? ( 0, 0 ) : 0 unless ($query);
Branch: 2 branches
BranchTrueFalseTotal
unless $query 0 0 0
wantarray ? : 0 0 0
149 0 ▶ if ( $rule->{not} and keys %{ $rule->{not} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"not"} and keys %{$$rule{"not"};}) 0 0 0
Condition0%0/3
$rule->{"not"} and keys %{$$rule{"not"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$rule->{"not"} and keys %{$$rule{"not"};}
$rule->{"not"} keys %{$$rule{"not"};}
Truth table0%0/3
A and B
A= $rule->{"not"}B= keys %{$$rule{"not"};}
ABresult
0X0
100
111
150 $query = "($query) AND " . join(
151 ' AND ',
152 map {
153 0 push @bind, $rule->{not}->{$_};
154 0 my $f = $class->_sqlField( $dbh, $_, $args );
155 0 "($f IS NULL OR $f <> ?)"
156 }
157 0 keys %{ $rule->{not} }
158 );
159 }
160 0 my $table_name = $class->_tableName( $dbh, $args );
161 my $rows =
162 0 eval { $dbh->do( "DELETE FROM $table_name WHERE $query", undef,
163 $class->_utf8(@bind) ) };
164 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
165 0 print STDERR "deleteIfLowerThan: $@\n";
166 0 ▶ return wantarray ? ( 0, 0 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
167 }
168 0 ▶ return wantarray ? ( 0, 0 ) : 0 unless defined $rows;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $rows 0 0 0
wantarray ? : 0 0 0
169  
170 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
171 0 ▶ $rows = 0 if $rows == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $rows == -1 0 0 0
172 0 return ( 1, $rows );
173 }
174 else {
175 0 return 1;
176 }
177 }
178  
179 0 ▶sub get_key_from_all_sessions {
Subroutine get_key_from_all_sessions: not called CC=4 SCAR=30.0
Pod undocumented
180 0 my ( $class, $args, $data ) = @_;
181  
182 0 my $dbh = $class->_classDbh($args);
183 0 my $table_name = $class->_tableName( $dbh, $args );
184 0 my $sth;
185  
186 # Special case if all wanted fields are indexed
187 0 ▶ if ( $data and ref($data) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data ne "CODE") 0 0 0
Condition0%0/3
$data and ref $data ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$data and ref $data ne "CODE"
$data ref $data ne "CODE"
Truth table0%0/3
A and B
A= $dataB= ref $data ne "CODE"
ABresult
0X0
100
111
188 0 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
189 0 my ( $select, $aliases ) = $class->_sqlSelect( $dbh, $data, $args );
190 0 my $fields = join ',', 'id', @$select;
191 0 $sth = $dbh->prepare("SELECT $fields from $table_name");
192 0 $sth->execute;
193 0 my $res = $sth->fetchall_hashref('id');
194 0 $class->_renameAliases( $res, $aliases );
195 0 return $res;
196 }
197 0 my %res;
198 0 my $sub = $class->_unserializer;
199 $class->_forEachSession(
200 $dbh,
201 $table_name,
202 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
203 0 my @row = @_;
204 0 eval {
205 0 my $tmp = &$sub( { serialized => $row[1] } );
206 0 ▶ if ( ref($data) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'CODE') { } 0 0 0
elsif ($data) { } 0 0 0
207 0 $tmp = &$data( $tmp, $row[0] );
208 0 ▶ $res{ $row[0] } = $tmp if ( defined($tmp) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 0 0 0
209 }
210 elsif ($data) {
211 0 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
212 0 $res{ $row[0] }->{$_} = $tmp->{$_} foreach (@$data);
213 }
214 else {
215 0 $res{ $row[0] } = $tmp;
216 }
217 };
218 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
219 0 print STDERR "Error in session $row[0]: $@\n";
220 0 delete $res{ $row[0] };
221 }
222 }
223 0 );
224 0 return \%res;
225 }
226  
227 # Same arguments as in DBI.pm plus the database handle (needed to quote the
228 # JSON path) and connection arguments. The handle is optional: without one,
229 # _sqlPath() quotes the path. The expression must match the documented
230 # generated columns
231 49 ▶sub _buildCompareExpression {
Subroutine _buildCompareExpression: called CC=1 SCAR=0.0
232 49 my ( $class, $field, $op, $value, $dbh, $args ) = @_;
233 49 $class->_checkOp($op);
234 41 my $f = $class->_sqlField( $dbh, $field, $args );
235 41 return "cast($f as UNSIGNED) $op $value";
236 }
237  
238 # Build SQL expression to get a field from a_session ($args: connection
239 # arguments, for subclasses)
240 23 ▶sub _sqlField {
Subroutine _sqlField: called CC=1 SCAR=0.0
241 23 my ( $class, $dbh, $field, $args ) = @_;
242 23 return 'a_session->>' . $class->_sqlPath( $dbh, $field );
243 }
244  
245 # Build the SQL literal of the JSON path of a field. Field name is used as
246 # JSON path member, quoted if needed. The database handle is only used to
247 # quote the path: it may be missing on the DBI deleteIfLowerThan() path
248 42 ▶sub _sqlPath {
Subroutine _sqlPath: called CC=3 SCAR=11.2
249 42 my ( $class, $dbh, $field ) = @_;
250 42 my $path;
251 42 ▶ if ( $field =~ /^[A-Za-z_][A-Za-z0-9_]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /^[A-Za-z_][A-Za-z0-9_]*\z/) { } 42 0 42
252 42 $path = "\$.$field";
253 }
254 else {
255 0 ( my $f = $field ) =~ s/(["\\])/\\$1/g;
256 0 $path = qq{\$."$f"};
257 }
258 42 ($path) = $class->_utf8($path);
259 42 ▶ return $dbh->quote($path) if ( defined $dbh );
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh 38 4 42
260  
261 # No database handle: quote the path here (MySQL escapes backslashes)
262 4 $path =~ s/\\/\\\\/g;
263 4 $path =~ s/'/''/g;
264 4 return "'$path'";
265 }
266  
267 # DBD::mysql sends strings as stored by Perl: store them in UTF-8 so that
268 # characters U+0080 to U+00FF are not sent in Latin-1. A non flagged string
269 # containing bytes >= 0x80 may be UTF-8 or Latin-1 encoded: try UTF-8 first
270 # (strictly), and fall back to Latin-1 (utf8::upgrade) only if it is not
271 # valid UTF-8
272 168 ▶sub _utf8 {
Subroutine _utf8: called CC=5 SCAR=16.3
273 168 my $class = shift;
274 return map {
275 168 my $s = $_;
276 213 ▶ if ( defined($s)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s and not utf8::is_utf8($s) and $s =~ /[\x80-\xff]/) 4 209 213
Condition50%3/6
defined $s and not utf8::is_utf8($s) and $s =~ /[\x80-\xff]/
!ll&&!rl&&r
02094
defined $s and not utf8::is_utf8($s)
!ll&&!rl&&r
00213
MC/DC33%1/3
defined $s and not utf8::is_utf8($s) and $s =~ /[\x80-\xff]/
defined $s not utf8::is_utf8($s) $s =~ /[\x80-\xff]/
Truth table50%2/4
A and not(B) and C
A= defined $sB= not utf8::is_utf8($s)C= $s =~ /[\x80-\xff]/
ABCresult
0XX0
10X0
1100
1111
277 and !utf8::is_utf8($s)
278 and $s =~ /[\x80-\xff]/ )
279 {
280 my $decoded =
281 4 eval { Encode::decode( 'UTF-8', $s, Encode::FB_CROAK ) };
282 4 ▶ if ( defined($decoded) ) { $s = $decoded }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded) { } 2 2 4
283 2 else { utf8::upgrade($s) }
284 }
285 $s
286 213 } @_;
287 }
288  
289 # Build the SELECT list for the given fields. Column aliases are generated
290 # (a field name may contain "?", which DBD::mysql takes for a placeholder even
291 # inside backquotes): returns the list and an alias => field name hash
292 0 ▶sub _sqlSelect {
Subroutine _sqlSelect: not called CC=2 SCAR=17.9
293 0 my ( $class, $dbh, $fields, $args ) = @_;
294 0 my ( @select, %aliases );
295 0 foreach my $field (@$fields) {
296 0 my $alias = 'f' . scalar(@select);
297 0 $aliases{$alias} = $field;
298 0 push @select, $class->_sqlField( $dbh, $field, $args ) . " AS `$alias`";
299 }
300 0 return ( \@select, \%aliases );
301 }
302  
303 # Replace generated aliases by field names in results
304 0 ▶sub _renameAliases {
Subroutine _renameAliases: not called CC=4 SCAR=30.0
305 0 my ( $class, $res, $aliases ) = @_;
306 0 ▶ return unless ( $res and %$aliases );
Branch: 1 branch
BranchTrueFalseTotal
unless $res and %$aliases 0 0 0
Condition0%0/3
$res and %$aliases
!ll&&!rl&&r
000
MC/DC0%0/2
$res and %$aliases
$res %$aliases
Truth table0%0/3
A and B
A= $resB= %$aliases
ABresult
0X0
100
111
307 0 foreach my $row ( values %$res ) {
308 %$row = (
309 0 ( map { $aliases->{$_} => $row->{$_} } keys %$aliases ),
310 id => $row->{id}
311 0 );
312 }
313 }
314  
315 2 ▶sub _classDbh {
Subroutine _classDbh: called CC=4 SCAR=14.3
316 2 my ( $class, $args ) = @_;
317  
318 2 ▶ my $datasource = $args->{DataSource} or die "No datasource given !";
Branch: 1 branch
BranchTrueFalseTotal
my $datasource = $args->{"DataSource"} or die "No datasource given !" 2 0 2
319 2 my $username = $args->{UserName};
320 2 my $password = $args->{Password};
321 2 ▶ my $dbh =
Condition50%1/2
'DBI'->connect_cached($datasource, $username, $password, 'Web::Session::_common'->_forkSafeAttr({'RaiseError', 1, 'AutoCommit', 1})) || die($DBI::errstr)
l!l
20
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
322 DBI->connect_cached( $datasource, $username, $password,
323 Web::Session::_common->_forkSafeAttr(
324 { RaiseError => 1, AutoCommit => 1 }
325 )
326 )
327 || die $DBI::errstr;
328  
329 # DBD::MariaDB always uses UTF-8
330 2 ▶ $dbh->{mysql_enable_utf8} = 1 if ( $dbh->{Driver}->{Name} eq 'mysql' );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Driver"}{"Name"} eq "mysql" 2 0 2
331 2 return $dbh;
332 }
333  
334 9 1;
335 __END__
« Session/Redis.pm ↑ Summary Session/Store/Redis.pm »