| 1 | package Catalyst::Plugin::Session::Store::DBI; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use base qw/Class::Data::Inheritable Catalyst::Plugin::Session::Store/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use DBI; | ||
| 7 | use MIME::Base64; | ||
| 8 | use MRO::Compat; | ||
| 9 | use Storable qw/nfreeze thaw/; | ||
| 10 | |||
| 11 | our $VERSION = '0.16'; | ||
| 12 | |||
| 13 | __PACKAGE__->mk_classdata('_session_sql'); | ||
| 14 | __PACKAGE__->mk_classdata('_session_dbh'); | ||
| 15 | __PACKAGE__->mk_classdata('_sth_get_session_data'); | ||
| 16 | __PACKAGE__->mk_classdata('_sth_get_expires'); | ||
| 17 | __PACKAGE__->mk_classdata('_sth_check_existing'); | ||
| 18 | __PACKAGE__->mk_classdata('_sth_update_session'); | ||
| 19 | __PACKAGE__->mk_classdata('_sth_insert_session'); | ||
| 20 | __PACKAGE__->mk_classdata('_sth_update_expires'); | ||
| 21 | __PACKAGE__->mk_classdata('_sth_delete_session'); | ||
| 22 | __PACKAGE__->mk_classdata('_sth_delete_expired_sessions'); | ||
| 23 | |||
| 24 | sub get_session_data { | ||
| 25 | my ( $c, $key ) = @_; | ||
| 26 | |||
| 27 | # expires:sid expects an expiration time | ||
| 28 | if ( my ($sid) = $key =~ /^expires:(.*)/ ) { | ||
| 29 | $key = "session:$sid"; | ||
| 30 | my $sth = $c->_session_sth('get_expires'); | ||
| 31 | $sth->execute($key); | ||
| 32 | my ($expires) = $sth->fetchrow_array; | ||
| 33 | return $expires; | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | my $sth = $c->_session_sth('get_session_data'); | ||
| 37 | $sth->execute($key); | ||
| 38 | if ( my ($data) = $sth->fetchrow_array ) { | ||
| 39 | return thaw( decode_base64($data) ); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub store_session_data { | ||
| 46 | my ( $c, $key, $data ) = @_; | ||
| 47 | |||
| 48 | # expires:sid keys only update the expiration time | ||
| 49 | if ( my ($sid) = $key =~ /^expires:(.*)/ ) { | ||
| 50 | $key = "session:$sid"; | ||
| 51 | my $sth = $c->_session_sth('update_expires'); | ||
| 52 | $sth->execute( $c->session_expires, $key ); | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | # check for existing record | ||
| 56 | my $sth = $c->_session_sth('check_existing'); | ||
| 57 | $sth->execute($key); | ||
| 58 | my ($exists) = $sth->fetchrow_array; | ||
| 59 | |||
| 60 | # update or insert as needed | ||
| 61 | my $sta = ($exists) | ||
| 62 | ? $c->_session_sth('update_session') | ||
| 63 | : $c->_session_sth('insert_session'); | ||
| 64 | |||
| 65 | my $frozen = encode_base64( nfreeze($data) ); | ||
| 66 | my $expires = $key =~ /^(?:session|flash):/ | ||
| 67 | ? $c->session_expires | ||
| 68 | : undef; | ||
| 69 | $sta->execute( $frozen, $expires, $key ); | ||
| 70 | } | ||
| 71 | |||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub delete_session_data { | ||
| 76 | my ( $c, $key ) = @_; | ||
| 77 | |||
| 78 | return if $key =~ /^expires/; | ||
| 79 | |||
| 80 | my $sth = $c->_session_sth('delete_session'); | ||
| 81 | $sth->execute($key); | ||
| 82 | |||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub delete_expired_sessions { | ||
| 87 | my $c = shift; | ||
| 88 | |||
| 89 | my $sth = $c->_session_sth('delete_expired_sessions'); | ||
| 90 | $sth->execute(time); | ||
| 91 | |||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub prepare { | ||
| 96 | my $c = shift; | ||
| 97 | |||
| 98 | my $cfg = $c->_session_plugin_config; | ||
| 99 | |||
| 100 | # If using DBIC/CDBI, always grab their dbh | ||
| 101 | if ( $cfg->{dbi_dbh} ) { | ||
| 102 | $c->_session_dbic_connect(); | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | # make sure the database is still connected | ||
| 106 | eval { $c->_session_dbh->ping or die "dbh->ping failed" }; | ||
| 107 | if ($@) { | ||
| 108 | # reconnect | ||
| 109 | $c->_session_dbi_connect(); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | $c->maybe::next::method(@_); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub session_store_dbi_table { | ||
| 118 | return shift->_session_plugin_config->{'dbi_table'} || 'sessions'; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub session_store_dbi_id_field { | ||
| 122 | return shift->_session_plugin_config->{'dbi_id_field'} || 'id'; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub session_store_dbi_data_field { | ||
| 126 | return shift->_session_plugin_config->{'dbi_data_field'} || 'session_data'; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub session_store_dbi_expires_field { | ||
| 130 | return shift->_session_plugin_config->{'dbi_expires_field'} || 'expires'; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub setup_session { | ||
| 134 | my $c = shift; | ||
| 135 | |||
| 136 | $c->maybe::next::method(@_); | ||
| 137 | |||
| 138 | my $cfg = $c->_session_plugin_config; | ||
| 139 | |||
| 140 | unless ( $cfg->{dbi_dbh} || $cfg->{dbi_dsn} ) { | ||
| 141 | Catalyst::Exception->throw( | ||
| 142 | message => 'Session::Store::DBI: No session configuration found, ' | ||
| 143 | . 'please configure dbi_dbh or dbi_dsn' | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | |||
| 147 | # Pre-generate all SQL statements | ||
| 148 | my ( $table, $id_field, $data_field, $expires_field ) = | ||
| 149 | map { $c->${\"session_store_$_"} } | ||
| 150 | qw/dbi_table dbi_id_field dbi_data_field dbi_expires_field/; | ||
| 151 | $c->_session_sql( { | ||
| 152 | get_session_data => | ||
| 153 | "SELECT $data_field FROM $table WHERE $id_field = ?", | ||
| 154 | get_expires => | ||
| 155 | "SELECT $expires_field FROM $table WHERE $id_field = ?", | ||
| 156 | check_existing => | ||
| 157 | "SELECT 1 FROM $table WHERE $id_field = ?", | ||
| 158 | update_session => | ||
| 159 | "UPDATE $table SET $data_field = ?, $expires_field = ? WHERE $id_field = ?", | ||
| 160 | insert_session => | ||
| 161 | "INSERT INTO $table ($data_field, $expires_field, $id_field) VALUES (?, ?, ?)", | ||
| 162 | update_expires => | ||
| 163 | "UPDATE $table SET $expires_field = ? WHERE $id_field = ?", | ||
| 164 | delete_session => | ||
| 165 | "DELETE FROM $table WHERE $id_field = ?", | ||
| 166 | delete_expired_sessions => | ||
| 167 | "DELETE FROM $table WHERE $expires_field IS NOT NULL AND $expires_field < ?", | ||
| 168 | } ); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _session_dbi_connect { | ||
| 172 | my $c = shift; | ||
| 173 | |||
| 174 | my $cfg = $c->_session_plugin_config; | ||
| 175 | |||
| 176 | if ( $cfg->{dbi_dsn} ) { | ||
| 177 | |||
| 178 | # Allow user-supplied options. | ||
| 179 | my %options = ( | ||
| 180 | AutoCommit => 1, | ||
| 181 | RaiseError => 1, | ||
| 182 | %{ $cfg->{dbi_options} || {} } | ||
| 183 | ); | ||
| 184 | |||
| 185 | my $dbh = DBI->connect( | ||
| 186 | $cfg->{'dbi_dsn'}, | ||
| 187 | $cfg->{'dbi_user'}, | ||
| 188 | $cfg->{'dbi_pass'}, | ||
| 189 | \%options, | ||
| 190 | ) or Catalyst::Exception->throw( message => $DBI::errstr ); | ||
| 191 | $c->_session_dbh($dbh); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _session_dbic_connect { | ||
| 196 | my $c = shift; | ||
| 197 | |||
| 198 | my $cfg = $c->_session_plugin_config; | ||
| 199 | |||
| 200 | if ( $cfg->{dbi_dbh} ) { | ||
| 201 | if ( ref $cfg->{dbi_dbh} ) { | ||
| 202 | |||
| 203 | # use an existing db handle | ||
| 204 | if ( !$cfg->{dbi_dbh}->{Active} ) { | ||
| 205 | Catalyst::Exception->throw( message => | ||
| 206 | 'Session: Database handle supplied is not active' ); | ||
| 207 | } | ||
| 208 | $c->_session_dbh( $cfg->{dbi_dbh} ); | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | |||
| 212 | # use a DBIC/CDBI/RDBO class | ||
| 213 | my $class = $cfg->{dbi_dbh}; | ||
| 214 | my $dbh; | ||
| 215 | |||
| 216 | # DBIC Schema support | ||
| 217 | if ( $c->model($class) | ||
| 218 | && $c->model($class)->isa('Catalyst::Model::DBIC::Schema') | ||
| 219 | ) { | ||
| 220 | eval { $dbh = $c->model($class)->schema->storage->dbh }; | ||
| 221 | if ($@) { | ||
| 222 | Catalyst::Exception->throw( | ||
| 223 | message => "Unable to get a handle from " | ||
| 224 | . "DBIx::Class Schema model '$class': $@" | ||
| 225 | ); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | # Class-based DBIC support | ||
| 230 | elsif ( $c->model($class) | ||
| 231 | && $c->model($class)->isa('DBIx::Class::DB') | ||
| 232 | ) { | ||
| 233 | eval { $dbh = $c->model($class)->storage->dbh }; | ||
| 234 | if ($@) { | ||
| 235 | Catalyst::Exception->throw( | ||
| 236 | message => "Unable to get a handle from " | ||
| 237 | . "DBIx::Class model '$class': $@" | ||
| 238 | ); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | # CDBI support | ||
| 243 | elsif ( $class->isa('Class::DBI') ) { | ||
| 244 | eval { $dbh = $class->db_Main }; | ||
| 245 | if ($@) { | ||
| 246 | Catalyst::Exception->throw( | ||
| 247 | message => "Unable to get a handle from " | ||
| 248 | . "Class::DBI model '$class': $@" | ||
| 249 | ); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | # RDBO support | ||
| 254 | elsif ( $class->isa('Rose::DB::Object') ) { | ||
| 255 | eval { $dbh = $class->new->db->retain_dbh }; | ||
| 256 | if ($@) { | ||
| 257 | Catalyst::Exception->throw( | ||
| 258 | message => "Unable to get a handle from " | ||
| 259 | . "Rose::DB::Object '$class': $@" | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | # Model::DBI support | ||
| 265 | elsif ( $c->model($class) | ||
| 266 | && $c->model($class)->isa('Catalyst::Model::DBI') | ||
| 267 | ) { | ||
| 268 | eval { $dbh = $c->model($class)->dbh }; | ||
| 269 | if ($@) { | ||
| 270 | Catalyst::Exception->throw( | ||
| 271 | message => "Unable to get a handle from " | ||
| 272 | . "DBI model '$class': $@" | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | else { | ||
| 278 | Catalyst::Exception->throw( | ||
| 279 | message => "Unable to get a handle from " | ||
| 280 | . "model '$class': Does not appear " | ||
| 281 | . "to be a DBIx::Class, Class::DBI, " | ||
| 282 | . "or Rose::DB::Object class" | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | |||
| 286 | $c->_session_dbh($dbh); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | # Prepares SQL statements as needed | ||
| 292 | sub _session_sth { | ||
| 293 | my ( $c, $key ) = @_; | ||
| 294 | |||
| 295 | if ( my $sql = $c->_session_sql->{$key} ) { | ||
| 296 | my $accessor = "_sth_$key"; | ||
| 297 | |||
| 298 | if ( defined $c->$accessor ) { | ||
| 299 | |||
| 300 | # Check for the 'morning bug', where the dbh may have gone away | ||
| 301 | # while we still have cached sth's using it. | ||
| 302 | if ( $c->$accessor->{Database} ne $c->_session_dbh ) { | ||
| 303 | # The sth has an old dbh, so we need to prepare it again | ||
| 304 | if ( $c->$accessor->{Active} ) { | ||
| 305 | $c->$accessor->finish; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | else { | ||
| 309 | return $c->$accessor; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | return $c->$accessor( $c->_session_dbh->prepare( $sql ) ); | ||
| 314 | } | ||
| 315 | |||
| 316 | return; | ||
| 317 | } | ||
| 318 | |||
| 319 | # close any active sth's to avoid warnings | ||
| 320 | sub DESTROY { | ||
| 321 | my $c = shift; | ||
| 322 | $c->maybe::next::method(@_); | ||
| 323 | |||
| 324 | for my $key ( keys %{ $c->_session_sql } ) { | ||
| 325 | my $accessor = "_sth_$key"; | ||
| 326 | if ( defined $c->$accessor && $c->$accessor->{Active} ) { | ||
| 327 | $c->$accessor->finish; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | 1; | ||
| 333 | __END__ | ||