]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/sql_session_store.rb
Merge 14059:14394 from trunk.
[rails.git] / vendor / plugins / sql_session_store / lib / sql_session_store.rb
1 require 'active_record'
2 require 'cgi'
3 require 'cgi/session'
4 begin
5   require 'base64'
6 rescue LoadError
7 end
8
9 # +SqlSessionStore+ is a stripped down, optimized for speed version of
10 # class +ActiveRecordStore+.
11
12 class SqlSessionStore
13
14   # The class to be used for creating, retrieving and updating sessions.
15   # Defaults to SqlSessionStore::Session, which is derived from +ActiveRecord::Base+.
16   #
17   # In order to achieve acceptable performance you should implement
18   # your own session class, similar to the one provided for Myqsl.
19   #
20   # Only functions +find_session+, +create_session+,
21   # +update_session+ and +destroy+ are required. See file +mysql_session.rb+.
22
23   cattr_accessor :session_class
24   @@session_class = SqlSession
25
26   # Create a new SqlSessionStore instance.
27   #
28   # +session+ is the session for which this instance is being created.
29   #
30   # +option+ is currently ignored as no options are recognized.
31
32   def initialize(session, option=nil)
33     if @session = @@session_class.find_session(session.session_id)
34       @data = unmarshalize(@session.data)
35     else
36       @session = @@session_class.create_session(session.session_id, marshalize({}))
37       @data = {}
38     end
39   end
40
41   # Update the database and disassociate the session object
42   def close
43     if @session
44       @session.update_session(marshalize(@data))
45       @session = nil
46     end
47   end
48
49   # Delete the current session, disassociate and destroy session object
50   def delete
51     if @session
52       @session.destroy
53       @session = nil
54     end
55   end
56
57   # Restore session data from the session object
58   def restore
59     if @session
60       @data = unmarshalize(@session.data)
61     end
62   end
63
64   # Save session data in the session object
65   def update
66     if @session
67       @session.update_session(marshalize(@data))
68     end
69   end
70
71   private
72   if defined?(Base64)
73     def unmarshalize(data)
74       Marshal.load(Base64.decode64(data))
75     end
76
77     def marshalize(data)
78       Base64.encode64(Marshal.dump(data))
79     end
80   else
81     def unmarshalize(data)
82       Marshal.load(data.unpack("m").first)
83     end
84
85     def marshalize(data)
86       [Marshal.dump(data)].pack("m")
87     end
88   end
89
90 end
91
92 __END__
93
94 # This software is released under the MIT license
95 #
96 # Copyright (c) 2005-2008 Stefan Kaes
97
98 # Permission is hereby granted, free of charge, to any person obtaining
99 # a copy of this software and associated documentation files (the
100 # "Software"), to deal in the Software without restriction, including
101 # without limitation the rights to use, copy, modify, merge, publish,
102 # distribute, sublicense, and/or sell copies of the Software, and to
103 # permit persons to whom the Software is furnished to do so, subject to
104 # the following conditions:
105
106 # The above copyright notice and this permission notice shall be
107 # included in all copies or substantial portions of the Software.
108
109 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
110 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
111 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
112 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
113 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
114 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
115 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
116