]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/postgresql_session.rb
Fix a few typos in translation resource names.
[rails.git] / vendor / plugins / sql_session_store / lib / postgresql_session.rb
1 require 'pg'
2
3 # allow access to the real Posqtgresql connection
4 class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
5   attr_reader :connection
6 end
7
8 # PostgresqlSession 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 PostgresqlSession
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' Postgresql 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       connection = session_connection
48       result = connection.query("SELECT id, data FROM sessions WHERE session_id = $1 LIMIT 1", [session_id])
49       if result.ntuples > 0
50         my_session = new(session_id, result.getvalue(0, 1))
51         my_session.id = result.getvalue(0, 0)
52       else
53         my_session = nil
54       end
55       result.clear
56       my_session
57     end
58
59     # create a new session with given +session_id+ and +data+
60     # and save it immediately to the database
61     def create_session(session_id, data)
62       new_session = new(session_id, data)
63       if @@eager_session_creation
64         connection = session_connection
65         connection.query("INSERT INTO sessions (created_at, updated_at, session_id, data) VALUES (NOW(), NOW(), $1, $2)", [session_id, data])
66         new_session.id = connection.lastval
67       end
68       new_session
69     end
70
71     # delete all sessions meeting a given +condition+. it is the
72     # caller's responsibility to pass a valid sql condition
73     def delete_all(id=nil)
74       if id
75         session_connection.query("DELETE FROM sessions WHERE session_id = $1", [id])
76       else
77         session_connection.query("DELETE FROM sessions")
78       end
79     end
80
81   end # class methods
82
83   # update session with given +data+.
84   # unlike the default implementation using ActiveRecord, updating of
85   # column `updated_at` will be done by the database itself
86   def update_session(data)
87     connection = self.class.session_connection
88     if @id
89       # if @id is not nil, this is a session already stored in the database
90       # update the relevant field using @id as key
91       connection.query("UPDATE sessions SET updated_at = NOW(), data = $1 WHERE id = $2", [data, @id])
92     else
93       # if @id is nil, we need to create a new session in the database
94       # and set @id to the primary key of the inserted record
95       result = connection.query("INSERT INTO sessions (created_at,  updated_at, session_id, data) VALUES (NOW(), NOW(), $1, $2) RETURNING id", [@session_id, data])
96       @id = result.getvalue(0, 0)
97       result.clear
98     end
99   end
100
101   # destroy the current session
102   def destroy
103     self.class.delete_all(session_id)
104   end
105
106 end
107
108 __END__
109
110 # This software is released under the MIT license
111 #
112 # Copyright (c) 2006-2008 Stefan Kaes
113
114 # Permission is hereby granted, free of charge, to any person obtaining
115 # a copy of this software and associated documentation files (the
116 # "Software"), to deal in the Software without restriction, including
117 # without limitation the rights to use, copy, modify, merge, publish,
118 # distribute, sublicense, and/or sell copies of the Software, and to
119 # permit persons to whom the Software is furnished to do so, subject to
120 # the following conditions:
121
122 # The above copyright notice and this permission notice shall be
123 # included in all copies or substantial portions of the Software.
124
125 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
126 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
127 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
128 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
129 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
130 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
131 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
132