Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 8
- Log:
Updated to HEAD revision from SVN repository for 22-Jul-2006 at
about 6:30pm.
- Author:
- adh
- Date:
- Sat Jul 22 19:30:50 +0100 2006
- Size:
- 4887 Bytes
- Properties:
- Property svn:executable is set to *
1 | # This software is released under the MIT license |
2 | # |
3 | # Copyright (c) 2005 Stefan Kaes |
4 | |
5 | # Permission is hereby granted, free of charge, to any person obtaining |
6 | # a copy of this software and associated documentation files (the |
7 | # "Software"), to deal in the Software without restriction, including |
8 | # without limitation the rights to use, copy, modify, merge, publish, |
9 | # distribute, sublicense, and/or sell copies of the Software, and to |
10 | # permit persons to whom the Software is furnished to do so, subject to |
11 | # the following conditions: |
12 | |
13 | # The above copyright notice and this permission notice shall be |
14 | # included in all copies or substantial portions of the Software. |
15 | |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
23 | |
24 | require 'mysql' |
25 | |
26 | # allow access to the real Mysql connection |
27 | class ActiveRecord::ConnectionAdapters::MysqlAdapter |
28 | attr_reader :connection |
29 | end |
30 | |
31 | # MysqlSession is a down to the bare metal session store |
32 | # implementation to be used with +SQLSessionStore+. It is much faster |
33 | # than the default ActiveRecord implementation. |
34 | # |
35 | # The implementation assumes that the table column names are 'id', |
36 | # 'data', 'created_at' and 'updated_at'. If you want use other names, |
37 | # you will need to change the SQL statments in the code. |
38 | |
39 | class MysqlSession |
40 | |
41 | # if you need Rails components, and you have a pages which create |
42 | # new sessions, and embed components insides this pages that need |
43 | # session access, then you *must* set +eager_session_creation+ to |
44 | # true (as of Rails 1.0). |
45 | cattr_accessor :eager_session_creation |
46 | @@eager_session_creation = false |
47 | |
48 | attr_accessor :id, :sessid, :data |
49 | |
50 | def initialize(session_id, data) |
51 | @sessid = session_id |
52 | @data = data |
53 | @id = nil |
54 | end |
55 | |
56 | class << self |
57 | |
58 | # retrieve an ActiveRecord connection and get the 'raw' Mysql connection from it |
59 | def session_connection |
60 | ActiveRecord::Base.connection.connection |
61 | end |
62 | |
63 | # try to find a session with a given +session_id+. returns nil if |
64 | # no such session exists. note that we don't retrieve |
65 | # +created_at+ and +updated_at+ as they are not accessed anywhyere |
66 | # outside this class |
67 | def find_session(session_id) |
68 | connection = session_connection |
69 | connection.query_with_result = true |
70 | result = connection.query("SELECT id, data FROM sessions WHERE `sessid`='#{session_id}' LIMIT 1") |
71 | my_session = nil |
72 | # each is used below, as other methods barf on my 64bit linux machine |
73 | # I suspect this to be a bug in mysql-ruby |
74 | result.each do |row| |
75 | my_session = new(session_id, row[1]) |
76 | my_session.id = row[0] |
77 | end |
78 | result.free |
79 | my_session |
80 | end |
81 | |
82 | # create a new session with given +session_id+ and +data+ |
83 | # and save it immediately to the database (components suck!) |
84 | def create_session(session_id, data) |
85 | new_session = new(session_id, data) |
86 | if @@eager_session_creation |
87 | connection = session_connection |
88 | connection.query("INSERT INTO sessions (`created_at`, `updated_at`, `sessid`, `data`) VALUES (NOW(), NOW(), '#{session_id}', '#{Mysql::quote(data)}')") |
89 | new_session.id = connection.insert_id |
90 | end |
91 | new_session |
92 | end |
93 | |
94 | # delete all sessions meeting a given +condition+. it is the |
95 | # callers responsibility to pass a valid sql condition |
96 | def delete_all(condition=nil) |
97 | if condition |
98 | session_connection.query("DELETE FROM sessions WHERE #{condition}") |
99 | else |
100 | session_connection.query("DELETE FROM sessions") |
101 | end |
102 | end |
103 | |
104 | end # class methods |
105 | |
106 | # update session with given +data'. |
107 | # unlike the default implementation using ActiveRecord, updating of |
108 | # column `updated_at` will be done by the datbase itself |
109 | def update_session(data) |
110 | connection = self.class.session_connection |
111 | if @id |
112 | # if @id is not nil, this is a session already stored in the database |
113 | # update the relevant field using @id as key |
114 | connection.query("UPDATE sessions SET `updated_at`=NOW(), `data`='#{Mysql::quote(data)}' WHERE id=#{@id}") |
115 | else |
116 | # if @id is nil, we need to create a new session in the database |
117 | # and set @id to the primary key of the inserted record |
118 | connection.query("INSERT INTO sessions (`created_at`, `updated_at`, `sessid`, `data`) VALUES (NOW(), NOW(), '#{@sessid}', '#{Mysql::quote(data)}')") |
119 | @id = connection.insert_id |
120 | end |
121 | end |
122 | |
123 | # destroy the current session |
124 | def destroy |
125 | self.class.delete_all("sessid='#{sessid}'") |
126 | end |
127 | |
128 | end |