Sessions/SQLite.pm

filter: Statementstmt 85.2% 116 / 136 Branchbran 62.5% 40 / 64 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 14 / 14 Podpod 85.7% 6 / 7 totaltotal 77.0% 185 / 240 CC 38 SCAR 19.3
CC
38
Cov
77%
new
12.5
update_session
9.8
create_session
8.3
CRAP 6.9
« Sessions/File.pm ↑ Summary Sessions/Session.pm »
1 package Concierge::Sessions::SQLite v0.11.3;
2 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use parent 'Concierge::Sessions::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 72 ▶sub new {
Subroutine new: called CC=8 SCAR=25.2
Pod undocumented
11 72 my ($class, %args) = @_;
12 72 my $self = $class->SUPER::new(%args);
13 72 ▶ $self->{storage_dir} = $args{storage_dir} || '/tmp/sessions';
Condition50%1/2
$args{'storage_dir'} || '/tmp/sessions'
l!l
720
MC/DC0%0/1
$args{'storage_dir'} || '/tmp/sessions'
$args{'storage_dir'}
Truth table50%1/2
A
A= $args{'storage_dir'}
Aresult
11
00
14  
15 72 ▶ unless (-d $self->{storage_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"storage_dir"}) 0 72 72
16 0 ▶ unless (mkdir $self->{storage_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $self->{"storage_dir"}) 0 0 0
17 0 croak "Failed to create storage directory '$self->{storage_dir}': $!";
18 }
19 }
20  
21 72 $self->{dsn} = File::Spec->catfile( $self->{storage_dir}, 'sessions.db' );
22  
23 72 $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{dsn}", "", "", {
24 RaiseError => 0,
25 AutoCommit => 1,
26 });
27  
28 72 ▶ unless ($self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}) 0 72 72
29 0 croak "Failed to connect to SQLite database '$self->{dsn}': " . DBI->errstr;
30 }
31  
32 72 my $result = $self->{dbh}->do(q{
33 CREATE TABLE IF NOT EXISTS sessions (
34 session_id TEXT PRIMARY KEY,
35 user_id TEXT,
36 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
37 expires_at TIMESTAMP,
38 last_updated TIMESTAMP,
39 session_timeout INTEGER,
40 status JSON,
41 data JSON
42 )
43 });
44  
45 72 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 72 72
46 0 croak "Failed to create sessions table: " . $self->{dbh}->errstr;
47 }
48  
49 72 $result = $self->{dbh}->do(q{ CREATE INDEX IF NOT EXISTS idx_session_id ON sessions (session_id) });
50 72 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 72 72
51 0 croak "Failed to create session_id index: " . $self->{dbh}->errstr;
52 }
53  
54 72 $result = $self->{dbh}->do(q{ CREATE INDEX IF NOT EXISTS expirations ON sessions (expires_at) });
55 72 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 72 72
56 0 croak "Failed to create expiration index: " . $self->{dbh}->errstr;
57 }
58  
59 72 return $self;
60 }
61  
62 63 ▶sub create_session {
Subroutine create_session: called CC=8 SCAR=21.2
Pod documented
63 63 my ($self, %args) = @_;
64  
65 return { success => 0, message => "Cannot create session without user_id" }
66 63 ▶ unless $args{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"user_id"} 1 62 63
67  
68 62 my $user_id = $args{user_id};
69  
70 # Delete any existing sessions for this user (enforce single session per user)
71 62 $self->delete_user_session($user_id);
72  
73 # Build session_info structure
74 62 my $session_id = $self->generate_session_id();
75  
76 62 my $now = time();
77  
78 # Handle session timeout: 'indefinite' or numeric value in seconds
79 62 ▶ my $timeout = $args{session_timeout} || $self->{session_timeout};
Condition33%1/3
$args{'session_timeout'} || $self->{'session_timeout'}
l!l&&r!l&&!r
6200
MC/DC0%0/2
$args{'session_timeout'} || $self->{'session_timeout'}
$args{'session_timeout'} $self->{'session_timeout'}
Truth table33%1/3
A || B
A= $args{'session_timeout'}B= $self->{'session_timeout'}
ABresult
1X1
011
000
80 62 my $expires_at;
81 62 ▶ if (defined $timeout && $timeout eq 'indefinite') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout and $timeout eq 'indefinite') { } 3 59 62
Condition66%2/3
defined $timeout and $timeout eq "indefinite"
!ll&&!rl&&r
0593
MC/DC50%1/2
defined $timeout and $timeout eq "indefinite"
defined $timeout $timeout eq "indefinite"
Truth table66%2/3
A and B
A= defined $timeoutB= $timeout eq "indefinite"
ABresult
0X0
100
111
82 3 $expires_at = 'indefinite';
83 } else {
84 59 $expires_at = $now + $timeout;
85 }
86  
87 62 my $created_at = $now;
88 62 my $last_updated = $now;
89 62 my $status = { state => 'active', dirty => 0 };
90 62 my $status_json = JSON::PP->new->utf8->encode( $status );
91 62 ▶ my $data = $args{data} || {}; # for app data
Condition100%2/2
$args{'data'} || {}
l!l
953
MC/DC100%1/1
$args{'data'} || {}
$args{'data'}
Truth table100%2/2
A
A= $args{'data'}
Aresult
11
00
92 62 my $data_json = JSON::PP->new->utf8->encode( $data );
93  
94 my $sth = $self->{dbh}->prepare(
95 62 "INSERT INTO sessions (
96 session_id,
97 user_id,
98 created_at,
99 expires_at,
100 last_updated,
101 session_timeout,
102 status,
103 data
104 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
105 );
106  
107 62 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 62 62
108 0 return { success => 0, message => "Failed to prepare insert statement: " . $self->{dbh}->errstr };
109 }
110  
111 62 my $result = $sth->execute(
112 $session_id,
113 $user_id,
114 $created_at,
115 $expires_at,
116 $last_updated,
117 $timeout,
118 $status_json,
119 $data_json
120 );
121  
122 62 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 62 62
123 0 return { success => 0, message => "Failed to insert session: " . $sth->errstr };
124 }
125  
126 62 return { success => 1, session_id => $session_id };
127 }
128  
129 84 ▶sub get_session_info {
Subroutine get_session_info: called CC=5 SCAR=16.4
Pod documented
130 84 my ($self, $session_id) = @_;
131  
132 84 ▶ unless ($session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 1 83 84
133 1 return { success => 0, message => "Session ID required to retrieve session from SQLite backend" };
134 }
135  
136 # Query filters expired sessions but allows indefinite sessions
137 my $sth = $self->{dbh}->prepare(
138 83 "SELECT * FROM sessions WHERE session_id = ? AND (expires_at = 'indefinite' OR expires_at > ?)"
139 );
140  
141 83 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 83 83
142 0 return { success => 0, message => "Failed to prepare select statement: " . $self->{dbh}->errstr };
143 }
144  
145 83 my $result = $sth->execute($session_id, time());
146  
147 83 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 83 83
148 0 return { success => 0, message => "Failed to execute session query: " . $sth->errstr };
149 }
150  
151 83 my $session_info = $sth->fetchrow_hashref;
152  
153 83 ▶ unless ($session_info) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_info) 8 75 83
154 8 return { success => 0, message => "Session not found or expired" };
155 }
156  
157 # Decode session_info from JSON to hashref
158 75 $session_info->{status} = JSON::PP->new->utf8->decode( $session_info->{status} );
159 75 $session_info->{data} = JSON::PP->new->utf8->decode( $session_info->{data} );
160  
161 75 return { success => 1, info => $session_info };
162 }
163  
164 14 ▶sub update_session {
Subroutine update_session: called CC=9 SCAR=22.8
Pod documented
165 14 my ($self, $session_id, $updates) = @_;
166  
167 14 ▶ unless ($session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 1 13 14
168 1 return { success => 0, message => "Session ID required to update session in SQLite backend" };
169 }
170  
171 13 ▶ unless ($updates) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($updates) 1 12 13
172 1 return { success => 1, message => "No updates specified for File backend session update" };
173 }
174  
175 # Always update last_updated timestamp
176 12 my $now = time();
177  
178 # Build SET clause dynamically based on what's being updated
179 12 my @set_clauses;
180 my @bind_values;
181  
182 12 ▶ if (exists $updates->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $updates->{"data"}) 12 0 12
183 12 push @set_clauses, 'data = ?';
184 12 ▶ push @bind_values, JSON::PP->new->utf8->encode($updates->{data} || {});
Condition50%1/2
$updates->{'data'} || {}
l!l
120
MC/DC0%0/1
$updates->{'data'} || {}
$updates->{'data'}
Truth table50%1/2
A
A= $updates->{'data'}
Aresult
11
00
185 }
186  
187 12 ▶ if (exists $updates->{expires_at}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $updates->{"expires_at"}) 12 0 12
188 12 push @set_clauses, 'expires_at = ?';
189 12 push @bind_values, $updates->{expires_at};
190 }
191  
192 # Always update last_updated
193 12 push @set_clauses, 'last_updated = ?';
194 12 push @bind_values, $now;
195  
196 # Build SQL statement
197 12 my $sql = 'UPDATE sessions SET ' . join(', ', @set_clauses) . ' WHERE session_id = ?';
198 12 push @bind_values, $session_id;
199  
200 12 my $sth = $self->{dbh}->prepare($sql);
201  
202 12 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 12 12
203 0 return { success => 0, message => "Failed to prepare update statement: " . $self->{dbh}->errstr };
204 }
205  
206 12 my $result = $sth->execute(@bind_values);
207  
208 12 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 12 12
209 0 return { success => 0, message => "Failed to update session: " . $sth->errstr };
210 }
211  
212 12 ▶ unless ($result > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result > 0) 0 12 12
213 0 return { success => 0, message => "Session not found or no changes made" };
214 }
215  
216 12 return { success => 1 };
217 }
218  
219 4 ▶sub delete_session {
Subroutine delete_session: called CC=4 SCAR=15.8
Pod documented
220 4 my ($self, $session_id) = @_;
221  
222 4 ▶ unless ($session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 0 4 4
223 0 return { success => 0, message => "Session ID required to delete session from SQLite backend" };
224 }
225  
226 4 my $sth = $self->{dbh}->prepare("DELETE FROM sessions WHERE session_id = ?");
227  
228 4 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 4 4
229 0 return { success => 0, message => "Failed to prepare delete statement: " . $self->{dbh}->errstr };
230 }
231  
232 4 my $result = $sth->execute($session_id);
233  
234 4 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 4 4
235 0 return { success => 0, message => "Failed to delete session: " . $sth->errstr };
236 }
237  
238 4 return { success => 1 };
239 }
240  
241 2 ▶sub cleanup_sessions {
Subroutine cleanup_sessions: called CC=4 SCAR=14.5
Pod documented
242 2 my ($self) = shift;
243  
244 # Delete only sessions with numeric expiration times that have passed
245 # Indefinite sessions (expires_at = 'indefinite') are preserved
246 2 my $sth = $self->{dbh}->prepare("DELETE FROM sessions WHERE expires_at != 'indefinite' AND expires_at < ?");
247  
248 2 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 2 2
249 0 return { success => 0, message => "Failed to prepare cleanup statement: " . $self->{dbh}->errstr };
250 }
251  
252 2 my $result = $sth->execute( time() );
253  
254 2 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 2 2
255 0 return { success => 0, message => "Failed to cleanup expired sessions: " . $sth->errstr };
256 }
257  
258 # Convert 0E0 to plain 0 if no rows deleted
259 2 ▶ my $deleted_count = $result eq '0E0' ? 0 : $result;
Branch: 1 branch
BranchTrueFalseTotal
$result eq '0E0' ? : 1 1 2
260
261 2 my $active = $self->{dbh}->selectcol_arrayref(qq{SELECT session_id FROM sessions WHERE session_id != '' });
262  
263 2 return { success => 1, deleted_count => $deleted_count, active => $active };
264 }
265  
266 # sub delete_user_sessions {
267 65 ▶sub delete_user_session {
Subroutine delete_user_session: called CC=5 SCAR=16.6
Pod documented
268 65 my ($self, $user_id) = @_;
269  
270 65 ▶ unless ($user_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_id) 1 64 65
271 1 return { success => 0, message => "user_id required to delete user sessions from SQLite backend" };
272 }
273  
274 64 my $sth = $self->{dbh}->prepare("DELETE FROM sessions WHERE user_id = ?");
275  
276 64 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 64 64
277 0 return { success => 0, message => "Failed to prepare delete user sessions statement: " . $self->{dbh}->errstr };
278 }
279  
280 64 my $result = $sth->execute($user_id);
281  
282 64 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 64 64
283 0 return { success => 0, message => "Failed to delete user sessions: " . $sth->errstr };
284 }
285  
286 # Convert 0E0 to plain 0 if no rows deleted
287 64 ▶ my $deleted_count = $result eq '0E0' ? 0 : $result;
Branch: 1 branch
BranchTrueFalseTotal
$result eq '0E0' ? : 60 4 64
288  
289 64 return { success => 1, deleted_count => $deleted_count };
290 }
291  
292 72 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
293 72 my ($self) = @_;
294 72 ▶ $self->{dbh}->disconnect if defined $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 72 0 72
295 }
296  
297 4 1;
298  
299 __END__
« Sessions/File.pm ↑ Summary Sessions/Session.pm »