3 # +SqlSessionStore+ is a stripped down, optimized for speed version of
4 # class +ActiveRecordStore+.
6 # Hack for older versions of Rails
7 unless defined?(ActionController::Session::AbstractStore)
8 module ActionController
16 class SqlSessionStore < ActionController::Session::AbstractStore
18 # The class to be used for creating, retrieving and updating sessions.
19 # Defaults to SqlSessionStore::SqlSession, which is derived from +ActiveRecord::Base+.
21 # In order to achieve acceptable performance you should implement
22 # your own session class, similar to the one provided for Myqsl.
24 # Only functions +find_session+, +create_session+,
25 # +update_session+ and +destroy+ are required. The best implementations
26 # are +postgresql_session.rb+ and +oracle_session.rb+.
27 cattr_accessor :session_class
28 self.session_class = SqlSession
30 # Rack-ism for Rails 2.3.0
31 SESSION_RECORD_KEY = 'rack.session.record'.freeze
33 # Backwards-compat indicators (booleans for speed)
34 cattr_accessor :use_rack_session, :use_cgi_session
35 self.use_rack_session = false
36 self.use_cgi_session = false
38 # For Rack compatibility (Rails 2.3.0+)
39 def get_session(env, sid)
41 #puts "get_session(#{sid})"
42 session = find_or_create_session(sid)
43 env[SESSION_RECORD_KEY] = session
47 # For Rack compatibility (Rails 2.3.0+)
48 def set_session(env, sid, session_data)
49 #puts "set_session(#{sid})"
50 session = env[SESSION_RECORD_KEY]
51 session.update_session(session_data)
52 return true # indicate ok to Rack
55 # Create a new SqlSessionStore instance. This method hooks into
56 # the find/create methods of a given driver class.
58 # +session_id+ is the session ID for which this instance is being created.
59 def find_or_create_session(session_id)
60 if @session = session_class.find_session(session_id)
63 @session = session_class.create_session(session_id)
69 # Below here is for pre-Rails 2.3.0 and not used in Rack-based servers
70 # The CGI::Session methods are a bit odd in that half are class and half
71 # are instance-based methods
72 # Note that +option+ is currently ignored as no options are recognized.
73 def initialize(session, options={})
74 # This is just some optimization since this is called over and over and over
75 if self.use_rack_session
76 super # MUST call super for Rack sessions
78 elsif self.use_cgi_session
79 find_or_create_session(session.session_id)
81 version ||= Rails.version.split('.')
82 if version[0].to_i == 2 && version[1].to_i < 3
83 find_or_create_session(session.session_id)
84 self.use_cgi_session = true
86 super # MUST call super for Rack sessions
87 self.use_rack_session = true
92 # Update the database and disassociate the session object
95 @session.update_session(@data)
100 # Delete the current session, disassociate and destroy session object
108 # Restore session data from the session object
111 @data = @session.data
115 # Save session data in the session object
118 @session.update_session(@data)
134 # This software is released under the MIT license
136 # Copyright (c) 2008, 2009 Nate Wiger
137 # Copyright (c) 2005, 2006 Stefan Kaes
139 # Permission is hereby granted, free of charge, to any person obtaining
140 # a copy of this software and associated documentation files (the
141 # "Software"), to deal in the Software without restriction, including
142 # without limitation the rights to use, copy, modify, merge, publish,
143 # distribute, sublicense, and/or sell copies of the Software, and to
144 # permit persons to whom the Software is furnished to do so, subject to
145 # the following conditions:
147 # The above copyright notice and this permission notice shall be
148 # included in all copies or substantial portions of the Software.
150 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
151 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
152 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
153 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
154 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
155 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
156 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.