1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
package Plack::Session::Store::DBI;
use strict;
use warnings;
# XXX Is there a notion of auto-expiry?
our $VERSION = '0.36';
our $AUTHORITY = 'cpan:STEVAN';
use MIME::Base64 ();
use Storable ();
use parent 'Plack::Session::Store';
use Plack::Util::Accessor qw[ dbh get_dbh table_name serializer deserializer id_column data_column];
sub new {
my ($class, %params) = @_;
if (! $params{dbh} && ! $params{get_dbh}) {
die "DBI instance or a callback was not available in the argument list";
}
$params{table_name} ||= 'sessions';
$params{data_column} ||= 'session_data';
$params{id_column} ||= 'id';
$params{serializer} ||=
sub { MIME::Base64::encode_base64( Storable::nfreeze( $_[0] ) ) };
$params{deserializer} ||=
sub { Storable::thaw( MIME::Base64::decode_base64( $_[0] ) ) };
my $self = bless { %params }, $class;
return $self;
}
sub _dbh {
my $self =shift;
( exists $self->{get_dbh} ) ? $self->{get_dbh}->() : $self->{dbh};
}
sub fetch {
my ($self, $session_id) = @_;
my $table_name = $self->{table_name};
my $data_column = $self->{data_column};
my $id_column = $self->{id_column};
my $dbh = $self->_dbh;
my $sth = $dbh->prepare_cached(
sprintf(
"SELECT %s FROM %s WHERE %s = ?",
$dbh->quote_identifier($data_column),
$self->_quote_table_name($table_name),
$dbh->quote_identifier($id_column),
)
);
$sth->execute( $session_id );
my ($data) = $sth->fetchrow_array();
$sth->finish;
return $data ? $self->deserializer->( $data ) : ();
}
sub store {
my ($self, $session_id, $session) = @_;
my $table_name = $self->{table_name};
my $data_column = $self->{data_column};
my $id_column = $self->{id_column};
my $dbh = $self->_dbh;
# XXX To be honest, I feel like there should be a transaction
# call here.... but Catalyst didn't have it, so I'm not so sure
my $sth = $dbh->prepare_cached(
sprintf(
"SELECT 1 FROM %s WHERE %s = ?",
$self->_quote_table_name($table_name),
$dbh->quote_identifier($id_column),
)
);
$sth->execute($session_id);
# need to fetch. on some DBD's execute()'s return status and
# rows() is not reliable
my ($exists) = $sth->fetchrow_array();
$sth->finish;
my %column_data = (
$self->additional_column_data($session),
$data_column => $self->serializer->($session),
$id_column => $session_id,
);
my @columns = sort keys %column_data;
if ($exists) {
my $sth = $dbh->prepare_cached(
sprintf(
"UPDATE %s SET %s WHERE %s = ?",
$self->_quote_table_name($table_name),
join(',', map { $dbh->quote_identifier($_).' = ?' } @columns),
$dbh->quote_identifier($id_column),
)
);
$sth->execute( @column_data{@columns}, $session_id );
}
else {
my $sth = $dbh->prepare_cached(
sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
$self->_quote_table_name($table_name),
join(',', map { $dbh->quote_identifier($_) } @columns),
join(',', map { '?' } @columns),
)
);
$sth->execute( @column_data{@columns});
}
}
sub additional_column_data { }
sub remove {
my ($self, $session_id) = @_;
my $table_name = $self->{table_name};
my $id_column = $self->{id_column};
my $sth = $self->_dbh->prepare_cached(
sprintf(
"DELETE FROM %s WHERE %s = ?",
$self->_quote_table_name($table_name),
$self->_dbh->quote_identifier($id_column),
)
);
$sth->execute( $session_id );
$sth->finish;
}
sub _quote_table_name {
my ($self, $table_name) = @_;
my @parts = split /\./, $table_name;
return join '.', map { $self->_dbh->quote_identifier( $_ ) } @parts;
}
1;
__END__
=head1 NAME
Plack::Session::Store::DBI - DBI-based session store
=head1 SYNOPSIS
use Plack::Builder;
use Plack::Middleware::Session;
use Plack::Session::Store::DBI;
my $app = sub {
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
};
builder {
enable 'Session',
store => Plack::Session::Store::DBI->new(
dbh => DBI->connect( @connect_args )
);
$app;
};
# set get_dbh callback for ondemand
builder {
enable 'Session',
store => Plack::Session::Store::DBI->new(
get_dbh => sub { DBI->connect( @connect_args ) }
);
$app;
};
# with custom serializer/deserializer
builder {
enable 'Session',
store => Plack::Session::Store::DBI->new(
dbh => DBI->connect( @connect_args )
# YAML takes its args in the opposite order
serializer => sub { YAML::DumpFile( reverse @_ ) },
deserializer => sub { YAML::LoadFile( @_ ) },
);
$app;
};
# use custom session table name, session ID or data columns
builder {
enable 'Session',
store => Plack::Session::Store::DBI->new(
dbh => DBI->connect( @connect_args ),
table_name => 'my_session_table',
id_column => 'session_id',
data_column => 'data',
);
$app;
};
=head1 DESCRIPTION
This implements a DBI based storage for session data. By
default it will use L<Storable> and L<MIME::Base64> to serialize and
deserialize the data, but this can be configured easily.
This is a subclass of L<Plack::Session::Store> and implements
its full interface.
=head1 SESSION TABLE SCHEMA
Your session table must have at least the following schema structure:
CREATE TABLE sessions (
id CHAR(72) PRIMARY KEY,
session_data TEXT
);
Note that MySQL TEXT fields only store 64KB, so if your session data
will exceed that size you'll want to move to MEDIUMTEXT, MEDIUMBLOB,
or larger.
You can opt to specify alternative table names (using table_name), as well as
alternative columns to use for session ID (id_column) and session data storage
(data_column), especially useful if you're converting from an existing session
mechanism.
=head1 EXTENDING
Plack::Session::Store::DBI has built in functionality to allow for inheriting
modules to set additional columns on each session row.
By overriding the additional_column_data function, you can return a hash of
columns and values to set. The session data hashref will be passed to the
overridden additional_column_data function as its only argument, so that you
can use session data values as appropriate for any additional column data you
would like to set.
=head1 AUTHORS
Many aspects of this module were partially based upon L<Catalyst::Plugin::Session::Store::DBI>
Daisuke Maki
=head1 COPYRIGHT AND LICENSE
Copyright 2009, 2010 Daisuke Maki C<< <daisuke@endeworks.jp> >>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|