File Coverage

blib/lib/WWW/Session/Storage/MySQL.pm
Criterion Covered Total %
statement 9 43 20.9
branch 0 8 0.0
condition 0 6 0.0
subroutine 3 8 37.5
pod 4 4 100.0
total 16 69 23.1


line stmt bran cond sub pod time code
1             package WWW::Session::Storage::MySQL;
2              
3 1     1   1041 use 5.006;
  1         3  
  1         43  
4 1     1   5 use strict;
  1         3  
  1         29  
5 1     1   22 use warnings;
  1         18  
  1         645  
6              
7             =head1 NAME
8              
9             WWW::Session::Storage::MySQL - MySQL storage for WWW::Session
10              
11             =head1 DESCRIPTION
12              
13             MySQL backend for WWW:Session
14              
15             =head1 VERSION
16              
17             Version 0.10
18              
19             =cut
20              
21             our $VERSION = '0.10';
22              
23              
24             =head1 SYNOPSIS
25              
26             This module is used for storring serialized WWW::Session objects in MySQL
27              
28             Usage :
29              
30             use WWW::Session::Storage::MySQL;
31              
32             my $storage = WWW::Session::Storage::MySQL->new({
33             dbh => $dbh,
34             table => 'sessions',
35             fields => {
36             sid => 'session_id',
37             expires => 'expires',
38             data => 'data'
39             }
40             });
41             ...
42            
43             $storage->save($session_id,$expires,$serialized_data);
44            
45             my $serialized_data = $storage->retrive($session_id);
46              
47              
48             The "fields" hasref contains the mapping of session internal data to the column names from MySQL.
49             The keys are the session fields ("sid","expires" and "data") and must all be present.
50              
51             The MySQL types of the columns should be :
52              
53             =over 4
54              
55             =item * sid => varchar(32)
56              
57             =item * expires => DATETIME or TIMESTAMP
58              
59             =item * data => text
60              
61             =back
62              
63             =head1 SUBROUTINES/METHODS
64              
65             =head2 new
66              
67             Creates a new WWW::Session::Storage::MySQL object
68              
69             This method accepts only one argument, a hashref that must contain the fallowing data:
70              
71             =over 4
72              
73             =item * dbh Database handle
74              
75             =item * table The name of the table where the sessions will be stored
76              
77             =item * fields A hash ref containing the falowing keys
78              
79             =over 8
80              
81             =item * sid The same of the database field which will store the session id
82              
83             =item * expires The same of the database field which will store the expiration time
84              
85             =item * data The name of the field where the session data will be stored
86              
87             =back
88              
89             =back
90              
91             =cut
92              
93             sub new {
94 0     0 1   my ($class,$params) = @_;
95            
96 0           my $self = {
97             dbh => $params->{dbh},
98             table => $params->{table},
99             fields => $params->{fields},
100             };
101            
102 0           bless $self,$class;
103            
104 0           $self->_check_table_structure();
105            
106 0           return $self;
107             }
108              
109             =head2 save
110              
111             Stores the given information into the database
112              
113             =cut
114             sub save {
115 0     0 1   my ($self,$sid,$expires,$string) = @_;
116              
117 0 0         $expires = 60*60*24*365*20 if $expires == -1;
118              
119 0           my $query = sprintf('INSERT INTO %s SET %s=?, %s=?,%s=FROM_UNIXTIME(?) ON DUPLICATE KEY UPDATE %s=?, %s=FROM_UNIXTIME(?)',
120             $self->{table},
121 0           @{$self->{fields}}{qw(sid data expires)},
122 0           @{$self->{fields}}{qw(data expires)}
123             );
124              
125 0           my $sth = $self->{dbh}->prepare($query);
126              
127 0           my $rv = $sth->execute($sid,$string,time() + $expires, $string,time() + $expires);
128            
129 0           return $rv;
130             }
131              
132             =head2 retrieve
133              
134             Retrieves the informations for a session, verifies that it's not expired and returns
135             the string containing the serialized data
136              
137             =cut
138             sub retrieve {
139 0     0 1   my ($self,$sid) = @_;
140            
141 0           my $query = sprintf('SELECT %s as sid,%s as data,UNIX_TIMESTAMP(%s) as expires FROM %s WHERE %s=?',
142 0           @{$self->{fields}}{qw(sid data expires)},
143             $self->{table},
144             $self->{fields}->{sid}
145             );
146              
147 0           my $sth = $self->{dbh}->prepare($query);
148 0           $sth->execute($sid);
149            
150 0           my $info = $sth->fetchrow_hashref();
151            
152 0 0         return undef unless defined $info;
153            
154 0 0         if ( $info->{expires} < time() ) {
155 0           $self->delete($sid);
156 0           return undef;
157             }
158            
159 0           return $info->{data};
160              
161             }
162              
163             =head2 delete
164              
165             Completely removes the session data for the given session id
166              
167             =cut
168             sub delete {
169 0     0 1   my ($self,$sid) = @_;
170              
171 0           my $query = sprintf('DELETE FROM %s WHERE %s=?',
172             $self->{table},
173             $self->{fields}->{sid}
174             );
175              
176 0           my $sth = $self->{dbh}->prepare($query);
177 0           my $rv = $sth->execute($sid);
178              
179 0           return $rv;
180             }
181              
182             =head1 Private methods
183              
184             =head2 _determine_expires_type
185              
186             Tries to determine if the expires field is UnixTimestamp or DateTime
187              
188             =cut
189             sub _check_table_structure {
190 0     0     my $self = shift;
191            
192 0           my $sth = $self->{dbh}->prepare("DESCRIBE ".$self->{table});
193 0           $sth->execute();
194            
195 0           my $table_fields = $sth->fetchall_hashref('Field');
196            
197 0 0 0       die "The table structure doesn't match the field names you specified!"
      0        
198             unless exists $table_fields->{$self->{fields}->{sid}} &&
199             exists $table_fields->{$self->{fields}->{expires}} &&
200             exists $table_fields->{$self->{fields}->{data}};
201             }
202              
203             =head1 AUTHOR
204              
205             Gligan Calin Horea, C<< >>
206              
207             =head1 BUGS
208              
209             Please report any bugs or feature requests to C, or through
210             the web interface at L. I will be notified, and then you'll
211             automatically be notified of progress on your bug as I make changes.
212              
213              
214              
215              
216             =head1 SUPPORT
217              
218             You can find documentation for this module with the perldoc command.
219              
220             perldoc WWW::Session::Storage::MySQL
221              
222              
223             You can also look for information at:
224              
225             =over 4
226              
227             =item * RT: CPAN's request tracker (report bugs here)
228              
229             L
230              
231             =item * AnnoCPAN: Annotated CPAN documentation
232              
233             L
234              
235             =item * CPAN Ratings
236              
237             L
238              
239             =item * Search CPAN
240              
241             L
242              
243             =back
244              
245              
246             =head1 ACKNOWLEDGEMENTS
247              
248              
249             =head1 LICENSE AND COPYRIGHT
250              
251             Copyright 2012 Gligan Calin Horea.
252              
253             This program is free software; you can redistribute it and/or modify it
254             under the terms of either: the GNU General Public License as published
255             by the Free Software Foundation; or the Artistic License.
256              
257             See http://dev.perl.org/licenses/ for more information.
258              
259              
260             =cut
261              
262             1; # End of WWW::Session::Storage::MySQL