Session/Driver/sqlite.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 114 CC 9 SCAR 25.0
CC
9
Cov
0%
store
20.0
DESTROY
20.0
init
12.0
CRAP 12.2
1 package CGI::Session::Driver::sqlite;
2  
3 # $Id$
4  
5 use strict;
6  
7 use File::Spec;
8 use base 'CGI::Session::Driver::DBI';
9 use DBI qw(SQL_BLOB);
10 use Fcntl;
11  
12 $CGI::Session::Driver::sqlite::VERSION = '4.43';
13  
14 sub init {
15 my $self = shift;
16  
17 unless ( $self->{Handle}) {
18 $self->{DataSource} = "dbi:SQLite:dbname=" . $self->{DataSource} unless ( $self->{DataSource} =~ /^dbi:sqlite/i );
19 }
20  
21 $self->SUPER::init() or return;
22
23 $self->{Handle}->{sqlite_handle_binary_nulls} = 1;
24 return 1;
25 }
26  
27 sub store {
28 my $self = shift;
29 my ($sid, $datastr) = @_;
30 return $self->set_error("store(): usage error") unless $sid && $datastr;
31  
32 my $dbh = $self->{Handle};
33  
34 my $sth = $dbh->prepare("SELECT $self->{IdColName} FROM " . $self->table_name . " WHERE $self->{IdColName}=?");
35 unless ( defined $sth ) {
36 return $self->set_error( "store(): \$sth->prepare failed with message " . $dbh->errstr );
37 }
38  
39 $sth->execute( $sid ) or return $self->set_error( "store(): \$sth->execute failed with message " . $dbh->errstr );
40 if ( $sth->fetchrow_array ) {
41 __ex_and_ret($dbh,"UPDATE " . $self->table_name . " SET $self->{DataColName}=? WHERE $self->{IdColName}=?",$datastr,$sid)
42 or return $self->set_error( "store(): serialize to db failed " . $dbh->errstr );
43 } else {
44 __ex_and_ret($dbh,"INSERT INTO " . $self->table_name . " ($self->{DataColName},$self->{IdColName}) VALUES(?, ?)",$datastr, $sid)
45 or return $self->set_error( "store(): serialize to db failed " . $dbh->errstr );
46 }
47 return 1;
48 }
49  
50 sub DESTROY {
51 my $self = shift;
52  
53 unless ( defined( $self->{Handle} ) && $self->{Handle} -> ping ) {
54 $self->set_error(__PACKAGE__ . '::DESTROY(). Database handle has gone away');
55 return;
56 }
57  
58 unless ( $self->{Handle}->{AutoCommit} ) {
59 $self->{Handle}->commit;
60 }
61  
62 if ( $self->{_disconnect} ) {
63 undef $self->{Handle};
64 }
65 }
66  
67 sub __ex_and_ret {
68 my ($dbh,$sql,$datastr,$sid) = @_;
69 # fix rt #18183
70 local $@;
71 eval {
72 my $sth = $dbh->prepare($sql) or return 0;
73 $sth->bind_param(1,$datastr,SQL_BLOB) or return 0;
74 $sth->bind_param(2,$sid) or return 0;
75 $sth->execute() or return 0;
76 };
77 return ! $@;
78 }
79  
80 1;
81  
82 __END__;