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