]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/oracle_session.rb
Log the request on a few requests when there is a bad request, probably should do...
[rails.git] / vendor / plugins / sql_session_store / lib / oracle_session.rb
1 require 'oci8'
2
3 # allow access to the real Oracle connection
4 class ActiveRecord::ConnectionAdapters::OracleAdapter
5   attr_reader :connection
6 end
7
8 # OracleSession is a down to the bare metal session store
9 # implementation to be used with +SQLSessionStore+. It is much faster
10 # than the default ActiveRecord implementation.
11 #
12 # The implementation assumes that the table column names are 'id',
13 # 'session_id', 'data', 'created_at' and 'updated_at'. If you want use
14 # other names, you will need to change the SQL statments in the code.
15 #
16 # This table layout is compatible with ActiveRecordStore.
17
18 class OracleSession
19
20   # if you need Rails components, and you have a pages which create
21   # new sessions, and embed components insides these pages that need
22   # session access, then you *must* set +eager_session_creation+ to
23   # true (as of Rails 1.0). Not needed for Rails 1.1 and up.
24   cattr_accessor :eager_session_creation
25   @@eager_session_creation = false
26
27   attr_accessor :id, :session_id, :data
28
29   def initialize(session_id, data)
30     @session_id = session_id
31     @data = data
32     @id = nil
33   end
34
35   class << self
36
37     # retrieve the session table connection and get the 'raw' Oracle connection from it
38     def session_connection
39       SqlSession.connection.connection
40     end
41
42     # try to find a session with a given +session_id+. returns nil if
43     # no such session exists. note that we don't retrieve
44     # +created_at+ and +updated_at+ as they are not accessed anywhyere
45     # outside this class.
46     def find_session(session_id)
47       new_session = nil
48       connection = session_connection
49       result = connection.exec("SELECT id, data FROM sessions WHERE session_id = :a and rownum=1", session_id)
50
51       # Make sure to save the @id if we find an existing session
52       while row = result.fetch
53         new_session = new(session_id,row[1].read)
54         new_session.id = row[0]
55       end
56       result.close
57       new_session
58     end
59
60     # create a new session with given +session_id+ and +data+
61     # and save it immediately to the database
62     def create_session(session_id, data)
63       new_session = new(session_id, data)
64       if @@eager_session_creation
65         connection = session_connection
66         connection.exec("INSERT INTO sessions (id, created_at, updated_at, session_id, data)"+
67                         " VALUES (sessions_seq.nextval, SYSDATE, SYSDATE, :a, :b)",
68                          session_id, data)
69         result = connection.exec("SELECT sessions_seq.currval FROM dual")
70         row = result.fetch
71         new_session.id = row[0].to_i
72       end
73       new_session
74     end
75
76     # delete all sessions meeting a given +condition+. it is the
77     # caller's responsibility to pass a valid sql condition
78     def delete_all(condition=nil)
79       if condition
80         session_connection.exec("DELETE FROM sessions WHERE #{condition}")
81       else
82         session_connection.exec("DELETE FROM sessions")
83       end
84     end
85
86   end # class methods
87
88   # update session with given +data+.
89   # unlike the default implementation using ActiveRecord, updating of
90   # column `updated_at` will be done by the database itself
91   def update_session(data)
92     connection = self.class.session_connection
93     if @id
94       # if @id is not nil, this is a session already stored in the database
95       # update the relevant field using @id as key
96       connection.exec("UPDATE sessions SET updated_at = SYSDATE, data = :a WHERE id = :b",
97                        data, @id)
98     else
99       # if @id is nil, we need to create a new session in the database
100       # and set @id to the primary key of the inserted record
101       connection.exec("INSERT INTO sessions (id, created_at, updated_at, session_id, data)"+
102                       " VALUES (sessions_seq.nextval, SYSDATE, SYSDATE, :a, :b)",
103                        @session_id, data)
104       result = connection.exec("SELECT sessions_seq.currval FROM dual")
105       row = result.fetch
106       @id = row[0].to_i
107     end
108   end
109
110   # destroy the current session
111   def destroy
112     self.class.delete_all("session_id='#{session_id}'")
113   end
114
115 end
116
117 __END__
118
119 # This software is released under the MIT license
120 #
121 # Copyright (c) 2006-2008 Stefan Kaes
122 # Copyright (c) 2006-2008 Tiago Macedo
123 # Copyright (c) 2007-2008 Nate Wiger
124 #
125 # Permission is hereby granted, free of charge, to any person obtaining
126 # a copy of this software and associated documentation files (the
127 # "Software"), to deal in the Software without restriction, including
128 # without limitation the rights to use, copy, modify, merge, publish,
129 # distribute, sublicense, and/or sell copies of the Software, and to
130 # permit persons to whom the Software is furnished to do so, subject to
131 # the following conditions:
132
133 # The above copyright notice and this permission notice shall be
134 # included in all copies or substantial portions of the Software.
135
136 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
137 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
138 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
139 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
140 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
141 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
142 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
143