[Catalyst-commits] r13071 - in Catalyst-Plugin-Session-Store-DBI/trunk: lib/Catalyst/Plugin/Session/Store t t/lib

wreis at dev.catalyst.perl.org wreis at dev.catalyst.perl.org
Tue Mar 23 21:42:56 GMT 2010


Author: wreis
Date: 2010-03-23 21:42:56 +0000 (Tue, 23 Mar 2010)
New Revision: 13071

Modified:
   Catalyst-Plugin-Session-Store-DBI/trunk/lib/Catalyst/Plugin/Session/Store/DBI.pm
   Catalyst-Plugin-Session-Store-DBI/trunk/t/04dbi.t
   Catalyst-Plugin-Session-Store-DBI/trunk/t/lib/TestApp.pm
Log:
added support to make table metadata configurable

Modified: Catalyst-Plugin-Session-Store-DBI/trunk/lib/Catalyst/Plugin/Session/Store/DBI.pm
===================================================================
--- Catalyst-Plugin-Session-Store-DBI/trunk/lib/Catalyst/Plugin/Session/Store/DBI.pm	2010-03-23 13:28:26 UTC (rev 13070)
+++ Catalyst-Plugin-Session-Store-DBI/trunk/lib/Catalyst/Plugin/Session/Store/DBI.pm	2010-03-23 21:42:56 UTC (rev 13071)
@@ -113,16 +113,31 @@
     $c->maybe::next::method(@_);
 }
 
+
+sub session_store_dbi_table {
+    return shift->_session_plugin_config->{'dbi_table'} || 'sessions';
+}
+
+sub session_store_dbi_id_field {
+    return shift->_session_plugin_config->{'dbi_id_field'} || 'id';
+}
+
+sub session_store_dbi_data_field {
+    return shift->_session_plugin_config->{'dbi_data_field'} || 'session_data';
+}
+
+sub session_store_dbi_expires_field {
+    return shift->_session_plugin_config->{'dbi_expires_field'} || 'expires';
+}
+
 sub setup_session {
     my $c = shift;
 
     $c->maybe::next::method(@_);
 
-    $c->_session_plugin_config->{dbi_table} ||= 'sessions';
+    my $cfg = $c->_session_plugin_config;
     
-    unless ( $c->_session_plugin_config->{dbi_dbh} 
-          || $c->_session_plugin_config->{dbi_dsn} 
-    ) {
+    unless ( $cfg->{dbi_dbh} || $cfg->{dbi_dsn} ) {
         Catalyst::Exception->throw( 
             message => 'Session::Store::DBI: No session configuration found, '
                      . 'please configure dbi_dbh or dbi_dsn'
@@ -130,24 +145,26 @@
     }
     
     # Pre-generate all SQL statements
-    my $table = $c->_session_plugin_config->{dbi_table};
+    my ( $table, $id_field, $data_field, $expires_field ) =
+        map { $c->${\"session_store_$_"} }
+            qw/dbi_table dbi_id_field dbi_data_field dbi_expires_field/;
     $c->_session_sql( {
         get_session_data        =>
-            "SELECT session_data FROM $table WHERE id = ?",
+            "SELECT $data_field FROM $table WHERE $id_field = ?",
         get_expires             =>
-            "SELECT expires FROM $table WHERE id = ?",
+            "SELECT $expires_field FROM $table WHERE $id_field = ?",
         check_existing          =>
-            "SELECT 1 FROM $table WHERE id = ?",
+            "SELECT 1 FROM $table WHERE $id_field = ?",
         update_session          =>
-            "UPDATE $table SET session_data = ?, expires = ? WHERE id = ?",
+            "UPDATE $table SET $data_field = ?, $expires_field = ? WHERE $id_field = ?",
         insert_session          =>
-            "INSERT INTO $table (session_data, expires, id) VALUES (?, ?, ?)",
+            "INSERT INTO $table ($data_field, $expires_field, $id_field) VALUES (?, ?, ?)",
         update_expires          =>
-            "UPDATE $table SET expires = ? WHERE id = ?",
+            "UPDATE $table SET $expires_field = ? WHERE $id_field = ?",
         delete_session          =>
-            "DELETE FROM $table WHERE id = ?",
+            "DELETE FROM $table WHERE $id_field = ?",
         delete_expired_sessions =>
-            "DELETE FROM $table WHERE expires IS NOT NULL AND expires < ?",
+            "DELETE FROM $table WHERE $expires_field IS NOT NULL AND $expires_field < ?",
     } );
 }
 
@@ -338,6 +355,9 @@
         dbi_user  => 'foo',
         dbi_pass  => 'bar',
         dbi_table => 'sessions',
+        dbi_id_field => 'id',
+        dbi_data_field => 'session_data',
+        dbi_expires_field => 'expires',
     });
     
     # Or use an existing database handle from a DBIC/CDBI class
