###############################################################################
# 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 ( <self>, <sid>, <options>, <data> )
###############################################################################
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 ( <self>, <sid>, <options> )
###############################################################################
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 ( <self>, <sid>, <options> )
###############################################################################
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 ( <self>, <sid>, <options> )
###############################################################################
sub teardown {
    my ($self, $sid, $options) = @_;
	return (1);
}
###############################################################################


###############################################################################
# getConnection ( <self>, <options> )
###############################################################################
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<CGI::Session> manual

=head1 DESCRIPTION

CGI::Session::Enhanced_MySQL is a CGI::Session driver.
To write your own drivers for B<CGI::Session> refere L<CGI::Session> 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|CGI::Session> - CGI::Session manual

=item *

L<CGI::Session::Tutorial|CGI::Session::Tutorial> - extended CGI::Session manual

=item *

L<CGI::Session::CookBook|CGI::Session::CookBook> - practical solutions for real life problems

=item *

B<RFC 2965> - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt

=item *

L<CGI|CGI> - standard CGI library

=item *

L<Apache::Session|Apache::Session> - another fine alternative to CGI::Session

=back

=cut

# $Id: Enhanced_MySQL.pm,v 1.6 2004/03/27 16:23:39 ngeren Exp $