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