@@ -345,6 +365,9 @@
         expires   => 3600,
         dbi_dbh   => 'DBIC', # which means MyApp::Model::DBIC
         dbi_table => 'sessions',
+        dbi_id_field => 'id',
+        dbi_data_field => 'session_data',
+        dbi_expires_field => 'expires',
     });
 
     # ... in an action:
@@ -397,6 +420,21 @@
 See the Schema section below for additional details.  The table name defaults
 to 'sessions'.
 
+=head2 dbi_id_field
+
+The name of the field on your sessions table which stores the session ID.
+Defaults to C<id>.
+
+=head2 dbi_data_field
+
+The name of the field on your sessions table which stores session data.
+Defaults to C<session_data>.
+
+=head2 dbi_expires_field
+
+The name of the field on your sessions table which stores the expiration
+time of the session. Defaults to C<expires>.
+
 =head1 SCHEMA
 
 Your 'sessions' table must contain at minimum the following 3 columns:
@@ -421,6 +459,9 @@
 The 'expires' column stores the future expiration time of the session.  This
 may be null for per-user and flash sessions.
 
+NOTE: Your column names do not need to match with this schema, use config to
+set custom column names.
+
 =head1 METHODS
 
 =head2 get_session_data
@@ -436,6 +477,22 @@
 These are implementations of the required methods for a store. See
 L<Catalyst::Plugin::Session::Store>.
 
+=head2 session_store_dbi_table
+
+Return the configured table name.
+
+=head2 session_store_dbi_id_field
+
+Return the configured ID field name.
+
+=head2 session_store_dbi_data_field
+
+Return the configured data field name.
+
+=head2 session_store_dbi_expires_field
+
+Return the configured expires field name.
+
 =head1 INTERNAL METHODS
 
 =head2 prepare

Modified: Catalyst-Plugin-Session-Store-DBI/trunk/t/04dbi.t
===================================================================
--- Catalyst-Plugin-Session-Store-DBI/trunk/t/04dbi.t	2010-03-23 13:28:26 UTC (rev 13070)
+++ Catalyst-Plugin-Session-Store-DBI/trunk/t/04dbi.t	2010-03-23 21:42:56 UTC (rev 13071)
@@ -27,7 +27,7 @@
 unless ( -e $db_file ) {
     mkdir "$FindBin::Bin/tmp" or die $!;
     my $sql =
-      'CREATE TABLE sessions (id TEXT PRIMARY KEY, session_data TEXT, expires INT);';
+      'CREATE TABLE sessions (id TEXT PRIMARY KEY, s_data TEXT, expires INT);';
     my $dbh = DBI->connect("dbi:SQLite:$db_file") or die $DBI::errstr;
     $dbh->do($sql);
     $dbh->disconnect;

Modified: Catalyst-Plugin-Session-Store-DBI/trunk/t/lib/TestApp.pm
===================================================================
--- Catalyst-Plugin-Session-Store-DBI/trunk/t/lib/TestApp.pm	2010-03-23 13:28:26 UTC (rev 13070)
+++ Catalyst-Plugin-Session-Store-DBI/trunk/t/lib/TestApp.pm	2010-03-23 21:42:56 UTC (rev 13071)
@@ -12,6 +12,9 @@
     'Plugin::Session' => {
         expires => 3600,
         dbi_dsn => "dbi:SQLite:$db_file",
+        dbi_table => 'sessions',
+        dbi_id_field => 'id',
+        dbi_data_field => 's_data',
     }
 );
 




More information about the Catalyst-commits mailing list