3 # allow access to the real Sqlite connection
4 #class ActiveRecord::ConnectionAdapters::SQLiteAdapter
5 # attr_reader :connection
8 # SqliteSession 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.
12 # The implementation assumes that the table column names are 'id',
13 # 'data', 'created_at' and 'updated_at'. If you want use other names,
14 # you will need to change the SQL statments in the code.
18 # if you need Rails components, and you have a pages which create
19 # new sessions, and embed components insides this pages that need
20 # session access, then you *must* set +eager_session_creation+ to
21 # true (as of Rails 1.0).
22 cattr_accessor :eager_session_creation
23 @@eager_session_creation = false
25 attr_accessor :id, :session_id, :data
27 def initialize(session_id, data)
28 @session_id = session_id
35 # retrieve the session table connection and get the 'raw' Sqlite connection from it
36 def session_connection
37 SqlSession.connection.instance_variable_get(:@connection)
40 # try to find a session with a given +session_id+. returns nil if
41 # no such session exists. note that we don't retrieve
42 # +created_at+ and +updated_at+ as they are not accessed anywhyere
44 def find_session(session_id)
45 connection = session_connection
46 session_id = SQLite3::Database.quote(session_id)
47 result = connection.execute("SELECT id, data FROM sessions WHERE `session_id`='#{session_id}' LIMIT 1")
49 # each is used below, as other methods barf on my 64bit linux machine
50 # I suspect this to be a bug in sqlite-ruby
52 my_session = new(session_id, row[1])
53 my_session.id = row[0]
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 session_id = SQLite3::Database.quote(session_id)
63 new_session = new(session_id, data)
64 if @@eager_session_creation
65 connection = session_connection
66 connection.execute("INSERT INTO sessions ('id', `created_at`, `updated_at`, `session_id`, `data`) VALUES (NULL, datetime('now'), datetime('now'), '#{session_id}', '#{SQLite3::Database.quote(data)}')")
67 new_session.id = connection.last_insert_row_id()
72 # delete all sessions meeting a given +condition+. it is the
73 # caller's responsibility to pass a valid sql condition
74 def delete_all(condition=nil)
76 session_connection.execute("DELETE FROM sessions WHERE #{condition}")
78 session_connection.execute("DELETE FROM sessions")
84 # update session with given +data+.
85 # unlike the default implementation using ActiveRecord, updating of
86 # column `updated_at` will be done by the database itself
87 def update_session(data)
88 connection = SqlSession.connection.instance_variable_get(:@connection) #self.class.session_connection
90 # if @id is not nil, this is a session already stored in the database
91 # update the relevant field using @id as key
92 connection.execute("UPDATE sessions SET `updated_at`=datetime('now'), `data`='#{SQLite3::Database.quote(data)}' WHERE id=#{@id}")
94 # if @id is nil, we need to create a new session in the database
95 # and set @id to the primary key of the inserted record
96 connection.execute("INSERT INTO sessions ('id', `created_at`, `updated_at`, `session_id`, `data`) VALUES (NULL, datetime('now'), datetime('now'), '#{@session_id}', '#{SQLite3::Database.quote(data)}')")
97 @id = connection.last_insert_row_id()
101 # destroy the current session
103 connection = SqlSession.connection.instance_variable_get(:@connection)
104 connection.execute("delete from sessions where session_id='#{session_id}'")
111 # This software is released under the MIT license
113 # Copyright (c) 2005-2008 Stefan Kaes
114 # Copyright (c) 2006-2008 Ted X Toth
116 # Permission is hereby granted, free of charge, to any person obtaining
117 # a copy of this software and associated documentation files (the
118 # "Software"), to deal in the Software without restriction, including
119 # without limitation the rights to use, copy, modify, merge, publish,
120 # distribute, sublicense, and/or sell copies of the Software, and to
121 # permit persons to whom the Software is furnished to do so, subject to
122 # the following conditions:
124 # The above copyright notice and this permission notice shall be
125 # included in all copies or substantial portions of the Software.
127 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
128 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
129 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
130 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
131 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
132 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
133 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.