Mercurial > p > roundup > code
comparison roundup/backends/back_sqlite.py @ 2082:c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Refactored the API of sessions and their interaction with the
backend database a fair bit too.
Added some session tests. Nothing testing ageing yet, 'cos that's a pain
inna ass to test :)
Note: metakit backend still uses the *dbm implementation. It might
want to implement its own session store some day, as it'll be faster than
the *dbm one.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 18 Mar 2004 01:58:46 +0000 |
| parents | 3e0961d6d44d |
| children | 3f6024ab2c7a |
comparison
equal
deleted
inserted
replaced
| 2081:fb4bf55b94d7 | 2082:c091cacdc505 |
|---|---|
| 1 # $Id: back_sqlite.py,v 1.16 2004-03-15 05:50:20 richard Exp $ | 1 # $Id: back_sqlite.py,v 1.17 2004-03-18 01:58:45 richard Exp $ |
| 2 '''Implements a backend for SQLite. | 2 '''Implements a backend for SQLite. |
| 3 | 3 |
| 4 See https://pysqlite.sourceforge.net/ for pysqlite info | 4 See https://pysqlite.sourceforge.net/ for pysqlite info |
| 5 ''' | 5 ''' |
| 6 __docformat__ = 'restructuredtext' | 6 __docformat__ = 'restructuredtext' |
| 15 class Database(rdbms_common.Database): | 15 class Database(rdbms_common.Database): |
| 16 # char to use for positional arguments | 16 # char to use for positional arguments |
| 17 arg = '%s' | 17 arg = '%s' |
| 18 | 18 |
| 19 def sql_open_connection(self): | 19 def sql_open_connection(self): |
| 20 db = os.path.join(self.config.DATABASE, 'db') | |
| 21 conn = sqlite.connect(db=db) | |
| 22 cursor = conn.cursor() | |
| 23 return (conn, cursor) | |
| 24 | |
| 25 def open_connection(self): | |
| 20 # ensure files are group readable and writable | 26 # ensure files are group readable and writable |
| 21 os.umask(0002) | 27 os.umask(0002) |
| 28 | |
| 29 # lock the database | |
| 22 db = os.path.join(self.config.DATABASE, 'db') | 30 db = os.path.join(self.config.DATABASE, 'db') |
| 23 | |
| 24 # lock it | |
| 25 lockfilenm = db[:-3] + 'lck' | 31 lockfilenm = db[:-3] + 'lck' |
| 26 self.lockfile = locking.acquire_lock(lockfilenm) | 32 self.lockfile = locking.acquire_lock(lockfilenm) |
| 27 self.lockfile.write(str(os.getpid())) | 33 self.lockfile.write(str(os.getpid())) |
| 28 self.lockfile.flush() | 34 self.lockfile.flush() |
| 29 | 35 |
| 30 self.conn = sqlite.connect(db=db) | 36 (self.conn, self.cursor) = self.sql_open_connection() |
| 31 self.cursor = self.conn.cursor() | 37 |
| 32 try: | 38 try: |
| 33 self.load_dbschema() | 39 self.load_dbschema() |
| 34 except sqlite.DatabaseError, error: | 40 except sqlite.DatabaseError, error: |
| 35 if str(error) != 'no such table: schema': | 41 if str(error) != 'no such table: schema': |
| 36 raise | 42 raise |
| 38 self.cursor.execute('create table schema (schema varchar)') | 44 self.cursor.execute('create table schema (schema varchar)') |
| 39 self.cursor.execute('create table ids (name varchar, num integer)') | 45 self.cursor.execute('create table ids (name varchar, num integer)') |
| 40 self.cursor.execute('create index ids_name_idx on ids(name)') | 46 self.cursor.execute('create index ids_name_idx on ids(name)') |
| 41 self.create_version_2_tables() | 47 self.create_version_2_tables() |
| 42 | 48 |
| 49 def close(self): | |
| 50 ''' Close off the connection. | |
| 51 ''' | |
| 52 self.sql_close() | |
| 53 if self.lockfile is not None: | |
| 54 locking.release_lock(self.lockfile) | |
| 55 if self.lockfile is not None: | |
| 56 self.lockfile.close() | |
| 57 self.lockfile = None | |
| 58 | |
| 43 def create_version_2_tables(self): | 59 def create_version_2_tables(self): |
| 44 self.cursor.execute('create table otks (otk_key varchar, ' | 60 self.cursor.execute('create table otks (otk_key varchar, ' |
| 45 'otk_value varchar, otk_time varchar)') | 61 'otk_value varchar, otk_time integer)') |
| 46 self.cursor.execute('create index otks_key_idx on otks(otk_key)') | 62 self.cursor.execute('create index otks_key_idx on otks(otk_key)') |
| 47 self.cursor.execute('create table sessions (s_key varchar, ' | 63 self.cursor.execute('create table sessions (session_key varchar, ' |
| 48 's_last_use varchar, s_user varchar)') | 64 'session_time integer, session_value varchar)') |
| 49 self.cursor.execute('create index sessions_key_idx on sessions(s_key)') | 65 self.cursor.execute('create index sessions_key_idx on ' |
| 66 'sessions(session_key)') | |
| 50 | 67 |
| 51 def add_actor_column(self): | 68 def add_actor_column(self): |
| 52 # update existing tables to have the new actor column | 69 # update existing tables to have the new actor column |
| 53 tables = self.database_schema['tables'] | 70 tables = self.database_schema['tables'] |
| 54 for classname, spec in self.classes.items(): | 71 for classname, spec in self.classes.items(): |
