lib/CPAN/lib/CGI/Session/Driver/sqlite.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
package CGI::Session::Driver::sqlite;
colas@0
     2
colas@0
     3
# $Id: sqlite.pm 351 2006-11-24 14:16:50Z markstos $
colas@0
     4
colas@0
     5
use strict;
colas@0
     6
colas@0
     7
use File::Spec;
colas@0
     8
use base 'CGI::Session::Driver::DBI';
colas@0
     9
use DBI qw(SQL_BLOB);
colas@0
    10
use Fcntl;
colas@0
    11
colas@0
    12
$CGI::Session::Driver::sqlite::VERSION    = "4.20";
colas@0
    13
colas@0
    14
sub init {
colas@0
    15
    my $self = shift;
colas@0
    16
colas@0
    17
    unless ( $self->{Handle}) {
colas@0
    18
       $self->{DataSource} = "dbi:SQLite:dbname=" . $self->{DataSource} unless ( $self->{DataSource} =~ /^dbi:sqlite/i );
colas@0
    19
    }
colas@0
    20
colas@0
    21
    $self->SUPER::init() or return;
colas@0
    22
    
colas@0
    23
    $self->{Handle}->{sqlite_handle_binary_nulls} = 1;
colas@0
    24
    return 1;
colas@0
    25
}
colas@0
    26
colas@0
    27
sub store {
colas@0
    28
    my $self = shift;
colas@0
    29
    my ($sid, $datastr) = @_;
colas@0
    30
    return $self->set_error("store(): usage error") unless $sid && $datastr;
colas@0
    31
colas@0
    32
    my $dbh = $self->{Handle};
colas@0
    33
colas@0
    34
    my $sth = $dbh->prepare("SELECT id FROM " . $self->table_name . " WHERE id=?");
colas@0
    35
    unless ( defined $sth ) {
colas@0
    36
        return $self->set_error( "store(): \$sth->prepare failed with message " . $dbh->errstr );
colas@0
    37
    }
colas@0
    38
colas@0
    39
    $sth->execute( $sid ) or return $self->set_error( "store(): \$sth->execute failed with message " . $dbh->errstr );
colas@0
    40
    if ( $sth->fetchrow_array ) {
colas@0
    41
        __ex_and_ret($dbh,"UPDATE " . $self->table_name . " SET a_session=? WHERE id=?",$datastr,$sid)
colas@0
    42
            or return $self->set_error( "store(): serialize to db failed " . $dbh->errstr );
colas@0
    43
    } else {
colas@0
    44
        __ex_and_ret($dbh,"INSERT INTO " . $self->table_name . " (a_session,id) VALUES(?, ?)",$datastr, $sid)
colas@0
    45
            or return $self->set_error( "store(): serialize to db failed " . $dbh->errstr );
colas@0
    46
    }
colas@0
    47
    return 1;
colas@0
    48
}
colas@0
    49
colas@0
    50
sub __ex_and_ret {
colas@0
    51
    my ($dbh,$sql,$datastr,$sid) = @_;
colas@0
    52
    # fix rt #18183
colas@0
    53
    local $@;
colas@0
    54
    eval {
colas@0
    55
        my $sth = $dbh->prepare($sql) or return 0;
colas@0
    56
        $sth->bind_param(1,$datastr,SQL_BLOB) or return 0;
colas@0
    57
        $sth->bind_param(2,$sid) or return 0;
colas@0
    58
        $sth->execute() or return 0;
colas@0
    59
    };
colas@0
    60
    return ! $@;
colas@0
    61
}
colas@0
    62
colas@0
    63
1;
colas@0
    64
colas@0
    65
__END__;
colas@0
    66
colas@0
    67
=pod
colas@0
    68
colas@0
    69
=head1 NAME
colas@0
    70
colas@0
    71
CGI::Session::Driver::sqlite - CGI::Session driver for SQLite
colas@0
    72
colas@0
    73
=head1 SYNOPSIS
colas@0
    74
colas@0
    75
    $s = new CGI::Session("driver:sqlite", $sid, {DataSource=>'/my/folder/sessions.sqlt'});
colas@0
    76
    $s = new CGI::Session("driver:sqlite", $sid, {Handle=>$dbh});
colas@0
    77
colas@0
    78
=head1 DESCRIPTION
colas@0
    79
colas@0
    80
B<sqlite> driver stores session data in SQLite files using L<DBD::SQLite|DBD::SQLite> DBI driver. More details see L<CGI::Session::Driver::DBI|CGI::Session::Driver::DBI>, its parent class.
colas@0
    81
colas@0
    82
=head1 DRIVER ARGUMENTS
colas@0
    83
colas@0
    84
Supported driver arguments are I<DataSource> and I<Handle>. B<At most> only one of these arguments can be set while creating session object.
colas@0
    85
colas@0
    86
I<DataSource> should be in the form of C<dbi:SQLite:dbname=/path/to/db.sqlt>. If C<dbi:SQLite:> is missing it will be prepended for you. If I<Handle> is present it should be database handle (C<$dbh>) returned by L<DBI::connect()|DBI/connect()>.
colas@0
    87
colas@0
    88
As of version 1.7 of this driver, the third argument is B<NOT> optional. Using a default database in the temporary directory is a security risk since anyone on the machine can create and/or read your session data. If you understand these risks and still want the old behavior, you can set the C<DataSource> option to I<'/tmp/sessions.sqlt'>.
colas@0
    89
colas@0
    90
=head1 BUGS AND LIMITATIONS
colas@0
    91
colas@0
    92
None known.
colas@0
    93
colas@0
    94
=head1 LICENSING
colas@0
    95
colas@0
    96
For support and licensing see L<CGI::Session|CGI::Session>
colas@0
    97
colas@0
    98
=cut
colas@0
    99