3 # allow access to the real Mysql connection
4 class ActiveRecord::ConnectionAdapters::MysqlAdapter
5 attr_reader :connection
8 # MysqlSession 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' Mysql connection from it
36 def session_connection
37 SqlSession.connection.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 connection.query_with_result = true
47 session_id = Mysql::quote(session_id)
48 result = connection.query("SELECT id, data FROM sessions WHERE `session_id`='#{session_id}' LIMIT 1")
50 # each is used below, as other methods barf on my 64bit linux machine
51 # I suspect this to be a bug in mysql-ruby
53 my_session = new(session_id, row[1])
54 my_session.id = row[0]
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 session_id = Mysql::quote(session_id)
64 new_session = new(session_id, data)
65 if @@eager_session_creation
66 connection = session_connection
67 connection.query("INSERT INTO sessions (`created_at`, `updated_at`, `session_id`, `data`) VALUES (NOW(), NOW(), '#{session_id}', '#{Mysql::quote(data)}')")
68 new_session.id = connection.insert_id
73 # delete all sessions meeting a given +condition+. it is the
74 # caller's responsibility to pass a valid sql condition
75 def delete_all(condition=nil)
77 session_connection.query("DELETE FROM sessions WHERE #{condition}")
79 session_connection.query("DELETE FROM sessions")
85 # update session with given +data+.
86 # unlike the default implementation using ActiveRecord, updating of
87 # column `updated_at` will be done by the datbase itself
88 def update_session(data)
89 connection = self.class.session_connection
91 # if @id is not nil, this is a session already stored in the database
92 # update the relevant field using @id as key
93 connection.query("UPDATE sessions SET `updated_at`=NOW(), `data`='#{Mysql::quote(data)}' WHERE id=#{@id}")
95 # if @id is nil, we need to create a new session in the database
96 # and set @id to the primary key of the inserted record
97 connection.query("INSERT INTO sessions (`created_at`, `updated_at`, `session_id`, `data`) VALUES (NOW(), NOW(), '#{@session_id}', '#{Mysql::quote(data)}')")
98 @id = connection.insert_id
102 # destroy the current session
104 self.class.delete_all("session_id='#{session_id}'")
111 # This software is released under the MIT license
113 # Copyright (c) 2005-2008 Stefan Kaes
115 # Permission is hereby granted, free of charge, to any person obtaining
116 # a copy of this software and associated documentation files (the
117 # "Software"), to deal in the Software without restriction, including
118 # without limitation the rights to use, copy, modify, merge, publish,
119 # distribute, sublicense, and/or sell copies of the Software, and to
120 # permit persons to whom the Software is furnished to do so, subject to
121 # the following conditions:
123 # The above copyright notice and this permission notice shall be
124 # included in all copies or substantial portions of the Software.
126 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
127 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
128 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
129 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
130 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
131 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
132 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.