]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/oracle_session.rb
Localisation updates from http://translatewiki.net
[rails.git] / vendor / plugins / sql_session_store / lib / oracle_session.rb
1 require 'oci8'
2
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.
6 #
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.
10 #
11 # This table layout is compatible with ActiveRecordStore.
12
13 class OracleSession < AbstractSession
14   class << self
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
18     # outside this class.
19     def find_session(session_id)
20       new_session = nil
21
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'])
26
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]
31         end
32       end
33
34       cursor.close
35       new_session
36     end
37
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)
45
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) || '')
51         end
52         cursor.bind_param(':data', marshalize(data))
53         cursor.exec
54         cursor.close
55       end
56       new_session
57     end
58
59     # Internal methods for generating SQL
60     # Get the next ID from the sequence
61     def next_id
62       cursor = session_connection.exec("SELECT #{table_name}_seq.nextval FROM dual")
63       id = cursor.fetch.first.to_i
64       cursor.close
65       id
66     end
67
68     # Dynamically generate finder SQL so we can include our special columns
69     def find_session_sql
70       @find_session_sql ||=
71         "SELECT " + ([:id, :data] + native_columns).join(', ') +
72         " FROM #{table_name} WHERE session_id = :session_id AND rownum = 1"
73     end
74
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)"
80     end
81
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"
87     end
88   end # class methods
89
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
95     cursor = nil
96     if @id
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)
100     else
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
104
105       cursor = connection.parse(self.class.insert_session_sql)
106       cursor.bind_param(':session_id', @session_id)
107     end
108
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) || '')
113     end
114     cursor.bind_param(':data', self.class.marshalize(data))
115     cursor.exec
116     cursor.close
117   end
118
119   # destroy the current session
120   def destroy
121     self.class.delete_all(["session_id = ?", session_id])
122   end
123
124 end
125
126 __END__
127
128 # This software is released under the MIT license
129 #
130 # Copyright (c) 2006 Stefan Kaes
131 # Copyright (c) 2006 Tiago Macedo
132 # Copyright (c) 2007 Nate Wiger
133 #
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:
141
142 # The above copyright notice and this permission notice shall be
143 # included in all copies or substantial portions of the Software.
144
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.
152