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