Mercurial > p > roundup > code
annotate roundup/backends/back_sqlite.py @ 1333:80d27b7d6db5
implemented whole-database locking
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 12 Dec 2002 09:31:04 +0000 |
| parents | 209a47ede743 |
| children | 2fc7d4a8c9e7 |
| rev | line source |
|---|---|
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1 # $Id: back_sqlite.py,v 1.8 2002-12-12 09:31:04 richard Exp $ |
| 1165 | 2 __doc__ = ''' |
| 3 See https://pysqlite.sourceforge.net/ for pysqlite info | |
| 4 ''' | |
| 5 import base64, marshal | |
| 6 from roundup.backends.rdbms_common import * | |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
7 from roundup.backends import locking |
| 1165 | 8 import sqlite |
| 9 | |
| 10 class Database(Database): | |
| 11 # char to use for positional arguments | |
| 12 arg = '%s' | |
| 13 | |
| 14 def open_connection(self): | |
| 15 # ensure files are group readable and writable | |
| 16 os.umask(0002) | |
| 17 db = os.path.join(self.config.DATABASE, 'db') | |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
18 |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
19 # lock it |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
20 lockfilenm = db[:-3] + 'lck' |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
21 self.lockfile = locking.acquire_lock(lockfilenm) |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
22 self.lockfile.write(str(os.getpid())) |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
23 self.lockfile.flush() |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
24 |
| 1165 | 25 self.conn = sqlite.connect(db=db) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
26 self.cursor = self.conn.cursor() |
| 1165 | 27 try: |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
28 self.database_schema = self.load_dbschema() |
| 1165 | 29 except sqlite.DatabaseError, error: |
| 30 if str(error) != 'no such table: schema': | |
| 31 raise | |
| 32 self.database_schema = {} | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
33 self.cursor.execute('create table schema (schema varchar)') |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
34 self.cursor.execute('create table ids (name varchar, num integer)') |
| 1165 | 35 |
| 1236 | 36 def close(self): |
| 37 ''' Close off the connection. | |
| 38 | |
| 39 Squash any error caused by us already having closed the | |
| 40 connection. | |
| 41 ''' | |
| 42 try: | |
| 43 self.conn.close() | |
| 44 except sqlite.ProgrammingError, value: | |
| 45 if str(value) != 'close failed - Connection is closed.': | |
| 46 raise | |
| 47 | |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
48 # release the lock too |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
49 if self.lockfile is not None: |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
50 locking.release_lock(self.lockfile) |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
51 if self.lockfile is not None: |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
52 self.lockfile.close() |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
53 self.lockfile = None |
| 1236 | 54 |
| 55 def rollback(self): | |
| 56 ''' Reverse all actions from the current transaction. | |
| 57 | |
| 58 Undo all the changes made since the database was opened or the | |
| 59 last commit() or rollback() was performed. | |
| 60 | |
| 61 Squash any error caused by us having closed the connection (and | |
| 62 therefore not having anything to roll back) | |
| 63 ''' | |
| 64 if __debug__: | |
| 65 print >>hyperdb.DEBUG, 'rollback', (self,) | |
| 66 | |
| 67 # roll back | |
| 68 try: | |
| 69 self.conn.rollback() | |
| 70 except sqlite.ProgrammingError, value: | |
| 71 if str(value) != 'rollback failed - Connection is closed.': | |
| 72 raise | |
| 73 | |
| 74 # roll back "other" transaction stuff | |
| 75 for method, args in self.transactions: | |
| 76 # delete temporary files | |
| 77 if method == self.doStoreFile: | |
| 78 self.rollbackStoreFile(*args) | |
| 79 self.transactions = [] | |
| 80 | |
| 1165 | 81 def __repr__(self): |
| 82 return '<roundlite 0x%x>'%id(self) | |
| 83 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
84 def sql_fetchone(self): |
| 1165 | 85 ''' Fetch a single row. If there's nothing to fetch, return None. |
| 86 ''' | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
87 return self.cursor.fetchone() |
| 1165 | 88 |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
89 def sql_fetchall(self): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
90 ''' Fetch a single row. If there's nothing to fetch, return []. |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
91 ''' |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
92 return self.cursor.fetchall() |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
93 |
| 1165 | 94 def sql_commit(self): |
| 95 ''' Actually commit to the database. | |
| 96 | |
| 97 Ignore errors if there's nothing to commit. | |
| 98 ''' | |
| 99 try: | |
| 100 self.conn.commit() | |
| 101 except sqlite.DatabaseError, error: | |
| 102 if str(error) != 'cannot commit - no transaction is active': | |
| 103 raise | |
| 104 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
105 def save_dbschema(self, schema): |
| 1165 | 106 ''' Save the schema definition that the database currently implements |
| 107 ''' | |
| 108 s = repr(self.database_schema) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
109 self.sql('insert into schema values (%s)', (s,)) |
| 1165 | 110 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
111 def load_dbschema(self): |
| 1165 | 112 ''' Load the schema definition that the database currently implements |
| 113 ''' | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
114 self.cursor.execute('select schema from schema') |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
115 return eval(self.cursor.fetchone()[0]) |
| 1165 | 116 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
117 def save_journal(self, classname, cols, nodeid, journaldate, |
| 1165 | 118 journaltag, action, params): |
| 119 ''' Save the journal entry to the database | |
| 120 ''' | |
| 121 # make the params db-friendly | |
| 122 params = repr(params) | |
| 123 entry = (nodeid, journaldate, journaltag, action, params) | |
| 124 | |
| 125 # do the insert | |
| 126 a = self.arg | |
| 127 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%(classname, | |
| 128 cols, a, a, a, a, a) | |
| 129 if __debug__: | |
| 130 print >>hyperdb.DEBUG, 'addjournal', (self, sql, entry) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
131 self.cursor.execute(sql, entry) |
| 1165 | 132 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
133 def load_journal(self, classname, cols, nodeid): |
| 1165 | 134 ''' Load the journal from the database |
| 135 ''' | |
| 136 # now get the journal entries | |
| 137 sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname, | |
| 138 self.arg) | |
| 139 if __debug__: | |
| 140 print >>hyperdb.DEBUG, 'getjournal', (self, sql, nodeid) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
141 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 142 res = [] |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
143 for nodeid, date_stamp, user, action, params in self.cursor.fetchall(): |
| 1165 | 144 params = eval(params) |
| 145 res.append((nodeid, date.Date(date_stamp), user, action, params)) | |
| 146 return res | |
| 147 | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
148 def unserialise(self, classname, node): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
149 ''' Decode the marshalled node data |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
150 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
151 SQLite stringifies _everything_... so we need to re-numberificate |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
152 Booleans and Numbers. |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
153 ''' |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
154 if __debug__: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
155 print >>hyperdb.DEBUG, 'unserialise', classname, node |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
156 properties = self.getclass(classname).getprops() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
157 d = {} |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
158 for k, v in node.items(): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
159 # if the property doesn't exist, or is the "retired" flag then |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
160 # it won't be in the properties dict |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
161 if not properties.has_key(k): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
162 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
163 continue |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
164 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
165 # get the property spec |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
166 prop = properties[k] |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
167 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
168 if isinstance(prop, Date) and v is not None: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
169 d[k] = date.Date(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
170 elif isinstance(prop, Interval) and v is not None: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
171 d[k] = date.Interval(v) |
|
1252
209a47ede743
allow blank passwords again [SF#619714]
Richard Jones <richard@users.sourceforge.net>
parents:
1236
diff
changeset
|
172 elif isinstance(prop, Password) and v is not None: |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
173 p = password.Password() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
174 p.unpack(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
175 d[k] = p |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
176 elif isinstance(prop, Boolean) and v is not None: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
177 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
178 elif isinstance(prop, Number) and v is not None: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
179 # try int first, then assume it's a float |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
180 try: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
181 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
182 except ValueError: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
183 d[k] = float(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
184 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
185 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
186 return d |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
187 |
