| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::Session::Store::Informix | ||
| 4 | # Implements session object storage via Informix | ||
| 5 | # Copyright(c) 1998, 1999, 2000, 2004 Jeffrey William Baker (jwbaker@acm.org) and Mike Langen (mike.langen@tamedia.ch) | ||
| 6 | # Distribute under the Perl License | ||
| 7 | # | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Apache::Session::Store::Informix; | ||
| 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.02'; | ||
| 21 | |||
| 22 | $Apache::Session::Store::Informix::DataSource = undef; | ||
| 23 | $Apache::Session::Store::Informix::UserName = undef; | ||
| 24 | $Apache::Session::Store::Informix::Password = undef; | ||
| 25 | |||
| 26 | sub connection { | ||
| 27 | my $self = shift; | ||
| 28 | my $session = shift; | ||
| 29 | |||
| 30 | return if (defined $self->{dbh}); | ||
| 31 | |||
| 32 | if (exists $session->{args}->{Handle}) { | ||
| 33 | $self->{dbh} = $session->{args}->{Handle}; | ||
| 34 | $self->{commit} = $session->{args}->{Commit}; | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $datasource = $session->{args}->{DataSource} || | ||
| 39 | $Apache::Session::Store::Informix::DataSource; | ||
| 40 | my $username = $session->{args}->{UserName} || | ||
| 41 | $Apache::Session::Store::Informix::UserName; | ||
| 42 | my $password = $session->{args}->{Password} || | ||
| 43 | $Apache::Session::Store::Informix::Password; | ||
| 44 | |||
| 45 | $self->{dbh} = DBI->connect( | ||
| 46 | $datasource, | ||
| 47 | $username, | ||
| 48 | $password, | ||
| 49 | { RaiseError => 1, AutoCommit => 0 } | ||
| 50 | ) || die $DBI::errstr; | ||
| 51 | |||
| 52 | |||
| 53 | #If we open the connection, we close the connection | ||
| 54 | $self->{disconnect} = 1; | ||
| 55 | |||
| 56 | #the programmer has to tell us what commit policy to use | ||
| 57 | $self->{commit} = $session->{args}->{Commit}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub materialize { | ||
| 61 | my $self = shift; | ||
| 62 | my $session = shift; | ||
| 63 | |||
| 64 | $self->connection($session); | ||
| 65 | |||
| 66 | local $self->{dbh}->{RaiseError} = 1; | ||
| 67 | local $self->{dbh}->{LongReadLen} = $session->{args}->{LongReadLen} || 8*2**10; | ||
| 68 | |||
| 69 | if (!defined $self->{materialize_sth}) { | ||
| 70 | $self->{materialize_sth} = | ||
| 71 | $self->{dbh}->prepare_cached(qq{ | ||
| 72 | SELECT a_session FROM sessions WHERE id = ? FOR UPDATE}); | ||
| 73 | } | ||
| 74 | |||
| 75 | $self->{materialize_sth}->bind_param(1, $session->{data}->{_session_id}); | ||
| 76 | $self->{materialize_sth}->execute; | ||
| 77 | |||
| 78 | my $results = $self->{materialize_sth}->fetchrow_arrayref; | ||
| 79 | |||
| 80 | if (!(defined $results)) { | ||
| 81 | die "Object does not exist in the data store"; | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->{materialize_sth}->finish; | ||
| 85 | |||
| 86 | $session->{serialized} = $results->[0]; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub DESTROY { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | if ($self->{commit}) { | ||
| 93 | $self->{dbh}->commit; | ||
| 94 | } | ||
| 95 | |||
| 96 | if ($self->{disconnect}) { | ||
| 97 | $self->{dbh}->disconnect; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | =pod | ||
| 104 | |||
| 105 | =head1 NAME | ||
| 106 | |||
| 107 | Apache::Session::Store::Informix - Store persistent data in a Informix database | ||
| 108 | |||
| 109 | =head1 SYNOPSIS | ||
| 110 | |||
| 111 | use Apache::Session::Store::Informix; | ||
| 112 | |||
| 113 | my $store = new Apache::Session::Store::Informix; | ||
| 114 | |||
| 115 | $store->insert($ref); | ||
| 116 | $store->update($ref); | ||
| 117 | $store->materialize($ref); | ||
| 118 | $store->remove($ref); | ||
| 119 | |||
| 120 | =head1 DESCRIPTION | ||
| 121 | |||
| 122 | Apache::Session::Store::Informix fulfills the storage interface of | ||
| 123 | Apache::Session. Session data is stored in a Informix database. | ||
| 124 | |||
| 125 | =head1 SCHEMA | ||
| 126 | |||
| 127 | To use this module, you will need at least these columns in a table | ||
| 128 | called 'sessions': | ||
| 129 | |||
| 130 | id char(32) # or however long your session IDs are. | ||
| 131 | a_session lvarchar | ||
| 132 | |||
| 133 | To create this schema, you can execute this command using the sqlplus program: | ||
| 134 | |||
| 135 | CREATE TABLE sessions ( | ||
| 136 | id char(32) not null primary key, | ||
| 137 | a_session lvarchar | ||
| 138 | ); | ||
| 139 | |||
| 140 | If you use some other command, ensure that there is a unique index on the | ||
| 141 | table's id column. | ||
| 142 | |||
| 143 | =head1 CONFIGURATION | ||
| 144 | |||
| 145 | The module must know what datasource, username, and password to use when | ||
| 146 | connecting to the database. These values can be set using the options hash | ||
| 147 | (see Apache::Session documentation). The options are DataSource, UserName, | ||
| 148 | and Password. | ||
| 149 | |||
| 150 | Example: | ||
| 151 | |||
| 152 | tie %hash, 'Apache::Session::Informix', $id, { | ||
| 153 | DataSource => 'dbi:Informix:database', | ||
| 154 | UserName => 'database_user', | ||
| 155 | Password => 'K00l' | ||
| 156 | }; | ||
| 157 | |||
| 158 | Instead, you may pass in an already-opened DBI handle to your database. | ||
| 159 | |||
| 160 | tie %hash, 'Apache::Session::Informix', $id, { | ||
| 161 | Handle => $dbh | ||
| 162 | }; | ||
| 163 | |||
| 164 | The last option is LongReadLen, which specifies the maximum size of the session | ||
| 165 | object. If not supplied, the default maximum size is 8 KB. | ||
| 166 | |||
| 167 | =head1 AUTHOR | ||
| 168 | |||
| 169 | This module was written by Mike Langen <mike.langen@tamedia.ch>, based | ||
| 170 | on the original for Oracle. | ||
| 171 | |||
| 172 | =head1 SEE ALSO | ||
| 173 | |||
| 174 | L<Apache::Session>, L<Apache::Session::Store::DBI> |