| 1 | ############################################################################# | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Web::Session::SQLite | ||||||||||||||||||||
| 4 | # Apache persistent user sessions in a SQLite database | ||||||||||||||||||||
| 5 | # Copyright(c) 2013 Xavier Guimard <x.guimard@free.fr> | ||||||||||||||||||||
| 6 | # Inspired by Apache::Session::Store::Postgres | ||||||||||||||||||||
| 7 | # (copyright(c) 1998, 1999, 2000 Jeffrey William Baker (jwbaker@acm.org)) | ||||||||||||||||||||
| 8 | # Distribute under the Perl License | ||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | ############################################################################ | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | package Web::Session::Store::SQLite; | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 14 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 14 | ▶ | use DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 14 | ▶ | use Apache::Session::Store::DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 14 | ▶ | use Web::Session::Store::DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 14 | our @ISA = qw(Web::Session::Store::DBI Apache::Session::Store::DBI); | |||||||||||||||||||
| 21 | 14 | our $VERSION = '1.0.0'; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 14 | $Web::Session::Store::SQLite::DataSource = undef; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 308 | ▶ | sub connection { | ||||||||||||||||||
|
Subroutine
connection: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||
| 26 | 308 | my $self = shift; | |||||||||||||||||||
| 27 | 308 | my $session = shift; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 308 | $self->_forgetInheritedHandle; | |||||||||||||||||||
| 30 | 308 | ▶ | return if ( defined $self->{dbh} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | $session->{args}->{Commit} = | ||||||||||||||||||||
| 32 | 175 | ▶ | exists( $session->{args}->{Commit} ) ? $session->{args}->{Commit} : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | $self->{'table_name'} = $session->{args}->{TableName} | ||||||||||||||||||||
| 35 | 175 | ▶ | || $Apache::Session::Store::DBI::TableName; | ||||||||||||||||||
|
Condition66%2/3
$session->{'args'}{'TableName'} || $Apache::Session::Store::DBI::TableName
MC/DC0%0/2
$session->{'args'}{'TableName'} || $Apache::Session::Store::DBI::TableName
$session->{'args'}{'TableName'}
$Apache::Session::Store::DBI::TableName
Truth table66%2/3
A || B
A= $session->{'args'}{'TableName'}B= $Apache::Session::Store::DBI::TableName
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 175 | ▶ | if ( exists $session->{args}->{Handle} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 25 | $self->{dbh} = $session->{args}->{Handle}; | |||||||||||||||||||
| 39 | 25 | $self->{commit} = $session->{args}->{Commit}; | |||||||||||||||||||
| 40 | 25 | return; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | my $datasource = $session->{args}->{DataSource} | ||||||||||||||||||||
| 44 | 150 | ▶ | || $Web::Session::Store::SQLite::DataSource; | ||||||||||||||||||
|
Condition33%1/3
$session->{'args'}{'DataSource'} || $Web::Session::Store::SQLite::DataSource
MC/DC0%0/2
$session->{'args'}{'DataSource'} || $Web::Session::Store::SQLite::DataSource
$session->{'args'}{'DataSource'}
$Web::Session::Store::SQLite::DataSource
Truth table33%1/3
A || B
A= $session->{'args'}{'DataSource'}B= $Web::Session::Store::SQLite::DataSource
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # AutoCommit off: see "Why AutoCommit differs" in Store/DBI.pm | ||||||||||||||||||||
| 47 | 150 | ▶ | if ( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | my $dbh = $self->_reusedHandle( | ||||||||||||||||||||
| 49 | $session->{args}, $datasource, '', '', | ||||||||||||||||||||
| 50 | { RaiseError => 1, AutoCommit => 0 } | ||||||||||||||||||||
| 51 | ) | ||||||||||||||||||||
| 52 | ) | ||||||||||||||||||||
| 53 | { | ||||||||||||||||||||
| 54 | 146 | $self->{dbh} = $dbh; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||
| 57 | $self->{dbh} = | ||||||||||||||||||||
| 58 | 4 | ▶ | DBI->connect( $datasource, '', '', | ||||||||||||||||||
|
Condition50%1/2
'DBI'->connect($datasource, '', '', {'RaiseError', 1, 'AutoCommit', 0}) || die($DBI::errstr)
MC/DC0%0/1
'DBI'->connect($datasource, '', '', {'RaiseError', 1, 'AutoCommit', 0}) || die($DBI::errstr)
'DBI'->connect($datasource, '', '', {'RaiseError', 1, 'AutoCommit', 0})
Truth table50%1/2
A
A= 'DBI'->connect($datasource, '', '', {'RaiseError', 1, 'AutoCommit', 0})
| |||||||||||||||||||||
| 59 | { RaiseError => 1, AutoCommit => 0 } ) | ||||||||||||||||||||
| 60 | || die $DBI::errstr; | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | #If we open the connection, we close the connection | ||||||||||||||||||||
| 63 | 4 | $self->{disconnect} = 1; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | 150 | $self->{dbh}->{sqlite_unicode} = 1; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | #the programmer has to tell us what commit policy to use | ||||||||||||||||||||
| 68 | 150 | $self->{commit} = $session->{args}->{Commit}; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 63 | ▶ | sub materialize { | ||||||||||||||||||
|
Subroutine
materialize: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 72 | 63 | my $self = shift; | |||||||||||||||||||
| 73 | 63 | my $session = shift; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 63 | $self->connection($session); | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 63 | local $self->{dbh}->{RaiseError} = 1; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 63 | ▶ | if ( !defined $self->{materialize_sth} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 63 | my $table = $self->_quoteTable( $self->{'table_name'} ); | |||||||||||||||||||
| 81 | $self->{materialize_sth} = $self->{dbh}->prepare_cached( | ||||||||||||||||||||
| 82 | 63 | qq{ | |||||||||||||||||||
| 83 | SELECT a_session FROM $table WHERE id = ?} | ||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 63 | $self->{materialize_sth}->bind_param( 1, $session->{data}->{_session_id} ); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 63 | $self->{materialize_sth}->execute; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 63 | my $results = $self->{materialize_sth}->fetchrow_arrayref; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 63 | ▶ | if ( !( defined $results ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 1 | $self->{materialize_sth}->finish; | |||||||||||||||||||
| 95 | 1 | die "Object does not exist in the data store"; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 62 | $self->{materialize_sth}->finish; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 62 | $session->{serialized} = $results->[0]; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # Overrides Web::Session::Store::DBI's materialize(): keep what was read, | ||||||||||||||||||||
| 103 | # see "Unchanged sessions are not rewritten" there | ||||||||||||||||||||
| 104 | 62 | $self->_remember($session); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 315 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 108 | 315 | my $self = shift; | |||||||||||||||||||
| 109 | 315 | ▶ | return if ( $self->_forgetInheritedHandle ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 314 | ▶ | if ( $self->{commit} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 150 | $self->{dbh}->commit; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 314 | ▶ | if ( $self->{disconnect} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 4 | $self->{dbh}->disconnect; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 14 | 1; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =pod | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head1 NAME | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | Web::Session::Store::SQLite - Store persistent data in a SQLite | ||||||||||||||||||||
| 127 | database | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head1 SYNOPSIS | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | use Web::Session::Store::SQLite; | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | my $store = new Web::Session::Store::SQLite; | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | $store->insert($ref); | ||||||||||||||||||||
| 136 | $store->update($ref); | ||||||||||||||||||||
| 137 | $store->materialize($ref); | ||||||||||||||||||||
| 138 | $store->remove($ref); | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 DESCRIPTION | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Web::Session::Store::SQLite fulfills the storage interface of | ||||||||||||||||||||
| 143 | Apache::Session. Session data is stored in a SQLite database. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 SCHEMA | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | To use this module, you will need at least these columns in a table | ||||||||||||||||||||
| 148 | called 'sessions', or another name if you supply the TableName parameter. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | id char(32) # or however long your session IDs are. | ||||||||||||||||||||
| 151 | a_session text # This has an ~8 KB limit :( | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | To create this schema, you can execute this command using the sqlite program: | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | CREATE TABLE sessions ( | ||||||||||||||||||||
| 156 | id char(32) not null primary key, | ||||||||||||||||||||
| 157 | a_session text | ||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | If you use some other command, ensure that there is a unique index on the | ||||||||||||||||||||
| 161 | table's id column. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head1 CONFIGURATION | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | The module must know what datasource, username, and password to use when | ||||||||||||||||||||
| 166 | connecting to the database. These values can be set using the options hash | ||||||||||||||||||||
| 167 | (see Apache::Session documentation). The options are: | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =over 4 | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =item DataSource | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =item UserName | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =item Password | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =item Handle | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =item TableName | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | =item noreuse | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | Open and close a connection for each session instead of keeping it open, see | ||||||||||||||||||||
| 184 | L<Web::Session/"PERSISTENT DATABASE CONNECTIONS">. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =back | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | Example: | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | tie %hash, 'Web::Session::SQLite', $id, { | ||||||||||||||||||||
| 191 | DataSource => 'dbi:Pg:dbname=database', | ||||||||||||||||||||
| 192 | UserName => 'database_user', | ||||||||||||||||||||
| 193 | Password => 'K00l' | ||||||||||||||||||||
| 194 | }; | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Instead, you may pass in an already-opened DBI handle to your database. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | tie %hash, 'Web::Session::SQLite', $id, { | ||||||||||||||||||||
| 199 | Handle => $dbh | ||||||||||||||||||||
| 200 | }; | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =head1 AUTHOR | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | This modules was written by Jeffrey William Baker <jwbaker@acm.org> | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | A fix for the commit policy was contributed by Michael Schout <mschout@gkg.net> | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | =head1 SEE ALSO | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | L<Apache::Session>, L<Apache::Session::Store::DBI> | ||||||||||||||||||||