| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::Session::Store::DBI | ||
| 4 | # A base class for the MySQL, Postgres, and other DBI stores | ||
| 5 | # Copyright(c) 2000, 2004 Jeffrey William Baker (jwbaker@acm.org) | ||
| 6 | # Distribute under the Perl License | ||
| 7 | # | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Apache::Session::Store::DBI; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use DBI; | ||
| 14 | |||
| 15 | use vars qw($VERSION); | ||
| 16 | |||
| 17 | $VERSION = '1.02'; | ||
| 18 | |||
| 19 | $Apache::Session::Store::DBI::TableName = "sessions"; | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | |||
| 24 | return bless { table_name => $Apache::Session::Store::DBI::TableName }, $class; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub insert { | ||
| 28 | my $self = shift; | ||
| 29 | my $session = shift; | ||
| 30 | |||
| 31 | $self->connection($session); | ||
| 32 | |||
| 33 | local $self->{dbh}->{RaiseError} = 1; | ||
| 34 | |||
| 35 | if (!defined $self->{insert_sth}) { | ||
| 36 | $self->{insert_sth} = | ||
| 37 | $self->{dbh}->prepare_cached(qq{ | ||
| 38 | INSERT INTO $self->{'table_name'} (id, a_session) VALUES (?,?)}); | ||
| 39 | } | ||
| 40 | |||
| 41 | $self->{insert_sth}->bind_param(1, $session->{data}->{_session_id}); | ||
| 42 | $self->{insert_sth}->bind_param(2, $session->{serialized}); | ||
| 43 | |||
| 44 | $self->{insert_sth}->execute; | ||
| 45 | |||
| 46 | $self->{insert_sth}->finish; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub update { | ||
| 51 | my $self = shift; | ||
| 52 | my $session = shift; | ||
| 53 | |||
| 54 | $self->connection($session); | ||
| 55 | |||
| 56 | local $self->{dbh}->{RaiseError} = 1; | ||
| 57 | |||
| 58 | if (!defined $self->{update_sth}) { | ||
| 59 | $self->{update_sth} = | ||
| 60 | $self->{dbh}->prepare_cached(qq{ | ||
| 61 | UPDATE $self->{'table_name'} SET a_session = ? WHERE id = ?}); | ||
| 62 | } | ||
| 63 | |||
| 64 | $self->{update_sth}->bind_param(1, $session->{serialized}); | ||
| 65 | $self->{update_sth}->bind_param(2, $session->{data}->{_session_id}); | ||
| 66 | |||
| 67 | $self->{update_sth}->execute; | ||
| 68 | |||
| 69 | $self->{update_sth}->finish; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub materialize { | ||
| 73 | my $self = shift; | ||
| 74 | my $session = shift; | ||
| 75 | |||
| 76 | $self->connection($session); | ||
| 77 | |||
| 78 | local $self->{dbh}->{RaiseError} = 1; | ||
| 79 | |||
| 80 | if (!defined $self->{materialize_sth}) { | ||
| 81 | $self->{materialize_sth} = | ||
| 82 | $self->{dbh}->prepare_cached(qq{ | ||
| 83 | SELECT a_session FROM $self->{'table_name'} WHERE id = ?}); | ||
| 84 | } | ||
| 85 | |||
| 86 | $self->{materialize_sth}->bind_param(1, $session->{data}->{_session_id}); | ||
| 87 | |||
| 88 | $self->{materialize_sth}->execute; | ||
| 89 | |||
| 90 | my $results = $self->{materialize_sth}->fetchrow_arrayref; | ||
| 91 | |||
| 92 | if (!(defined $results)) { | ||
| 93 | die "Object does not exist in the data store"; | ||
| 94 | } | ||
| 95 | |||
| 96 | $self->{materialize_sth}->finish; | ||
| 97 | |||
| 98 | $session->{serialized} = $results->[0]; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub remove { | ||
| 102 | my $self = shift; | ||
| 103 | my $session = shift; | ||
| 104 | |||
| 105 | $self->connection($session); | ||
| 106 | |||
| 107 | local $self->{dbh}->{RaiseError} = 1; | ||
| 108 | |||
| 109 | if (!defined $self->{remove_sth}) { | ||
| 110 | $self->{remove_sth} = | ||
| 111 | $self->{dbh}->prepare_cached(qq{ | ||
| 112 | DELETE FROM $self->{'table_name'} WHERE id = ?}); | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->{remove_sth}->bind_param(1, $session->{data}->{_session_id}); | ||
| 116 | |||
| 117 | $self->{remove_sth}->execute; | ||
| 118 | $self->{remove_sth}->finish; | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; |