| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::Session::Store::MySQL | ||
| 4 | # Implements session object storage via MySQL | ||
| 5 | # Copyright(c) 1998, 1999, 2000, 2004 Jeffrey William Baker (jwbaker@acm.org) | ||
| 6 | # Distribute under the Perl License | ||
| 7 | # | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Apache::Session::Store::MySQL; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | |||
| 14 | use DBI; | ||
| 15 | use Apache::Session::Store::DBI; | ||
| 16 | |||
| 17 | use vars qw(@ISA $VERSION); | ||
| 18 | |||
| 19 | @ISA = qw(Apache::Session::Store::DBI); | ||
| 20 | $VERSION = '1.04'; | ||
| 21 | |||
| 22 | $Apache::Session::Store::MySQL::DataSource = undef; | ||
| 23 | $Apache::Session::Store::MySQL::UserName = undef; | ||
| 24 | $Apache::Session::Store::MySQL::Password = undef; | ||
| 25 | |||
| 26 | sub connection { | ||
| 27 | my $self = shift; | ||
| 28 | my $session = shift; | ||
| 29 | |||
| 30 | return if (defined $self->{dbh}); | ||
| 31 | |||
| 32 | $self->{'table_name'} = $session->{args}->{TableName} || $Apache::Session::Store::DBI::TableName; | ||
| 33 | |||
| 34 | if (exists $session->{args}->{Handle}) { | ||
| 35 | $self->{dbh} = $session->{args}->{Handle}; | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $datasource = $session->{args}->{DataSource} || | ||
| 40 | $Apache::Session::Store::MySQL::DataSource; | ||
| 41 | my $username = $session->{args}->{UserName} || | ||
| 42 | $Apache::Session::Store::MySQL::UserName; | ||
| 43 | my $password = $session->{args}->{Password} || | ||
| 44 | $Apache::Session::Store::MySQL::Password; | ||
| 45 | |||
| 46 | $self->{dbh} = DBI->connect( | ||
| 47 | $datasource, | ||
| 48 | $username, | ||
| 49 | $password, | ||
| 50 | { RaiseError => 1, AutoCommit => 1 } | ||
| 51 | ) || die $DBI::errstr; | ||
| 52 | |||
| 53 | |||
| 54 | #If we open the connection, we close the connection | ||
| 55 | $self->{disconnect} = 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub DESTROY { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | if ($self->{disconnect}) { | ||
| 62 | $self->{dbh}->disconnect; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | =pod | ||
| 70 | |||
| 71 | =head1 NAME | ||
| 72 | |||
| 73 | Apache::Session::Store::MySQL - Store persistent data in a MySQL database | ||
| 74 | |||
| 75 | =head1 SYNOPSIS | ||
| 76 | |||
| 77 | use Apache::Session::Store::MySQL; | ||
| 78 | |||
| 79 | my $store = new Apache::Session::Store::MySQL; | ||
| 80 | |||
| 81 | $store->insert($ref); | ||
| 82 | $store->update($ref); | ||
| 83 | $store->materialize($ref); | ||
| 84 | $store->remove($ref); | ||
| 85 | |||
| 86 | =head1 DESCRIPTION | ||
| 87 | |||
| 88 | Apache::Session::Store::MySQL fulfills the storage interface of Apache::Session. | ||
| 89 | Session data is stored in a MySQL database. | ||
| 90 | |||
| 91 | =head1 SCHEMA | ||
| 92 | |||
| 93 | To use this module, you will need at least these columns in a table | ||
| 94 | called 'sessions', or another table name if you provide the TableName | ||
| 95 | argument: | ||
| 96 | |||
| 97 | id char(32) # or however long your session IDs are. | ||
| 98 | a_session blob # or varbinary if you plan to use a big session (>64k after serialization) | ||
| 99 | |||
| 100 | To create this schema, you can execute this command using the mysql program: | ||
| 101 | |||
| 102 | CREATE TABLE sessions ( | ||
| 103 | id char(32) not null primary key, | ||
| 104 | a_session blob | ||
| 105 | ); | ||
| 106 | |||
| 107 | If you use some other command, ensure that there is a unique index on the | ||
| 108 | table's id column. | ||
| 109 | |||
| 110 | =head1 CONFIGURATION | ||
| 111 | |||
| 112 | The module must know what datasource, username, and password to use when | ||
| 113 | connecting to the database. These values can be set using the options hash | ||
| 114 | (see Apache::Session documentation). The options are: | ||
| 115 | |||
| 116 | =over 4 | ||
| 117 | |||
| 118 | =item DataSource | ||
| 119 | |||
| 120 | =item UserName | ||
| 121 | |||
| 122 | =item Password | ||
| 123 | |||
| 124 | =item TableName | ||
| 125 | |||
| 126 | =item Handle | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | Example: | ||
| 131 | |||
| 132 | tie %hash, 'Apache::Session::MySQL', $id, { | ||
| 133 | DataSource => 'dbi:mysql:database', | ||
| 134 | UserName => 'database_user', | ||
| 135 | Password => 'K00l', | ||
| 136 | TableName => 'sessions' | ||
| 137 | }; | ||
| 138 | |||
| 139 | Instead, you may pass in an already-opened DBI handle to your database. | ||
| 140 | |||
| 141 | tie %hash, 'Apache::Session::MySQL', $id, { | ||
| 142 | Handle => $dbh | ||
| 143 | }; | ||
| 144 | |||
| 145 | =head1 AUTHOR | ||
| 146 | |||
| 147 | This modules was written by Jeffrey William Baker <jwbaker@acm.org> | ||
| 148 | |||
| 149 | =head1 SEE ALSO | ||
| 150 | |||
| 151 | L<Apache::Session> |