| 1 | package CGI::Scribe::DBI; | ||
| 2 | |||
| 3 | $MAX_TRIES = 100; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use vars qw( @ISA $MAX_TRIES ); | ||
| 7 | use Carp; | ||
| 8 | use Storable qw( freeze thaw ); | ||
| 9 | use DBI; | ||
| 10 | use CGI::Scribe; | ||
| 11 | |||
| 12 | @ISA = qw( CGI::Scribe ); | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | |||
| 17 | $class = ref $class || $class; | ||
| 18 | return $class->SUPER::new( @_ ); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub initialize { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | $self->SUPER::initialize; | ||
| 25 | |||
| 26 | $self->{autoload}{db_source} = 1; | ||
| 27 | $self->{autoload}{db_auth} = 1; | ||
| 28 | $self->{autoload}{db_table} = 1; | ||
| 29 | $self->{db_table} = 'sessions'; | ||
| 30 | } | ||
| 31 | |||
| 32 | # XXX There's a weird problem, at least with perl 5.005_03, | ||
| 33 | # DBI 1.06, and DBD::mysql 2.0217. It appears that the database | ||
| 34 | # handle is lost in our DESTROY() method -- as though the database | ||
| 35 | # handle's destructor is being called before ours. Adding | ||
| 36 | # the InactiveDestroy parameter fixed it. Even weirder, the | ||
| 37 | # value of InactiveDestroy doesn't seem to matter -- either 0 or 1 | ||
| 38 | # fixed the problem. It just needs to be there. This should | ||
| 39 | # be investigated. | ||
| 40 | # | ||
| 41 | # UPDATE: weird things are still occuring with the inherited | ||
| 42 | # destructor. So for now, we're including all of the | ||
| 43 | # necessary code in the subclass destructor and are not | ||
| 44 | # calling our parent's. | ||
| 45 | sub _connect { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | $self->{_dbh} = DBI->connect( $self->{db_source}, | ||
| 49 | split(':', $self->{db_auth}, 2), | ||
| 50 | { RaiseError => 0, | ||
| 51 | AutoCommit => 1, | ||
| 52 | InactiveDestroy => 0, | ||
| 53 | } | ||
| 54 | ) | ||
| 55 | or croak "unable to connect to database: $DBI::errstr"; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _new_session { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | # We don't need to do this here, since the call to SUPER::_new_session() | ||
| 62 | # sets these values appropriately | ||
| 63 | # | ||
| 64 | # $self->{_fetched} = 1; | ||
| 65 | # $self->{_dirty} = 0; | ||
| 66 | # $self->{is_new} = 1; | ||
| 67 | |||
| 68 | $self->_connect unless ref $self->{_dbh}; | ||
| 69 | |||
| 70 | my $frozen = freeze( $self->{_data} ); | ||
| 71 | |||
| 72 | my $sth = $self->{_dbh}->prepare_cached("INSERT INTO $self->{db_table} | ||
| 73 | (session, session_data, created) VALUES(?, ?, ?)"); | ||
| 74 | |||
| 75 | # now create a new session, and make sure it's unique | ||
| 76 | my $try = 0; | ||
| 77 | while(++$try <= $MAX_TRIES) { | ||
| 78 | $self->{session} = $self->SUPER::_new_session( $self->{session} ); | ||
| 79 | last if $sth->execute( $self->{session}, $frozen, time ); | ||
| 80 | $self->_debug('collision', 2) if $self->{debug}; | ||
| 81 | } | ||
| 82 | $sth->finish; | ||
| 83 | croak "unable to generate unique session id" if $try > $MAX_TRIES; | ||
| 84 | |||
| 85 | $self->_debug('selected as new session', 1) if $self->{debug}; | ||
| 86 | |||
| 87 | $self->{session}; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _fetch { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | $self->{_fetched} = 1; | ||
| 94 | |||
| 95 | $self->_connect unless ref $self->{_dbh}; | ||
| 96 | |||
| 97 | my $sth = $self->{_dbh}->prepare_cached("SELECT session_data FROM | ||
| 98 | $self->{db_table} WHERE session=?"); | ||
| 99 | $sth->execute( $self->{session} ) | ||
| 100 | or croak "error fetching session $self->{session}: $DBI::errstr"; | ||
| 101 | my $row = $sth->fetchrow_arrayref; | ||
| 102 | $sth->finish; | ||
| 103 | # If the fetch succeeded but was empty, return success | ||
| 104 | defined $row or return 1; | ||
| 105 | |||
| 106 | my $thawed = eval { thaw( $row->[0] ) }; | ||
| 107 | defined $thawed and ref $thawed eq 'HASH' | ||
| 108 | or croak "error thawing session $self->{session}"; | ||
| 109 | |||
| 110 | foreach my $key (keys %$thawed) { | ||
| 111 | $self->{_data}{ $key } = $thawed->{ $key }; | ||
| 112 | } | ||
| 113 | |||
| 114 | if ($self->{debug}) { | ||
| 115 | $self->_debug('fetched from server', 1); | ||
| 116 | foreach my $key (keys %$thawed) { | ||
| 117 | $self->_debug("server data: $key=$thawed->{$key}", 2); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _store { | ||
| 125 | my $self = shift; | ||
| 126 | |||
| 127 | $self->_debug('storing on server', 1) if $self->{debug}; | ||
| 128 | |||
| 129 | $self->{_dirty} = 0; | ||
| 130 | $self->_fetch unless $self->{_fetched}; | ||
| 131 | |||
| 132 | $self->_connect unless ref $self->{_dbh}; | ||
| 133 | |||
| 134 | my $frozen = eval { freeze( $self->{_data} ) }; | ||
| 135 | croak "error freezing session $self->{session}" unless defined $frozen; | ||
| 136 | |||
| 137 | my $sth = $self->{_dbh}->prepare_cached("UPDATE $self->{db_table} SET | ||
| 138 | session_data=? WHERE session=?"); | ||
| 139 | my $rows = $sth->execute( $frozen, $self->{session} ); | ||
| 140 | # XXX MySQL does not report all matched rows -- only changed rows | ||
| 141 | croak "error updating session $self->{session}: $DBI::errstr" | ||
| 142 | unless defined $rows; | ||
| 143 | $sth->finish; | ||
| 144 | |||
| 145 | # If we failed to update any rows, the session has probably expired | ||
| 146 | # and we need to create it again. | ||
| 147 | if ($rows < 1) { | ||
| 148 | $self->_debug('missing on server -- recreating', 1) if $self->{debug}; | ||
| 149 | $sth = $self->{_dbh}->prepare_cached("INSERT INTO $self->{db_table} | ||
| 150 | (session, session_data, created) VALUES(?, ?, ?)"); | ||
| 151 | $sth->execute( $self->{session}, $frozen, time ) | ||
| 152 | or croak "error recreating session $self->{session}: $DBI::errstr"; | ||
| 153 | $sth->finish; | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub DESTROY { | ||
| 160 | my $self = shift; | ||
| 161 | |||
| 162 | $self->_debug('destroying session object', 2) if $self->{debug}; | ||
| 163 | |||
| 164 | $self->_store if $self->{_dirty}; | ||
| 165 | $self->{_dbh}->disconnect if ref $self->{_dbh}; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; |