]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/sqlite_session.rb
Localisation updates from http://translatewiki.net
[rails.git] / vendor / plugins / sql_session_store / lib / sqlite_session.rb
1 require 'sqlite3'
2
3 # SqliteSession 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.
6 #
7 # The implementation assumes that the table column names are 'id',
8 # 'data', 'created_at' and 'updated_at'. If you want use other names,
9 # you will need to change the SQL statments in the code.
10
11 class SqliteSession < AbstractSession
12
13   class << self
14     # try to find a session with a given +session_id+. returns nil if
15     # no such session exists. note that we don't retrieve
16     # +created_at+ and +updated_at+ as they are not accessed anywhyere
17     # outside this class
18     def find_session(session_id)
19       connection = session_connection
20       result = connection.execute("SELECT id, data FROM sessions WHERE `session_id`='#{session_id}' LIMIT 1")
21       my_session = nil
22       # each is used below, as other methods barf on my 64bit linux machine
23       # I suspect this to be a bug in sqlite-ruby
24       result.each do |row|
25         my_session = new(session_id, row[1])
26         my_session.id = row[0]
27       end
28 #      result.free
29       my_session
30     end
31
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.execute("INSERT INTO sessions ('id', `created_at`, `updated_at`, `session_id`, `data`) VALUES (NULL, datetime('now'), datetime('now'), '#{session_id}', '#{SQLite3::Database.quote(data)}')")
39         new_session.id = connection.last_insert_row_id()
40       end
41       new_session
42     end
43
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)
47       if condition
48         session_connection.execute("DELETE FROM sessions WHERE #{condition}")
49       else
50         session_connection.execute("DELETE FROM sessions")
51       end
52     end
53
54   end # class methods
55
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 = SqlSession.connection.instance_variable_get(:@connection) #self.class.session_connection
61     if @id
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.execute("UPDATE sessions SET `updated_at`=datetime('now'), `data`='#{SQLite3::Database.quote(data)}' WHERE id=#{@id}")
65     else
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       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)}')")
69       @id = connection.last_insert_row_id()
70     end
71   end
72
73   # destroy the current session
74   def destroy
75     connection = SqlSession.connection.instance_variable_get(:@connection)
76     connection.execute("delete from sessions where session_id='#{session_id}'")
77   end
78
79 end
80
81 __END__
82
83 # This software is released under the MIT license
84 #
85 # Copyright (c) 2005, 2006 Stefan Kaes
86 # Copyright (c) 2006 Ted X Toth
87
88 # Permission is hereby granted, free of charge, to any person obtaining
89 # a copy of this software and associated documentation files (the
90 # "Software"), to deal in the Software without restriction, including
91 # without limitation the rights to use, copy, modify, merge, publish,
92 # distribute, sublicense, and/or sell copies of the Software, and to
93 # permit persons to whom the Software is furnished to do so, subject to
94 # the following conditions:
95
96 # The above copyright notice and this permission notice shall be
97 # included in all copies or substantial portions of the Software.
98
99 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
100 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
101 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
102 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
103 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
104 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
105 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.