Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 10
- Log:
Checking in HEAD from RForum's SVN of 22-Jul-2006, 8pm (revision 906).
- Author:
- adh
- Date:
- Sat Jul 22 20:02:44 +0100 2006
- Size:
- 4912 Bytes
- Properties:
- Property svn:executable is set to *
1 | # This software is released under the MIT license |
2 | # |
3 | # Copyright (c) 2006 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 'postgres' |
25 | |
26 | # allow access to the real Mysql connection |
27 | class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter |
28 | attr_reader :connection |
29 | end |
30 | |
31 | # PostgresqlSession 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 PostgresqlSession |
40 | |
41 | # if you need Rails components, and you have a pages which create |
42 | # new sessions, and embed components insides these pages that need |
43 | # session access, then you *must* set +eager_session_creation+ to |
44 | # true (as of Rails 1.0). Not needed for Rails 1.1. |
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' Postgresql 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 | result = connection.query("SELECT id, data FROM sessions WHERE \"sessid\"='#{session_id}' LIMIT 1") |
70 | my_session = nil |
71 | # each is used below, as other methods barf on my 64bit linux machine |
72 | # I suspect this to be a bug in mysql-ruby |
73 | result.each do |row| |
74 | my_session = new(session_id, row[1]) |
75 | my_session.id = row[0] |
76 | end |
77 | result.clear |
78 | my_session |
79 | end |
80 | |
81 | # create a new session with given +session_id+ and +data+ |
82 | # and save it immediately to the database (components suck!) |
83 | def create_session(session_id, data) |
84 | new_session = new(session_id, data) |
85 | if @@eager_session_creation |
86 | connection = session_connection |
87 | connection.query("INSERT INTO sessions (\"created_at\", \"updated_at\", \"sessid\", \"data\") VALUES (NOW(), NOW(), '#{session_id}', #{PGconn::quote(data)})") |
88 | new_session.id = connection.lastval |
89 | end |
90 | new_session |
91 | end |
92 | |
93 | # delete all sessions meeting a given +condition+. it is the |
94 | # callers responsibility to pass a valid sql condition |
95 | def delete_all(condition=nil) |
96 | if condition |
97 | session_connection.query("DELETE FROM sessions WHERE #{condition}") |
98 | else |
99 | session_connection.query("DELETE FROM sessions") |
100 | end |
101 | end |
102 | |
103 | end # class methods |
104 | |
105 | # update session with given +data'. |
106 | # unlike the default implementation using ActiveRecord, updating of |
107 | # column `updated_at` will be done by the datbase itself |
108 | def update_session(data) |
109 | connection = self.class.session_connection |
110 | if @id |
111 | # if @id is not nil, this is a session already stored in the database |
112 | # update the relevant field using @id as key |
113 | connection.query("UPDATE sessions SET \"updated_at\"=NOW(), \"data\"=#{PGconn::quote(data)} WHERE id=#{@id}") |
114 | else |
115 | # if @id is nil, we need to create a new session in the database |
116 | # and set @id to the primary key of the inserted record |
117 | connection.query("INSERT INTO sessions (\"created_at\", \"updated_at\", \"sessid\", \"data\") VALUES (NOW(), NOW(), '#{@sessid}', #{PGconn::quote(data)})") |
118 | @id = connection.lastval |
119 | end |
120 | end |
121 | |
122 | # destroy the current session |
123 | def destroy |
124 | self.class.delete_all("sessid='#{sessid}'") |
125 | end |
126 | |
127 | end |