Mercurial > p > roundup > code
annotate roundup/backends/back_sqlite.py @ 1170:af104fa52746
Added some words to the installation doc about choosing backends.
Added hyperdb Class.filter unit tests - gadfly currently fails
substring searching, but I knew it would :(
Lots of fixes to the RDBMS backend - it works a treat now!
A couple of other cleanups in CGI land...
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 19 Sep 2002 02:37:41 +0000 |
| parents | 94620e088e3a |
| children | 08a13a84ed43 |
| rev | line source |
|---|---|
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1 # $Id: back_sqlite.py,v 1.3 2002-09-19 02:37:41 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) | |
| 18 cursor = self.conn.cursor() | |
| 19 try: | |
| 20 self.database_schema = self.load_dbschema(cursor) | |
| 21 except sqlite.DatabaseError, error: | |
| 22 if str(error) != 'no such table: schema': | |
| 23 raise | |
| 24 self.database_schema = {} | |
| 25 cursor = self.conn.cursor() | |
| 26 cursor.execute('create table schema (schema varchar)') | |
| 27 cursor.execute('create table ids (name varchar, num integer)') | |
| 28 | |
| 29 def __repr__(self): | |
| 30 return '<roundlite 0x%x>'%id(self) | |
| 31 | |
| 32 def sql_fetchone(self, cursor): | |
| 33 ''' Fetch a single row. If there's nothing to fetch, return None. | |
| 34 ''' | |
| 35 return cursor.fetchone() | |
| 36 | |
| 37 def sql_commit(self): | |
| 38 ''' Actually commit to the database. | |
| 39 | |
| 40 Ignore errors if there's nothing to commit. | |
| 41 ''' | |
| 42 try: | |
| 43 self.conn.commit() | |
| 44 except sqlite.DatabaseError, error: | |
| 45 if str(error) != 'cannot commit - no transaction is active': | |
| 46 raise | |
| 47 | |
| 48 def save_dbschema(self, cursor, schema): | |
| 49 ''' Save the schema definition that the database currently implements | |
| 50 ''' | |
| 51 s = repr(self.database_schema) | |
| 52 self.sql(cursor, 'insert into schema values (%s)', (s,)) | |
| 53 | |
| 54 def load_dbschema(self, cursor): | |
| 55 ''' Load the schema definition that the database currently implements | |
| 56 ''' | |
| 57 cursor.execute('select schema from schema') | |
| 58 return eval(cursor.fetchone()[0]) | |
| 59 | |
| 60 def save_journal(self, cursor, classname, cols, nodeid, journaldate, | |
| 61 journaltag, action, params): | |
| 62 ''' Save the journal entry to the database | |
| 63 ''' | |
| 64 # make the params db-friendly | |
| 65 params = repr(params) | |
| 66 entry = (nodeid, journaldate, journaltag, action, params) | |
| 67 | |
| 68 # do the insert | |
| 69 a = self.arg | |
| 70 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%(classname, | |
| 71 cols, a, a, a, a, a) | |
| 72 if __debug__: | |
| 73 print >>hyperdb.DEBUG, 'addjournal', (self, sql, entry) | |
| 74 cursor.execute(sql, entry) | |
| 75 | |
| 76 def load_journal(self, cursor, classname, cols, nodeid): | |
| 77 ''' Load the journal from the database | |
| 78 ''' | |
| 79 # now get the journal entries | |
| 80 sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname, | |
| 81 self.arg) | |
| 82 if __debug__: | |
| 83 print >>hyperdb.DEBUG, 'getjournal', (self, sql, nodeid) | |
| 84 cursor.execute(sql, (nodeid,)) | |
| 85 res = [] | |
| 86 for nodeid, date_stamp, user, action, params in cursor.fetchall(): | |
| 87 params = eval(params) | |
| 88 res.append((nodeid, date.Date(date_stamp), user, action, params)) | |
| 89 return res | |
| 90 | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
91 def unserialise(self, classname, node): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
92 ''' Decode the marshalled node data |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
93 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
94 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
|
95 Booleans and Numbers. |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
96 ''' |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
97 if __debug__: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
98 print >>hyperdb.DEBUG, 'unserialise', classname, node |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
99 properties = self.getclass(classname).getprops() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
100 d = {} |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
101 for k, v in node.items(): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
102 # 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
|
103 # it won't be in the properties dict |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
104 if not properties.has_key(k): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
105 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
106 continue |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
107 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
108 # get the property spec |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
109 prop = properties[k] |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
110 |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
111 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
|
112 d[k] = date.Date(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
113 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
|
114 d[k] = date.Interval(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
115 elif isinstance(prop, Password): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
116 p = password.Password() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
117 p.unpack(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
118 d[k] = p |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
119 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
|
120 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
121 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
|
122 # 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
|
123 try: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
124 d[k] = int(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
125 except ValueError: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
126 d[k] = float(v) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
127 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
128 d[k] = v |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
129 return d |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
130 |
