| 1 | |||
| 2 | package CGI::Session::Driver::pure_sql; | ||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION @ISA $SERIALIZER); | ||
| 5 | |||
| 6 | require CGI::Session::Driver::DBI; | ||
| 7 | @ISA = qw( CGI::Session::Driver::DBI ); # provides 'table_name' | ||
| 8 | #use base 'CGI::Session::Driver::DBI'; | ||
| 9 | |||
| 10 | $VERSION = '0.70'; | ||
| 11 | |||
| 12 | $SERIALIZER = 'CGI::Session::Serialize::sql_abstract'; | ||
| 13 | require CGI::Session::Serialize::sql_abstract; | ||
| 14 | |||
| 15 | sub init { | ||
| 16 | my $self = shift; | ||
| 17 | return $self->SUPER::init() | ||
| 18 | } | ||
| 19 | |||
| 20 | sub store { | ||
| 21 | my ($self, $sid, $data_href) = @_; | ||
| 22 | |||
| 23 | my $dbh = $self->{Handle}; | ||
| 24 | |||
| 25 | my $session_exists; | ||
| 26 | eval { | ||
| 27 | ($session_exists) = $dbh->selectrow_array( | ||
| 28 | ' SELECT session_id FROM '.$self->table_name. | ||
| 29 | ' WHERE session_id = ? FOR UPDATE',{},$sid); | ||
| 30 | |||
| 31 | }; | ||
| 32 | if( $@ ) { | ||
| 33 | $self->error("Couldn't acquire data on id '$sid'"); | ||
| 34 | return undef; | ||
| 35 | } | ||
| 36 | |||
| 37 | eval { require SQL::Abstract; }; | ||
| 38 | if ($@) { | ||
| 39 | $self->error('SQL::Abstract required but not found.'); | ||
| 40 | return undef; | ||
| 41 | } | ||
| 42 | my $sa = SQL::Abstract->new(); | ||
| 43 | |||
| 44 | eval { | ||
| 45 | if ($session_exists) { | ||
| 46 | my($stmt, @bind) = | ||
| 47 | $sa->update( | ||
| 48 | $self->table_name, | ||
| 49 | $data_href, | ||
| 50 | { session_id => $sid }); | ||
| 51 | $dbh->do($stmt,{},@bind); | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | my($stmt, @bind) = $sa->insert( $self->table_name, $data_href ); | ||
| 55 | |||
| 56 | $dbh->do($stmt,{},@bind); | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | }; | ||
| 61 | |||
| 62 | if( $@ ) { | ||
| 63 | $self->error("Error in session update on id '$sid'. $@"); | ||
| 64 | warn("Error in session update on id '$sid'. $@"); | ||
| 65 | return undef; | ||
| 66 | } | ||
| 67 | |||
| 68 | return 1; | ||
| 69 | |||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub retrieve { | ||
| 75 | my ($self, $sid ) = @_; | ||
| 76 | my $dbh = $self->{Handle}; | ||
| 77 | my $drv = $dbh->{Driver}->{Name}; | ||
| 78 | |||
| 79 | my $data; | ||
| 80 | |||
| 81 | my $epoch_func; | ||
| 82 | if ($dbh->{Driver}->{Name} eq 'mysql') { | ||
| 83 | $epoch_func = sub { sprintf 'UNIX_TIMESTAMP(%s)', $_[0] }; | ||
| 84 | } | ||
| 85 | elsif ($dbh->{Driver}->{Name} eq 'Pg') { | ||
| 86 | $epoch_func = sub { sprintf 'EXTRACT(EPOCH FROM %s)', $_[0] }; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | $self->error('Unsupported DBI driver. Currently only Pg and mysql are supported.'); | ||
| 90 | return undef; | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | eval { | ||
| 95 | $data = $dbh->selectrow_hashref( | ||
| 96 | ' SELECT * | ||
| 97 | , '.$epoch_func->('creation_time') .' as creation_time | ||
| 98 | , '.$epoch_func->('last_access_time') .' as last_access_time | ||
| 99 | , '.$epoch_func->("last_access_time + duration").'as end_time | ||
| 100 | FROM '.$self->table_name. | ||
| 101 | ' WHERE session_id = '.$dbh->quote($sid) | ||
| 102 | ); | ||
| 103 | }; | ||
| 104 | if( $@ ) { | ||
| 105 | $self->error("Couldn't acquire data on id '$sid'"); | ||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | return 0 unless $data; | ||
| 110 | |||
| 111 | my $thawed_data = $SERIALIZER->thaw( $data ); | ||
| 112 | unless ( defined $thawed_data ) { | ||
| 113 | return $self->set_error( "couldn't freeze data: " . $SERIALIZER->errstr() ); | ||
| 114 | } | ||
| 115 | |||
| 116 | return $thawed_data; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | sub remove { | ||
| 122 | my ($self, $sid, $options) = @_; | ||
| 123 | my $dbh = $self->{Handle}; | ||
| 124 | |||
| 125 | eval { $dbh->do( 'DELETE FROM '.$self->table_name.' WHERE session_id = '.$dbh->quote($sid)) }; | ||
| 126 | if( $@ ) { | ||
| 127 | warn $@; | ||
| 128 | $self->error("Couldn't delete session row for: '$sid'"); | ||
| 129 | return undef; | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | die "testing!"; | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | # Called right before the object is destroyed to do cleanup | ||
| 140 | sub DESTROY { | ||
| 141 | my ($self, $sid, $options) = @_; | ||
| 142 | |||
| 143 | my $dbh = $self->{Handle}; | ||
| 144 | |||
| 145 | # Call commit if we are in control of the handle | ||
| 146 | # /and/ AutoCommit is not in effect | ||
| 147 | # /and/ the object is modified or deleted. | ||
| 148 | if ($self->{_disconnect} && | ||
| 149 | !$dbh->{AutoCommit} && | ||
| 150 | (($self->{_STATUS} == MODIFIED() ) or ($self->{_STATUS} == DELETED()))) { | ||
| 151 | $dbh->commit(); | ||
| 152 | } | ||
| 153 | |||
| 154 | if ( $self->{_disconnect} ) { | ||
| 155 | $dbh->disconnect(); | ||
| 156 | } | ||
| 157 | |||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | =pod | ||
| 164 | |||
| 165 | =head1 NAME | ||
| 166 | |||
| 167 | CGI::Session::Driver::pure_sql - Pure SQL driver with no embedded Perl stored in the database | ||
| 168 | |||
| 169 | =head1 SYNOPSIS | ||
| 170 | |||
| 171 | use CGI::Session::Driver::pure_sql; | ||
| 172 | $session = CGI::Session->new("driver:pure_sql;serializer:sql_abstract", undef, {Handle=>$dbh}); | ||
| 173 | |||
| 174 | For more examples, consult L<CGI::Session> manual | ||
| 175 | |||
| 176 | =head1 DESCRIPTION | ||
| 177 | |||
| 178 | *Disclaimer* While this software is complete and includes a working test suite, | ||
| 179 | I'm marking it as a development release to leave room for feedback on the | ||
| 180 | interface. Until that happens, it's possible I may make changes that aren't | ||
| 181 | backwards compatible. You can help things along by communicating by providing | ||
| 182 | feedback about the module yourself. | ||
| 183 | |||
| 184 | CGI::Session::Driver::pure_sql is a CGI::Session driver to store session | ||
| 185 | data in a SQL table. Unlike the C<CGI::Session::Driver::postgresql> driver, this | ||
| 186 | "pure SQL" driver does not serialize any Perl data structures to the database. | ||
| 187 | |||
| 188 | The means that you can access all the data in the session easily using standard | ||
| 189 | SQL syntax. | ||
| 190 | |||
| 191 | The downside side is that you have create the columns for any data you want to | ||
| 192 | store, and each field will have just one value: You can't store arbitrary data | ||
| 193 | like you can with the L<CGI::Session::Driver::postgresql> driver. However, you may already be in | ||
| 194 | the habit of writing applications which use standard SQL structures, so this | ||
| 195 | may not be much of a drawback. :) | ||
| 196 | |||
| 197 | It currently requires the sql_abstract serializer to work, which is included in | ||
| 198 | the distribution. | ||
| 199 | |||
| 200 | =head1 STORAGE | ||
| 201 | |||
| 202 | To store session data in SQL database, you first need | ||
| 203 | to create a suitable table for it with the following command: | ||
| 204 | |||
| 205 | -- This syntax for for Postgres; flavor to taste | ||
| 206 | CREATE TABLE sessions ( | ||
| 207 | session_id CHAR(32) NOT NULL, | ||
| 208 | remote_addr inet, | ||
| 209 | creation_time timestamp, | ||
| 210 | last_access_time timestamp, | ||
| 211 | duration interval | ||
| 212 | ); | ||
| 213 | |||
| 214 | You can also add any number of additional columns to the table, | ||
| 215 | but the above fields are required. | ||
| 216 | |||
| 217 | For any additional columns you add, if you would like to | ||
| 218 | expire that column individually, you need to an additional | ||
| 219 | column to do that. For example, to add a column named C<order_id> | ||
| 220 | which you want to allow to be expired, you would add these two columns: | ||
| 221 | |||
| 222 | order_id int, | ||
| 223 | order_id_exp_secs int, | ||
| 224 | |||
| 225 | If you want to store the session data in other table than "sessions", | ||
| 226 | you will also need to specify B<TableName> attribute as the | ||
| 227 | first argument to new(): | ||
| 228 | |||
| 229 | use CGI::Session; | ||
| 230 | |||
| 231 | $session = CGI::Session->new("driver:pure_sql;serializer:sql_abstract", undef, | ||
| 232 | {Handle=>$dbh, TableName=>'my_sessions'}); | ||
| 233 | |||
| 234 | Every write access to session records is done through PostgreSQL own row locking mechanism, | ||
| 235 | enabled by `FOR UPDATE' clauses in SELECTs or implicitly enabled in UPDATEs and DELETEs. | ||
| 236 | |||
| 237 | To write your own drivers for B<CGI::Session> refere L<CGI::Session> manual. | ||
| 238 | |||
| 239 | =head1 COPYRIGHT | ||
| 240 | |||
| 241 | Copyright (C) 2003-2010 Mark Stosberg. All rights reserved. | ||
| 242 | |||
| 243 | This library is free software and can be modified and distributed under the same | ||
| 244 | terms as Perl itself. | ||
| 245 | |||
| 246 | =head1 CONTRIBUTING | ||
| 247 | |||
| 248 | Patches, questions and feedback are welcome. Please use the bug tracker to submit | ||
| 249 | bugs and patches, and e-mail directly with questions and feedback. | ||
| 250 | |||
| 251 | https://rt.cpan.org/Public/Dist/Display.html?Name=CGI-Session-Driver-pure_sql | ||
| 252 | |||
| 253 | =head1 AUTHOR | ||
| 254 | |||
| 255 | Mark Stosberg <mark@summersault.com> | ||
| 256 | |||
| 257 | =head1 SEE ALSO | ||
| 258 | |||
| 259 | =over 4 | ||
| 260 | |||
| 261 | =item * | ||
| 262 | |||
| 263 | L<CGI::Session|CGI::Session> - CGI::Session manual | ||
| 264 | |||
| 265 | =item * | ||
| 266 | |||
| 267 | L<CGI::Session::Tutorial|CGI::Session::Tutorial> - extended CGI::Session manual | ||
| 268 | |||
| 269 | =item * | ||
| 270 | |||
| 271 | L<CGI::Session::CookBook|CGI::Session::CookBook> - practical solutions for real life problems | ||
| 272 | |||
| 273 | =item * | ||
| 274 | |||
| 275 | B<RFC 2965> - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt | ||
| 276 | |||
| 277 | =item * | ||
| 278 | |||
| 279 | L<CGI|CGI> - standard CGI library | ||
| 280 | |||
| 281 | =item * | ||
| 282 | |||
| 283 | L<Apache::Session|Apache::Session> - another fine alternative to CGI::Session | ||
| 284 | |||
| 285 | =back | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | |||
| 290 |