| 1 | package CGI::Session::SQLite; | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: SQLite.pm,v 1.7 2004/10/21 23:10:32 bmoyles Exp $ | ||
| 5 | # | ||
| 6 | |||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use base qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | CGI::Session | ||
| 10 | CGI::Session::ID::MD5 | ||
| 11 | CGI::Session::Serialize::Default | ||
| 12 | 1 | ); | |
| 13 | |||
| 14 | |||
| 15 | # Load neccessary libraries below | ||
| 16 | |||
| 17 | our($VERSION, $TABLE_NAME); | ||
| 18 | |||
| 19 | $VERSION = '1.0'; | ||
| 20 | $TABLE_NAME = 'sessions'; | ||
| 21 | |||
| 22 | sub store { | ||
| 23 | my ($self, $sid, $options, $data) = @_; | ||
| 24 | my $dbh = $self->SQLite_dbh($options); | ||
| 25 | |||
| 26 | my $storable_data = $self->freeze($data); | ||
| 27 | |||
| 28 | $dbh->do( | ||
| 29 | ' INSERT OR REPLACE INTO ' . $TABLE_NAME . ' (id, a_session) '. | ||
| 30 | ' VALUES( '.$dbh->quote($sid).', '.$dbh->quote($storable_data).')' | ||
| 31 | ); | ||
| 32 | |||
| 33 | return 1; | ||
| 34 | |||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub retrieve { | ||
| 39 | my ($self, $sid, $options) = @_; | ||
| 40 | |||
| 41 | my $dbh = $self->SQLite_dbh($options); | ||
| 42 | my $data; | ||
| 43 | |||
| 44 | $data = $dbh->selectrow_arrayref ( | ||
| 45 | ' SELECT a_session FROM '.$TABLE_NAME. | ||
| 46 | ' WHERE id='.$dbh->quote($sid) | ||
| 47 | ); | ||
| 48 | |||
| 49 | return $self->thaw(@$data); | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | sub remove { | ||
| 57 | my ($self, $sid, $options) = @_; | ||
| 58 | |||
| 59 | my $dbh = $self->SQLite_dbh($options); | ||
| 60 | |||
| 61 | $dbh->do( | ||
| 62 | ' DELETE FROM '.$TABLE_NAME. | ||
| 63 | ' WHERE id='.$dbh->quote($sid) | ||
| 64 | ); | ||
| 65 | |||
| 66 | return 1; | ||
| 67 | |||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | sub teardown { | ||
| 73 | my ($self, $sid, $options) = @_; | ||
| 74 | |||
| 75 | my $dbh = $self->SQLite_dbh($options); | ||
| 76 | |||
| 77 | unless ($dbh->{AutoCommit}) { | ||
| 78 | $dbh->commit(); | ||
| 79 | } | ||
| 80 | |||
| 81 | if ($self->{SQLite_disconnect}) { | ||
| 82 | $dbh->disconnect(); | ||
| 83 | } | ||
| 84 | |||
| 85 | return 1; | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | sub SQLite_dbh { | ||
| 90 | my ($self, $options) = @_; | ||
| 91 | |||
| 92 | my $args = $options->[1] || {}; | ||
| 93 | |||
| 94 | if (defined($self->{SQLite_dbh})) { | ||
| 95 | return $self->{SQLite_dbh}; | ||
| 96 | } | ||
| 97 | |||
| 98 | if (defined($args->{TableName})) { | ||
| 99 | $TABLE_NAME = $args->{TableName}; | ||
| 100 | } | ||
| 101 | |||
| 102 | require DBI; | ||
| 103 | |||
| 104 | $self->{SQLite_dbh} = $args->{Handle} || DBI->connect( | ||
| 105 | $args->{DataSource}, | ||
| 106 | $args->{User} || undef, | ||
| 107 | $args->{Password} || undef, | ||
| 108 | { RaiseError =>1, PrintError =>1, AutoCommit =>1 } | ||
| 109 | ); | ||
| 110 | $args->{Handle} or $self->{SQLite_disconnect} =1; | ||
| 111 | |||
| 112 | return $self->{SQLite_dbh}; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | # $Id: SQLite.pm,v 1.7 2004/10/21 23:10:32 bmoyles Exp $ | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =head1 NAME | ||
| 123 | |||
| 124 | CGI::Session::SQLite - CGI::Session driver for SQLite | ||
| 125 | |||
| 126 | =head1 SYNOPSIS | ||
| 127 | |||
| 128 | use CGI::Session::SQLite | ||
| 129 | $session = new CGI::Session("driver:SQLite", undef, {...}); | ||
| 130 | |||
| 131 | For more examples, consult L<CGI::Session> manual | ||
| 132 | |||
| 133 | =head1 DESCRIPTION | ||
| 134 | |||
| 135 | CGI::Session::SQLite is a CGI::Session driver utilizing the SQLite DBMS. | ||
| 136 | To write your own drivers for B<CGI::Session> refer to the L<CGI::Session> manual. | ||
| 137 | |||
| 138 | =head1 STORAGE | ||
| 139 | |||
| 140 | To store session data in SQLite database, you first need to create a | ||
| 141 | suitable table for it with the following command: | ||
| 142 | |||
| 143 | CREATE TABLE sessions ( | ||
| 144 | id CHAR(32) NOT NULL UNIQUE, | ||
| 145 | a_session TEXT NOT NULL | ||
| 146 | ); | ||
| 147 | |||
| 148 | You can also add any number of additional columns to the table, but the | ||
| 149 | above "id" and "a_session" are required. | ||
| 150 | |||
| 151 | If you want to store the session data in other table than "sessions", | ||
| 152 | before creating the session object you need to set the special variable | ||
| 153 | $CGI::Session::SQLite::TABLE_NAME to the name of the table: | ||
| 154 | |||
| 155 | use CGI::Session; | ||
| 156 | $CGI::Session::SQLite::TABLE_NAME = 'my_sessions'; | ||
| 157 | $session = new CGI::Session("driver:SQLite", undef, {Handle=>$dbh}); | ||
| 158 | |||
| 159 | =head1 COPYRIGHT | ||
| 160 | |||
| 161 | Copyright (C) 2004 Brian Moyles <bmoyles@gmail.com>. All rights reserved. | ||
| 162 | |||
| 163 | This library is free software and can be modified and distributed under the same | ||
| 164 | terms as Perl itself. | ||
| 165 | |||
| 166 | =head1 AUTHOR | ||
| 167 | |||
| 168 | Brian Moyles <bmoyles@gmail.com> | ||
| 169 | |||
| 170 | =head1 SEE ALSO | ||
| 171 | |||
| 172 | =over 4 | ||
| 173 | |||
| 174 | =item * | ||
| 175 | |||
| 176 | L<CGI::Session|CGI::Session> - CGI::Session manual | ||
| 177 | |||
| 178 | =item * | ||
| 179 | |||
| 180 | L<CGI::Session::Tutorial|CGI::Session::Tutorial> - extended CGI::Session manual | ||
| 181 | |||
| 182 | =item * | ||
| 183 | |||
| 184 | L<CGI::Session::CookBook|CGI::Session::CookBook> - practical solutions for real life problems | ||
| 185 | |||
| 186 | =item * | ||
| 187 | |||
| 188 | B<RFC 2965> - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt | ||
| 189 | |||
| 190 | =item * | ||
| 191 | |||
| 192 | L<CGI|CGI> - standard CGI library | ||
| 193 | |||
| 194 | =item * | ||
| 195 | |||
| 196 | L<Apache::Session|Apache::Session> - another fine alternative to CGI::Session | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | |||
| 203 | # $Id: SQLite.pm,v 1.7 2004/10/21 23:10:32 bmoyles Exp $ | ||