############################################################################### # Title: Enhanced_MySQL.pm # By: Noel Geren # Memo: Re-write of the MySQL driver # $Id: Enhanced_MySQL.pm,v 1.6 2004/03/27 16:23:39 ngeren Exp $ ############################################################################### package CGI::Session::Enhanced_MySQL; use strict; use base qw( CGI::Session CGI::Session::ID::MD5 CGI::Session::Serialize::Default ); use Data::Dumper; use DBI; use vars qw($VERSION $TABLE_NAME); $VERSION = '$Id: Enhanced_MySQL.pm,v 1.6 2004/03/27 16:23:39 ngeren Exp $'; $TABLE_NAME = 'sessions'; ############################################################################### # store ( , , , ) ############################################################################### sub store { my ($self, $sid, $options, $data) = @_; my ($dbh) = $self->getConnection($options); if ($dbh) { my $storable_data = $self->freeze($data); my ($replace_sth) = $dbh->prepare("REPLACE $TABLE_NAME SET id = ?, session = ?"); my ($replaced) = $replace_sth->execute($sid,$storable_data); $replace_sth->finish(); $dbh->disconnect(); return (1); } return (0); } ############################################################################### ############################################################################### # retrieve ( , , ) ############################################################################### sub retrieve { my ($self, $sid, $options) = @_; my ($dbh) = $self->getConnection($options); if ($dbh) { my ($sth) = $dbh->prepare("SELECT session FROM $TABLE_NAME WHERE id = ?"); $sth->execute($sid); my ($frozen_data) = $sth->fetchrow_array(); my ($found) = $sth->rows(); $sth->finish(); $dbh->disconnect(); return ($self->thaw($frozen_data)); } return undef; } ############################################################################### ############################################################################### # remove ( , , ) ############################################################################### sub remove { my ($self, $sid, $options) = @_; my ($dbh) = $self->getConnection($options); if ($dbh) { my ($removed) = $dbh->do("DELETE FROM $TABLE_NAME WHERE id = ?",undef,$sid); $dbh->disconnect(); if ($removed>0) { return (1); } else { return (0); } } } ############################################################################### ############################################################################### # teardown ( , , ) ############################################################################### sub teardown { my ($self, $sid, $options) = @_; return (1); } ############################################################################### ############################################################################### # getConnection ( , ) ############################################################################### sub getConnection { my ($self,$options) = @_; my (%connection_options) = %{@{$options}[1]}; my ($user) = $connection_options{'User'}; my ($password) = $connection_options{'Password'}; my ($dsn) = $connection_options{'DSN'}; if(!$dsn) { print STDERR "No DSN Specified\n"; return (undef); } else { my ($dbh) = DBI->connect($dsn,$user,$password) || die "$!\n\n"; return ($dbh); } } ############################################################################### 1; =pod =head1 NAME CGI::Session::Enhanced_MySQL - Updated MySQL CGI::Session driver =head1 SYNOPSIS use CGI::Session:: $session = new CGI::Session("driver:Enhanced_MySQL", undef, { 'DSN' => 'DBI:mysql:[table]:[host]', 'User' => '[username]', 'Password' => '[password]', }); For more examples, consult L manual =head1 DESCRIPTION CGI::Session::Enhanced_MySQL is a CGI::Session driver. To write your own drivers for B refere L manual. =head1 COPYRIGHT Copyright (C) 2004 Noel Geren. All rights reserved. This library is free software and can be modified and distributed under the same terms as Perl itself. =head1 AUTHOR Noel Geren =head1 SEE ALSO =over 4 =item * L - CGI::Session manual =item * L - extended CGI::Session manual =item * L - practical solutions for real life problems =item * B - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt =item * L - standard CGI library =item * L - another fine alternative to CGI::Session =back =cut # $Id: Enhanced_MySQL.pm,v 1.6 2004/03/27 16:23:39 ngeren Exp $