Mercurial > p > roundup > code
annotate roundup/backends/back_sqlite.py @ 1236:dd52bf10f934
Bug fixes.
- fixed bug in login if the username wasn't known
- handle close/rollback of already-closed sqlite database
- added example for external passwd-style user password verification
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 27 Sep 2002 01:04:38 +0000 |
| parents | e0032f4ab334 |
| children | 209a47ede743 |
| rev | line source |
|---|---|
| 1236 | 1 # $Id: back_sqlite.py,v 1.6 2002-09-27 01:04:38 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 * | |
| 7 import sqlite | |
| 8 | |
| 9 class Database(Database): | |
| 10 # char to use for positional arguments | |
| 11 arg = '%s' | |
| 12 | |
| 13 def open_connection(self): | |
| 14 # ensure files are group readable and writable | |
| 15 os.umask(0002) | |
| 16 db = os.path.join(self.config.DATABASE, 'db') | |
| 17 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
|
18 self.cursor = self.conn.cursor() |
| 1165 | 19 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
|
20 self.database_schema = self.load_dbschema() |
| 1165 | 21 except sqlite.DatabaseError, error: |
| 22 if str(error) != 'no such table: schema': | |
| 23 raise | |
| 24 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
|
25 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
|
26 self.cursor.execute('create table ids (name varchar, num integer)') |
| 1165 | 27 |
| 1236 | 28 def close(self): |
| 29 ''' Close off the connection. | |
| 30 | |
| 31 Squash any error caused by us already having closed the | |
| 32 connection. | |
| 33 ''' | |
| 34 try: | |
| 35 self.conn.close() | |
| 36 except sqlite.ProgrammingError, value: | |
| 37 if str(value) != 'close failed - Connection is closed.': | |
| 38 raise | |
| 39 | |
| 40 | |
| 41 def rollback(self): | |
| 42 ''' Reverse all actions from the current transaction. | |
| 43 | |
| 44 Undo all the changes made since the database was opened or the | |
| 45 last commit() or rollback() was performed. | |
| 46 | |
| 47 Squash any error caused by us having closed the connection (and | |
| 48 therefore not having anything to roll back) | |
| 49 ''' | |
| 50 if __debug__: | |
| 51 print >>hyperdb.DEBUG, 'rollback', (self,) | |
| 52 | |
| 53 # roll back | |
| 54 try: | |
| 55 self.conn.rollback() | |
| 56 except sqlite.ProgrammingError, value: | |
| 57 if str(value) != 'rollback failed - Connection is closed.': | |
| 58 raise | |
| 59 | |
| 60 # roll back "other" transaction stuff | |
| 61 for method, args in self.transactions: | |
| 62 # delete temporary files | |
| 63 if method == self.doStoreFile: | |
| 64 self.rollbackStoreFile(*args) | |
| 65 self.transactions = [] | |
| 66 | |
| 1165 | 67 def __repr__(self): |
| 68 return '<roundlite 0x%x>'%id(self) | |
| 69 | |
|
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
|
70 def sql_fetchone(self): |
| 1165 | 71 ''' Fetch a single row. If there's nothing to fetch, return None. |
| 72 ''' | |
|
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
|
73 return self.cursor.fetchone() |
| 1165 | 74 |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
75 def sql_fetchall(self): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
76 ''' 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
|
77 ''' |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
78 return self.cursor.fetchall() |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
79 |
| 1165 | 80 def sql_commit(self): |
| 81 ''' Actually commit to the database. | |
| 82 | |
| 83 Ignore errors if there's nothing to commit. | |
| 84 ''' | |
| 85 try: | |
| 86 self.conn.commit() | |
| 87 except sqlite.DatabaseError, error: | |
| 88 if str(error) != 'cannot commit - no transaction is active': | |
| 89 raise | |
| 90 | |
|
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
|
91 def save_dbschema(self, schema): |
| 1165 | 92 ''' Save the schema definition that the database currently implements |
| 93 ''' | |
| 94 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
|
95 self.sql('insert into schema values (%s)', (s,)) |
| 1165 | 96 |
|
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
|
97 def load_dbschema(self): |
| 1165 | 98 ''' Load the schema definition that the database currently implements |
| 99 ''' | |
|
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
|
100 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
|
101 return eval(self.cursor.fetchone()[0]) |
| 1165 | 102 |
|
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
|
103 def save_journal(self, classname, cols, nodeid, journaldate, |
| 1165 | 104 journaltag, action, params): |
| 105 ''' Save the journal entry to the database | |
| 106 ''' | |
| 107 # make the params db-friendly | |
| 108 params = repr(params) | |
| 109 entry = (nodeid, journaldate, journaltag, action, params) | |
| 110 | |
| 111 # do the insert | |
| 112 a = self.arg | |
| 113 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%(classname, | |
| 114 cols, a, a, a, a, a) | |
| 115 if __debug__: | |
| 116 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
|
117 self.cursor.execute(sql, entry) |
| 1165 | 118 |
|
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
|
119 def load_journal(self, classname, cols, nodeid): |
| 1165 | 120 ''' Load the journal from the database |
| 121 ''' | |
| 122 # now get the journal entries | |
| 123 sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname, | |
| 124 self.arg) | |
| 125 if __debug__: | |
| 126 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
|
127 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 128 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
|
129 for nodeid, date_stamp, user, action, params in self.cursor.fetchall(): |
| 1165 | 130 params = eval(params) |
| 131 res.append((nodeid, date.Date(date_stamp), user, action, params)) | |
| 132 return res | |
| 133 | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
134 def unserialise(self, classname, node): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
135 ''' Decode the marshalled node data |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
136 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
137 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
|
138 Booleans and Numbers. |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
139 ''' |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
140 if __debug__: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
141 print >>hyperdb.DEBUG, 'unserialise', classname, node |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
142 properties = self.getclass(classname).getprops() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
143 d = {} |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
144 for k, v in node.items(): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
145 # 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
|
146 # it won't be in the properties dict |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
147 if not properties.has_key(k): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
148 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
149 continue |
|
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 # get the property spec |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
152 prop = properties[k] |
|
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 isinstance(prop, Date) and v is not None: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
155 d[k] = date.Date(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
156 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
|
157 d[k] = date.Interval(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
158 elif isinstance(prop, Password): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
159 p = password.Password() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
160 p.unpack(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
161 d[k] = p |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
162 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
|
163 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
164 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
|
165 # 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
|
166 try: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
167 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
168 except ValueError: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
169 d[k] = float(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
170 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
171 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
172 return d |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
173 |
