Subject: | Store method fails in CGI::Session::Driver::mysql for MySQL 4.0.16 |
According to these docs:
http://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html
"If you specify ON DUPLICATE KEY UPDATE (added in MySQL 4.1.0), and a
row is inserted that would cause a duplicate value in a UNIQUE index or
PRIMARY KEY, an UPDATE of the old row is performed"
So, I was unable to store session data using MySQL 4.0.16 It's not a
situation where I can easily upgrade MySQL, so I had to write a custom
driver with a store method which uses the "REPLACE INTO " syntax. I've
attached it in case anyone finds it helpful.
It might be helpful to have some kind of MySQL version parameter that
could be passed to the driver in order to support older versions of
MySQL. I can supply a patch if that's something that you might consider.
All the best,
Olaf
Subject: | mysql4.pm |
package CGI::Session::Driver::mysql4;
use strict;
use warnings;
use base 'CGI::Session::Driver::mysql';
use Carp qw( croak );
sub store {
my $self = shift;
my ($sid, $datastr) = @_;
croak "store(): usage error" unless $sid && $datastr;
my $dbh = $self->{Handle};
my $table = $self->table_name;
my $replace_query = qq[
REPLACE INTO
$table
( $self->{IdColName}, $self->{DataColName} )
VALUES
(?, ?)
];
$dbh->do( $replace_query, undef, $sid, $datastr, $datastr )
or return $self->set_error( "store(): \$dbh->do failed " . $dbh->errstr );
return 1;
}
1;