Mercurial > p > roundup > code
annotate roundup/backends/rdbms_common.py @ 2165:e52da2b47edd maint-0.6
make rdbms serialise() less trusting
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 31 Mar 2004 01:13:22 +0000 |
| parents | 84b6d5b07626 |
| children | fc1d10a74651 |
| rev | line source |
|---|---|
|
2165
e52da2b47edd
make rdbms serialise() less trusting
Richard Jones <richard@users.sourceforge.net>
parents:
1922
diff
changeset
|
1 # $Id: rdbms_common.py,v 1.58.2.4 2004-03-31 01:13:22 richard Exp $ |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
2 ''' Relational database (SQL) backend common code. |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
3 |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
4 Basics: |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
5 |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
6 - map roundup classes to relational tables |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
7 - automatically detect schema changes and modify the table schemas |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
8 appropriately (we store the "database version" of the schema in the |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
9 database itself as the only row of the "schema" table) |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
10 - multilinks (which represent a many-to-many relationship) are handled through |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
11 intermediate tables |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
12 - journals are stored adjunct to the per-class tables |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
13 - table names and columns have "_" prepended so the names can't clash with |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
14 restricted names (like "order") |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
15 - retirement is determined by the __retired__ column being true |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
16 |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
17 Database-specific changes may generally be pushed out to the overridable |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
18 sql_* methods, since everything else should be fairly generic. There's |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
19 probably a bit of work to be done if a database is used that actually |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
20 honors column typing, since the initial databases don't (sqlite stores |
|
1528
96cd422532ef
bye bye gadfly - you served your purpose well [SF#701127]
Richard Jones <richard@users.sourceforge.net>
parents:
1523
diff
changeset
|
21 everything as a string.) |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
22 ''' |
| 1165 | 23 |
| 24 # standard python modules | |
| 25 import sys, os, time, re, errno, weakref, copy | |
| 26 | |
| 27 # roundup modules | |
| 28 from roundup import hyperdb, date, password, roundupdb, security | |
| 29 from roundup.hyperdb import String, Password, Date, Interval, Link, \ | |
|
1417
472c21af7f69
fixed error in indexargs_url (thanks Patrick Ohly)
Richard Jones <richard@users.sourceforge.net>
parents:
1415
diff
changeset
|
30 Multilink, DatabaseError, Boolean, Number, Node |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
31 from roundup.backends import locking |
| 1165 | 32 |
| 33 # support | |
| 34 from blobfiles import FileStorage | |
| 35 from roundup.indexer import Indexer | |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1442
diff
changeset
|
36 from sessions import Sessions, OneTimeKeys |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
37 from roundup.date import Range |
| 1165 | 38 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
39 # number of rows to keep in memory |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
40 ROW_CACHE_SIZE = 100 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
41 |
| 1165 | 42 class Database(FileStorage, hyperdb.Database, roundupdb.Database): |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
43 ''' Wrapper around an SQL database that presents a hyperdb interface. |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
44 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
45 - some functionality is specific to the actual SQL database, hence |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
46 the sql_* methods that are NotImplemented |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
47 - we keep a cache of the latest ROW_CACHE_SIZE row fetches. |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
48 ''' |
| 1165 | 49 def __init__(self, config, journaltag=None): |
| 50 ''' Open the database and load the schema from it. | |
| 51 ''' | |
| 52 self.config, self.journaltag = config, journaltag | |
| 53 self.dir = config.DATABASE | |
| 54 self.classes = {} | |
| 55 self.indexer = Indexer(self.dir) | |
| 56 self.sessions = Sessions(self.config) | |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1442
diff
changeset
|
57 self.otks = OneTimeKeys(self.config) |
| 1165 | 58 self.security = security.Security(self) |
| 59 | |
| 60 # additional transaction support for external files and the like | |
| 61 self.transactions = [] | |
| 62 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
63 # keep a cache of the N most recently retrieved rows of any kind |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
64 # (classname, nodeid) = row |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
65 self.cache = {} |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
66 self.cache_lru = [] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
67 |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
68 # database lock |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
69 self.lockfile = None |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
70 |
| 1165 | 71 # open a connection to the database, creating the "conn" attribute |
| 72 self.open_connection() | |
| 73 | |
|
1181
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
74 def clearCache(self): |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
75 self.cache = {} |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
76 self.cache_lru = [] |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
77 |
| 1165 | 78 def open_connection(self): |
| 79 ''' Open a connection to the database, creating it if necessary | |
| 80 ''' | |
| 81 raise NotImplemented | |
| 82 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
83 def sql(self, sql, args=None): |
| 1165 | 84 ''' Execute the sql with the optional args. |
| 85 ''' | |
| 86 if __debug__: | |
| 87 print >>hyperdb.DEBUG, (self, sql, args) | |
| 88 if args: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
89 self.cursor.execute(sql, args) |
| 1165 | 90 else: |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
91 self.cursor.execute(sql) |
| 1165 | 92 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
93 def sql_fetchone(self): |
| 1165 | 94 ''' Fetch a single row. If there's nothing to fetch, return None. |
| 95 ''' | |
| 96 raise NotImplemented | |
| 97 | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
98 def sql_stringquote(self, value): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
99 ''' Quote the string so it's safe to put in the 'sql quotes' |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
100 ''' |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
101 return re.sub("'", "''", str(value)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
102 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
103 def save_dbschema(self, schema): |
| 1165 | 104 ''' Save the schema definition that the database currently implements |
| 105 ''' | |
| 106 raise NotImplemented | |
| 107 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
108 def load_dbschema(self): |
| 1165 | 109 ''' Load the schema definition that the database currently implements |
| 110 ''' | |
| 111 raise NotImplemented | |
| 112 | |
| 113 def post_init(self): | |
| 114 ''' Called once the schema initialisation has finished. | |
| 115 | |
| 116 We should now confirm that the schema defined by our "classes" | |
| 117 attribute actually matches the schema in the database. | |
| 118 ''' | |
| 119 # now detect changes in the schema | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
120 save = 0 |
| 1165 | 121 for classname, spec in self.classes.items(): |
| 122 if self.database_schema.has_key(classname): | |
| 123 dbspec = self.database_schema[classname] | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
124 if self.update_class(spec, dbspec): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
125 self.database_schema[classname] = spec.schema() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
126 save = 1 |
| 1165 | 127 else: |
| 128 self.create_class(spec) | |
| 129 self.database_schema[classname] = spec.schema() | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
130 save = 1 |
| 1165 | 131 |
| 132 for classname in self.database_schema.keys(): | |
| 133 if not self.classes.has_key(classname): | |
| 134 self.drop_class(classname) | |
| 135 | |
| 136 # update the database version of the schema | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
137 if save: |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
138 self.sql('delete from schema') |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
139 self.save_dbschema(self.database_schema) |
| 1165 | 140 |
| 141 # reindex the db if necessary | |
| 142 if self.indexer.should_reindex(): | |
| 143 self.reindex() | |
| 144 | |
| 145 # commit | |
| 146 self.conn.commit() | |
| 147 | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
148 # figure the "curuserid" |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
149 if self.journaltag is None: |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
150 self.curuserid = None |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
151 elif self.journaltag == 'admin': |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
152 # admin user may not exist, but always has ID 1 |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
153 self.curuserid = '1' |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
154 else: |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
155 self.curuserid = self.user.lookup(self.journaltag) |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
156 |
| 1165 | 157 def reindex(self): |
| 158 for klass in self.classes.values(): | |
| 159 for nodeid in klass.list(): | |
| 160 klass.index(nodeid) | |
| 161 self.indexer.save_index() | |
| 162 | |
| 163 def determine_columns(self, properties): | |
| 164 ''' Figure the column names and multilink properties from the spec | |
| 165 | |
| 166 "properties" is a list of (name, prop) where prop may be an | |
| 167 instance of a hyperdb "type" _or_ a string repr of that type. | |
| 168 ''' | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
169 cols = ['_activity', '_creator', '_creation'] |
| 1165 | 170 mls = [] |
| 171 # add the multilinks separately | |
| 172 for col, prop in properties: | |
| 173 if isinstance(prop, Multilink): | |
| 174 mls.append(col) | |
| 175 elif isinstance(prop, type('')) and prop.find('Multilink') != -1: | |
| 176 mls.append(col) | |
| 177 else: | |
| 178 cols.append('_'+col) | |
| 179 cols.sort() | |
| 180 return cols, mls | |
| 181 | |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
182 def update_class(self, spec, old_spec): |
| 1165 | 183 ''' Determine the differences between the current spec and the |
| 184 database version of the spec, and update where necessary | |
| 185 ''' | |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
186 new_spec = spec |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
187 new_has = new_spec.properties.has_key |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
188 |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
189 new_spec = new_spec.schema() |
|
1515
a516bbb9896b
fixed rdbms table update detection logic [SF#703297]
Richard Jones <richard@users.sourceforge.net>
parents:
1500
diff
changeset
|
190 new_spec[1].sort() |
|
a516bbb9896b
fixed rdbms table update detection logic [SF#703297]
Richard Jones <richard@users.sourceforge.net>
parents:
1500
diff
changeset
|
191 old_spec[1].sort() |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
192 if new_spec == old_spec: |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
193 # no changes |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
194 return 0 |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
195 |
| 1165 | 196 if __debug__: |
| 197 print >>hyperdb.DEBUG, 'update_class FIRING' | |
| 198 | |
| 199 # key property changed? | |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
200 if old_spec[0] != new_spec[0]: |
| 1165 | 201 if __debug__: |
| 202 print >>hyperdb.DEBUG, 'update_class setting keyprop', `spec[0]` | |
| 203 # XXX turn on indexing for the key property | |
| 204 | |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
205 # detect multilinks that have been removed, and drop their table |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
206 old_has = {} |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
207 for name,prop in old_spec[1]: |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
208 old_has[name] = 1 |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
209 if not new_has(name) and isinstance(prop, Multilink): |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
210 # it's a multilink, and it's been removed - drop the old |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
211 # table |
| 1165 | 212 sql = 'drop table %s_%s'%(spec.classname, prop) |
| 213 if __debug__: | |
| 214 print >>hyperdb.DEBUG, 'update_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
215 self.cursor.execute(sql) |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
216 continue |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
217 old_has = old_has.has_key |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
218 |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
219 # now figure how we populate the new table |
|
1500
7cd110cee870
Fixed issue w. gadfly and exact column matching when table schemas are updated.
Richard Jones <richard@users.sourceforge.net>
parents:
1499
diff
changeset
|
220 fetch = ['_activity', '_creation', '_creator'] |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
221 properties = spec.getprops() |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
222 for propname,x in new_spec[1]: |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
223 prop = properties[propname] |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
224 if isinstance(prop, Multilink): |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
225 if not old_has(propname): |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
226 # we need to create the new table |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
227 self.create_multilink_table(spec, propname) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
228 elif old_has(propname): |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
229 # we copy this col over from the old table |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
230 fetch.append('_'+propname) |
| 1165 | 231 |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
232 # select the data out of the old table |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
233 fetch.append('id') |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
234 fetch.append('__retired__') |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
235 fetchcols = ','.join(fetch) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
236 cn = spec.classname |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
237 sql = 'select %s from _%s'%(fetchcols, cn) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
238 if __debug__: |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
239 print >>hyperdb.DEBUG, 'update_class', (self, sql) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
240 self.cursor.execute(sql) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
241 olddata = self.cursor.fetchall() |
| 1165 | 242 |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
243 # drop the old table |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
244 self.cursor.execute('drop table _%s'%cn) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
245 |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
246 # create the new table |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
247 self.create_class_table(spec) |
| 1165 | 248 |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
249 if olddata: |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
250 # do the insert |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
251 args = ','.join([self.arg for x in fetch]) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
252 sql = 'insert into _%s (%s) values (%s)'%(cn, fetchcols, args) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
253 if __debug__: |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
254 print >>hyperdb.DEBUG, 'update_class', (self, sql, olddata[0]) |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
255 for entry in olddata: |
|
1500
7cd110cee870
Fixed issue w. gadfly and exact column matching when table schemas are updated.
Richard Jones <richard@users.sourceforge.net>
parents:
1499
diff
changeset
|
256 self.cursor.execute(sql, tuple(entry)) |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
257 |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
258 return 1 |
| 1165 | 259 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
260 def create_class_table(self, spec): |
| 1165 | 261 ''' create the class table for the given spec |
| 262 ''' | |
| 263 cols, mls = self.determine_columns(spec.properties.items()) | |
| 264 | |
| 265 # add on our special columns | |
| 266 cols.append('id') | |
| 267 cols.append('__retired__') | |
| 268 | |
| 269 # create the base table | |
| 270 scols = ','.join(['%s varchar'%x for x in cols]) | |
| 271 sql = 'create table _%s (%s)'%(spec.classname, scols) | |
| 272 if __debug__: | |
| 273 print >>hyperdb.DEBUG, 'create_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
274 self.cursor.execute(sql) |
| 1165 | 275 |
| 276 return cols, mls | |
| 277 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
278 def create_journal_table(self, spec): |
| 1165 | 279 ''' create the journal table for a class given the spec and |
| 280 already-determined cols | |
| 281 ''' | |
| 282 # journal table | |
| 283 cols = ','.join(['%s varchar'%x | |
| 284 for x in 'nodeid date tag action params'.split()]) | |
| 285 sql = 'create table %s__journal (%s)'%(spec.classname, cols) | |
| 286 if __debug__: | |
| 287 print >>hyperdb.DEBUG, 'create_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
288 self.cursor.execute(sql) |
| 1165 | 289 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
290 def create_multilink_table(self, spec, ml): |
| 1165 | 291 ''' Create a multilink table for the "ml" property of the class |
| 292 given by the spec | |
| 293 ''' | |
| 294 sql = 'create table %s_%s (linkid varchar, nodeid varchar)'%( | |
| 295 spec.classname, ml) | |
| 296 if __debug__: | |
| 297 print >>hyperdb.DEBUG, 'create_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
298 self.cursor.execute(sql) |
| 1165 | 299 |
| 300 def create_class(self, spec): | |
| 301 ''' Create a database table according to the given spec. | |
| 302 ''' | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
303 cols, mls = self.create_class_table(spec) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
304 self.create_journal_table(spec) |
| 1165 | 305 |
| 306 # now create the multilink tables | |
| 307 for ml in mls: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
308 self.create_multilink_table(spec, ml) |
| 1165 | 309 |
| 310 # ID counter | |
| 311 sql = 'insert into ids (name, num) values (%s,%s)'%(self.arg, self.arg) | |
| 312 vals = (spec.classname, 1) | |
| 313 if __debug__: | |
| 314 print >>hyperdb.DEBUG, 'create_class', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
315 self.cursor.execute(sql, vals) |
| 1165 | 316 |
| 317 def drop_class(self, spec): | |
| 318 ''' Drop the given table from the database. | |
| 319 | |
| 320 Drop the journal and multilink tables too. | |
| 321 ''' | |
| 322 # figure the multilinks | |
| 323 mls = [] | |
|
1922
84b6d5b07626
testing fixes for mysql, backported from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
1921
diff
changeset
|
324 for col, prop in spec.getprops().items(): |
| 1165 | 325 if isinstance(prop, Multilink): |
| 326 mls.append(col) | |
| 327 | |
| 328 sql = 'drop table _%s'%spec.classname | |
| 329 if __debug__: | |
| 330 print >>hyperdb.DEBUG, 'drop_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
331 self.cursor.execute(sql) |
| 1165 | 332 |
| 333 sql = 'drop table %s__journal'%spec.classname | |
| 334 if __debug__: | |
| 335 print >>hyperdb.DEBUG, 'drop_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
336 self.cursor.execute(sql) |
| 1165 | 337 |
| 338 for ml in mls: | |
| 339 sql = 'drop table %s_%s'%(spec.classname, ml) | |
| 340 if __debug__: | |
| 341 print >>hyperdb.DEBUG, 'drop_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
342 self.cursor.execute(sql) |
| 1165 | 343 |
| 344 # | |
| 345 # Classes | |
| 346 # | |
| 347 def __getattr__(self, classname): | |
| 348 ''' A convenient way of calling self.getclass(classname). | |
| 349 ''' | |
| 350 if self.classes.has_key(classname): | |
| 351 if __debug__: | |
| 352 print >>hyperdb.DEBUG, '__getattr__', (self, classname) | |
| 353 return self.classes[classname] | |
| 354 raise AttributeError, classname | |
| 355 | |
| 356 def addclass(self, cl): | |
| 357 ''' Add a Class to the hyperdatabase. | |
| 358 ''' | |
| 359 if __debug__: | |
| 360 print >>hyperdb.DEBUG, 'addclass', (self, cl) | |
| 361 cn = cl.classname | |
| 362 if self.classes.has_key(cn): | |
| 363 raise ValueError, cn | |
| 364 self.classes[cn] = cl | |
| 365 | |
| 366 def getclasses(self): | |
| 367 ''' Return a list of the names of all existing classes. | |
| 368 ''' | |
| 369 if __debug__: | |
| 370 print >>hyperdb.DEBUG, 'getclasses', (self,) | |
| 371 l = self.classes.keys() | |
| 372 l.sort() | |
| 373 return l | |
| 374 | |
| 375 def getclass(self, classname): | |
| 376 '''Get the Class object representing a particular class. | |
| 377 | |
| 378 If 'classname' is not a valid class name, a KeyError is raised. | |
| 379 ''' | |
| 380 if __debug__: | |
| 381 print >>hyperdb.DEBUG, 'getclass', (self, classname) | |
| 382 try: | |
| 383 return self.classes[classname] | |
| 384 except KeyError: | |
| 385 raise KeyError, 'There is no class called "%s"'%classname | |
| 386 | |
| 387 def clear(self): | |
| 388 ''' Delete all database contents. | |
| 389 | |
| 390 Note: I don't commit here, which is different behaviour to the | |
| 391 "nuke from orbit" behaviour in the *dbms. | |
| 392 ''' | |
| 393 if __debug__: | |
| 394 print >>hyperdb.DEBUG, 'clear', (self,) | |
| 395 for cn in self.classes.keys(): | |
| 396 sql = 'delete from _%s'%cn | |
| 397 if __debug__: | |
| 398 print >>hyperdb.DEBUG, 'clear', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
399 self.cursor.execute(sql) |
| 1165 | 400 |
| 401 # | |
| 402 # Node IDs | |
| 403 # | |
| 404 def newid(self, classname): | |
| 405 ''' Generate a new id for the given class | |
| 406 ''' | |
| 407 # get the next ID | |
| 408 sql = 'select num from ids where name=%s'%self.arg | |
| 409 if __debug__: | |
| 410 print >>hyperdb.DEBUG, 'newid', (self, sql, classname) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
411 self.cursor.execute(sql, (classname, )) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
412 newid = self.cursor.fetchone()[0] |
| 1165 | 413 |
| 414 # update the counter | |
| 415 sql = 'update ids set num=%s where name=%s'%(self.arg, self.arg) | |
| 416 vals = (int(newid)+1, classname) | |
| 417 if __debug__: | |
| 418 print >>hyperdb.DEBUG, 'newid', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
419 self.cursor.execute(sql, vals) |
| 1165 | 420 |
| 421 # return as string | |
| 422 return str(newid) | |
| 423 | |
| 424 def setid(self, classname, setid): | |
| 425 ''' Set the id counter: used during import of database | |
| 426 ''' | |
| 427 sql = 'update ids set num=%s where name=%s'%(self.arg, self.arg) | |
|
1171
8784a11f8c2f
fix to setid
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
428 vals = (setid, classname) |
| 1165 | 429 if __debug__: |
| 430 print >>hyperdb.DEBUG, 'setid', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
431 self.cursor.execute(sql, vals) |
| 1165 | 432 |
| 433 # | |
| 434 # Nodes | |
| 435 # | |
| 436 def addnode(self, classname, nodeid, node): | |
| 437 ''' Add the specified node to its class's db. | |
| 438 ''' | |
| 439 if __debug__: | |
| 440 print >>hyperdb.DEBUG, 'addnode', (self, classname, nodeid, node) | |
|
1528
96cd422532ef
bye bye gadfly - you served your purpose well [SF#701127]
Richard Jones <richard@users.sourceforge.net>
parents:
1523
diff
changeset
|
441 |
|
96cd422532ef
bye bye gadfly - you served your purpose well [SF#701127]
Richard Jones <richard@users.sourceforge.net>
parents:
1523
diff
changeset
|
442 # determine the column definitions and multilink tables |
| 1165 | 443 cl = self.classes[classname] |
| 444 cols, mls = self.determine_columns(cl.properties.items()) | |
| 445 | |
|
1189
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
446 # we'll be supplied these props if we're doing an import |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
447 if not node.has_key('creator'): |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
448 # add in the "calculated" properties (dupe so we don't affect |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
449 # calling code's node assumptions) |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
450 node = node.copy() |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
451 node['creation'] = node['activity'] = date.Date() |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
452 node['creator'] = self.curuserid |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
453 |
| 1165 | 454 # default the non-multilink columns |
| 455 for col, prop in cl.properties.items(): | |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
456 if not node.has_key(col): |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
457 if isinstance(prop, Multilink): |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
458 node[col] = [] |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
459 else: |
| 1165 | 460 node[col] = None |
| 461 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
462 # clear this node out of the cache if it's in there |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
463 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
464 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
465 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
466 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
467 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
468 # make the node data safe for the DB |
| 1165 | 469 node = self.serialise(classname, node) |
| 470 | |
| 471 # make sure the ordering is correct for column name -> column value | |
| 472 vals = tuple([node[col[1:]] for col in cols]) + (nodeid, 0) | |
| 473 s = ','.join([self.arg for x in cols]) + ',%s,%s'%(self.arg, self.arg) | |
| 474 cols = ','.join(cols) + ',id,__retired__' | |
| 475 | |
| 476 # perform the inserts | |
| 477 sql = 'insert into _%s (%s) values (%s)'%(classname, cols, s) | |
| 478 if __debug__: | |
| 479 print >>hyperdb.DEBUG, 'addnode', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
480 self.cursor.execute(sql, vals) |
| 1165 | 481 |
| 482 # insert the multilink rows | |
| 483 for col in mls: | |
| 484 t = '%s_%s'%(classname, col) | |
| 485 for entry in node[col]: | |
| 486 sql = 'insert into %s (linkid, nodeid) values (%s,%s)'%(t, | |
| 487 self.arg, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
488 self.sql(sql, (entry, nodeid)) |
| 1165 | 489 |
| 490 # make sure we do the commit-time extra stuff for this node | |
| 491 self.transactions.append((self.doSaveNode, (classname, nodeid, node))) | |
| 492 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
493 def setnode(self, classname, nodeid, values, multilink_changes): |
| 1165 | 494 ''' Change the specified node. |
| 495 ''' | |
| 496 if __debug__: | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
497 print >>hyperdb.DEBUG, 'setnode', (self, classname, nodeid, values) |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
498 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
499 # clear this node out of the cache if it's in there |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
500 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
501 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
502 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
503 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
504 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
505 # add the special props |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
506 values = values.copy() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
507 values['activity'] = date.Date() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
508 |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
509 # make db-friendly |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
510 values = self.serialise(classname, values) |
| 1165 | 511 |
| 512 cl = self.classes[classname] | |
| 513 cols = [] | |
| 514 mls = [] | |
| 515 # add the multilinks separately | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
516 props = cl.getprops() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
517 for col in values.keys(): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
518 prop = props[col] |
| 1165 | 519 if isinstance(prop, Multilink): |
| 520 mls.append(col) | |
| 521 else: | |
| 522 cols.append('_'+col) | |
| 523 cols.sort() | |
| 524 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
525 # if there's any updates to regular columns, do them |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
526 if cols: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
527 # make sure the ordering is correct for column name -> column value |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
528 sqlvals = tuple([values[col[1:]] for col in cols]) + (nodeid,) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
529 s = ','.join(['%s=%s'%(x, self.arg) for x in cols]) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
530 cols = ','.join(cols) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
531 |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
532 # perform the update |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
533 sql = 'update _%s set %s where id=%s'%(classname, s, self.arg) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
534 if __debug__: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
535 print >>hyperdb.DEBUG, 'setnode', (self, sql, sqlvals) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
536 self.cursor.execute(sql, sqlvals) |
| 1165 | 537 |
| 538 # now the fun bit, updating the multilinks ;) | |
| 539 for col, (add, remove) in multilink_changes.items(): | |
| 540 tn = '%s_%s'%(classname, col) | |
| 541 if add: | |
| 542 sql = 'insert into %s (nodeid, linkid) values (%s,%s)'%(tn, | |
| 543 self.arg, self.arg) | |
| 544 for addid in add: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
545 self.sql(sql, (nodeid, addid)) |
| 1165 | 546 if remove: |
| 547 sql = 'delete from %s where nodeid=%s and linkid=%s'%(tn, | |
| 548 self.arg, self.arg) | |
| 549 for removeid in remove: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
550 self.sql(sql, (nodeid, removeid)) |
| 1165 | 551 |
| 552 # make sure we do the commit-time extra stuff for this node | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
553 self.transactions.append((self.doSaveNode, (classname, nodeid, values))) |
| 1165 | 554 |
| 555 def getnode(self, classname, nodeid): | |
| 556 ''' Get a node from the database. | |
| 557 ''' | |
| 558 if __debug__: | |
| 559 print >>hyperdb.DEBUG, 'getnode', (self, classname, nodeid) | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
560 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
561 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
562 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
563 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
564 # push us back to the top of the LRU |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
565 self.cache_lru.remove(key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
566 self.cache_lru.insert(0, key) |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
567 # return the cached information |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
568 return self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
569 |
| 1165 | 570 # figure the columns we're fetching |
| 571 cl = self.classes[classname] | |
| 572 cols, mls = self.determine_columns(cl.properties.items()) | |
| 573 scols = ','.join(cols) | |
| 574 | |
| 575 # perform the basic property fetch | |
| 576 sql = 'select %s from _%s where id=%s'%(scols, classname, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
577 self.sql(sql, (nodeid,)) |
| 1165 | 578 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
579 values = self.sql_fetchone() |
| 1165 | 580 if values is None: |
| 581 raise IndexError, 'no such %s node %s'%(classname, nodeid) | |
| 582 | |
| 583 # make up the node | |
| 584 node = {} | |
| 585 for col in range(len(cols)): | |
| 586 node[cols[col][1:]] = values[col] | |
| 587 | |
| 588 # now the multilinks | |
| 589 for col in mls: | |
| 590 # get the link ids | |
| 591 sql = 'select linkid from %s_%s where nodeid=%s'%(classname, col, | |
| 592 self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
593 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 594 # extract the first column from the result |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
595 node[col] = [x[0] for x in self.cursor.fetchall()] |
| 1165 | 596 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
597 # un-dbificate the node data |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
598 node = self.unserialise(classname, node) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
599 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
600 # save off in the cache |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
601 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
602 self.cache[key] = node |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
603 # update the LRU |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
604 self.cache_lru.insert(0, key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
605 if len(self.cache_lru) > ROW_CACHE_SIZE: |
|
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
606 del self.cache[self.cache_lru.pop()] |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
607 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
608 return node |
| 1165 | 609 |
| 610 def destroynode(self, classname, nodeid): | |
| 611 '''Remove a node from the database. Called exclusively by the | |
| 612 destroy() method on Class. | |
| 613 ''' | |
| 614 if __debug__: | |
| 615 print >>hyperdb.DEBUG, 'destroynode', (self, classname, nodeid) | |
| 616 | |
| 617 # make sure the node exists | |
| 618 if not self.hasnode(classname, nodeid): | |
| 619 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 620 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
621 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
622 if self.cache.has_key((classname, nodeid)): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
623 del self.cache[(classname, nodeid)] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
624 |
| 1165 | 625 # see if there's any obvious commit actions that we should get rid of |
| 626 for entry in self.transactions[:]: | |
| 627 if entry[1][:2] == (classname, nodeid): | |
| 628 self.transactions.remove(entry) | |
| 629 | |
| 630 # now do the SQL | |
| 631 sql = 'delete from _%s where id=%s'%(classname, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
632 self.sql(sql, (nodeid,)) |
| 1165 | 633 |
| 634 # remove from multilnks | |
| 635 cl = self.getclass(classname) | |
| 636 x, mls = self.determine_columns(cl.properties.items()) | |
| 637 for col in mls: | |
| 638 # get the link ids | |
| 639 sql = 'delete from %s_%s where nodeid=%s'%(classname, col, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
640 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 641 |
| 642 # remove journal entries | |
| 643 sql = 'delete from %s__journal where nodeid=%s'%(classname, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
644 self.sql(sql, (nodeid,)) |
| 1165 | 645 |
| 646 def serialise(self, classname, node): | |
| 647 '''Copy the node contents, converting non-marshallable data into | |
| 648 marshallable data. | |
| 649 ''' | |
| 650 if __debug__: | |
| 651 print >>hyperdb.DEBUG, 'serialise', classname, node | |
| 652 properties = self.getclass(classname).getprops() | |
| 653 d = {} | |
| 654 for k, v in node.items(): | |
| 655 # if the property doesn't exist, or is the "retired" flag then | |
| 656 # it won't be in the properties dict | |
| 657 if not properties.has_key(k): | |
| 658 d[k] = v | |
| 659 continue | |
| 660 | |
| 661 # get the property spec | |
| 662 prop = properties[k] | |
| 663 | |
|
1252
209a47ede743
allow blank passwords again [SF#619714]
Richard Jones <richard@users.sourceforge.net>
parents:
1249
diff
changeset
|
664 if isinstance(prop, Password) and v is not None: |
| 1165 | 665 d[k] = str(v) |
| 666 elif isinstance(prop, Date) and v is not None: | |
| 667 d[k] = v.serialise() | |
| 668 elif isinstance(prop, Interval) and v is not None: | |
| 669 d[k] = v.serialise() | |
|
2165
e52da2b47edd
make rdbms serialise() less trusting
Richard Jones <richard@users.sourceforge.net>
parents:
1922
diff
changeset
|
670 elif isinstance(prop, Boolean) and v is not None: |
|
e52da2b47edd
make rdbms serialise() less trusting
Richard Jones <richard@users.sourceforge.net>
parents:
1922
diff
changeset
|
671 d[k] = int(v) |
| 1165 | 672 else: |
| 673 d[k] = v | |
| 674 return d | |
| 675 | |
| 676 def unserialise(self, classname, node): | |
| 677 '''Decode the marshalled node data | |
| 678 ''' | |
| 679 if __debug__: | |
| 680 print >>hyperdb.DEBUG, 'unserialise', classname, node | |
| 681 properties = self.getclass(classname).getprops() | |
| 682 d = {} | |
| 683 for k, v in node.items(): | |
| 684 # if the property doesn't exist, or is the "retired" flag then | |
| 685 # it won't be in the properties dict | |
| 686 if not properties.has_key(k): | |
| 687 d[k] = v | |
| 688 continue | |
| 689 | |
| 690 # get the property spec | |
| 691 prop = properties[k] | |
| 692 | |
| 693 if isinstance(prop, Date) and v is not None: | |
| 694 d[k] = date.Date(v) | |
| 695 elif isinstance(prop, Interval) and v is not None: | |
| 696 d[k] = date.Interval(v) | |
|
1252
209a47ede743
allow blank passwords again [SF#619714]
Richard Jones <richard@users.sourceforge.net>
parents:
1249
diff
changeset
|
697 elif isinstance(prop, Password) and v is not None: |
| 1165 | 698 p = password.Password() |
| 699 p.unpack(v) | |
| 700 d[k] = p | |
|
1415
6883852e9b15
mysql backend passes all tests (at last!)
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1409
diff
changeset
|
701 elif (isinstance(prop, Boolean) or isinstance(prop, Number)) and v is not None: |
|
6883852e9b15
mysql backend passes all tests (at last!)
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1409
diff
changeset
|
702 d[k]=float(v) |
| 1165 | 703 else: |
| 704 d[k] = v | |
| 705 return d | |
| 706 | |
| 707 def hasnode(self, classname, nodeid): | |
| 708 ''' Determine if the database has a given node. | |
| 709 ''' | |
| 710 sql = 'select count(*) from _%s where id=%s'%(classname, self.arg) | |
| 711 if __debug__: | |
| 712 print >>hyperdb.DEBUG, 'hasnode', (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:
1181
diff
changeset
|
713 self.cursor.execute(sql, (nodeid,)) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
714 return int(self.cursor.fetchone()[0]) |
| 1165 | 715 |
| 716 def countnodes(self, classname): | |
| 717 ''' Count the number of nodes that exist for a particular Class. | |
| 718 ''' | |
| 719 sql = 'select count(*) from _%s'%classname | |
| 720 if __debug__: | |
| 721 print >>hyperdb.DEBUG, 'countnodes', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
722 self.cursor.execute(sql) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
723 return self.cursor.fetchone()[0] |
| 1165 | 724 |
| 725 def addjournal(self, classname, nodeid, action, params, creator=None, | |
| 726 creation=None): | |
| 727 ''' Journal the Action | |
| 728 'action' may be: | |
| 729 | |
| 730 'create' or 'set' -- 'params' is a dictionary of property values | |
| 731 'link' or 'unlink' -- 'params' is (classname, nodeid, propname) | |
| 732 'retire' -- 'params' is None | |
| 733 ''' | |
| 734 # serialise the parameters now if necessary | |
| 735 if isinstance(params, type({})): | |
| 736 if action in ('set', 'create'): | |
| 737 params = self.serialise(classname, params) | |
| 738 | |
| 739 # handle supply of the special journalling parameters (usually | |
| 740 # supplied on importing an existing database) | |
| 741 if creator: | |
| 742 journaltag = creator | |
| 743 else: | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
744 journaltag = self.curuserid |
| 1165 | 745 if creation: |
| 746 journaldate = creation.serialise() | |
| 747 else: | |
| 748 journaldate = date.Date().serialise() | |
| 749 | |
| 750 # create the journal entry | |
| 751 cols = ','.join('nodeid date tag action params'.split()) | |
| 752 | |
| 753 if __debug__: | |
| 754 print >>hyperdb.DEBUG, 'addjournal', (nodeid, journaldate, | |
| 755 journaltag, action, params) | |
| 756 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
757 self.save_journal(classname, cols, nodeid, journaldate, |
| 1165 | 758 journaltag, action, params) |
| 759 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
760 def save_journal(self, classname, cols, nodeid, journaldate, |
| 1165 | 761 journaltag, action, params): |
| 762 ''' Save the journal entry to the database | |
| 763 ''' | |
| 764 raise NotImplemented | |
| 765 | |
| 766 def getjournal(self, classname, nodeid): | |
| 767 ''' get the journal for id | |
| 768 ''' | |
| 769 # make sure the node exists | |
| 770 if not self.hasnode(classname, nodeid): | |
| 771 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 772 | |
| 773 cols = ','.join('nodeid date tag action params'.split()) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
774 return self.load_journal(classname, cols, nodeid) |
| 1165 | 775 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
776 def load_journal(self, classname, cols, nodeid): |
| 1165 | 777 ''' Load the journal from the database |
| 778 ''' | |
| 779 raise NotImplemented | |
| 780 | |
| 781 def pack(self, pack_before): | |
| 782 ''' Delete all journal entries except "create" before 'pack_before'. | |
| 783 ''' | |
| 784 # get a 'yyyymmddhhmmss' version of the date | |
| 785 date_stamp = pack_before.serialise() | |
| 786 | |
| 787 # do the delete | |
| 788 for classname in self.classes.keys(): | |
| 789 sql = "delete from %s__journal where date<%s and "\ | |
| 790 "action<>'create'"%(classname, self.arg) | |
| 791 if __debug__: | |
| 792 print >>hyperdb.DEBUG, 'pack', (self, sql, date_stamp) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
793 self.cursor.execute(sql, (date_stamp,)) |
| 1165 | 794 |
| 795 def sql_commit(self): | |
| 796 ''' Actually commit to the database. | |
| 797 ''' | |
| 798 self.conn.commit() | |
| 799 | |
| 800 def commit(self): | |
| 801 ''' Commit the current transactions. | |
| 802 | |
| 803 Save all data changed since the database was opened or since the | |
| 804 last commit() or rollback(). | |
| 805 ''' | |
| 806 if __debug__: | |
| 807 print >>hyperdb.DEBUG, 'commit', (self,) | |
| 808 | |
| 809 # commit the database | |
| 810 self.sql_commit() | |
| 811 | |
| 812 # now, do all the other transaction stuff | |
| 813 reindex = {} | |
| 814 for method, args in self.transactions: | |
| 815 reindex[method(*args)] = 1 | |
| 816 | |
| 817 # reindex the nodes that request it | |
| 818 for classname, nodeid in filter(None, reindex.keys()): | |
| 819 print >>hyperdb.DEBUG, 'commit.reindex', (classname, nodeid) | |
| 820 self.getclass(classname).index(nodeid) | |
| 821 | |
| 822 # save the indexer state | |
| 823 self.indexer.save_index() | |
| 824 | |
| 825 # clear out the transactions | |
| 826 self.transactions = [] | |
| 827 | |
| 828 def rollback(self): | |
| 829 ''' Reverse all actions from the current transaction. | |
| 830 | |
| 831 Undo all the changes made since the database was opened or the last | |
| 832 commit() or rollback() was performed. | |
| 833 ''' | |
| 834 if __debug__: | |
| 835 print >>hyperdb.DEBUG, 'rollback', (self,) | |
| 836 | |
| 837 # roll back | |
| 838 self.conn.rollback() | |
| 839 | |
| 840 # roll back "other" transaction stuff | |
| 841 for method, args in self.transactions: | |
| 842 # delete temporary files | |
| 843 if method == self.doStoreFile: | |
| 844 self.rollbackStoreFile(*args) | |
| 845 self.transactions = [] | |
| 846 | |
|
1492
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
847 # clear the cache |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
848 self.clearCache() |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
849 |
| 1165 | 850 def doSaveNode(self, classname, nodeid, node): |
| 851 ''' dummy that just generates a reindex event | |
| 852 ''' | |
| 853 # return the classname, nodeid so we reindex this content | |
| 854 return (classname, nodeid) | |
| 855 | |
| 856 def close(self): | |
| 857 ''' Close off the connection. | |
| 858 ''' | |
| 859 self.conn.close() | |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
860 if self.lockfile is not None: |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
861 locking.release_lock(self.lockfile) |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
862 if self.lockfile is not None: |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
863 self.lockfile.close() |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
864 self.lockfile = None |
| 1165 | 865 |
| 866 # | |
| 867 # The base Class class | |
| 868 # | |
| 869 class Class(hyperdb.Class): | |
| 870 ''' The handle to a particular class of nodes in a hyperdatabase. | |
| 871 | |
| 872 All methods except __repr__ and getnode must be implemented by a | |
| 873 concrete backend Class. | |
| 874 ''' | |
| 875 | |
| 876 def __init__(self, db, classname, **properties): | |
| 877 '''Create a new class with a given name and property specification. | |
| 878 | |
| 879 'classname' must not collide with the name of an existing class, | |
| 880 or a ValueError is raised. The keyword arguments in 'properties' | |
| 881 must map names to property objects, or a TypeError is raised. | |
| 882 ''' | |
| 883 if (properties.has_key('creation') or properties.has_key('activity') | |
| 884 or properties.has_key('creator')): | |
| 885 raise ValueError, '"creation", "activity" and "creator" are '\ | |
| 886 'reserved' | |
| 887 | |
| 888 self.classname = classname | |
| 889 self.properties = properties | |
| 890 self.db = weakref.proxy(db) # use a weak ref to avoid circularity | |
| 891 self.key = '' | |
| 892 | |
| 893 # should we journal changes (default yes) | |
| 894 self.do_journal = 1 | |
| 895 | |
| 896 # do the db-related init stuff | |
| 897 db.addclass(self) | |
| 898 | |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
899 self.auditors = {'create': [], 'set': [], 'retire': [], 'restore': []} |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
900 self.reactors = {'create': [], 'set': [], 'retire': [], 'restore': []} |
| 1165 | 901 |
| 902 def schema(self): | |
| 903 ''' A dumpable version of the schema that we can store in the | |
| 904 database | |
| 905 ''' | |
| 906 return (self.key, [(x, repr(y)) for x,y in self.properties.items()]) | |
| 907 | |
| 908 def enableJournalling(self): | |
| 909 '''Turn journalling on for this class | |
| 910 ''' | |
| 911 self.do_journal = 1 | |
| 912 | |
| 913 def disableJournalling(self): | |
| 914 '''Turn journalling off for this class | |
| 915 ''' | |
| 916 self.do_journal = 0 | |
| 917 | |
| 918 # Editing nodes: | |
| 919 def create(self, **propvalues): | |
| 920 ''' Create a new node of this class and return its id. | |
| 921 | |
| 922 The keyword arguments in 'propvalues' map property names to values. | |
| 923 | |
| 924 The values of arguments must be acceptable for the types of their | |
| 925 corresponding properties or a TypeError is raised. | |
| 926 | |
| 927 If this class has a key property, it must be present and its value | |
| 928 must not collide with other key strings or a ValueError is raised. | |
| 929 | |
| 930 Any other properties on this class that are missing from the | |
| 931 'propvalues' dictionary are set to None. | |
| 932 | |
| 933 If an id in a link or multilink property does not refer to a valid | |
| 934 node, an IndexError is raised. | |
| 935 ''' | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
936 self.fireAuditors('create', None, propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
937 newid = self.create_inner(**propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
938 self.fireReactors('create', newid, None) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
939 return newid |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
940 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
941 def create_inner(self, **propvalues): |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
942 ''' Called by create, in-between the audit and react calls. |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
943 ''' |
| 1165 | 944 if propvalues.has_key('id'): |
| 945 raise KeyError, '"id" is reserved' | |
| 946 | |
| 947 if self.db.journaltag is None: | |
| 948 raise DatabaseError, 'Database open read-only' | |
| 949 | |
| 950 if propvalues.has_key('creation') or propvalues.has_key('activity'): | |
| 951 raise KeyError, '"creation" and "activity" are reserved' | |
| 952 | |
| 953 # new node's id | |
| 954 newid = self.db.newid(self.classname) | |
| 955 | |
| 956 # validate propvalues | |
| 957 num_re = re.compile('^\d+$') | |
| 958 for key, value in propvalues.items(): | |
| 959 if key == self.key: | |
| 960 try: | |
| 961 self.lookup(value) | |
| 962 except KeyError: | |
| 963 pass | |
| 964 else: | |
| 965 raise ValueError, 'node with key "%s" exists'%value | |
| 966 | |
| 967 # try to handle this property | |
| 968 try: | |
| 969 prop = self.properties[key] | |
| 970 except KeyError: | |
| 971 raise KeyError, '"%s" has no property "%s"'%(self.classname, | |
| 972 key) | |
| 973 | |
| 974 if value is not None and isinstance(prop, Link): | |
| 975 if type(value) != type(''): | |
| 976 raise ValueError, 'link value must be String' | |
| 977 link_class = self.properties[key].classname | |
| 978 # if it isn't a number, it's a key | |
| 979 if not num_re.match(value): | |
| 980 try: | |
| 981 value = self.db.classes[link_class].lookup(value) | |
| 982 except (TypeError, KeyError): | |
| 983 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 984 key, value, link_class) | |
| 985 elif not self.db.getclass(link_class).hasnode(value): | |
| 986 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 987 | |
| 988 # save off the value | |
| 989 propvalues[key] = value | |
| 990 | |
| 991 # register the link with the newly linked node | |
| 992 if self.do_journal and self.properties[key].do_journal: | |
| 993 self.db.addjournal(link_class, value, 'link', | |
| 994 (self.classname, newid, key)) | |
| 995 | |
| 996 elif isinstance(prop, Multilink): | |
| 997 if type(value) != type([]): | |
| 998 raise TypeError, 'new property "%s" not a list of ids'%key | |
| 999 | |
| 1000 # clean up and validate the list of links | |
| 1001 link_class = self.properties[key].classname | |
| 1002 l = [] | |
| 1003 for entry in value: | |
| 1004 if type(entry) != type(''): | |
| 1005 raise ValueError, '"%s" multilink value (%r) '\ | |
| 1006 'must contain Strings'%(key, value) | |
| 1007 # if it isn't a number, it's a key | |
| 1008 if not num_re.match(entry): | |
| 1009 try: | |
| 1010 entry = self.db.classes[link_class].lookup(entry) | |
| 1011 except (TypeError, KeyError): | |
| 1012 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1013 key, entry, self.properties[key].classname) | |
| 1014 l.append(entry) | |
| 1015 value = l | |
| 1016 propvalues[key] = value | |
| 1017 | |
| 1018 # handle additions | |
| 1019 for nodeid in value: | |
| 1020 if not self.db.getclass(link_class).hasnode(nodeid): | |
| 1021 raise IndexError, '%s has no node %s'%(link_class, | |
| 1022 nodeid) | |
| 1023 # register the link with the newly linked node | |
| 1024 if self.do_journal and self.properties[key].do_journal: | |
| 1025 self.db.addjournal(link_class, nodeid, 'link', | |
| 1026 (self.classname, newid, key)) | |
| 1027 | |
| 1028 elif isinstance(prop, String): | |
|
1383
f19dde90e473
applied unicode patch
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1365
diff
changeset
|
1029 if type(value) != type('') and type(value) != type(u''): |
| 1165 | 1030 raise TypeError, 'new property "%s" not a string'%key |
| 1031 | |
| 1032 elif isinstance(prop, Password): | |
| 1033 if not isinstance(value, password.Password): | |
| 1034 raise TypeError, 'new property "%s" not a Password'%key | |
| 1035 | |
| 1036 elif isinstance(prop, Date): | |
| 1037 if value is not None and not isinstance(value, date.Date): | |
| 1038 raise TypeError, 'new property "%s" not a Date'%key | |
| 1039 | |
| 1040 elif isinstance(prop, Interval): | |
| 1041 if value is not None and not isinstance(value, date.Interval): | |
| 1042 raise TypeError, 'new property "%s" not an Interval'%key | |
| 1043 | |
| 1044 elif value is not None and isinstance(prop, Number): | |
| 1045 try: | |
| 1046 float(value) | |
| 1047 except ValueError: | |
| 1048 raise TypeError, 'new property "%s" not numeric'%key | |
| 1049 | |
| 1050 elif value is not None and isinstance(prop, Boolean): | |
| 1051 try: | |
| 1052 int(value) | |
| 1053 except ValueError: | |
| 1054 raise TypeError, 'new property "%s" not boolean'%key | |
| 1055 | |
| 1056 # make sure there's data where there needs to be | |
| 1057 for key, prop in self.properties.items(): | |
| 1058 if propvalues.has_key(key): | |
| 1059 continue | |
| 1060 if key == self.key: | |
| 1061 raise ValueError, 'key property "%s" is required'%key | |
| 1062 if isinstance(prop, Multilink): | |
| 1063 propvalues[key] = [] | |
| 1064 else: | |
| 1065 propvalues[key] = None | |
| 1066 | |
| 1067 # done | |
| 1068 self.db.addnode(self.classname, newid, propvalues) | |
| 1069 if self.do_journal: | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1070 self.db.addjournal(self.classname, newid, 'create', {}) |
| 1165 | 1071 |
| 1072 return newid | |
| 1073 | |
| 1074 def export_list(self, propnames, nodeid): | |
| 1075 ''' Export a node - generate a list of CSV-able data in the order | |
| 1076 specified by propnames for the given node. | |
| 1077 ''' | |
| 1078 properties = self.getprops() | |
| 1079 l = [] | |
| 1080 for prop in propnames: | |
| 1081 proptype = properties[prop] | |
| 1082 value = self.get(nodeid, prop) | |
| 1083 # "marshal" data where needed | |
| 1084 if value is None: | |
| 1085 pass | |
| 1086 elif isinstance(proptype, hyperdb.Date): | |
| 1087 value = value.get_tuple() | |
| 1088 elif isinstance(proptype, hyperdb.Interval): | |
| 1089 value = value.get_tuple() | |
| 1090 elif isinstance(proptype, hyperdb.Password): | |
| 1091 value = str(value) | |
| 1092 l.append(repr(value)) | |
|
1921
4ad75ca452d9
fixed retirement of items in rdbms imports [SF#841355]
Richard Jones <richard@users.sourceforge.net>
parents:
1752
diff
changeset
|
1093 l.append(repr(self.is_retired(nodeid))) |
| 1165 | 1094 return l |
| 1095 | |
| 1096 def import_list(self, propnames, proplist): | |
| 1097 ''' Import a node - all information including "id" is present and | |
| 1098 should not be sanity checked. Triggers are not triggered. The | |
| 1099 journal should be initialised using the "creator" and "created" | |
| 1100 information. | |
| 1101 | |
| 1102 Return the nodeid of the node imported. | |
| 1103 ''' | |
| 1104 if self.db.journaltag is None: | |
| 1105 raise DatabaseError, 'Database open read-only' | |
| 1106 properties = self.getprops() | |
| 1107 | |
| 1108 # make the new node's property map | |
| 1109 d = {} | |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1110 retire = 0 |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1111 newid = None |
| 1165 | 1112 for i in range(len(propnames)): |
| 1113 # Use eval to reverse the repr() used to output the CSV | |
| 1114 value = eval(proplist[i]) | |
| 1115 | |
| 1116 # Figure the property for this column | |
| 1117 propname = propnames[i] | |
| 1118 | |
| 1119 # "unmarshal" where necessary | |
| 1120 if propname == 'id': | |
| 1121 newid = value | |
| 1122 continue | |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1123 elif propname == 'is retired': |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1124 # is the item retired? |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1125 if int(value): |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1126 retire = 1 |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1127 continue |
|
1752
27917dce3814
backporting fix from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
1740
diff
changeset
|
1128 elif value is None: |
|
27917dce3814
backporting fix from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
1740
diff
changeset
|
1129 d[propname] = None |
|
27917dce3814
backporting fix from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
1740
diff
changeset
|
1130 continue |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1131 |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1132 prop = properties[propname] |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1133 if value is None: |
| 1165 | 1134 # don't set Nones |
| 1135 continue | |
| 1136 elif isinstance(prop, hyperdb.Date): | |
| 1137 value = date.Date(value) | |
| 1138 elif isinstance(prop, hyperdb.Interval): | |
| 1139 value = date.Interval(value) | |
| 1140 elif isinstance(prop, hyperdb.Password): | |
| 1141 pwd = password.Password() | |
| 1142 pwd.unpack(value) | |
| 1143 value = pwd | |
| 1144 d[propname] = value | |
| 1145 | |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1146 # get a new id if necessary |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1147 if newid is None: |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1148 newid = self.db.newid(self.classname) |
|
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1149 |
|
1921
4ad75ca452d9
fixed retirement of items in rdbms imports [SF#841355]
Richard Jones <richard@users.sourceforge.net>
parents:
1752
diff
changeset
|
1150 # add the node and journal |
|
4ad75ca452d9
fixed retirement of items in rdbms imports [SF#841355]
Richard Jones <richard@users.sourceforge.net>
parents:
1752
diff
changeset
|
1151 self.db.addnode(self.classname, newid, d) |
|
4ad75ca452d9
fixed retirement of items in rdbms imports [SF#841355]
Richard Jones <richard@users.sourceforge.net>
parents:
1752
diff
changeset
|
1152 |
|
1476
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1153 # retire? |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
1154 if retire: |
|
1476
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1155 # use the arg for __retired__ to cope with any odd database type |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1156 # conversion (hello, sqlite) |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1157 sql = 'update _%s set __retired__=%s where id=%s'%(self.classname, |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1158 self.db.arg, self.db.arg) |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1159 if __debug__: |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1160 print >>hyperdb.DEBUG, 'retire', (self, sql, newid) |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1161 self.db.cursor.execute(sql, (1, newid)) |
|
5a01e90b7dc9
fixed export/import of retired nodes [SF#685273]
Richard Jones <richard@users.sourceforge.net>
parents:
1467
diff
changeset
|
1162 |
| 1165 | 1163 # extract the extraneous journalling gumpf and nuke it |
| 1164 if d.has_key('creator'): | |
| 1165 creator = d['creator'] | |
| 1166 del d['creator'] | |
|
1187
84e40c9bcd99
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1185
diff
changeset
|
1167 else: |
|
84e40c9bcd99
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1185
diff
changeset
|
1168 creator = None |
| 1165 | 1169 if d.has_key('creation'): |
| 1170 creation = d['creation'] | |
| 1171 del d['creation'] | |
|
1187
84e40c9bcd99
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1185
diff
changeset
|
1172 else: |
|
84e40c9bcd99
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1185
diff
changeset
|
1173 creation = None |
| 1165 | 1174 if d.has_key('activity'): |
| 1175 del d['activity'] | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1176 self.db.addjournal(self.classname, newid, 'create', {}, creator, |
| 1165 | 1177 creation) |
| 1178 return newid | |
| 1179 | |
| 1180 _marker = [] | |
| 1181 def get(self, nodeid, propname, default=_marker, cache=1): | |
| 1182 '''Get the value of a property on an existing node of this class. | |
| 1183 | |
| 1184 'nodeid' must be the id of an existing node of this class or an | |
| 1185 IndexError is raised. 'propname' must be the name of a property | |
| 1186 of this class or a KeyError is raised. | |
| 1187 | |
| 1188 'cache' indicates whether the transaction cache should be queried | |
| 1189 for the node. If the node has been modified and you need to | |
| 1190 determine what its values prior to modification are, you need to | |
| 1191 set cache=0. | |
| 1192 ''' | |
| 1193 if propname == 'id': | |
| 1194 return nodeid | |
| 1195 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1196 # get the node's dict |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1197 d = self.db.getnode(self.classname, nodeid) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1198 |
| 1165 | 1199 if propname == 'creation': |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1200 if d.has_key('creation'): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1201 return d['creation'] |
| 1165 | 1202 else: |
| 1203 return date.Date() | |
| 1204 if propname == 'activity': | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1205 if d.has_key('activity'): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1206 return d['activity'] |
| 1165 | 1207 else: |
| 1208 return date.Date() | |
| 1209 if propname == 'creator': | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1210 if d.has_key('creator'): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1211 return d['creator'] |
| 1165 | 1212 else: |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
1213 return self.db.curuserid |
| 1165 | 1214 |
| 1215 # get the property (raises KeyErorr if invalid) | |
| 1216 prop = self.properties[propname] | |
| 1217 | |
| 1218 if not d.has_key(propname): | |
| 1219 if default is self._marker: | |
| 1220 if isinstance(prop, Multilink): | |
| 1221 return [] | |
| 1222 else: | |
| 1223 return None | |
| 1224 else: | |
| 1225 return default | |
| 1226 | |
| 1227 # don't pass our list to other code | |
| 1228 if isinstance(prop, Multilink): | |
| 1229 return d[propname][:] | |
| 1230 | |
| 1231 return d[propname] | |
| 1232 | |
| 1233 def getnode(self, nodeid, cache=1): | |
| 1234 ''' Return a convenience wrapper for the node. | |
| 1235 | |
| 1236 'nodeid' must be the id of an existing node of this class or an | |
| 1237 IndexError is raised. | |
| 1238 | |
| 1239 'cache' indicates whether the transaction cache should be queried | |
| 1240 for the node. If the node has been modified and you need to | |
| 1241 determine what its values prior to modification are, you need to | |
| 1242 set cache=0. | |
| 1243 ''' | |
| 1244 return Node(self, nodeid, cache=cache) | |
| 1245 | |
| 1246 def set(self, nodeid, **propvalues): | |
| 1247 '''Modify a property on an existing node of this class. | |
| 1248 | |
| 1249 'nodeid' must be the id of an existing node of this class or an | |
| 1250 IndexError is raised. | |
| 1251 | |
| 1252 Each key in 'propvalues' must be the name of a property of this | |
| 1253 class or a KeyError is raised. | |
| 1254 | |
| 1255 All values in 'propvalues' must be acceptable types for their | |
| 1256 corresponding properties or a TypeError is raised. | |
| 1257 | |
| 1258 If the value of the key property is set, it must not collide with | |
| 1259 other key strings or a ValueError is raised. | |
| 1260 | |
| 1261 If the value of a Link or Multilink property contains an invalid | |
| 1262 node id, a ValueError is raised. | |
| 1263 ''' | |
| 1264 if not propvalues: | |
| 1265 return propvalues | |
| 1266 | |
| 1267 if propvalues.has_key('creation') or propvalues.has_key('activity'): | |
| 1268 raise KeyError, '"creation" and "activity" are reserved' | |
| 1269 | |
| 1270 if propvalues.has_key('id'): | |
| 1271 raise KeyError, '"id" is reserved' | |
| 1272 | |
| 1273 if self.db.journaltag is None: | |
| 1274 raise DatabaseError, 'Database open read-only' | |
| 1275 | |
| 1276 self.fireAuditors('set', nodeid, propvalues) | |
| 1277 # Take a copy of the node dict so that the subsequent set | |
| 1278 # operation doesn't modify the oldvalues structure. | |
| 1279 # XXX used to try the cache here first | |
| 1280 oldvalues = copy.deepcopy(self.db.getnode(self.classname, nodeid)) | |
| 1281 | |
| 1282 node = self.db.getnode(self.classname, nodeid) | |
| 1283 if self.is_retired(nodeid): | |
| 1284 raise IndexError, 'Requested item is retired' | |
| 1285 num_re = re.compile('^\d+$') | |
| 1286 | |
| 1287 # if the journal value is to be different, store it in here | |
| 1288 journalvalues = {} | |
| 1289 | |
| 1290 # remember the add/remove stuff for multilinks, making it easier | |
| 1291 # for the Database layer to do its stuff | |
| 1292 multilink_changes = {} | |
| 1293 | |
| 1294 for propname, value in propvalues.items(): | |
| 1295 # check to make sure we're not duplicating an existing key | |
| 1296 if propname == self.key and node[propname] != value: | |
| 1297 try: | |
| 1298 self.lookup(value) | |
| 1299 except KeyError: | |
| 1300 pass | |
| 1301 else: | |
| 1302 raise ValueError, 'node with key "%s" exists'%value | |
| 1303 | |
| 1304 # this will raise the KeyError if the property isn't valid | |
| 1305 # ... we don't use getprops() here because we only care about | |
| 1306 # the writeable properties. | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1307 try: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1308 prop = self.properties[propname] |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1309 except KeyError: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1310 raise KeyError, '"%s" has no property named "%s"'%( |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1311 self.classname, propname) |
| 1165 | 1312 |
| 1313 # if the value's the same as the existing value, no sense in | |
| 1314 # doing anything | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1315 current = node.get(propname, None) |
|
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1316 if value == current: |
| 1165 | 1317 del propvalues[propname] |
| 1318 continue | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1319 journalvalues[propname] = current |
| 1165 | 1320 |
| 1321 # do stuff based on the prop type | |
| 1322 if isinstance(prop, Link): | |
| 1323 link_class = prop.classname | |
| 1324 # if it isn't a number, it's a key | |
| 1325 if value is not None and not isinstance(value, type('')): | |
| 1326 raise ValueError, 'property "%s" link value be a string'%( | |
| 1327 propname) | |
| 1328 if isinstance(value, type('')) and not num_re.match(value): | |
| 1329 try: | |
| 1330 value = self.db.classes[link_class].lookup(value) | |
| 1331 except (TypeError, KeyError): | |
| 1332 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1333 propname, value, prop.classname) | |
| 1334 | |
| 1335 if (value is not None and | |
| 1336 not self.db.getclass(link_class).hasnode(value)): | |
| 1337 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 1338 | |
| 1339 if self.do_journal and prop.do_journal: | |
| 1340 # register the unlink with the old linked node | |
| 1341 if node[propname] is not None: | |
| 1342 self.db.addjournal(link_class, node[propname], 'unlink', | |
| 1343 (self.classname, nodeid, propname)) | |
| 1344 | |
| 1345 # register the link with the newly linked node | |
| 1346 if value is not None: | |
| 1347 self.db.addjournal(link_class, value, 'link', | |
| 1348 (self.classname, nodeid, propname)) | |
| 1349 | |
| 1350 elif isinstance(prop, Multilink): | |
| 1351 if type(value) != type([]): | |
| 1352 raise TypeError, 'new property "%s" not a list of'\ | |
| 1353 ' ids'%propname | |
| 1354 link_class = self.properties[propname].classname | |
| 1355 l = [] | |
| 1356 for entry in value: | |
| 1357 # if it isn't a number, it's a key | |
| 1358 if type(entry) != type(''): | |
| 1359 raise ValueError, 'new property "%s" link value ' \ | |
| 1360 'must be a string'%propname | |
| 1361 if not num_re.match(entry): | |
| 1362 try: | |
| 1363 entry = self.db.classes[link_class].lookup(entry) | |
| 1364 except (TypeError, KeyError): | |
| 1365 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1366 propname, entry, | |
| 1367 self.properties[propname].classname) | |
| 1368 l.append(entry) | |
| 1369 value = l | |
| 1370 propvalues[propname] = value | |
| 1371 | |
| 1372 # figure the journal entry for this property | |
| 1373 add = [] | |
| 1374 remove = [] | |
| 1375 | |
| 1376 # handle removals | |
| 1377 if node.has_key(propname): | |
| 1378 l = node[propname] | |
| 1379 else: | |
| 1380 l = [] | |
| 1381 for id in l[:]: | |
| 1382 if id in value: | |
| 1383 continue | |
| 1384 # register the unlink with the old linked node | |
| 1385 if self.do_journal and self.properties[propname].do_journal: | |
| 1386 self.db.addjournal(link_class, id, 'unlink', | |
| 1387 (self.classname, nodeid, propname)) | |
| 1388 l.remove(id) | |
| 1389 remove.append(id) | |
| 1390 | |
| 1391 # handle additions | |
| 1392 for id in value: | |
| 1393 if not self.db.getclass(link_class).hasnode(id): | |
| 1394 raise IndexError, '%s has no node %s'%(link_class, id) | |
| 1395 if id in l: | |
| 1396 continue | |
| 1397 # register the link with the newly linked node | |
| 1398 if self.do_journal and self.properties[propname].do_journal: | |
| 1399 self.db.addjournal(link_class, id, 'link', | |
| 1400 (self.classname, nodeid, propname)) | |
| 1401 l.append(id) | |
| 1402 add.append(id) | |
| 1403 | |
| 1404 # figure the journal entry | |
| 1405 l = [] | |
| 1406 if add: | |
| 1407 l.append(('+', add)) | |
| 1408 if remove: | |
| 1409 l.append(('-', remove)) | |
| 1410 multilink_changes[propname] = (add, remove) | |
| 1411 if l: | |
| 1412 journalvalues[propname] = tuple(l) | |
| 1413 | |
| 1414 elif isinstance(prop, String): | |
|
1383
f19dde90e473
applied unicode patch
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1365
diff
changeset
|
1415 if value is not None and type(value) != type('') and type(value) != type(u''): |
| 1165 | 1416 raise TypeError, 'new property "%s" not a string'%propname |
| 1417 | |
| 1418 elif isinstance(prop, Password): | |
| 1419 if not isinstance(value, password.Password): | |
| 1420 raise TypeError, 'new property "%s" not a Password'%propname | |
| 1421 propvalues[propname] = value | |
| 1422 | |
| 1423 elif value is not None and isinstance(prop, Date): | |
| 1424 if not isinstance(value, date.Date): | |
| 1425 raise TypeError, 'new property "%s" not a Date'% propname | |
| 1426 propvalues[propname] = value | |
| 1427 | |
| 1428 elif value is not None and isinstance(prop, Interval): | |
| 1429 if not isinstance(value, date.Interval): | |
| 1430 raise TypeError, 'new property "%s" not an '\ | |
| 1431 'Interval'%propname | |
| 1432 propvalues[propname] = value | |
| 1433 | |
| 1434 elif value is not None and isinstance(prop, Number): | |
| 1435 try: | |
| 1436 float(value) | |
| 1437 except ValueError: | |
| 1438 raise TypeError, 'new property "%s" not numeric'%propname | |
| 1439 | |
| 1440 elif value is not None and isinstance(prop, Boolean): | |
| 1441 try: | |
| 1442 int(value) | |
| 1443 except ValueError: | |
| 1444 raise TypeError, 'new property "%s" not boolean'%propname | |
| 1445 | |
| 1446 # nothing to do? | |
| 1447 if not propvalues: | |
| 1448 return propvalues | |
| 1449 | |
| 1450 # do the set, and journal it | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1451 self.db.setnode(self.classname, nodeid, propvalues, multilink_changes) |
| 1165 | 1452 |
| 1453 if self.do_journal: | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1454 self.db.addjournal(self.classname, nodeid, 'set', journalvalues) |
| 1165 | 1455 |
| 1456 self.fireReactors('set', nodeid, oldvalues) | |
| 1457 | |
| 1458 return propvalues | |
| 1459 | |
| 1460 def retire(self, nodeid): | |
| 1461 '''Retire a node. | |
| 1462 | |
| 1463 The properties on the node remain available from the get() method, | |
| 1464 and the node's id is never reused. | |
| 1465 | |
| 1466 Retired nodes are not returned by the find(), list(), or lookup() | |
| 1467 methods, and other nodes may reuse the values of their key properties. | |
| 1468 ''' | |
| 1469 if self.db.journaltag is None: | |
| 1470 raise DatabaseError, 'Database open read-only' | |
| 1471 | |
|
1345
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1472 self.fireAuditors('retire', nodeid, None) |
|
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1473 |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1474 # use the arg for __retired__ to cope with any odd database type |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1475 # conversion (hello, sqlite) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1476 sql = 'update _%s set __retired__=%s where id=%s'%(self.classname, |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1477 self.db.arg, self.db.arg) |
| 1165 | 1478 if __debug__: |
| 1479 print >>hyperdb.DEBUG, 'retire', (self, sql, nodeid) | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1480 self.db.cursor.execute(sql, (1, nodeid)) |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1481 if self.do_journal: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1482 self.db.addjournal(self.classname, nodeid, 'retired', None) |
| 1165 | 1483 |
|
1345
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1484 self.fireReactors('retire', nodeid, None) |
|
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1485 |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1486 def restore(self, nodeid): |
|
1740
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
1487 '''Restore a retired node. |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1488 |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1489 Make node available for all operations like it was before retirement. |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1490 ''' |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1491 if self.db.journaltag is None: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1492 raise DatabaseError, 'Database open read-only' |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1493 |
|
1523
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1494 node = self.db.getnode(self.classname, nodeid) |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1495 # check if key property was overrided |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1496 key = self.getkey() |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1497 try: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1498 id = self.lookup(node[key]) |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1499 except KeyError: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1500 pass |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1501 else: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1502 raise KeyError, "Key property (%s) of retired node clashes with \ |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1503 existing one (%s)" % (key, node[key]) |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1504 |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1505 self.fireAuditors('restore', nodeid, None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1506 # use the arg for __retired__ to cope with any odd database type |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1507 # conversion (hello, sqlite) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1508 sql = 'update _%s set __retired__=%s where id=%s'%(self.classname, |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1509 self.db.arg, self.db.arg) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1510 if __debug__: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1511 print >>hyperdb.DEBUG, 'restore', (self, sql, nodeid) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1512 self.db.cursor.execute(sql, (0, nodeid)) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1513 if self.do_journal: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1514 self.db.addjournal(self.classname, nodeid, 'restored', None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1515 |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1516 self.fireReactors('restore', nodeid, None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1517 |
| 1165 | 1518 def is_retired(self, nodeid): |
| 1519 '''Return true if the node is rerired | |
| 1520 ''' | |
| 1521 sql = 'select __retired__ from _%s where id=%s'%(self.classname, | |
| 1522 self.db.arg) | |
| 1523 if __debug__: | |
| 1524 print >>hyperdb.DEBUG, 'is_retired', (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:
1181
diff
changeset
|
1525 self.db.cursor.execute(sql, (nodeid,)) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1526 return int(self.db.sql_fetchone()[0]) |
| 1165 | 1527 |
| 1528 def destroy(self, nodeid): | |
| 1529 '''Destroy a node. | |
| 1530 | |
| 1531 WARNING: this method should never be used except in extremely rare | |
| 1532 situations where there could never be links to the node being | |
| 1533 deleted | |
| 1534 WARNING: use retire() instead | |
| 1535 WARNING: the properties of this node will not be available ever again | |
| 1536 WARNING: really, use retire() instead | |
| 1537 | |
| 1538 Well, I think that's enough warnings. This method exists mostly to | |
| 1539 support the session storage of the cgi interface. | |
| 1540 | |
| 1541 The node is completely removed from the hyperdb, including all journal | |
| 1542 entries. It will no longer be available, and will generally break code | |
| 1543 if there are any references to the node. | |
| 1544 ''' | |
| 1545 if self.db.journaltag is None: | |
| 1546 raise DatabaseError, 'Database open read-only' | |
| 1547 self.db.destroynode(self.classname, nodeid) | |
| 1548 | |
| 1549 def history(self, nodeid): | |
| 1550 '''Retrieve the journal of edits on a particular node. | |
| 1551 | |
| 1552 'nodeid' must be the id of an existing node of this class or an | |
| 1553 IndexError is raised. | |
| 1554 | |
| 1555 The returned list contains tuples of the form | |
| 1556 | |
|
1409
8dc60d87ab42
Fixed a backlog of bug reports, and worked on python 2.3 compatibility:
Richard Jones <richard@users.sourceforge.net>
parents:
1383
diff
changeset
|
1557 (nodeid, date, tag, action, params) |
| 1165 | 1558 |
| 1559 'date' is a Timestamp object specifying the time of the change and | |
| 1560 'tag' is the journaltag specified when the database was opened. | |
| 1561 ''' | |
| 1562 if not self.do_journal: | |
| 1563 raise ValueError, 'Journalling is disabled for this class' | |
| 1564 return self.db.getjournal(self.classname, nodeid) | |
| 1565 | |
| 1566 # Locating nodes: | |
| 1567 def hasnode(self, nodeid): | |
| 1568 '''Determine if the given nodeid actually exists | |
| 1569 ''' | |
| 1570 return self.db.hasnode(self.classname, nodeid) | |
| 1571 | |
| 1572 def setkey(self, propname): | |
| 1573 '''Select a String property of this class to be the key property. | |
| 1574 | |
| 1575 'propname' must be the name of a String property of this class or | |
| 1576 None, or a TypeError is raised. The values of the key property on | |
| 1577 all existing nodes must be unique or a ValueError is raised. | |
| 1578 ''' | |
| 1579 # XXX create an index on the key prop column | |
| 1580 prop = self.getprops()[propname] | |
| 1581 if not isinstance(prop, String): | |
| 1582 raise TypeError, 'key properties must be String' | |
| 1583 self.key = propname | |
| 1584 | |
| 1585 def getkey(self): | |
| 1586 '''Return the name of the key property for this class or None.''' | |
| 1587 return self.key | |
| 1588 | |
| 1589 def labelprop(self, default_to_id=0): | |
| 1590 ''' Return the property name for a label for the given node. | |
| 1591 | |
| 1592 This method attempts to generate a consistent label for the node. | |
| 1593 It tries the following in order: | |
| 1594 1. key property | |
| 1595 2. "name" property | |
| 1596 3. "title" property | |
| 1597 4. first property from the sorted property name list | |
| 1598 ''' | |
| 1599 k = self.getkey() | |
| 1600 if k: | |
| 1601 return k | |
| 1602 props = self.getprops() | |
| 1603 if props.has_key('name'): | |
| 1604 return 'name' | |
| 1605 elif props.has_key('title'): | |
| 1606 return 'title' | |
| 1607 if default_to_id: | |
| 1608 return 'id' | |
| 1609 props = props.keys() | |
| 1610 props.sort() | |
| 1611 return props[0] | |
| 1612 | |
| 1613 def lookup(self, keyvalue): | |
| 1614 '''Locate a particular node by its key property and return its id. | |
| 1615 | |
| 1616 If this class has no key property, a TypeError is raised. If the | |
| 1617 'keyvalue' matches one of the values for the key property among | |
| 1618 the nodes in this class, the matching node's id is returned; | |
| 1619 otherwise a KeyError is raised. | |
| 1620 ''' | |
| 1621 if not self.key: | |
| 1622 raise TypeError, 'No key property set for class %s'%self.classname | |
| 1623 | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1624 # use the arg to handle any odd database type conversion (hello, |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1625 # sqlite) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1626 sql = "select id from _%s where _%s=%s and __retired__ <> %s"%( |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1627 self.classname, self.key, self.db.arg, self.db.arg) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1628 self.db.sql(sql, (keyvalue, 1)) |
| 1165 | 1629 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1630 # see if there was a result that's not retired |
|
1203
735adcbfc665
fix to SQL lookup() and retirement
Richard Jones <richard@users.sourceforge.net>
parents:
1195
diff
changeset
|
1631 row = self.db.sql_fetchone() |
|
735adcbfc665
fix to SQL lookup() and retirement
Richard Jones <richard@users.sourceforge.net>
parents:
1195
diff
changeset
|
1632 if not row: |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1633 raise KeyError, 'No key (%s) value "%s" for "%s"'%(self.key, |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1634 keyvalue, self.classname) |
| 1165 | 1635 |
| 1636 # return the id | |
|
1203
735adcbfc665
fix to SQL lookup() and retirement
Richard Jones <richard@users.sourceforge.net>
parents:
1195
diff
changeset
|
1637 return row[0] |
| 1165 | 1638 |
| 1639 def find(self, **propspec): | |
| 1640 '''Get the ids of nodes in this class which link to the given nodes. | |
| 1641 | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1642 'propspec' consists of keyword args propname=nodeid or |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1643 propname={nodeid:1, } |
| 1165 | 1644 'propname' must be the name of a property in this class, or a |
| 1645 KeyError is raised. That property must be a Link or Multilink | |
| 1646 property, or a TypeError is raised. | |
| 1647 | |
| 1648 Any node in this class whose 'propname' property links to any of the | |
| 1649 nodeids will be returned. Used by the full text indexing, which knows | |
| 1650 that "foo" occurs in msg1, msg3 and file7, so we have hits on these | |
| 1651 issues: | |
| 1652 | |
| 1653 db.issue.find(messages={'1':1,'3':1}, files={'7':1}) | |
| 1654 ''' | |
| 1655 if __debug__: | |
| 1656 print >>hyperdb.DEBUG, 'find', (self, propspec) | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1657 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1658 # shortcut |
| 1165 | 1659 if not propspec: |
| 1660 return [] | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1661 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1662 # validate the args |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1663 props = self.getprops() |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1664 propspec = propspec.items() |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1665 for propname, nodeids in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1666 # check the prop is OK |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1667 prop = props[propname] |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1668 if not isinstance(prop, Link) and not isinstance(prop, Multilink): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1669 raise TypeError, "'%s' not a Link/Multilink property"%propname |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1670 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1671 # first, links |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1672 where = [] |
| 1165 | 1673 allvalues = () |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1674 a = self.db.arg |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1675 for prop, values in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1676 if not isinstance(props[prop], hyperdb.Link): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1677 continue |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1678 if type(values) is type(''): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1679 allvalues += (values,) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1680 where.append('_%s = %s'%(prop, a)) |
|
1563
e2a8ce4d2317
Class.find() may now find unset Links [SF#700620]
Richard Jones <richard@users.sourceforge.net>
parents:
1557
diff
changeset
|
1681 elif values is None: |
|
e2a8ce4d2317
Class.find() may now find unset Links [SF#700620]
Richard Jones <richard@users.sourceforge.net>
parents:
1557
diff
changeset
|
1682 where.append('_%s is NULL'%prop) |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1683 else: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1684 allvalues += tuple(values.keys()) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1685 where.append('_%s in (%s)'%(prop, ','.join([a]*len(values)))) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1686 tables = [] |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1687 if where: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1688 tables.append('select id as nodeid from _%s where %s'%( |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1689 self.classname, ' and '.join(where))) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1690 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1691 # now multilinks |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1692 for prop, values in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1693 if not isinstance(props[prop], hyperdb.Multilink): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1694 continue |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1695 if type(values) is type(''): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1696 allvalues += (values,) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1697 s = a |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1698 else: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1699 allvalues += tuple(values.keys()) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1700 s = ','.join([a]*len(values)) |
| 1165 | 1701 tables.append('select nodeid from %s_%s where linkid in (%s)'%( |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1702 self.classname, prop, s)) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1703 sql = '\nunion\n'.join(tables) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1704 self.db.sql(sql, allvalues) |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1705 l = [x[0] for x in self.db.sql_fetchall()] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1706 if __debug__: |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1707 print >>hyperdb.DEBUG, 'find ... ', l |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1708 return l |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1709 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1710 def stringFind(self, **requirements): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1711 '''Locate a particular node by matching a set of its String |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1712 properties in a caseless search. |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1713 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1714 If the property is not a String property, a TypeError is raised. |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1715 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1716 The return is a list of the id of all nodes that match. |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1717 ''' |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1718 where = [] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1719 args = [] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1720 for propname in requirements.keys(): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1721 prop = self.properties[propname] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1722 if isinstance(not prop, String): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1723 raise TypeError, "'%s' not a String property"%propname |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1724 where.append(propname) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1725 args.append(requirements[propname].lower()) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1726 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1727 # generate the where clause |
|
1549
a53a7e197360
fixed rdbms email address lookup (case insensitivity)
Richard Jones <richard@users.sourceforge.net>
parents:
1539
diff
changeset
|
1728 s = ' and '.join(['lower(_%s)=%s'%(col, self.db.arg) for col in where]) |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1729 sql = 'select id from _%s where %s'%(self.classname, s) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1730 self.db.sql(sql, tuple(args)) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
1731 l = [x[0] for x in self.db.sql_fetchall()] |
| 1165 | 1732 if __debug__: |
| 1733 print >>hyperdb.DEBUG, 'find ... ', l | |
| 1734 return l | |
| 1735 | |
| 1736 def list(self): | |
| 1737 ''' Return a list of the ids of the active nodes in this class. | |
| 1738 ''' | |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1739 return self.getnodeids(retired=0) |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1740 |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1741 def getnodeids(self, retired=None): |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1742 ''' Retrieve all the ids of the nodes for a particular Class. |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1743 |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1744 Set retired=None to get all nodes. Otherwise it'll get all the |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1745 retired or non-retired nodes, depending on the flag. |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1746 ''' |
|
1707
3d627e34f18e
sqlite backend now passes all tests under 2.3.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1599
diff
changeset
|
1747 # flip the sense of the 'retired' flag if we don't want all of them |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1748 if retired is not None: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
1749 if retired: |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
1750 args = (0, ) |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
1751 else: |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
1752 args = (1, ) |
|
1539
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1753 sql = 'select id from _%s where __retired__ <> %s'%(self.classname, |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1754 self.db.arg) |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1755 else: |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1756 args = () |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1757 sql = 'select id from _%s'%self.classname |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1758 if __debug__: |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
1759 print >>hyperdb.DEBUG, 'getnodeids', (self, sql, retired) |
|
1539
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1760 self.db.cursor.execute(sql, args) |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1761 ids = [x[0] for x in self.db.cursor.fetchall()] |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
1762 return ids |
| 1165 | 1763 |
|
1249
6c24a86a12ae
Fixes for SourceForge tracker bugs.
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
1764 def filter(self, search_matches, filterspec, sort=(None,None), |
|
6c24a86a12ae
Fixes for SourceForge tracker bugs.
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
1765 group=(None,None)): |
| 1165 | 1766 ''' Return a list of the ids of the active nodes in this class that |
| 1767 match the 'filter' spec, sorted by the group spec and then the | |
| 1768 sort spec | |
| 1769 | |
| 1770 "filterspec" is {propname: value(s)} | |
| 1771 "sort" and "group" are (dir, prop) where dir is '+', '-' or None | |
| 1772 and prop is a prop name or None | |
| 1773 "search_matches" is {nodeid: marker} | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1774 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1775 The filter must match all properties specificed - but if the |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1776 property value to match is a list, any one of the values in the |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1777 list may match for that property to match. |
| 1165 | 1778 ''' |
|
1205
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
1779 # just don't bother if the full-text search matched diddly |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
1780 if search_matches == {}: |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
1781 return [] |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
1782 |
| 1165 | 1783 cn = self.classname |
| 1784 | |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1785 timezone = self.db.getUserTimezone() |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1786 |
| 1165 | 1787 # figure the WHERE clause from the filterspec |
| 1788 props = self.getprops() | |
| 1789 frum = ['_'+cn] | |
| 1790 where = [] | |
| 1791 args = [] | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1792 a = self.db.arg |
| 1165 | 1793 for k, v in filterspec.items(): |
| 1794 propclass = props[k] | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1795 # now do other where clause stuff |
| 1165 | 1796 if isinstance(propclass, Multilink): |
| 1797 tn = '%s_%s'%(cn, k) | |
|
1556
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
1798 if v in ('-1', ['-1']): |
|
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
1799 # only match rows that have count(linkid)=0 in the |
|
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
1800 # corresponding multilink table) |
|
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
1801 where.append('id not in (select nodeid from %s)'%tn) |
|
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
1802 elif isinstance(v, type([])): |
|
1555
948c7764d46c
implemented ability to search for multilink properties with no value (not in mk)
Richard Jones <richard@users.sourceforge.net>
parents:
1549
diff
changeset
|
1803 frum.append(tn) |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1804 s = ','.join([a for x in v]) |
| 1165 | 1805 where.append('id=%s.nodeid and %s.linkid in (%s)'%(tn,tn,s)) |
| 1806 args = args + v | |
| 1807 else: | |
|
1555
948c7764d46c
implemented ability to search for multilink properties with no value (not in mk)
Richard Jones <richard@users.sourceforge.net>
parents:
1549
diff
changeset
|
1808 frum.append(tn) |
|
948c7764d46c
implemented ability to search for multilink properties with no value (not in mk)
Richard Jones <richard@users.sourceforge.net>
parents:
1549
diff
changeset
|
1809 where.append('id=%s.nodeid and %s.linkid=%s'%(tn, tn, a)) |
| 1165 | 1810 args.append(v) |
|
1365
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1811 elif k == 'id': |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1812 if isinstance(v, type([])): |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1813 s = ','.join([a for x in v]) |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1814 where.append('%s in (%s)'%(k, s)) |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1815 args = args + v |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1816 else: |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1817 where.append('%s=%s'%(k, a)) |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
1818 args.append(v) |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1819 elif isinstance(propclass, String): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1820 if not isinstance(v, type([])): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1821 v = [v] |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1822 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1823 # Quote the bits in the string that need it and then embed |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1824 # in a "substring" search. Note - need to quote the '%' so |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1825 # they make it through the python layer happily |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1826 v = ['%%'+self.db.sql_stringquote(s)+'%%' for s in v] |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1827 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1828 # now add to the where clause |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1829 where.append(' or '.join(["_%s LIKE '%s'"%(k, s) for s in v])) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1830 # note: args are embedded in the query string now |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1831 elif isinstance(propclass, Link): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1832 if isinstance(v, type([])): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1833 if '-1' in v: |
|
1578
ba13c04814f0
don't modify filterspec passed in (bad Richard)
Richard Jones <richard@users.sourceforge.net>
parents:
1563
diff
changeset
|
1834 v = v[:] |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1835 v.remove('-1') |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1836 xtra = ' or _%s is NULL'%k |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
1837 else: |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
1838 xtra = '' |
|
1295
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1839 if v: |
|
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1840 s = ','.join([a for x in v]) |
|
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1841 where.append('(_%s in (%s)%s)'%(k, s, xtra)) |
|
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1842 args = args + v |
|
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1843 else: |
|
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
1844 where.append('_%s is NULL'%k) |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1845 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1846 if v == '-1': |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1847 v = None |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1848 where.append('_%s is NULL'%k) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1849 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1850 where.append('_%s=%s'%(k, a)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1851 args.append(v) |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1852 elif isinstance(propclass, Date): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1853 if isinstance(v, type([])): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1854 s = ','.join([a for x in v]) |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1855 where.append('_%s in (%s)'%(k, s)) |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1856 args = args + [date.Date(x).serialise() for x in v] |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1857 else: |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1858 try: |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1859 # Try to filter on range of dates |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1860 date_rng = Range(v, date.Date, offset=timezone) |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1861 if (date_rng.from_value): |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1596
diff
changeset
|
1862 where.append('_%s >= %s'%(k, a)) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1863 args.append(date_rng.from_value.serialise()) |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1864 if (date_rng.to_value): |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1596
diff
changeset
|
1865 where.append('_%s <= %s'%(k, a)) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1866 args.append(date_rng.to_value.serialise()) |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1867 except ValueError: |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1868 # If range creation fails - ignore that search parameter |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
1869 pass |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1870 elif isinstance(propclass, Interval): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1871 if isinstance(v, type([])): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1872 s = ','.join([a for x in v]) |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1873 where.append('_%s in (%s)'%(k, s)) |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1874 args = args + [date.Interval(x).serialise() for x in v] |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
1875 else: |
|
1596
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1876 try: |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1877 # Try to filter on range of intervals |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1878 date_rng = Range(v, date.Interval) |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1879 if (date_rng.from_value): |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1596
diff
changeset
|
1880 where.append('_%s >= %s'%(k, a)) |
|
1596
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1881 args.append(date_rng.from_value.serialise()) |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1882 if (date_rng.to_value): |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1596
diff
changeset
|
1883 where.append('_%s <= %s'%(k, a)) |
|
1596
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1884 args.append(date_rng.to_value.serialise()) |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1885 except ValueError: |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1886 # If range creation fails - ignore that search parameter |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1887 pass |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1888 #where.append('_%s=%s'%(k, a)) |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
1889 #args.append(date.Interval(v).serialise()) |
| 1165 | 1890 else: |
| 1891 if isinstance(v, type([])): | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1892 s = ','.join([a for x in v]) |
| 1165 | 1893 where.append('_%s in (%s)'%(k, s)) |
| 1894 args = args + v | |
| 1895 else: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1896 where.append('_%s=%s'%(k, a)) |
| 1165 | 1897 args.append(v) |
| 1898 | |
|
1740
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
1899 # don't match retired nodes |
|
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
1900 where.append('__retired__ <> 1') |
|
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
1901 |
| 1165 | 1902 # add results of full text search |
| 1903 if search_matches is not None: | |
| 1904 v = search_matches.keys() | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1905 s = ','.join([a for x in v]) |
| 1165 | 1906 where.append('id in (%s)'%s) |
| 1907 args = args + v | |
| 1908 | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1909 # "grouping" is just the first-order sorting in the SQL fetch |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1910 # can modify it...) |
| 1165 | 1911 orderby = [] |
| 1912 ordercols = [] | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1913 if group[0] is not None and group[1] is not None: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1914 if group[0] != '-': |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1915 orderby.append('_'+group[1]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1916 ordercols.append('_'+group[1]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1917 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1918 orderby.append('_'+group[1]+' desc') |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1919 ordercols.append('_'+group[1]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1920 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1921 # now add in the sorting |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1922 group = '' |
| 1165 | 1923 if sort[0] is not None and sort[1] is not None: |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1924 direction, colname = sort |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1925 if direction != '-': |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1926 if colname == 'id': |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1927 orderby.append(colname) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1928 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1929 orderby.append('_'+colname) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1930 ordercols.append('_'+colname) |
| 1165 | 1931 else: |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1932 if colname == 'id': |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1933 orderby.append(colname+' desc') |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1934 ordercols.append(colname) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1935 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1936 orderby.append('_'+colname+' desc') |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1937 ordercols.append('_'+colname) |
| 1165 | 1938 |
| 1939 # construct the SQL | |
| 1940 frum = ','.join(frum) | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1941 if where: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1942 where = ' where ' + (' and '.join(where)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1943 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1944 where = '' |
| 1165 | 1945 cols = ['id'] |
| 1946 if orderby: | |
| 1947 cols = cols + ordercols | |
| 1948 order = ' order by %s'%(','.join(orderby)) | |
| 1949 else: | |
| 1950 order = '' | |
| 1951 cols = ','.join(cols) | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1952 sql = 'select %s from %s %s%s%s'%(cols, frum, where, group, order) |
| 1165 | 1953 args = tuple(args) |
| 1954 if __debug__: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1955 print >>hyperdb.DEBUG, 'filter', (self, sql, args) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1956 self.db.cursor.execute(sql, args) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1957 l = self.db.cursor.fetchall() |
| 1165 | 1958 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1959 # return the IDs (the first column) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1960 # XXX The filter(None, l) bit is sqlite-specific... if there's _NO_ |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1961 # XXX matches to a fetch, it returns NULL instead of nothing!?! |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1962 return filter(None, [row[0] for row in l]) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1963 |
| 1165 | 1964 def count(self): |
| 1965 '''Get the number of nodes in this class. | |
| 1966 | |
| 1967 If the returned integer is 'numnodes', the ids of all the nodes | |
| 1968 in this class run from 1 to numnodes, and numnodes+1 will be the | |
| 1969 id of the next node to be created in this class. | |
| 1970 ''' | |
| 1971 return self.db.countnodes(self.classname) | |
| 1972 | |
| 1973 # Manipulating properties: | |
| 1974 def getprops(self, protected=1): | |
| 1975 '''Return a dictionary mapping property names to property objects. | |
| 1976 If the "protected" flag is true, we include protected properties - | |
| 1977 those which may not be modified. | |
| 1978 ''' | |
| 1979 d = self.properties.copy() | |
| 1980 if protected: | |
| 1981 d['id'] = String() | |
| 1982 d['creation'] = hyperdb.Date() | |
| 1983 d['activity'] = hyperdb.Date() | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
1984 d['creator'] = hyperdb.Link('user') |
| 1165 | 1985 return d |
| 1986 | |
| 1987 def addprop(self, **properties): | |
| 1988 '''Add properties to this class. | |
| 1989 | |
| 1990 The keyword arguments in 'properties' must map names to property | |
| 1991 objects, or a TypeError is raised. None of the keys in 'properties' | |
| 1992 may collide with the names of existing properties, or a ValueError | |
| 1993 is raised before any properties have been added. | |
| 1994 ''' | |
| 1995 for key in properties.keys(): | |
| 1996 if self.properties.has_key(key): | |
| 1997 raise ValueError, key | |
| 1998 self.properties.update(properties) | |
| 1999 | |
| 2000 def index(self, nodeid): | |
| 2001 '''Add (or refresh) the node to search indexes | |
| 2002 ''' | |
| 2003 # find all the String properties that have indexme | |
| 2004 for prop, propclass in self.getprops().items(): | |
| 2005 if isinstance(propclass, String) and propclass.indexme: | |
| 2006 try: | |
| 2007 value = str(self.get(nodeid, prop)) | |
| 2008 except IndexError: | |
| 2009 # node no longer exists - entry should be removed | |
| 2010 self.db.indexer.purge_entry((self.classname, nodeid, prop)) | |
| 2011 else: | |
| 2012 # and index them under (classname, nodeid, property) | |
| 2013 self.db.indexer.add_text((self.classname, nodeid, prop), | |
| 2014 value) | |
| 2015 | |
| 2016 | |
| 2017 # | |
| 2018 # Detector interface | |
| 2019 # | |
| 2020 def audit(self, event, detector): | |
| 2021 '''Register a detector | |
| 2022 ''' | |
| 2023 l = self.auditors[event] | |
| 2024 if detector not in l: | |
| 2025 self.auditors[event].append(detector) | |
| 2026 | |
| 2027 def fireAuditors(self, action, nodeid, newvalues): | |
| 2028 '''Fire all registered auditors. | |
| 2029 ''' | |
| 2030 for audit in self.auditors[action]: | |
| 2031 audit(self.db, self, nodeid, newvalues) | |
| 2032 | |
| 2033 def react(self, event, detector): | |
| 2034 '''Register a detector | |
| 2035 ''' | |
| 2036 l = self.reactors[event] | |
| 2037 if detector not in l: | |
| 2038 self.reactors[event].append(detector) | |
| 2039 | |
| 2040 def fireReactors(self, action, nodeid, oldvalues): | |
| 2041 '''Fire all registered reactors. | |
| 2042 ''' | |
| 2043 for react in self.reactors[action]: | |
| 2044 react(self.db, self, nodeid, oldvalues) | |
| 2045 | |
|
1442
b42fa71754c9
don't attempt to create FileClass items if no content is supplied
Richard Jones <richard@users.sourceforge.net>
parents:
1431
diff
changeset
|
2046 class FileClass(Class, hyperdb.FileClass): |
| 1165 | 2047 '''This class defines a large chunk of data. To support this, it has a |
| 2048 mandatory String property "content" which is typically saved off | |
| 2049 externally to the hyperdb. | |
| 2050 | |
| 2051 The default MIME type of this data is defined by the | |
| 2052 "default_mime_type" class attribute, which may be overridden by each | |
| 2053 node if the class defines a "type" String property. | |
| 2054 ''' | |
| 2055 default_mime_type = 'text/plain' | |
| 2056 | |
| 2057 def create(self, **propvalues): | |
| 2058 ''' snaffle the file propvalue and store in a file | |
| 2059 ''' | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2060 # we need to fire the auditors now, or the content property won't |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2061 # be in propvalues for the auditors to play with |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2062 self.fireAuditors('create', None, propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2063 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2064 # now remove the content property so it's not stored in the db |
| 1165 | 2065 content = propvalues['content'] |
| 2066 del propvalues['content'] | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2067 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2068 # do the database create |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2069 newid = Class.create_inner(self, **propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2070 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2071 # fire reactors |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2072 self.fireReactors('create', newid, None) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2073 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2074 # store off the content as a file |
| 1165 | 2075 self.db.storefile(self.classname, newid, None, content) |
| 2076 return newid | |
| 2077 | |
| 2078 def import_list(self, propnames, proplist): | |
| 2079 ''' Trap the "content" property... | |
| 2080 ''' | |
| 2081 # dupe this list so we don't affect others | |
| 2082 propnames = propnames[:] | |
| 2083 | |
| 2084 # extract the "content" property from the proplist | |
| 2085 i = propnames.index('content') | |
| 2086 content = eval(proplist[i]) | |
| 2087 del propnames[i] | |
| 2088 del proplist[i] | |
| 2089 | |
| 2090 # do the normal import | |
| 2091 newid = Class.import_list(self, propnames, proplist) | |
| 2092 | |
| 2093 # save off the "content" file | |
| 2094 self.db.storefile(self.classname, newid, None, content) | |
| 2095 return newid | |
| 2096 | |
| 2097 _marker = [] | |
| 2098 def get(self, nodeid, propname, default=_marker, cache=1): | |
| 2099 ''' trap the content propname and get it from the file | |
| 2100 ''' | |
| 2101 poss_msg = 'Possibly a access right configuration problem.' | |
| 2102 if propname == 'content': | |
| 2103 try: | |
| 2104 return self.db.getfile(self.classname, nodeid, None) | |
| 2105 except IOError, (strerror): | |
| 2106 # BUG: by catching this we donot see an error in the log. | |
| 2107 return 'ERROR reading file: %s%s\n%s\n%s'%( | |
| 2108 self.classname, nodeid, poss_msg, strerror) | |
| 2109 if default is not self._marker: | |
| 2110 return Class.get(self, nodeid, propname, default, cache=cache) | |
| 2111 else: | |
| 2112 return Class.get(self, nodeid, propname, cache=cache) | |
| 2113 | |
| 2114 def getprops(self, protected=1): | |
| 2115 ''' In addition to the actual properties on the node, these methods | |
| 2116 provide the "content" property. If the "protected" flag is true, | |
| 2117 we include protected properties - those which may not be | |
| 2118 modified. | |
| 2119 ''' | |
| 2120 d = Class.getprops(self, protected=protected).copy() | |
| 2121 d['content'] = hyperdb.String() | |
| 2122 return d | |
| 2123 | |
| 2124 def index(self, nodeid): | |
| 2125 ''' Index the node in the search index. | |
| 2126 | |
| 2127 We want to index the content in addition to the normal String | |
| 2128 property indexing. | |
| 2129 ''' | |
| 2130 # perform normal indexing | |
| 2131 Class.index(self, nodeid) | |
| 2132 | |
| 2133 # get the content to index | |
| 2134 content = self.get(nodeid, 'content') | |
| 2135 | |
| 2136 # figure the mime type | |
| 2137 if self.properties.has_key('type'): | |
| 2138 mime_type = self.get(nodeid, 'type') | |
| 2139 else: | |
| 2140 mime_type = self.default_mime_type | |
| 2141 | |
| 2142 # and index! | |
| 2143 self.db.indexer.add_text((self.classname, nodeid, 'content'), content, | |
| 2144 mime_type) | |
| 2145 | |
| 2146 # XXX deviation from spec - was called ItemClass | |
| 2147 class IssueClass(Class, roundupdb.IssueClass): | |
| 2148 # Overridden methods: | |
| 2149 def __init__(self, db, classname, **properties): | |
| 2150 '''The newly-created class automatically includes the "messages", | |
| 2151 "files", "nosy", and "superseder" properties. If the 'properties' | |
| 2152 dictionary attempts to specify any of these properties or a | |
| 2153 "creation" or "activity" property, a ValueError is raised. | |
| 2154 ''' | |
| 2155 if not properties.has_key('title'): | |
| 2156 properties['title'] = hyperdb.String(indexme='yes') | |
| 2157 if not properties.has_key('messages'): | |
| 2158 properties['messages'] = hyperdb.Multilink("msg") | |
| 2159 if not properties.has_key('files'): | |
| 2160 properties['files'] = hyperdb.Multilink("file") | |
| 2161 if not properties.has_key('nosy'): | |
| 2162 # note: journalling is turned off as it really just wastes | |
| 2163 # space. this behaviour may be overridden in an instance | |
| 2164 properties['nosy'] = hyperdb.Multilink("user", do_journal="no") | |
| 2165 if not properties.has_key('superseder'): | |
| 2166 properties['superseder'] = hyperdb.Multilink(classname) | |
| 2167 Class.__init__(self, db, classname, **properties) | |
| 2168 |
