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