3 # OracleSession is a down to the bare metal session store
4 # implementation to be used with +SQLSessionStore+. It is much faster
5 # than the default ActiveRecord implementation.
7 # The implementation assumes that the table column names are 'id',
8 # 'session_id', 'data', 'created_at' and 'updated_at'. If you want use
9 # other names, you will need to change the SQL statments in the code.
11 # This table layout is compatible with ActiveRecordStore.
13 class OracleSession < AbstractSession
15 # try to find a session with a given +session_id+. returns nil if
16 # no such session exists. note that we don't retrieve
17 # +created_at+ and +updated_at+ as they are not accessed anywhyere
19 def find_session(session_id)
22 # Make sure to save the @id if we find an existing session
23 cursor = session_connection.exec(find_session_sql, session_id)
24 if row = cursor.fetch_hash
25 new_session = new(session_id, unmarshalize(row['DATA'].read), row['ID'])
27 # Pull out native columns
28 native_columns.each do |col|
29 new_session.data[col] = row[col.to_s.upcase]
30 new_session.data[col] = row[col.to_s.upcase]
38 # create a new session with given +session_id+ and +data+
39 # and save it immediately to the database
40 def create_session(session_id, data={})
41 new_session = new(session_id, data)
42 if eager_session_creation
43 new_session.id = next_id
44 cursor = session_connection.parse(insert_session_sql)
46 # Now bind all variables
47 cursor.bind_param(':id', new_session.id)
48 cursor.bind_param(':session_id', session_id)
49 native_columns.each do |col|
50 cursor.bind_param(":#{col}", data.delete(col) || '')
52 cursor.bind_param(':data', marshalize(data))
59 # Internal methods for generating SQL
60 # Get the next ID from the sequence
62 cursor = session_connection.exec("SELECT #{table_name}_seq.nextval FROM dual")
63 id = cursor.fetch.first.to_i
68 # Dynamically generate finder SQL so we can include our special columns
71 "SELECT " + ([:id, :data] + native_columns).join(', ') +
72 " FROM #{table_name} WHERE session_id = :session_id AND rownum = 1"
75 def insert_session_sql
76 @insert_session_sql ||=
77 "INSERT INTO #{table_name} (" + ([:id, :data, :session_id] + native_columns + [:created_at, :updated_at]).join(', ') + ")" +
78 " VALUES (" + ([:id, :data, :session_id] + native_columns).collect{|col| ":#{col}" }.join(', ') +
79 " , SYSDATE, SYSDATE)"
82 def update_session_sql
83 @update_session_sql ||=
84 "UPDATE #{table_name} SET "+
85 ([:data] + native_columns).collect{|col| "#{col} = :#{col}"}.join(', ') +
86 " , updated_at = SYSDATE WHERE ID = :id"
90 # update session with given +data+.
91 # unlike the default implementation using ActiveRecord, updating of
92 # column `updated_at` will be done by the database itself
93 def update_session(data)
94 connection = self.class.session_connection
97 # if @id is not nil, this is a session already stored in the database
98 # update the relevant field using @id as key
99 cursor = connection.parse(self.class.update_session_sql)
101 # if @id is nil, we need to create a new session in the database
102 # and set @id to the primary key of the inserted record
103 @id = self.class.next_id
105 cursor = connection.parse(self.class.insert_session_sql)
106 cursor.bind_param(':session_id', @session_id)
109 # These are always the same, as @id is set above!
110 cursor.bind_param(':id', @id, Fixnum)
111 native_columns.each do |col|
112 cursor.bind_param(":#{col}", data.delete(col) || '')
114 cursor.bind_param(':data', self.class.marshalize(data))
119 # destroy the current session
121 self.class.delete_all(["session_id = ?", session_id])
128 # This software is released under the MIT license
130 # Copyright (c) 2006 Stefan Kaes
131 # Copyright (c) 2006 Tiago Macedo
132 # Copyright (c) 2007 Nate Wiger
134 # Permission is hereby granted, free of charge, to any person obtaining
135 # a copy of this software and associated documentation files (the
136 # "Software"), to deal in the Software without restriction, including
137 # without limitation the rights to use, copy, modify, merge, publish,
138 # distribute, sublicense, and/or sell copies of the Software, and to
139 # permit persons to whom the Software is furnished to do so, subject to
140 # the following conditions:
142 # The above copyright notice and this permission notice shall be
143 # included in all copies or substantial portions of the Software.
145 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
146 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
147 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
148 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
149 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
150 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
151 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.