3 # PostgresqlSession 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 PostgresqlSession < 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)
20 connection = session_connection
21 result = connection.query("SELECT id, data FROM sessions WHERE session_id = $1 LIMIT 1", [session_id])
23 my_session = new(session_id, AbstractSession.unmarshalize(result.getvalue(0, 1)))
24 my_session.id = result.getvalue(0, 0)
32 # create a new session with given +session_id+ and +data+
33 # and save it immediately to the database
34 def create_session(session_id, data={})
35 new_session = new(session_id, data)
36 if @@eager_session_creation
37 connection = session_connection
38 connection.query("INSERT INTO sessions (created_at, updated_at, session_id, data) VALUES (NOW(), NOW(), $1, $2)", [session_id, AbstractSession.marshalize(data)])
39 new_session.id = connection.lastval
44 # delete all sessions meeting a given +condition+. it is the
45 # caller's responsibility to pass a valid sql condition
46 def delete_all(condition=nil)
48 session_connection.query("DELETE FROM sessions WHERE #{condition}")
50 session_connection.query("DELETE FROM sessions")
56 # update session with given +data+.
57 # unlike the default implementation using ActiveRecord, updating of
58 # column `updated_at` will be done by the database itself
59 def update_session(data)
60 connection = self.class.session_connection
62 # if @id is not nil, this is a session already stored in the database
63 # update the relevant field using @id as key
64 connection.query("UPDATE sessions SET updated_at = NOW(), data = $1 WHERE id = $2", [AbstractSession.marshalize(data), @id])
66 # if @id is nil, we need to create a new session in the database
67 # and set @id to the primary key of the inserted record
68 result = connection.query("INSERT INTO sessions (created_at, updated_at, session_id, data) VALUES (NOW(), NOW(), $1, $2) RETURNING id", [@session_id, AbstractSession.marshalize(data)])
69 @id = result.getvalue(0, 0)
74 # destroy the current session
76 self.class.delete_all("session_id='#{session_id}'")
83 # This software is released under the MIT license
85 # Copyright (c) 2006 Stefan Kaes
87 # Permission is hereby granted, free of charge, to any person obtaining
88 # a copy of this software and associated documentation files (the
89 # "Software"), to deal in the Software without restriction, including
90 # without limitation the rights to use, copy, modify, merge, publish,
91 # distribute, sublicense, and/or sell copies of the Software, and to
92 # permit persons to whom the Software is furnished to do so, subject to
93 # the following conditions:
95 # The above copyright notice and this permission notice shall be
96 # included in all copies or substantial portions of the Software.
98 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
99 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
100 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
101 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
102 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
103 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
104 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.