Session/PgJSON.pm

filter: Statementstmt 96.3% 187 / 194 Branchbran 76.3% 58 / 76 Conditioncond 89.1% 41 / 46 MC/DCmcdc 75.0% 18 / 24 Subroutinesub 100% 31 / 31 Podpod 57.1% 4 / 7 totaltotal 89.6% 339 / 378 CC 60 SCAR 22.0
CC
60
Cov
91%
deleteIfLowerThan
20.8
_searchOnQuery
15.0
__ANON__
8.0
CRAP 9.0
« Session/DBI.pm ↑ Summary Session/Store/Postgres.pm »
1 package Web::Session::PgJSON;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Apache::Session::Lock::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Web::Session::Store::Postgres;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Web::Session::_common;
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 JSON qw(to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 our $VERSION = '1.0.0';
14 9 our @ISA = qw(Apache::Session);
15  
16 50 ▶sub populate {
Subroutine populate: called CC=1 SCAR=0.0
Pod undocumented
17 50 my $self = shift;
18  
19 $self->{object_store} =
20 50 new Web::Session::Store::Postgres $self;
21 50 $self->{lock_manager} = new Apache::Session::Lock::Null $self;
22 50 $self->{generate} = \&Web::Session::Generate::SHA256::generate;
23 50 $self->{validate} = \&Web::Session::Generate::SHA256::validate;
24 50 $self->{serialize} = \&Web::Session::Serialize::JSON::serialize;
25 50 $self->{unserialize} = \&Web::Session::Serialize::JSON::unserialize;
26  
27 50 return $self;
28 }
29  
30 27 ▶sub searchOn {
Subroutine searchOn: called CC=3 SCAR=11.0
Pod documented
31 27 my ( $class, $args, $selectField, $value, @fields ) = @_;
32 27 my ( $dbh, $loan ) = $class->_borrowDbh($args);
33 27 ▶ if ( $args->{GinIndex} and !$class->_jsonbColumn( $args, $dbh ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"GinIndex"} and not $class->_jsonbColumn($args, $dbh)) 2 25 27
Condition100%3/3
$args->{"GinIndex"} and not $class->_jsonbColumn($args, $dbh)
!ll&&!rl&&r
2142
MC/DC100%2/2
$args->{"GinIndex"} and not $class->_jsonbColumn($args, $dbh)
$args->{"GinIndex"} not $class->_jsonbColumn($args, $dbh)
Truth table100%3/3
A and not(B)
A= $args->{"GinIndex"}B= not $class->_jsonbColumn($args, $dbh)
ABresult
0X0
100
111
34 2 $args = { %$args };
35 2 delete $args->{GinIndex};
36 }
37 27 return $class->_queryDbh( $dbh, $args,
38 $class->_searchOnQuery( $args, $selectField, $value ), @fields );
39 }
40  
41 # With GinIndex, add jsonb containment conditions that can use a GIN index
42 # on a_session. "->>" also returns the text of numbers and booleans, so a
43 # value that looks like one of them is searched in both forms. The "->>"
44 # comparison is kept: results are exactly the same as without GinIndex.
45 73 ▶sub _searchOnQuery {
Subroutine _searchOnQuery: called CC=15 SCAR=27.1
46 73 my ( $class, $args, $field, $value ) = @_;
47 73 my $f = $class->_sqlField($field);
48  
49 # "->>" returns arrays and objects as JSON text; jsonb rejects NUL
50 return { query => "$f =?", values => [$value] }
51 unless ( $args->{GinIndex}
52 73 ▶ and defined($value)
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"GinIndex"} and defined $value and not $value =~ /^[\[{]/ and not "$field$value" =~ /\0/ 32 41 73
Condition100%9/9
$args->{"GinIndex"} and defined $value and not $value =~ /^[\[{]/ and not "$field$value" =~ /\0/
!ll&&!rl&&r
30241
$args->{"GinIndex"} and defined $value and not $value =~ /^[\[{]/
!ll&&!rl&&r
28243
$args->{"GinIndex"} and defined $value
!ll&&!rl&&r
27145
MC/DC100%4/4
$args->{"GinIndex"} and defined $value and not $value =~ /^[\[{]/ and not "$field$value" =~ /\0/
$args->{"GinIndex"} defined $value not $value =~ /^[\[{]/ not "$field$value" =~ /\0/
Truth table100%5/5
A and B and not(C) and not(D)
A= $args->{"GinIndex"}B= defined $valueC= not $value =~ /^[\[{]/D= not "$field$value" =~ /\0/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
53 and $value !~ /^[\[{]/
54 and "$field$value" !~ /\0/ );
55  
56 # A caller may pass the UTF-8 bytes of a non-ASCII field or value without
57 # the UTF-8 flag: to_json() would then read them as Latin-1 and the JSON
58 # document wouldn't match the stored data. Decode copies, used only to
59 # build the documents: the "->>" recheck keeps the arguments as given,
60 # like the query used without GinIndex.
61 41 my ( $dfield, $dvalue ) = ( $field, $value );
62 41 for ( $dfield, $dvalue ) {
63 82 ▶ if ( !utf8::is_utf8($_) and /[\x80-\xff]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not utf8::is_utf8($_) and /[\x80-\xff]/) 6 76 82
Condition100%3/3
not utf8::is_utf8($_) and /[\x80-\xff]/
!ll&&!rl&&r
1756
MC/DC100%2/2
not utf8::is_utf8($_) and /[\x80-\xff]/
not utf8::is_utf8($_) /[\x80-\xff]/
Truth table100%3/3
not(A) and B
A= not utf8::is_utf8($_)B= /[\x80-\xff]/
ABresult
0X0
100
111
64 6 my $decoded = $_;
65 6 ▶ $_ = $decoded if ( utf8::decode($decoded) );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::decode($decoded) 3 3 6
66 }
67 }
68  
69 # Numbers beyond PostgreSQL numeric limits can't be stored in jsonb
70 41 ▶ my $literal = $value =~ /^(?:true|false)\z/
Condition100%11/11
$value =~ /^(?:true|false)\z/ || $value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/ && length $1 <= 131072 && length($2 // '') <= 16383
l!l&&r!l&&!r
2831
$value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/ and length $1 <= 131072 and length($2 // "") <= 16383
!ll&&!rl&&r
2928
$value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/ and length $1 <= 131072
!ll&&!rl&&r
27210
$2 // ""
l!l
55
MC/DC100%4/4
$value =~ /^(?:true|false)\z/ || $value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/ && length $1 <= 131072 && length($2 // '') <= 16383
$value =~ /^(?:true|false)\z/ $value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/ length $1 <= 131072 $2
Truth table100%5/5
A || B and C and D
A= $value =~ /^(?:true|false)\z/B= $value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/C= length $1 <= 131072D= $2
ABCDresult
1XXX1
01111
00XX0
010X0
01100
71 || ( $value =~ /^-?(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/
72 and length($1) <= 131072
73 and length( $2 // '' ) <= 16383 );
74 41 my @docs = ( to_json( { $dfield => "$dvalue" } ) );
75 41 ▶ push @docs, '{' . to_json( "$dfield", { allow_nonref => 1 } ) . ":$value}"
Branch: 1 branch
BranchTrueFalseTotal
if $literal 10 31 41
76 if ($literal);
77  
78 # JSON::PP doesn't flag its result as UTF-8 when it holds only Latin-1
79 # characters: DBD::Pg would then send Latin-1 bytes
80 41 utf8::upgrade($_) foreach (@docs);
81 return {
82 41 query => '('
83 . join( ' OR ', ('a_session @> ?::jsonb') x @docs )
84 . ") AND $f =?",
85 values => [ @docs, $value ],
86 };
87 }
88  
89 # The "a_session @> ..." conditions need a jsonb column: with a json column
90 # every searchOn() would fail. Look the type up once per handle and table,
91 # warn and fall back to the plain query when it is not jsonb. An unknown type
92 # (table not created yet) isn't cached. $dbh (required) is the handle of the
93 # caller's query, from _borrowDbh(): the caller keeps the loan until its query
94 # is done.
95 6 ▶sub _jsonbColumn {
Subroutine _jsonbColumn: called CC=7 SCAR=19.5
96 6 my ( $class, $args, $dbh ) = @_;
97 6 ▶ my $table = $args->{TableName} || $Apache::Session::Store::DBI::TableName;
Condition66%2/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
510
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table66%2/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
98 6 ▶ my $cache = $dbh->{private_asb_pgjson_type} ||= {};
Condition100%2/2
$dbh->{"private_asb_pgjson_type"} ||= {}
l!l
24
MC/DC100%1/1
$dbh->{"private_asb_pgjson_type"} ||= {}
$dbh->{"private_asb_pgjson_type"}
Truth table100%2/2
A
A= $dbh->{"private_asb_pgjson_type"}
Aresult
11
00
99 6 ▶ unless ( defined $cache->{$table} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache->{$table}) 5 1 6
100 5 ( $cache->{$table} ) = $dbh->selectrow_array(
101 q{SELECT t.typname FROM pg_attribute a
102 JOIN pg_type t ON t.oid = a.atttypid
103 WHERE a.attrelid = to_regclass(?) AND a.attname = 'a_session'
104 AND NOT a.attisdropped},
105 undef, $table
106 );
107 warn "GinIndex ignored: $table.a_session is not jsonb\n"
108 5 ▶ unless ( $cache->{$table} || '' ) eq 'jsonb';
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache->{$table} || "") eq "jsonb" 2 3 5
Condition100%2/2
$cache->{$table} || ''
l!l
41
MC/DC100%1/1
$cache->{$table} || ''
$cache->{$table}
Truth table100%2/2
A
A= $cache->{$table}
Aresult
11
00
109 }
110 6 ▶ return ( $cache->{$table} || '' ) eq 'jsonb';
Condition100%2/2
$cache->{$table} || ''
l!l
51
MC/DC100%1/1
$cache->{$table} || ''
$cache->{$table}
Truth table100%2/2
A
A= $cache->{$table}
Aresult
11
00
111 }
112  
113 2 ▶sub searchOnExpr {
Subroutine searchOnExpr: called CC=1 SCAR=0.0
Pod undocumented
114 2 my ( $class, $args, $selectField, $value, @fields ) = @_;
115 2 $value =~ s/\*/%/g;
116 2 my $query = {
117 query => $class->_sqlField($selectField) . ' like ?',
118 values => [$value]
119 };
120 2 return $class->_query( $args, $query, @fields );
121 }
122  
123 35 ▶sub searchLt {
Subroutine searchLt: called CC=1 SCAR=0.0
Pod documented
124 35 my $class = shift;
125 35 return $class->_searchCompare( '<', @_ );
126 }
127  
128 11 ▶sub searchGt {
Subroutine searchGt: called CC=1 SCAR=0.0
Pod documented
129 11 my $class = shift;
130 11 return $class->_searchCompare( '>', @_ );
131 }
132  
133 # $value is checked, then inserted like deleteIfLowerThan() thresholds: bound,
134 # it would be typed bigint and decimal values would fail
135 46 ▶sub _searchCompare {
Subroutine _searchCompare: called CC=2 SCAR=6.9
136 46 my ( $class, $op, $args, $selectField, $value, @fields ) = @_;
137 46 $value =
138 Web::Session::_common->_checkSearchValue( $op, $value );
139 46 ▶ return {} unless ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 24 22 46
140 22 my $query = {
141 query => $class->_buildCompareExpression( $selectField, $op, $value ),
142 values => []
143 };
144 return Web::Session::_common->_searchQuery(
145 $op,
146 22 ▶ sub { $class->_query( $args, $query, @fields ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
147 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 9 Web::Session::_common::_searchByTest( $class, $args, $selectField,
149 Web::Session::_common->_compareTest( $op, $value ), @fields );
150 }
151 22 );
152 }
153  
154 6 ▶sub _query {
Subroutine _query: called CC=1 SCAR=0.0
155 6 my ( $class, $args, $query, @fields ) = @_;
156 6 my ( $dbh, $loan ) = $class->_borrowDbh($args);
157 6 return $class->_queryDbh( $dbh, $args, $query, @fields );
158 }
159  
160 # Same as _query() with an already connected handle
161 29 ▶sub _queryDbh {
Subroutine _queryDbh: called CC=6 SCAR=18.1
162 29 my ( $class, $dbh, $args, $query, @fields ) = @_;
163 29 my %res = ();
164  
165 29 my $table_name =
166 Web::Session::_common->_tableName( $dbh, $args );
167  
168 29 my $sth;
169 my $fields =
170 @fields
171 ? join( ',',
172 'id',
173 29 ▶ map { $class->_sqlField($_) . ' AS ' . $class->_sqlAlias($_) } @fields )
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 2 27 29
174 : '*';
175 29 $sth =
176 $dbh->prepare("SELECT $fields from $table_name where $query->{query}");
177 29 $sth->execute( @{ $query->{values} } );
178  
179 27 ▶ my $res = $sth->fetchall_hashref('id') or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $res = $sth->fetchall_hashref("id") or return {} 27 0 27
180 27 ▶ unless (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fields) 25 2 27
181 25 my $sub = Web::Session::_common::_unserializer($class);
182 25 foreach my $s ( keys %$res ) {
183 49 eval {
184 49 my $tmp = &$sub( { serialized => $res->{$s}->{a_session} } );
185 49 $res->{$s} = $tmp;
186 };
187 49 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 49 49
188 0 print STDERR "Error in session $s: $@\n";
189 0 delete $res->{$s};
190 }
191 }
192 }
193 27 return $res;
194 }
195  
196 16 ▶sub deleteIfLowerThan {
Subroutine deleteIfLowerThan: called CC=18 SCAR=30.4
Pod documented
197 16 my ( $class, $args, $rule ) = @_;
198 16 ▶ return wantarray ? ( 0, 0 ) : 0
Branch: 2 branches
BranchTrueFalseTotal
unless "Web::Session::_common"->_checkRule($rule) and "Web::Session::_common"->_checkThresholds($rule) 0 16 16
wantarray ? : 0 0 0
Condition33%1/3
"Web::Session::_common"->_checkRule($rule) and "Web::Session::_common"->_checkThresholds($rule)
!ll&&!rl&&r
0016
MC/DC0%0/2
"Web::Session::_common"->_checkRule($rule) and "Web::Session::_common"->_checkThresholds($rule)
"Web::Session::_common"->_checkRule($rule) "Web::Session::_common"->_checkThresholds($rule)
Truth table33%1/3
A and B
A= "Web::Session::_common"->_checkRule($rule)B= "Web::Session::_common"->_checkThresholds($rule)
ABresult
0X0
100
111
199 unless ( Web::Session::_common->_checkRule($rule)
200 and Web::Session::_common->_checkThresholds($rule) );
201 16 my ( $query, @bind );
202 16 ▶ if ( $rule->{or} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'or'}) { } 16 0 16
elsif ($rule->{'and'}) { } 0 0 0
203 $query = join ' OR ', map {
204 16 $class->_buildLowerThanExpression( $_, $rule->{or}->{$_} )
205 }
206 16 keys %{ $rule->{or} };
207 }
208 elsif ( $rule->{and} ) {
209 $query = join ' AND ', map {
210 0 $class->_buildLowerThanExpression( $_, $rule->{and}->{$_} )
211 }
212 0 keys %{ $rule->{and} };
213 }
214 16 ▶ return wantarray ? ( 0, 0 ) : 0 unless ($query);
Branch: 2 branches
BranchTrueFalseTotal
unless $query 0 16 16
wantarray ? : 0 0 0
215 16 ▶ if ( $rule->{not} and keys %{ $rule->{not} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"not"} and keys %{$$rule{"not"};}) 12 4 16
Condition66%2/3
$rule->{"not"} and keys %{$$rule{"not"};}
!ll&&!rl&&r
4012
MC/DC50%1/2
$rule->{"not"} and keys %{$$rule{"not"};}
$rule->{"not"} keys %{$$rule{"not"};}
Truth table66%2/3
A and B
A= $rule->{"not"}B= keys %{$$rule{"not"};}
ABresult
0X0
100
111
216 $query = "($query) AND " . join(
217 ' AND ',
218 map {
219 12 push @bind, $rule->{not}->{$_};
220 12 my $f = $class->_sqlField($_);
221 12 "($f IS NULL OR $f <> ?)"
222 }
223 12 keys %{ $rule->{not} }
224 );
225 }
226  
227 # Delete by batches: a single DELETE on hundreds of thousands of sessions
228 # is one long transaction (WAL spike, locks held, replication lag). Each
229 # batch is its own statement (class methods' connections are AutoCommit).
230 # The WHERE expression is the same as before, so the expression indexes
231 # are still usable by the sub-select. Rows deleted by a concurrent purge
232 # are simply not counted.
233 16 my $total = 0;
234 16 my $ok = eval {
235  
236 # Held for all the batches. A pooled connection is given back when
237 # $loan goes out of scope, also when a batch dies (it is then closed:
238 # see _flagError() in Store/Postgres.pm)
239 16 my ( $dbh, $loan ) = $class->_borrowDbh($args);
240 16 my $table_name =
241 Web::Session::_common->_tableName( $dbh, $args );
242 16 my $limit = Web::Session::_common->_batchSize;
243 16 my $sql = $class->_deleteBatchStatement( $table_name, $query, $limit );
244 16 while (1) {
245 26 my $n = $dbh->do( $sql, undef, @bind );
246 22 ▶ return 0 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 21 22
247  
248 # Count unknown to the driver: a short batch can't be detected,
249 # go on while a session still matches
250 21 ▶ if ( $n == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == -1) 3 18 21
251 last
252 3 ▶ unless $dbh->selectrow_array(
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->selectrow_array("SELECT 1 FROM $table_name WHERE $query LIMIT 1", undef, @bind) 1 2 3
253 "SELECT 1 FROM $table_name WHERE $query LIMIT 1",
254 undef, @bind );
255 2 next;
256 }
257 18 $total += $n;
258  
259 # A short batch means that nothing else matches (an exact
260 # multiple of the batch size costs one more, empty, batch)
261 18 ▶ last if $n < $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $n < $limit 10 8 18
262 }
263 11 1;
264 };
265 16 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 5 11 16
266 5 ▶ print STDERR "deleteIfLowerThan: $@\n" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 4 1 5
267 5 ▶ return wantarray ? ( 0, $total ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
268 }
269 11 ▶ return wantarray ? ( 1, $total ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 3 11
270 }
271  
272 # Statement of one batch of deleteIfLowerThan(), $where being the rule
273 16 ▶sub _deleteBatchStatement {
Subroutine _deleteBatchStatement: called CC=1 SCAR=0.0
274 16 my ( $class, $table_name, $where, $limit ) = @_;
275 16 return "DELETE FROM $table_name WHERE id IN "
276 . "(SELECT id FROM $table_name WHERE $where LIMIT $limit)";
277 }
278  
279 18 ▶sub get_key_from_all_sessions {
Subroutine get_key_from_all_sessions: called CC=4 SCAR=14.0
Pod undocumented
280 18 my ( $class, $args, $data ) = @_;
281  
282 # Held until the end of the call, callbacks included: a callback that
283 # ties a session or calls a class method gets another connection of the
284 # pool, never this one (lent, it is not idle). $loan gives it back when
285 # this method returns or dies
286 18 my ( $dbh, $loan ) = $class->_borrowDbh($args);
287 18 my $table_name =
288 Web::Session::_common->_tableName( $dbh, $args );
289 18 my $sth;
290  
291 # Special case if all wanted fields are indexed
292 18 ▶ if ( $data and ref($data) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data ne "CODE") 2 16 18
Condition100%3/3
$data and ref $data ne "CODE"
!ll&&!rl&&r
2142
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
293 2 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 2 2
294 my $fields = join ',', 'id',
295 2 map { $class->_sqlField($_) . ' AS ' . $class->_sqlAlias($_) } @$data;
296 2 $sth = $dbh->prepare("SELECT $fields from $table_name");
297 2 $sth->execute;
298 2 return $sth->fetchall_hashref('id');
299 }
300 16 my %res;
301 16 my $sub = Web::Session::_common::_unserializer($class);
302 Web::Session::_common->_forEachSession(
303 $dbh,
304 $table_name,
305 28 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=20.8
306 28 my @row = @_;
307 28 eval {
308 28 my $tmp = &$sub( { serialized => $row[1] } );
309 28 ▶ if ( ref($data) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'CODE') { } 24 4 28
elsif ($data) { } 0 4 4
310 24 $tmp = &$data( $tmp, $row[0] );
311 17 ▶ $res{ $row[0] } = $tmp if ( defined($tmp) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 17 0 17
312 }
313 elsif ($data) {
314 0 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
315 0 $res{ $row[0] }->{$_} = $tmp->{$_} foreach (@$data);
316 }
317 else {
318 4 $res{ $row[0] } = $tmp;
319 }
320 };
321 28 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 7 21 28
322 7 print STDERR "Error in session $row[0]: $@\n";
323 7 delete $res{ $row[0] };
324 }
325 }
326 16 );
327 15 return \%res;
328 }
329  
330 20 ▶sub _buildLowerThanExpression {
Subroutine _buildLowerThanExpression: called CC=1 SCAR=0.0
331 20 my ( $class, $field, $value ) = @_;
332 20 return $class->_buildCompareExpression( $field, '<', $value );
333 }
334  
335 # Must match the documented expression indexes (_utime, _lastSeen). $op is
336 # "<" or ">", $value a number checked by the caller
337 62 ▶sub _buildCompareExpression {
Subroutine _buildCompareExpression: called CC=1 SCAR=0.0
338 62 my ( $class, $field, $op, $value ) = @_;
339 62 Web::Session::_common->_checkOp($op);
340 50 my $f = $class->_sqlField($field);
341 50 return "cast($f as bigint) $op $value";
342 }
343  
344 # Build SQL expression to get a field from a_session
345 141 ▶sub _sqlField {
Subroutine _sqlField: called CC=2 SCAR=6.9
346 141 my ( $class, $field ) = @_;
347  
348 # With standard_conforming_strings=off, backslashes are escape characters
349 # in plain literals: use an escape string literal in this case
350 141 ▶ if ( $field =~ /\\/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /\\/) 1 140 141
351 1 $field =~ s/(['\\])/$1$1/g;
352 1 return "a_session ->> E'$field'";
353 }
354 140 $field =~ s/'/''/g;
355 140 return "a_session ->> '$field'";
356 }
357  
358 # Build a column alias that preserves field name case
359 4 ▶sub _sqlAlias {
Subroutine _sqlAlias: called CC=1 SCAR=0.0
360 4 my ( $class, $field ) = @_;
361 4 $field =~ s/"/""/g;
362 4 return qq{"$field"};
363 }
364  
365 # Connection of a class method: returns ( $dbh, $loan ). Keep $loan until
366 # the handle is no longer needed: when the connection pool of
367 # Web::Session::Store::Postgres applies (see _classPool()), $dbh is lent by
368 # the pool and given back when $loan is freed, so at the end of the call,
369 # also when it dies. Otherwise $dbh is the handle of _classDbh() and $loan is
370 # undef.
371 55 ▶sub _borrowDbh {
Subroutine _borrowDbh: called CC=3 SCAR=11.0
372 55 my ( $class, $args ) = @_;
373 55 ▶ return ( $class->_classDbh($args), undef )
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_classPool($args) 13 42 55
374 unless ( $class->_classPool($args) );
375 42 ▶ $args->{DataSource} or die "No datasource given !";
Branch: 1 branch
BranchTrueFalseTotal
$args->{"DataSource"} or die "No datasource given !" 42 0 42
376 42 my $loan = Web::Session::Store::Postgres->_lend($args);
377 42 return ( $loan->{dbh}, $loan );
378 }
379  
380 # Whether class methods borrow their connection from the pool: with the
381 # arguments that make sessions use it (see _poolApplies() in
382 # Store/Postgres.pm). A subclass whose sessions don't use the pool overrides
383 # it (Web::Session::Patroni)
384 50 ▶sub _classPool {
Subroutine _classPool: called CC=1 SCAR=0.0
385 50 my ( $class, $args ) = @_;
386 50 return Web::Session::Store::Postgres::_poolApplies($args);
387 }
388  
389 # Handle cached by DBI connect_cached(), used by class methods when the pool
390 # doesn't apply (see _borrowDbh())
391 14 ▶sub _classDbh {
Subroutine _classDbh: called CC=3 SCAR=11.2
392 14 my ( $class, $args ) = @_;
393  
394 14 ▶ my $datasource = $args->{DataSource} or die "No datasource given !";
Branch: 1 branch
BranchTrueFalseTotal
my $datasource = $args->{"DataSource"} or die "No datasource given !" 14 0 14
395 14 my $username = $args->{UserName};
396 14 my $password = $args->{Password};
397 14 ▶ my $dbh =
Condition50%1/2
'DBI'->connect_cached($datasource, $username, $password, 'Web::Session::_common'->_forkSafeAttr({'RaiseError', 1, 'AutoCommit', 1})) || die($DBI::errstr)
l!l
140
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
398 DBI->connect_cached( $datasource, $username, $password,
399 Web::Session::_common->_forkSafeAttr(
400 { RaiseError => 1, AutoCommit => 1 }
401 )
402 )
403 || die $DBI::errstr;
404 14 $dbh->{pg_enable_utf8} = 1;
405 14 return $dbh;
406 }
407  
408 9 1;
409 __END__
« Session/DBI.pm ↑ Summary Session/Store/Postgres.pm »