Mercurial > p > roundup > code
annotate roundup/backends/back_sqlite.py @ 1492:2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 06 Mar 2003 06:03:51 +0000 |
| parents | 80d27b7d6db5 |
| children | 94e430ad4fdb |
| rev | line source |
|---|---|
|
1492
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1 # $Id: back_sqlite.py,v 1.9 2003-03-06 06:03:51 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 | |
|
1492
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
81 # clear the cache |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
82 self.clearCache() |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
83 |
| 1165 | 84 def __repr__(self): |
| 85 return '<roundlite 0x%x>'%id(self) | |
| 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 def sql_fetchone(self): |
| 1165 | 88 ''' Fetch a single row. If there's nothing to fetch, return None. |
| 89 ''' | |
|
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
|
90 return self.cursor.fetchone() |
| 1165 | 91 |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
92 def sql_fetchall(self): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
93 ''' 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
|
94 ''' |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
95 return self.cursor.fetchall() |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
96 |
| 1165 | 97 def sql_commit(self): |
| 98 ''' Actually commit to the database. | |
| 99 | |
| 100 Ignore errors if there's nothing to commit. | |
| 101 ''' | |
| 102 try: | |
| 103 self.conn.commit() | |
| 104 except sqlite.DatabaseError, error: | |
| 105 if str(error) != 'cannot commit - no transaction is active': | |
| 106 raise | |
| 107 | |
|
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
|
108 def save_dbschema(self, schema): |
| 1165 | 109 ''' Save the schema definition that the database currently implements |
| 110 ''' | |
| 111 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
|
112 self.sql('insert into schema values (%s)', (s,)) |
| 1165 | 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 def load_dbschema(self): |
| 1165 | 115 ''' Load the schema definition that the database currently implements |
| 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 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
|
118 return eval(self.cursor.fetchone()[0]) |
| 1165 | 119 |
|
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
|
120 def save_journal(self, classname, cols, nodeid, journaldate, |
| 1165 | 121 journaltag, action, params): |
| 122 ''' Save the journal entry to the database | |
| 123 ''' | |
| 124 # make the params db-friendly | |
| 125 params = repr(params) | |
| 126 entry = (nodeid, journaldate, journaltag, action, params) | |
| 127 | |
| 128 # do the insert | |
| 129 a = self.arg | |
| 130 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%(classname, | |
| 131 cols, a, a, a, a, a) | |
| 132 if __debug__: | |
| 133 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
|
134 self.cursor.execute(sql, entry) |
| 1165 | 135 |
|
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
|
136 def load_journal(self, classname, cols, nodeid): |
| 1165 | 137 ''' Load the journal from the database |
| 138 ''' | |
| 139 # now get the journal entries | |
| 140 sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname, | |
| 141 self.arg) | |
| 142 if __debug__: | |
| 143 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
|
144 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 145 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
|
146 for nodeid, date_stamp, user, action, params in self.cursor.fetchall(): |
| 1165 | 147 params = eval(params) |
| 148 res.append((nodeid, date.Date(date_stamp), user, action, params)) | |
| 149 return res | |
| 150 | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
151 def unserialise(self, classname, node): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
152 ''' Decode the marshalled node data |
|
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 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
|
155 Booleans and Numbers. |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
156 ''' |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
157 if __debug__: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
158 print >>hyperdb.DEBUG, 'unserialise', classname, node |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
159 properties = self.getclass(classname).getprops() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
160 d = {} |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
161 for k, v in node.items(): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
162 # 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
|
163 # it won't be in the properties dict |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
164 if not properties.has_key(k): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
165 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
166 continue |
|
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 # get the property spec |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
169 prop = properties[k] |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
170 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
171 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
|
172 d[k] = date.Date(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
173 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
|
174 d[k] = date.Interval(v) |
|
1252
209a47ede743
allow blank passwords again [SF#619714]
Richard Jones <richard@users.sourceforge.net>
parents:
1236
diff
changeset
|
175 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
|
176 p = password.Password() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
177 p.unpack(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
178 d[k] = p |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
179 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
|
180 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
181 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
|
182 # 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
|
183 try: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
184 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
185 except ValueError: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
186 d[k] = float(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
187 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
188 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
189 return d |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
190 |
