Mercurial > p > roundup > code
annotate roundup/backends/rdbms_common.py @ 3305:e074c641cb5e maint-0.7
fix RDBMS clear() so it resets all class itemid counters
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 02 May 2005 01:03:41 +0000 |
| parents | 60dcb64d25d3 |
| children | c7c25f2103b2 |
| rev | line source |
|---|---|
|
3305
e074c641cb5e
fix RDBMS clear() so it resets all class itemid counters
Richard Jones <richard@users.sourceforge.net>
parents:
3055
diff
changeset
|
1 # $Id: rdbms_common.py,v 1.98.2.31 2005-05-02 01:03:40 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.) |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
22 |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
23 The schema of the hyperdb being mapped to the database is stored in the |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
24 database itself as a repr()'ed dictionary of information about each Class |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
25 that maps to a table. If that information differs from the hyperdb schema, |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
26 then we update it. We also store in the schema dict a version which |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
27 allows us to upgrade the database schema when necessary. See upgrade_db(). |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1222
diff
changeset
|
28 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
29 __docformat__ = 'restructuredtext' |
| 1165 | 30 |
| 31 # standard python modules | |
| 32 import sys, os, time, re, errno, weakref, copy | |
| 33 | |
| 34 # roundup modules | |
| 35 from roundup import hyperdb, date, password, roundupdb, security | |
| 36 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
|
37 Multilink, DatabaseError, Boolean, Number, Node |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
38 from roundup.backends import locking |
| 1165 | 39 |
| 40 # support | |
| 41 from blobfiles import FileStorage | |
|
2093
3f6024ab2c7a
That's the last of the RDBMS migration steps done! Yay!
Richard Jones <richard@users.sourceforge.net>
parents:
2089
diff
changeset
|
42 from indexer_rdbms import Indexer |
|
2082
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
43 from sessions_rdbms import Sessions, OneTimeKeys |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
44 from roundup.date import Range |
| 1165 | 45 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
46 # number of rows to keep in memory |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
47 ROW_CACHE_SIZE = 100 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
48 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
49 def _num_cvt(num): |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
50 num = str(num) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
51 try: |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
52 return int(num) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
53 except: |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
54 return float(num) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
55 |
|
2482
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
56 def _bool_cvt(value): |
|
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
57 if value in ('TRUE', 'FALSE'): |
|
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
58 return {'TRUE': 1, 'FALSE': 0}[value] |
|
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
59 # assume it's a number returned from the db API |
|
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
60 return int(value) |
|
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
61 |
| 1165 | 62 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
|
63 ''' 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
|
64 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
65 - 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
|
66 the sql_* methods that are NotImplemented |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
67 - 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
|
68 ''' |
| 1165 | 69 def __init__(self, config, journaltag=None): |
| 70 ''' Open the database and load the schema from it. | |
| 71 ''' | |
| 72 self.config, self.journaltag = config, journaltag | |
| 73 self.dir = config.DATABASE | |
| 74 self.classes = {} | |
|
2093
3f6024ab2c7a
That's the last of the RDBMS migration steps done! Yay!
Richard Jones <richard@users.sourceforge.net>
parents:
2089
diff
changeset
|
75 self.indexer = Indexer(self) |
| 1165 | 76 self.security = security.Security(self) |
| 77 | |
| 78 # additional transaction support for external files and the like | |
| 79 self.transactions = [] | |
| 80 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
81 # 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
|
82 # (classname, nodeid) = row |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
83 self.cache = {} |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
84 self.cache_lru = [] |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
85 self.stats = {'cache_hits': 0, 'cache_misses': 0, 'get_items': 0, |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
86 'filtering': 0} |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
87 |
|
1333
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
88 # database lock |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
89 self.lockfile = None |
|
80d27b7d6db5
implemented whole-database locking
Richard Jones <richard@users.sourceforge.net>
parents:
1304
diff
changeset
|
90 |
| 1165 | 91 # open a connection to the database, creating the "conn" attribute |
|
2082
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
92 self.open_connection() |
| 1165 | 93 |
|
1181
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
94 def clearCache(self): |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
95 self.cache = {} |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
96 self.cache_lru = [] |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
97 |
|
2082
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
98 def getSessionManager(self): |
|
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
99 return Sessions(self) |
|
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
100 |
|
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
101 def getOTKManager(self): |
|
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
102 return OneTimeKeys(self) |
|
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
103 |
|
c091cacdc505
Finished implementation of session and one-time-key stores for RDBMS backends.
Richard Jones <richard@users.sourceforge.net>
parents:
2081
diff
changeset
|
104 def open_connection(self): |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
105 ''' Open a connection to the database, creating it if necessary. |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
106 |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
107 Must call self.load_dbschema() |
| 1165 | 108 ''' |
| 109 raise NotImplemented | |
| 110 | |
|
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
|
111 def sql(self, sql, args=None): |
| 1165 | 112 ''' Execute the sql with the optional args. |
| 113 ''' | |
| 114 if __debug__: | |
| 115 print >>hyperdb.DEBUG, (self, sql, args) | |
| 116 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
|
117 self.cursor.execute(sql, args) |
| 1165 | 118 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
|
119 self.cursor.execute(sql) |
| 1165 | 120 |
|
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
|
121 def sql_fetchone(self): |
| 1165 | 122 ''' Fetch a single row. If there's nothing to fetch, return None. |
| 123 ''' | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
124 return self.cursor.fetchone() |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
125 |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
126 def sql_fetchall(self): |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
127 ''' 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
|
128 ''' |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
129 return self.cursor.fetchall() |
| 1165 | 130 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
131 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
|
132 ''' 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
|
133 ''' |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
134 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
|
135 |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
136 def init_dbschema(self): |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
137 self.database_schema = { |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
138 'version': self.current_db_version, |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
139 'tables': {} |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
140 } |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
141 |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
142 def load_dbschema(self): |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
143 ''' Load the schema definition that the database currently implements |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
144 ''' |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
145 self.cursor.execute('select schema from schema') |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
146 schema = self.cursor.fetchone() |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
147 if schema: |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
148 self.database_schema = eval(schema[0]) |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
149 else: |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
150 self.database_schema = {} |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
151 |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
152 def save_dbschema(self): |
| 1165 | 153 ''' Save the schema definition that the database currently implements |
| 154 ''' | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
155 s = repr(self.database_schema) |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
156 self.sql('delete from schema') |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
157 self.sql('insert into schema values (%s)', (s,)) |
| 1165 | 158 |
| 159 def post_init(self): | |
| 160 ''' Called once the schema initialisation has finished. | |
| 161 | |
| 162 We should now confirm that the schema defined by our "classes" | |
| 163 attribute actually matches the schema in the database. | |
| 164 ''' | |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
165 save = self.upgrade_db() |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
166 |
| 1165 | 167 # now detect changes in the schema |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
168 tables = self.database_schema['tables'] |
| 1165 | 169 for classname, spec in self.classes.items(): |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
170 if tables.has_key(classname): |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
171 dbspec = tables[classname] |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
172 if self.update_class(spec, dbspec): |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
173 tables[classname] = spec.schema() |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
174 save = 1 |
| 1165 | 175 else: |
| 176 self.create_class(spec) | |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
177 tables[classname] = spec.schema() |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
178 save = 1 |
| 1165 | 179 |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
180 for classname, spec in tables.items(): |
| 1165 | 181 if not self.classes.has_key(classname): |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
182 self.drop_class(classname, tables[classname]) |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
183 del tables[classname] |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
184 save = 1 |
| 1165 | 185 |
| 186 # update the database version of the schema | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
187 if save: |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
188 self.save_dbschema() |
| 1165 | 189 |
| 190 # reindex the db if necessary | |
| 191 if self.indexer.should_reindex(): | |
| 192 self.reindex() | |
| 193 | |
| 194 # commit | |
|
2093
3f6024ab2c7a
That's the last of the RDBMS migration steps done! Yay!
Richard Jones <richard@users.sourceforge.net>
parents:
2089
diff
changeset
|
195 self.sql_commit() |
| 1165 | 196 |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
197 # update this number when we need to make changes to the SQL structure |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
198 # of the backen database |
|
2722
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
199 current_db_version = 4 |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
200 def upgrade_db(self): |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
201 ''' Update the SQL database to reflect changes in the backend code. |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
202 |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
203 Return boolean whether we need to save the schema. |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
204 ''' |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
205 version = self.database_schema.get('version', 1) |
|
2081
fb4bf55b94d7
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2077
diff
changeset
|
206 if version == self.current_db_version: |
|
fb4bf55b94d7
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2077
diff
changeset
|
207 # nothing to do |
|
fb4bf55b94d7
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2077
diff
changeset
|
208 return 0 |
|
fb4bf55b94d7
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2077
diff
changeset
|
209 |
|
2428
a8db3996b4f5
better upgrading
Richard Jones <richard@users.sourceforge.net>
parents:
2425
diff
changeset
|
210 if version < 2: |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
211 # change the schema structure |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
212 self.database_schema = {'tables': self.database_schema} |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
213 |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
214 # version 1 didn't have the actor column (note that in |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
215 # MySQL this will also transition the tables to typed columns) |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
216 self.add_new_columns_v2() |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
217 |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
218 # version 1 doesn't have the OTK, session and indexing in the |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
219 # database |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
220 self.create_version_2_tables() |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
221 |
|
2428
a8db3996b4f5
better upgrading
Richard Jones <richard@users.sourceforge.net>
parents:
2425
diff
changeset
|
222 if version < 3: |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
223 self.fix_version_2_tables() |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
224 |
|
2722
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
225 if version < 4: |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
226 self.fix_version_3_tables() |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
227 |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
228 self.database_schema['version'] = self.current_db_version |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
229 return 1 |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
230 |
|
2722
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
231 def fix_version_3_tables(self): |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
232 # drop the shorter VARCHAR OTK column and add a new TEXT one |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
233 for name in ('otk', 'session'): |
|
2756
b654a33346a6
merge from HEAD, pre-release stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2734
diff
changeset
|
234 self.sql('DELETE FROM %ss'%name) |
|
2722
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
235 self.sql('ALTER TABLE %ss DROP %s_value'%(name, name)) |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
236 self.sql('ALTER TABLE %ss ADD %s_value TEXT'%(name, name)) |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
237 |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
238 def fix_version_2_tables(self): |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
239 '''Default (used by sqlite): NOOP''' |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
240 pass |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
241 |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
242 def _convert_journal_tables(self): |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
243 '''Get current journal table contents, drop the table and re-create''' |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
244 c = self.cursor |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
245 cols = ','.join('nodeid date tag action params'.split()) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
246 for klass in self.classes.values(): |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
247 # slurp and drop |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
248 sql = 'select %s from %s__journal order by date'%(cols, |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
249 klass.classname) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
250 c.execute(sql) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
251 contents = c.fetchall() |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
252 self.drop_journal_table_indexes(klass.classname) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
253 c.execute('drop table %s__journal'%klass.classname) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
254 |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
255 # re-create and re-populate |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
256 self.create_journal_table(klass) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
257 a = self.arg |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
258 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%( |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
259 klass.classname, cols, a, a, a, a, a) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
260 for row in contents: |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
261 # no data conversion needed |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
262 self.cursor.execute(sql, row) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
263 |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
264 def _convert_string_properties(self): |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
265 '''Get current Class tables that contain String properties, and |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
266 convert the VARCHAR columns to TEXT''' |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
267 c = self.cursor |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
268 for klass in self.classes.values(): |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
269 # slurp and drop |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
270 cols, mls = self.determine_columns(klass.properties.items()) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
271 scols = ','.join([i[0] for i in cols]) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
272 sql = 'select id,%s from _%s'%(scols, klass.classname) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
273 c.execute(sql) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
274 contents = c.fetchall() |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
275 self.drop_class_table_indexes(klass.classname, klass.getkey()) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
276 c.execute('drop table _%s'%klass.classname) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
277 |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
278 # re-create and re-populate |
|
2425
e4f06fcbbc89
argh! backwards compat
Richard Jones <richard@users.sourceforge.net>
parents:
2417
diff
changeset
|
279 self.create_class_table(klass, create_sequence=0) |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
280 a = ','.join([self.arg for i in range(len(cols)+1)]) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
281 sql = 'insert into _%s (id,%s) values (%s)'%(klass.classname, |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
282 scols, a) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
283 for row in contents: |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
284 l = [] |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
285 for entry in row: |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
286 # mysql will already be a string - psql needs "help" |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
287 if entry is not None and not isinstance(entry, type('')): |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
288 entry = str(entry) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
289 l.append(entry) |
|
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
290 self.cursor.execute(sql, l) |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
291 |
|
1840
91a4619b1a14
hyperdb grows a refresh_database() method.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1838
diff
changeset
|
292 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
|
293 self.post_init() |
|
1840
91a4619b1a14
hyperdb grows a refresh_database() method.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1838
diff
changeset
|
294 |
| 1165 | 295 def reindex(self): |
| 296 for klass in self.classes.values(): | |
| 297 for nodeid in klass.list(): | |
| 298 klass.index(nodeid) | |
| 299 self.indexer.save_index() | |
| 300 | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
301 hyperdb_to_sql_datatypes = { |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
302 hyperdb.String : 'TEXT', |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
303 hyperdb.Date : 'TIMESTAMP', |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
304 hyperdb.Link : 'INTEGER', |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
305 hyperdb.Interval : 'VARCHAR(255)', |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
306 hyperdb.Password : 'VARCHAR(255)', |
|
2166
cd42c3c7173a
MySQL and Postgresql use BOOL/BOOLEAN for Boolean types
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
307 hyperdb.Boolean : 'BOOLEAN', |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
308 hyperdb.Number : 'REAL', |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
309 } |
| 1165 | 310 def determine_columns(self, properties): |
| 311 ''' Figure the column names and multilink properties from the spec | |
| 312 | |
| 313 "properties" is a list of (name, prop) where prop may be an | |
| 314 instance of a hyperdb "type" _or_ a string repr of that type. | |
| 315 ''' | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
316 cols = [ |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
317 ('_actor', self.hyperdb_to_sql_datatypes[hyperdb.Link]), |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
318 ('_activity', self.hyperdb_to_sql_datatypes[hyperdb.Date]), |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
319 ('_creator', self.hyperdb_to_sql_datatypes[hyperdb.Link]), |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
320 ('_creation', self.hyperdb_to_sql_datatypes[hyperdb.Date]), |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
321 ] |
| 1165 | 322 mls = [] |
| 323 # add the multilinks separately | |
| 324 for col, prop in properties: | |
| 325 if isinstance(prop, Multilink): | |
| 326 mls.append(col) | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
327 continue |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
328 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
329 if isinstance(prop, type('')): |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
330 raise ValueError, "string property spec!" |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
331 #and prop.find('Multilink') != -1: |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
332 #mls.append(col) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
333 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
334 datatype = self.hyperdb_to_sql_datatypes[prop.__class__] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
335 cols.append(('_'+col, datatype)) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
336 |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
337 # Intervals stored as two columns |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
338 if isinstance(prop, Interval): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
339 cols.append(('__'+col+'_int__', 'BIGINT')) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
340 |
| 1165 | 341 cols.sort() |
| 342 return cols, mls | |
| 343 | |
|
1840
91a4619b1a14
hyperdb grows a refresh_database() method.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1838
diff
changeset
|
344 def update_class(self, spec, old_spec, force=0): |
| 1165 | 345 ''' 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
|
346 database version of the spec, and update where necessary. |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
347 |
|
1840
91a4619b1a14
hyperdb grows a refresh_database() method.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1838
diff
changeset
|
348 If 'force' is true, update the database anyway. |
| 1165 | 349 ''' |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
350 new_has = spec.properties.has_key |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
351 new_spec = spec.schema() |
|
1515
a516bbb9896b
fixed rdbms table update detection logic [SF#703297]
Richard Jones <richard@users.sourceforge.net>
parents:
1500
diff
changeset
|
352 new_spec[1].sort() |
|
a516bbb9896b
fixed rdbms table update detection logic [SF#703297]
Richard Jones <richard@users.sourceforge.net>
parents:
1500
diff
changeset
|
353 old_spec[1].sort() |
|
1840
91a4619b1a14
hyperdb grows a refresh_database() method.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1838
diff
changeset
|
354 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
|
355 # no changes |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
356 return 0 |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
357 |
| 1165 | 358 if __debug__: |
| 359 print >>hyperdb.DEBUG, 'update_class FIRING' | |
| 360 | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
361 # detect key prop change for potential index change |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
362 keyprop_changes = {} |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
363 if new_spec[0] != old_spec[0]: |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
364 keyprop_changes = {'remove': old_spec[0], 'add': new_spec[0]} |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
365 |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
366 # 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
|
367 old_has = {} |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
368 for name, prop in old_spec[1]: |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
369 old_has[name] = 1 |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
370 if new_has(name): |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
371 continue |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
372 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
373 if prop.find('Multilink to') != -1: |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
374 # first drop indexes. |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
375 self.drop_multilink_table_indexes(spec.classname, name) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
376 |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
377 # now the multilink table itself |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
378 sql = 'drop table %s_%s'%(spec.classname, name) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
379 else: |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
380 # if this is the key prop, drop the index first |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
381 if old_spec[0] == prop: |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
382 self.drop_class_table_key_index(spec.classname, name) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
383 del keyprop_changes['remove'] |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
384 |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
385 # drop the column |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
386 sql = 'alter table _%s drop column _%s'%(spec.classname, name) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
387 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
388 if __debug__: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
389 print >>hyperdb.DEBUG, 'update_class', (self, sql) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
390 self.cursor.execute(sql) |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
391 old_has = old_has.has_key |
|
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
392 |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
393 # if we didn't remove the key prop just then, but the key prop has |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
394 # changed, we still need to remove the old index |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
395 if keyprop_changes.has_key('remove'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
396 self.drop_class_table_key_index(spec.classname, |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
397 keyprop_changes['remove']) |
| 1165 | 398 |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
399 # add new columns |
|
2196
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
400 for propname, prop in new_spec[1]: |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
401 if old_has(propname): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
402 continue |
|
2196
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
403 prop = spec.properties[propname] |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
404 if isinstance(prop, Multilink): |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
405 self.create_multilink_table(spec, propname) |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
406 else: |
|
2728
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
407 # add the column |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
408 coltype = self.hyperdb_to_sql_datatypes[prop.__class__] |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
409 sql = 'alter table _%s add column _%s %s'%( |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
410 spec.classname, propname, coltype) |
|
2196
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
411 if __debug__: |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
412 print >>hyperdb.DEBUG, 'update_class', (self, sql) |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
413 self.cursor.execute(sql) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
414 |
|
2728
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
415 # extra Interval column |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
416 if isinstance(prop, Interval): |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
417 sql = 'alter table _%s add column __%s_int__ BIGINT'%( |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
418 spec.classname, propname) |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
419 if __debug__: |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
420 print >>hyperdb.DEBUG, 'update_class', (self, sql) |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
421 self.cursor.execute(sql) |
|
572746c94537
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2722
diff
changeset
|
422 |
|
2196
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
423 # if the new column is a key prop, we need an index! |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
424 if new_spec[0] == propname: |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
425 self.create_class_table_key_index(spec.classname, propname) |
|
85954067e496
mysql and postgresql schema mutation now handle added Multilinks; fixed test too
Richard Jones <richard@users.sourceforge.net>
parents:
2185
diff
changeset
|
426 del keyprop_changes['add'] |
| 1165 | 427 |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
428 # if we didn't add the key prop just then, but the key prop has |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
429 # changed, we still need to add the new index |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
430 if keyprop_changes.has_key('add'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
431 self.create_class_table_key_index(spec.classname, |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
432 keyprop_changes['add']) |
|
1479
405e91b5be46
fixed rdbms mutation of properties
Richard Jones <richard@users.sourceforge.net>
parents:
1476
diff
changeset
|
433 |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
434 return 1 |
| 1165 | 435 |
|
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
|
436 def create_class_table(self, spec): |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
437 '''Create the class table for the given Class "spec". Creates the |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
438 indexes too.''' |
| 1165 | 439 cols, mls = self.determine_columns(spec.properties.items()) |
| 440 | |
| 441 # add on our special columns | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
442 cols.append(('id', 'INTEGER PRIMARY KEY')) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
443 cols.append(('__retired__', 'INTEGER DEFAULT 0')) |
| 1165 | 444 |
| 445 # create the base table | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
446 scols = ','.join(['%s %s'%x for x in cols]) |
| 1165 | 447 sql = 'create table _%s (%s)'%(spec.classname, scols) |
| 448 if __debug__: | |
| 449 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
|
450 self.cursor.execute(sql) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
451 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
452 self.create_class_table_indexes(spec) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
453 |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
454 return cols, mls |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
455 |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
456 def create_class_table_indexes(self, spec): |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
457 ''' create the class table for the given spec |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
458 ''' |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
459 # 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
|
460 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
|
461 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
|
462 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
|
463 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
|
464 self.cursor.execute(index_sql2) |
| 1165 | 465 |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
466 # create index for key property |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
467 if spec.key: |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
468 if __debug__: |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
469 print >>hyperdb.DEBUG, 'update_class setting keyprop %r'% \ |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
470 spec.key |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
471 index_sql3 = 'create index _%s_%s_idx on _%s(_%s)'%( |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
472 spec.classname, spec.key, |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
473 spec.classname, spec.key) |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
474 if __debug__: |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
475 print >>hyperdb.DEBUG, 'create_index', (self, index_sql3) |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
476 self.cursor.execute(index_sql3) |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
477 |
|
2228
1d1362c54c94
Some doc / comment fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
2217
diff
changeset
|
478 # TODO: create indexes on (selected?) Link property columns, as |
|
1d1362c54c94
Some doc / comment fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
2217
diff
changeset
|
479 # they're more likely to be used for lookup |
|
1d1362c54c94
Some doc / comment fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
2217
diff
changeset
|
480 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
481 def drop_class_table_indexes(self, cn, key): |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
482 # drop the old table indexes first |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
483 l = ['_%s_id_idx'%cn, '_%s_retired_idx'%cn] |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
484 if key: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
485 l.append('_%s_%s_idx'%(cn, key)) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
486 |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
487 table_name = '_%s'%cn |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
488 for index_name in l: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
489 if not self.sql_index_exists(table_name, index_name): |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
490 continue |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
491 index_sql = 'drop index '+index_name |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
492 if __debug__: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
493 print >>hyperdb.DEBUG, 'drop_index', (self, index_sql) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
494 self.cursor.execute(index_sql) |
| 1165 | 495 |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
496 def create_class_table_key_index(self, cn, key): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
497 ''' create the class table for the given spec |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
498 ''' |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
499 sql = 'create index _%s_%s_idx on _%s(_%s)'%(cn, key, cn, key) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
500 if __debug__: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
501 print >>hyperdb.DEBUG, 'create_class_tab_key_index', (self, sql) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
502 self.cursor.execute(sql) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
503 |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
504 def drop_class_table_key_index(self, cn, key): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
505 table_name = '_%s'%cn |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
506 index_name = '_%s_%s_idx'%(cn, key) |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
507 if not self.sql_index_exists(table_name, index_name): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
508 return |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
509 sql = 'drop index '+index_name |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
510 if __debug__: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
511 print >>hyperdb.DEBUG, 'drop_class_tab_key_index', (self, sql) |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
512 self.cursor.execute(sql) |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
513 |
|
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
|
514 def create_journal_table(self, spec): |
| 1165 | 515 ''' create the journal table for a class given the spec and |
| 516 already-determined cols | |
| 517 ''' | |
| 518 # journal table | |
| 519 cols = ','.join(['%s varchar'%x | |
| 520 for x in 'nodeid date tag action params'.split()]) | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
521 sql = '''create table %s__journal ( |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
522 nodeid integer, date timestamp, tag varchar(255), |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
523 action varchar(255), params text)'''%spec.classname |
| 1165 | 524 if __debug__: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
525 print >>hyperdb.DEBUG, 'create_journal_table', (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
|
526 self.cursor.execute(sql) |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
527 self.create_journal_table_indexes(spec) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
528 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
529 def create_journal_table_indexes(self, spec): |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
530 # index on nodeid |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
531 sql = 'create index %s_journ_idx on %s__journal(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
|
532 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
|
533 if __debug__: |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
534 print >>hyperdb.DEBUG, 'create_index', (self, sql) |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
535 self.cursor.execute(sql) |
| 1165 | 536 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
537 def drop_journal_table_indexes(self, classname): |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
538 index_name = '%s_journ_idx'%classname |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
539 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
|
540 return |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
541 index_sql = 'drop index '+index_name |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
542 if __debug__: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
543 print >>hyperdb.DEBUG, 'drop_index', (self, index_sql) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
544 self.cursor.execute(index_sql) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
545 |
|
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
|
546 def create_multilink_table(self, spec, ml): |
| 1165 | 547 ''' Create a multilink table for the "ml" property of the class |
| 548 given by the spec | |
| 549 ''' | |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
550 # create the table |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
551 sql = 'create table %s_%s (linkid varchar(255), nodeid varchar(255))'%( |
| 1165 | 552 spec.classname, ml) |
| 553 if __debug__: | |
| 554 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
|
555 self.cursor.execute(sql) |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
556 self.create_multilink_table_indexes(spec, ml) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
557 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
558 def create_multilink_table_indexes(self, spec, ml): |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
559 # 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
|
560 index_sql = 'create index %s_%s_l_idx on %s_%s(linkid)'%( |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
561 spec.classname, ml, spec.classname, ml) |
|
1836
94e430ad4fdb
make the RDBMS common backend and the SQLite and MYsql backend create...
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1800
diff
changeset
|
562 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
|
563 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
|
564 self.cursor.execute(index_sql) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
565 |
|
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
566 # 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
|
567 index_sql = 'create index %s_%s_n_idx on %s_%s(nodeid)'%( |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
568 spec.classname, ml, spec.classname, ml) |
|
1836
94e430ad4fdb
make the RDBMS common backend and the SQLite and MYsql backend create...
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1800
diff
changeset
|
569 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
|
570 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
|
571 self.cursor.execute(index_sql) |
| 1165 | 572 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
573 def drop_multilink_table_indexes(self, classname, ml): |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
574 l = [ |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
575 '%s_%s_l_idx'%(classname, ml), |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
576 '%s_%s_n_idx'%(classname, ml) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
577 ] |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
578 table_name = '%s_%s'%(classname, ml) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
579 for index_name in l: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
580 if not self.sql_index_exists(table_name, index_name): |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
581 continue |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
582 index_sql = 'drop index %s'%index_name |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
583 if __debug__: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
584 print >>hyperdb.DEBUG, 'drop_index', (self, index_sql) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
585 self.cursor.execute(index_sql) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
586 |
| 1165 | 587 def create_class(self, spec): |
| 588 ''' Create a database table according to the given spec. | |
| 589 ''' | |
|
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
|
590 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
|
591 self.create_journal_table(spec) |
| 1165 | 592 |
| 593 # now create the multilink tables | |
| 594 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
|
595 self.create_multilink_table(spec, ml) |
| 1165 | 596 |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
597 def drop_class(self, cn, spec): |
| 1165 | 598 ''' Drop the given table from the database. |
| 599 | |
| 600 Drop the journal and multilink tables too. | |
| 601 ''' | |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
602 properties = spec[1] |
| 1165 | 603 # figure the multilinks |
| 604 mls = [] | |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
605 for propanme, prop in properties: |
| 1165 | 606 if isinstance(prop, Multilink): |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
607 mls.append(propname) |
| 1165 | 608 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
609 # drop class table and indexes |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
610 self.drop_class_table_indexes(cn, spec[0]) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
611 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
612 self.drop_class_table(cn) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
613 |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
614 # drop journal table and indexes |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
615 self.drop_journal_table_indexes(cn) |
|
1873
f63aa57386b0
Backend improvements.
Richard Jones <richard@users.sourceforge.net>
parents:
1840
diff
changeset
|
616 sql = 'drop table %s__journal'%cn |
| 1165 | 617 if __debug__: |
| 618 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
|
619 self.cursor.execute(sql) |
| 1165 | 620 |
| 621 for ml in mls: | |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
622 # drop multilink table and indexes |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
623 self.drop_multilink_table_indexes(cn, ml) |
| 1165 | 624 sql = 'drop table %s_%s'%(spec.classname, ml) |
| 625 if __debug__: | |
| 626 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
|
627 self.cursor.execute(sql) |
| 1165 | 628 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
629 def drop_class_table(self, cn): |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
630 sql = 'drop table _%s'%cn |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
631 if __debug__: |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
632 print >>hyperdb.DEBUG, 'drop_class', (self, sql) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
633 self.cursor.execute(sql) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
634 |
| 1165 | 635 # |
| 636 # Classes | |
| 637 # | |
| 638 def __getattr__(self, classname): | |
| 639 ''' A convenient way of calling self.getclass(classname). | |
| 640 ''' | |
| 641 if self.classes.has_key(classname): | |
| 642 if __debug__: | |
| 643 print >>hyperdb.DEBUG, '__getattr__', (self, classname) | |
| 644 return self.classes[classname] | |
| 645 raise AttributeError, classname | |
| 646 | |
| 647 def addclass(self, cl): | |
| 648 ''' Add a Class to the hyperdatabase. | |
| 649 ''' | |
| 650 if __debug__: | |
| 651 print >>hyperdb.DEBUG, 'addclass', (self, cl) | |
| 652 cn = cl.classname | |
| 653 if self.classes.has_key(cn): | |
| 654 raise ValueError, cn | |
| 655 self.classes[cn] = cl | |
| 656 | |
|
2076
2a4309450202
security fixes and doc updates
Richard Jones <richard@users.sourceforge.net>
parents:
2075
diff
changeset
|
657 # add default Edit and View permissions |
|
2a4309450202
security fixes and doc updates
Richard Jones <richard@users.sourceforge.net>
parents:
2075
diff
changeset
|
658 self.security.addPermission(name="Edit", klass=cn, |
|
2a4309450202
security fixes and doc updates
Richard Jones <richard@users.sourceforge.net>
parents:
2075
diff
changeset
|
659 description="User is allowed to edit "+cn) |
|
2a4309450202
security fixes and doc updates
Richard Jones <richard@users.sourceforge.net>
parents:
2075
diff
changeset
|
660 self.security.addPermission(name="View", klass=cn, |
|
2a4309450202
security fixes and doc updates
Richard Jones <richard@users.sourceforge.net>
parents:
2075
diff
changeset
|
661 description="User is allowed to access "+cn) |
|
2a4309450202
security fixes and doc updates
Richard Jones <richard@users.sourceforge.net>
parents:
2075
diff
changeset
|
662 |
| 1165 | 663 def getclasses(self): |
| 664 ''' Return a list of the names of all existing classes. | |
| 665 ''' | |
| 666 if __debug__: | |
| 667 print >>hyperdb.DEBUG, 'getclasses', (self,) | |
| 668 l = self.classes.keys() | |
| 669 l.sort() | |
| 670 return l | |
| 671 | |
| 672 def getclass(self, classname): | |
| 673 '''Get the Class object representing a particular class. | |
| 674 | |
| 675 If 'classname' is not a valid class name, a KeyError is raised. | |
| 676 ''' | |
| 677 if __debug__: | |
| 678 print >>hyperdb.DEBUG, 'getclass', (self, classname) | |
| 679 try: | |
| 680 return self.classes[classname] | |
| 681 except KeyError: | |
| 682 raise KeyError, 'There is no class called "%s"'%classname | |
| 683 | |
| 684 def clear(self): | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
685 '''Delete all database contents. |
| 1165 | 686 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
687 Note: I don't commit here, which is different behaviour to the |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
688 "nuke from orbit" behaviour in the dbs. |
| 1165 | 689 ''' |
| 690 if __debug__: | |
| 691 print >>hyperdb.DEBUG, 'clear', (self,) | |
| 692 for cn in self.classes.keys(): | |
| 693 sql = 'delete from _%s'%cn | |
| 694 if __debug__: | |
| 695 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
|
696 self.cursor.execute(sql) |
|
3305
e074c641cb5e
fix RDBMS clear() so it resets all class itemid counters
Richard Jones <richard@users.sourceforge.net>
parents:
3055
diff
changeset
|
697 self.setid(cn, 1) |
| 1165 | 698 |
| 699 # | |
| 700 # Nodes | |
| 701 # | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
702 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
703 hyperdb_to_sql_value = { |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
704 hyperdb.String : str, |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
705 # fractional seconds by default |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
706 hyperdb.Date : lambda x: x.formal(sep=' ', sec='%.3f'), |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
707 hyperdb.Link : int, |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
708 hyperdb.Interval : str, |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
709 hyperdb.Password : str, |
|
2166
cd42c3c7173a
MySQL and Postgresql use BOOL/BOOLEAN for Boolean types
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
710 hyperdb.Boolean : lambda x: x and 'TRUE' or 'FALSE', |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
711 hyperdb.Number : lambda x: x, |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
712 hyperdb.Multilink : lambda x: x, # used in journal marshalling |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
713 } |
| 1165 | 714 def addnode(self, classname, nodeid, node): |
| 715 ''' Add the specified node to its class's db. | |
| 716 ''' | |
| 717 if __debug__: | |
| 718 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
|
719 |
|
96cd422532ef
bye bye gadfly - you served your purpose well [SF#701127]
Richard Jones <richard@users.sourceforge.net>
parents:
1523
diff
changeset
|
720 # determine the column definitions and multilink tables |
| 1165 | 721 cl = self.classes[classname] |
| 722 cols, mls = self.determine_columns(cl.properties.items()) | |
| 723 | |
|
1189
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
724 # we'll be supplied these props if we're doing an import |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
725 values = node.copy() |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
726 if not values.has_key('creator'): |
|
1189
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
727 # add in the "calculated" properties (dupe so we don't affect |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
728 # calling code's node assumptions) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
729 values['creation'] = values['activity'] = date.Date() |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
730 values['actor'] = values['creator'] = self.getuid() |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
731 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
732 cl = self.classes[classname] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
733 props = cl.getprops(protected=1) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
734 del props['id'] |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
735 |
| 1165 | 736 # default the non-multilink columns |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
737 for col, prop in props.items(): |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
738 if not values.has_key(col): |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
739 if isinstance(prop, Multilink): |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
740 values[col] = [] |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
741 else: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
742 values[col] = None |
| 1165 | 743 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
744 # 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
|
745 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
746 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
747 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
748 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
749 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
750 # figure the values to insert |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
751 vals = [] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
752 for col,dt in cols: |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
753 # this is somewhat dodgy.... |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
754 if col.endswith('_int__'): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
755 # XXX eugh, this test suxxors |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
756 value = values[col[2:-6]] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
757 # this is an Interval special "int" column |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
758 if value is not None: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
759 vals.append(value.as_seconds()) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
760 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
761 vals.append(value) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
762 continue |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
763 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
764 prop = props[col[1:]] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
765 value = values[col[1:]] |
|
2482
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
766 if value is not None: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
767 value = self.hyperdb_to_sql_value[prop.__class__](value) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
768 vals.append(value) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
769 vals.append(nodeid) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
770 vals = tuple(vals) |
| 1165 | 771 |
| 772 # make sure the ordering is correct for column name -> column value | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
773 s = ','.join([self.arg for x in cols]) + ',%s'%self.arg |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
774 cols = ','.join([col for col,dt in cols]) + ',id' |
| 1165 | 775 |
| 776 # perform the inserts | |
| 777 sql = 'insert into _%s (%s) values (%s)'%(classname, cols, s) | |
| 778 if __debug__: | |
| 779 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
|
780 self.cursor.execute(sql, vals) |
| 1165 | 781 |
| 782 # insert the multilink rows | |
| 783 for col in mls: | |
| 784 t = '%s_%s'%(classname, col) | |
| 785 for entry in node[col]: | |
| 786 sql = 'insert into %s (linkid, nodeid) values (%s,%s)'%(t, | |
| 787 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
|
788 self.sql(sql, (entry, nodeid)) |
| 1165 | 789 |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
790 def setnode(self, classname, nodeid, values, multilink_changes={}): |
| 1165 | 791 ''' Change the specified node. |
| 792 ''' | |
| 793 if __debug__: | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
794 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
|
795 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
796 # 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
|
797 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
798 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
799 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
800 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
801 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
802 # 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
|
803 values = values.copy() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
804 values['activity'] = date.Date() |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
805 values['actor'] = self.getuid() |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
806 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
807 cl = self.classes[classname] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
808 props = cl.getprops() |
| 1165 | 809 |
| 810 cols = [] | |
| 811 mls = [] | |
| 812 # 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
|
813 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
|
814 prop = props[col] |
| 1165 | 815 if isinstance(prop, Multilink): |
| 816 mls.append(col) | |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
817 elif isinstance(prop, Interval): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
818 # Intervals store the seconds value too |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
819 cols.append(col) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
820 # extra leading '_' added by code below |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
821 cols.append('_' +col + '_int__') |
| 1165 | 822 else: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
823 cols.append(col) |
| 1165 | 824 cols.sort() |
| 825 | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
826 # figure the values to insert |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
827 vals = [] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
828 for col in cols: |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
829 if col.endswith('_int__'): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
830 # XXX eugh, this test suxxors |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
831 # Intervals store the seconds value too |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
832 col = col[1:-6] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
833 prop = props[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
834 value = values[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
835 if value is None: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
836 vals.append(None) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
837 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
838 vals.append(value.as_seconds()) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
839 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
840 prop = props[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
841 value = values[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
842 if value is None: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
843 e = None |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
844 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
845 e = self.hyperdb_to_sql_value[prop.__class__](value) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
846 vals.append(e) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
847 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
848 vals.append(int(nodeid)) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
849 vals = tuple(vals) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
850 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
851 # 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
|
852 if cols: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
853 # make sure the ordering is correct for column name -> column value |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
854 s = ','.join(['_%s=%s'%(x, self.arg) for x in cols]) |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
855 cols = ','.join(cols) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
856 |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
857 # perform the update |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
858 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
|
859 if __debug__: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
860 print >>hyperdb.DEBUG, 'setnode', (self, sql, vals) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
861 self.cursor.execute(sql, vals) |
| 1165 | 862 |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
863 # we're probably coming from an import, not a change |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
864 if not multilink_changes: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
865 for name in mls: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
866 prop = props[name] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
867 value = values[name] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
868 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
869 t = '%s_%s'%(classname, name) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
870 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
871 # clear out previous values for this node |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
872 # XXX numeric ids |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
873 self.sql('delete from %s where nodeid=%s'%(t, self.arg), |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
874 (nodeid,)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
875 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
876 # insert the values for this node |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
877 for entry in values[name]: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
878 sql = 'insert into %s (linkid, nodeid) values (%s,%s)'%(t, |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
879 self.arg, self.arg) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
880 # XXX numeric ids |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
881 self.sql(sql, (entry, nodeid)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
882 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
883 # we have multilink changes to apply |
| 1165 | 884 for col, (add, remove) in multilink_changes.items(): |
| 885 tn = '%s_%s'%(classname, col) | |
| 886 if add: | |
| 887 sql = 'insert into %s (nodeid, linkid) values (%s,%s)'%(tn, | |
| 888 self.arg, self.arg) | |
| 889 for addid in add: | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
890 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
891 self.sql(sql, (int(nodeid), int(addid))) |
| 1165 | 892 if remove: |
| 893 sql = 'delete from %s where nodeid=%s and linkid=%s'%(tn, | |
| 894 self.arg, self.arg) | |
| 895 for removeid in remove: | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
896 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
897 self.sql(sql, (int(nodeid), int(removeid))) |
| 1165 | 898 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
899 sql_to_hyperdb_value = { |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
900 hyperdb.String : str, |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
901 hyperdb.Date : lambda x:date.Date(str(x).replace(' ', '.')), |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
902 # hyperdb.Link : int, # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
903 hyperdb.Link : str, |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
904 hyperdb.Interval : date.Interval, |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
905 hyperdb.Password : lambda x: password.Password(encrypted=x), |
|
2482
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
906 hyperdb.Boolean : _bool_cvt, |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
907 hyperdb.Number : _num_cvt, |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
908 hyperdb.Multilink : lambda x: x, # used in journal marshalling |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
909 } |
| 1165 | 910 def getnode(self, classname, nodeid): |
| 911 ''' Get a node from the database. | |
| 912 ''' | |
| 913 if __debug__: | |
| 914 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
|
915 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
916 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
917 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
918 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
919 # 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
|
920 self.cache_lru.remove(key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
921 self.cache_lru.insert(0, key) |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
922 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
923 self.stats['cache_hits'] += 1 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
924 # return the cached information |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
925 return self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
926 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
927 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
928 self.stats['cache_misses'] += 1 |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
929 start_t = time.time() |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
930 |
| 1165 | 931 # figure the columns we're fetching |
| 932 cl = self.classes[classname] | |
| 933 cols, mls = self.determine_columns(cl.properties.items()) | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
934 scols = ','.join([col for col,dt in cols]) |
| 1165 | 935 |
| 936 # perform the basic property fetch | |
| 937 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
|
938 self.sql(sql, (nodeid,)) |
| 1165 | 939 |
|
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
|
940 values = self.sql_fetchone() |
| 1165 | 941 if values is None: |
| 942 raise IndexError, 'no such %s node %s'%(classname, nodeid) | |
| 943 | |
| 944 # make up the node | |
| 945 node = {} | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
946 props = cl.getprops(protected=1) |
| 1165 | 947 for col in range(len(cols)): |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
948 name = cols[col][0][1:] |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
949 if name.endswith('_int__'): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
950 # XXX eugh, this test suxxors |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
951 # ignore the special Interval-as-seconds column |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
952 continue |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
953 value = values[col] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
954 if value is not None: |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
955 value = self.sql_to_hyperdb_value[props[name].__class__](value) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
956 node[name] = value |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
957 |
| 1165 | 958 |
| 959 # now the multilinks | |
| 960 for col in mls: | |
| 961 # get the link ids | |
| 962 sql = 'select linkid from %s_%s where nodeid=%s'%(classname, col, | |
| 963 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
|
964 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 965 # extract the first column from the result |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
966 # XXX numeric ids |
|
2888
95a8b49090de
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2762
diff
changeset
|
967 items = [int(x[0]) for x in self.cursor.fetchall()] |
|
95a8b49090de
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2762
diff
changeset
|
968 items.sort () |
|
95a8b49090de
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2762
diff
changeset
|
969 node[col] = [str(x) for x in items] |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
970 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
971 # save off in the cache |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
972 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
973 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
|
974 # update the LRU |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
975 self.cache_lru.insert(0, key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
976 if len(self.cache_lru) > ROW_CACHE_SIZE: |
|
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
977 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
|
978 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
979 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
980 self.stats['get_items'] += (time.time() - start_t) |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
981 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
982 return node |
| 1165 | 983 |
| 984 def destroynode(self, classname, nodeid): | |
| 985 '''Remove a node from the database. Called exclusively by the | |
| 986 destroy() method on Class. | |
| 987 ''' | |
| 988 if __debug__: | |
| 989 print >>hyperdb.DEBUG, 'destroynode', (self, classname, nodeid) | |
| 990 | |
| 991 # make sure the node exists | |
| 992 if not self.hasnode(classname, nodeid): | |
| 993 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 994 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
995 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
996 if self.cache.has_key((classname, nodeid)): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
997 del self.cache[(classname, nodeid)] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
998 |
| 1165 | 999 # see if there's any obvious commit actions that we should get rid of |
| 1000 for entry in self.transactions[:]: | |
| 1001 if entry[1][:2] == (classname, nodeid): | |
| 1002 self.transactions.remove(entry) | |
| 1003 | |
| 1004 # now do the SQL | |
| 1005 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
|
1006 self.sql(sql, (nodeid,)) |
| 1165 | 1007 |
| 1008 # remove from multilnks | |
| 1009 cl = self.getclass(classname) | |
| 1010 x, mls = self.determine_columns(cl.properties.items()) | |
| 1011 for col in mls: | |
| 1012 # get the link ids | |
| 1013 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
|
1014 self.sql(sql, (nodeid,)) |
| 1165 | 1015 |
| 1016 # remove journal entries | |
| 1017 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
|
1018 self.sql(sql, (nodeid,)) |
| 1165 | 1019 |
| 1020 def hasnode(self, classname, nodeid): | |
| 1021 ''' Determine if the database has a given node. | |
| 1022 ''' | |
| 1023 sql = 'select count(*) from _%s where id=%s'%(classname, self.arg) | |
| 1024 if __debug__: | |
| 1025 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
|
1026 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
|
1027 return int(self.cursor.fetchone()[0]) |
| 1165 | 1028 |
| 1029 def countnodes(self, classname): | |
| 1030 ''' Count the number of nodes that exist for a particular Class. | |
| 1031 ''' | |
| 1032 sql = 'select count(*) from _%s'%classname | |
| 1033 if __debug__: | |
| 1034 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
|
1035 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
|
1036 return self.cursor.fetchone()[0] |
| 1165 | 1037 |
| 1038 def addjournal(self, classname, nodeid, action, params, creator=None, | |
| 1039 creation=None): | |
| 1040 ''' Journal the Action | |
| 1041 'action' may be: | |
| 1042 | |
| 1043 'create' or 'set' -- 'params' is a dictionary of property values | |
| 1044 'link' or 'unlink' -- 'params' is (classname, nodeid, propname) | |
| 1045 'retire' -- 'params' is None | |
| 1046 ''' | |
| 1047 # handle supply of the special journalling parameters (usually | |
| 1048 # supplied on importing an existing database) | |
| 1049 if creator: | |
| 1050 journaltag = creator | |
| 1051 else: | |
|
1800
a3b1b1dcf639
Use getuid(), not figure_curuserid()
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1794
diff
changeset
|
1052 journaltag = self.getuid() |
| 1165 | 1053 if creation: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1054 journaldate = creation |
| 1165 | 1055 else: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1056 journaldate = date.Date() |
| 1165 | 1057 |
| 1058 # create the journal entry | |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1059 cols = 'nodeid,date,tag,action,params' |
| 1165 | 1060 |
| 1061 if __debug__: | |
| 1062 print >>hyperdb.DEBUG, 'addjournal', (nodeid, journaldate, | |
| 1063 journaltag, action, params) | |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1064 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1065 # make the journalled data marshallable |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1066 if isinstance(params, type({})): |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1067 self._journal_marshal(params, classname) |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1068 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1069 params = repr(params) |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1070 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1071 dc = self.hyperdb_to_sql_value[hyperdb.Date] |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1072 journaldate = dc(journaldate) |
| 1165 | 1073 |
|
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
|
1074 self.save_journal(classname, cols, nodeid, journaldate, |
| 1165 | 1075 journaltag, action, params) |
| 1076 | |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1077 def setjournal(self, classname, nodeid, journal): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1078 '''Set the journal to the "journal" list.''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1079 # clear out any existing entries |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1080 self.sql('delete from %s__journal where nodeid=%s'%(classname, |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1081 self.arg), (nodeid,)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1082 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1083 # create the journal entry |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1084 cols = 'nodeid,date,tag,action,params' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1085 |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1086 dc = self.hyperdb_to_sql_value[hyperdb.Date] |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1087 for nodeid, journaldate, journaltag, action, params in journal: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1088 if __debug__: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1089 print >>hyperdb.DEBUG, 'setjournal', (nodeid, journaldate, |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1090 journaltag, action, params) |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1091 |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1092 # make the journalled data marshallable |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1093 if isinstance(params, type({})): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1094 self._journal_marshal(params, classname) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1095 params = repr(params) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1096 |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1097 self.save_journal(classname, cols, nodeid, dc(journaldate), |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1098 journaltag, action, params) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1099 |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1100 def _journal_marshal(self, params, classname): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1101 '''Convert the journal params values into safely repr'able and |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1102 eval'able values.''' |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1103 properties = self.getclass(classname).getprops() |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1104 for param, value in params.items(): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1105 if not value: |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1106 continue |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1107 property = properties[param] |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1108 cvt = self.hyperdb_to_sql_value[property.__class__] |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1109 if isinstance(property, Password): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1110 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1111 elif isinstance(property, Date): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1112 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1113 elif isinstance(property, Interval): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1114 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1115 elif isinstance(property, Boolean): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1116 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1117 |
| 1165 | 1118 def getjournal(self, classname, nodeid): |
| 1119 ''' get the journal for id | |
| 1120 ''' | |
| 1121 # make sure the node exists | |
| 1122 if not self.hasnode(classname, nodeid): | |
| 1123 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 1124 | |
| 1125 cols = ','.join('nodeid date tag action params'.split()) | |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1126 journal = self.load_journal(classname, cols, nodeid) |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1127 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1128 # now unmarshal the data |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1129 dc = self.sql_to_hyperdb_value[hyperdb.Date] |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1130 res = [] |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1131 properties = self.getclass(classname).getprops() |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1132 for nodeid, date_stamp, user, action, params in journal: |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1133 params = eval(params) |
|
2248
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1134 if isinstance(params, type({})): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1135 for param, value in params.items(): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1136 if not value: |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1137 continue |
|
2732
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1138 property = properties.get(param, None) |
|
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1139 if property is None: |
|
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1140 # deleted property |
|
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1141 continue |
|
2248
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1142 cvt = self.sql_to_hyperdb_value[property.__class__] |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1143 if isinstance(property, Password): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1144 params[param] = cvt(value) |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1145 elif isinstance(property, Date): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1146 params[param] = cvt(value) |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1147 elif isinstance(property, Interval): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1148 params[param] = cvt(value) |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1149 elif isinstance(property, Boolean): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1150 params[param] = cvt(value) |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1151 # XXX numeric ids |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1152 res.append((str(nodeid), dc(date_stamp), user, action, params)) |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1153 return res |
| 1165 | 1154 |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1155 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
|
1156 journaltag, action, params): |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1157 ''' 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
|
1158 ''' |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1159 entry = (nodeid, journaldate, journaltag, action, params) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1160 |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1161 # do the insert |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1162 a = self.arg |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1163 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%( |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1164 classname, cols, a, a, a, a, a) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1165 if __debug__: |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
1166 print >>hyperdb.DEBUG, 'save_journal', (self, sql, entry) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1167 self.cursor.execute(sql, entry) |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1168 |
|
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
|
1169 def load_journal(self, classname, cols, nodeid): |
| 1165 | 1170 ''' Load the journal from the database |
| 1171 ''' | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1172 # now get the journal entries |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1173 sql = 'select %s from %s__journal where nodeid=%s order by date'%( |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1174 cols, classname, self.arg) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1175 if __debug__: |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1176 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
|
1177 self.cursor.execute(sql, (nodeid,)) |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1178 return self.cursor.fetchall() |
| 1165 | 1179 |
| 1180 def pack(self, pack_before): | |
| 1181 ''' Delete all journal entries except "create" before 'pack_before'. | |
| 1182 ''' | |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
1183 date_stamp = self.hyperdb_to_sql_value[Date](pack_before) |
| 1165 | 1184 |
| 1185 # do the delete | |
| 1186 for classname in self.classes.keys(): | |
| 1187 sql = "delete from %s__journal where date<%s and "\ | |
| 1188 "action<>'create'"%(classname, self.arg) | |
| 1189 if __debug__: | |
| 1190 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
|
1191 self.cursor.execute(sql, (date_stamp,)) |
| 1165 | 1192 |
| 1193 def sql_commit(self): | |
| 1194 ''' Actually commit to the database. | |
| 1195 ''' | |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
1196 if __debug__: |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
1197 print >>hyperdb.DEBUG, '+++ commit database connection +++' |
| 1165 | 1198 self.conn.commit() |
| 1199 | |
| 1200 def commit(self): | |
| 1201 ''' Commit the current transactions. | |
| 1202 | |
| 1203 Save all data changed since the database was opened or since the | |
| 1204 last commit() or rollback(). | |
| 1205 ''' | |
| 1206 if __debug__: | |
| 1207 print >>hyperdb.DEBUG, 'commit', (self,) | |
| 1208 | |
| 1209 # commit the database | |
| 1210 self.sql_commit() | |
| 1211 | |
| 1212 # now, do all the other transaction stuff | |
| 1213 for method, args in self.transactions: | |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1214 method(*args) |
| 1165 | 1215 |
| 1216 # save the indexer state | |
| 1217 self.indexer.save_index() | |
| 1218 | |
| 1219 # clear out the transactions | |
| 1220 self.transactions = [] | |
| 1221 | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1222 def sql_rollback(self): |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1223 self.conn.rollback() |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1224 |
| 1165 | 1225 def rollback(self): |
| 1226 ''' Reverse all actions from the current transaction. | |
| 1227 | |
| 1228 Undo all the changes made since the database was opened or the last | |
| 1229 commit() or rollback() was performed. | |
| 1230 ''' | |
| 1231 if __debug__: | |
| 1232 print >>hyperdb.DEBUG, 'rollback', (self,) | |
| 1233 | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1234 self.sql_rollback() |
| 1165 | 1235 |
| 1236 # roll back "other" transaction stuff | |
| 1237 for method, args in self.transactions: | |
| 1238 # delete temporary files | |
| 1239 if method == self.doStoreFile: | |
| 1240 self.rollbackStoreFile(*args) | |
| 1241 self.transactions = [] | |
| 1242 | |
|
1492
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
1243 # clear the cache |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
1244 self.clearCache() |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
1245 |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1246 def sql_close(self): |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
1247 if __debug__: |
|
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
1248 print >>hyperdb.DEBUG, '+++ close database connection +++' |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1249 self.conn.close() |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1250 |
| 1165 | 1251 def close(self): |
| 1252 ''' Close off the connection. | |
| 1253 ''' | |
|
2093
3f6024ab2c7a
That's the last of the RDBMS migration steps done! Yay!
Richard Jones <richard@users.sourceforge.net>
parents:
2089
diff
changeset
|
1254 self.indexer.close() |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1255 self.sql_close() |
| 1165 | 1256 |
| 1257 # | |
| 1258 # The base Class class | |
| 1259 # | |
| 1260 class Class(hyperdb.Class): | |
| 1261 ''' The handle to a particular class of nodes in a hyperdatabase. | |
| 1262 | |
| 1263 All methods except __repr__ and getnode must be implemented by a | |
| 1264 concrete backend Class. | |
| 1265 ''' | |
| 1266 | |
| 1267 def __init__(self, db, classname, **properties): | |
| 1268 '''Create a new class with a given name and property specification. | |
| 1269 | |
| 1270 'classname' must not collide with the name of an existing class, | |
| 1271 or a ValueError is raised. The keyword arguments in 'properties' | |
| 1272 must map names to property objects, or a TypeError is raised. | |
| 1273 ''' | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1274 for name in 'creation activity creator actor'.split(): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1275 if properties.has_key(name): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1276 raise ValueError, '"creation", "activity", "creator" and '\ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1277 '"actor" are reserved' |
| 1165 | 1278 |
| 1279 self.classname = classname | |
| 1280 self.properties = properties | |
| 1281 self.db = weakref.proxy(db) # use a weak ref to avoid circularity | |
| 1282 self.key = '' | |
| 1283 | |
| 1284 # should we journal changes (default yes) | |
| 1285 self.do_journal = 1 | |
| 1286 | |
| 1287 # do the db-related init stuff | |
| 1288 db.addclass(self) | |
| 1289 | |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1290 self.auditors = {'create': [], 'set': [], 'retire': [], 'restore': []} |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1291 self.reactors = {'create': [], 'set': [], 'retire': [], 'restore': []} |
| 1165 | 1292 |
| 1293 def schema(self): | |
| 1294 ''' A dumpable version of the schema that we can store in the | |
| 1295 database | |
| 1296 ''' | |
| 1297 return (self.key, [(x, repr(y)) for x,y in self.properties.items()]) | |
| 1298 | |
| 1299 def enableJournalling(self): | |
| 1300 '''Turn journalling on for this class | |
| 1301 ''' | |
| 1302 self.do_journal = 1 | |
| 1303 | |
| 1304 def disableJournalling(self): | |
| 1305 '''Turn journalling off for this class | |
| 1306 ''' | |
| 1307 self.do_journal = 0 | |
| 1308 | |
| 1309 # Editing nodes: | |
| 1310 def create(self, **propvalues): | |
| 1311 ''' Create a new node of this class and return its id. | |
| 1312 | |
| 1313 The keyword arguments in 'propvalues' map property names to values. | |
| 1314 | |
| 1315 The values of arguments must be acceptable for the types of their | |
| 1316 corresponding properties or a TypeError is raised. | |
| 1317 | |
| 1318 If this class has a key property, it must be present and its value | |
| 1319 must not collide with other key strings or a ValueError is raised. | |
| 1320 | |
| 1321 Any other properties on this class that are missing from the | |
| 1322 'propvalues' dictionary are set to None. | |
| 1323 | |
| 1324 If an id in a link or multilink property does not refer to a valid | |
| 1325 node, an IndexError is raised. | |
| 1326 ''' | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1327 self.fireAuditors('create', None, propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1328 newid = self.create_inner(**propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1329 self.fireReactors('create', newid, None) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1330 return newid |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1331 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1332 def create_inner(self, **propvalues): |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1333 ''' 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
|
1334 ''' |
| 1165 | 1335 if propvalues.has_key('id'): |
| 1336 raise KeyError, '"id" is reserved' | |
| 1337 | |
| 1338 if self.db.journaltag is None: | |
| 1339 raise DatabaseError, 'Database open read-only' | |
| 1340 | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1341 if propvalues.has_key('creator') or propvalues.has_key('actor') or \ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1342 propvalues.has_key('creation') or propvalues.has_key('activity'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1343 raise KeyError, '"creator", "actor", "creation" and '\ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1344 '"activity" are reserved' |
| 1165 | 1345 |
| 1346 # new node's id | |
| 1347 newid = self.db.newid(self.classname) | |
| 1348 | |
| 1349 # validate propvalues | |
| 1350 num_re = re.compile('^\d+$') | |
| 1351 for key, value in propvalues.items(): | |
| 1352 if key == self.key: | |
| 1353 try: | |
| 1354 self.lookup(value) | |
| 1355 except KeyError: | |
| 1356 pass | |
| 1357 else: | |
| 1358 raise ValueError, 'node with key "%s" exists'%value | |
| 1359 | |
| 1360 # try to handle this property | |
| 1361 try: | |
| 1362 prop = self.properties[key] | |
| 1363 except KeyError: | |
| 1364 raise KeyError, '"%s" has no property "%s"'%(self.classname, | |
| 1365 key) | |
| 1366 | |
| 1367 if value is not None and isinstance(prop, Link): | |
| 1368 if type(value) != type(''): | |
| 1369 raise ValueError, 'link value must be String' | |
| 1370 link_class = self.properties[key].classname | |
| 1371 # if it isn't a number, it's a key | |
| 1372 if not num_re.match(value): | |
| 1373 try: | |
| 1374 value = self.db.classes[link_class].lookup(value) | |
| 1375 except (TypeError, KeyError): | |
| 1376 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1377 key, value, link_class) | |
| 1378 elif not self.db.getclass(link_class).hasnode(value): | |
| 1379 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 1380 | |
| 1381 # save off the value | |
| 1382 propvalues[key] = value | |
| 1383 | |
| 1384 # register the link with the newly linked node | |
| 1385 if self.do_journal and self.properties[key].do_journal: | |
| 1386 self.db.addjournal(link_class, value, 'link', | |
| 1387 (self.classname, newid, key)) | |
| 1388 | |
| 1389 elif isinstance(prop, Multilink): | |
| 1390 if type(value) != type([]): | |
| 1391 raise TypeError, 'new property "%s" not a list of ids'%key | |
| 1392 | |
| 1393 # clean up and validate the list of links | |
| 1394 link_class = self.properties[key].classname | |
| 1395 l = [] | |
| 1396 for entry in value: | |
| 1397 if type(entry) != type(''): | |
| 1398 raise ValueError, '"%s" multilink value (%r) '\ | |
| 1399 'must contain Strings'%(key, value) | |
| 1400 # if it isn't a number, it's a key | |
| 1401 if not num_re.match(entry): | |
| 1402 try: | |
| 1403 entry = self.db.classes[link_class].lookup(entry) | |
| 1404 except (TypeError, KeyError): | |
| 1405 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1406 key, entry, self.properties[key].classname) | |
| 1407 l.append(entry) | |
| 1408 value = l | |
| 1409 propvalues[key] = value | |
| 1410 | |
| 1411 # handle additions | |
| 1412 for nodeid in value: | |
| 1413 if not self.db.getclass(link_class).hasnode(nodeid): | |
| 1414 raise IndexError, '%s has no node %s'%(link_class, | |
| 1415 nodeid) | |
| 1416 # register the link with the newly linked node | |
| 1417 if self.do_journal and self.properties[key].do_journal: | |
| 1418 self.db.addjournal(link_class, nodeid, 'link', | |
| 1419 (self.classname, newid, key)) | |
| 1420 | |
| 1421 elif isinstance(prop, String): | |
|
1383
f19dde90e473
applied unicode patch
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1365
diff
changeset
|
1422 if type(value) != type('') and type(value) != type(u''): |
| 1165 | 1423 raise TypeError, 'new property "%s" not a string'%key |
|
2893
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
1424 if prop.indexme: |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
1425 self.db.indexer.add_text((self.classname, newid, key), |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
1426 value) |
| 1165 | 1427 |
| 1428 elif isinstance(prop, Password): | |
| 1429 if not isinstance(value, password.Password): | |
| 1430 raise TypeError, 'new property "%s" not a Password'%key | |
| 1431 | |
| 1432 elif isinstance(prop, Date): | |
| 1433 if value is not None and not isinstance(value, date.Date): | |
| 1434 raise TypeError, 'new property "%s" not a Date'%key | |
| 1435 | |
| 1436 elif isinstance(prop, Interval): | |
| 1437 if value is not None and not isinstance(value, date.Interval): | |
| 1438 raise TypeError, 'new property "%s" not an Interval'%key | |
| 1439 | |
| 1440 elif value is not None and isinstance(prop, Number): | |
| 1441 try: | |
| 1442 float(value) | |
| 1443 except ValueError: | |
| 1444 raise TypeError, 'new property "%s" not numeric'%key | |
| 1445 | |
| 1446 elif value is not None and isinstance(prop, Boolean): | |
| 1447 try: | |
| 1448 int(value) | |
| 1449 except ValueError: | |
| 1450 raise TypeError, 'new property "%s" not boolean'%key | |
| 1451 | |
| 1452 # make sure there's data where there needs to be | |
| 1453 for key, prop in self.properties.items(): | |
| 1454 if propvalues.has_key(key): | |
| 1455 continue | |
| 1456 if key == self.key: | |
| 1457 raise ValueError, 'key property "%s" is required'%key | |
| 1458 if isinstance(prop, Multilink): | |
| 1459 propvalues[key] = [] | |
| 1460 else: | |
| 1461 propvalues[key] = None | |
| 1462 | |
| 1463 # done | |
| 1464 self.db.addnode(self.classname, newid, propvalues) | |
| 1465 if self.do_journal: | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1466 self.db.addjournal(self.classname, newid, 'create', {}) |
| 1165 | 1467 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1468 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1469 return str(newid) |
| 1165 | 1470 |
| 1471 _marker = [] | |
| 1472 def get(self, nodeid, propname, default=_marker, cache=1): | |
| 1473 '''Get the value of a property on an existing node of this class. | |
| 1474 | |
| 1475 'nodeid' must be the id of an existing node of this class or an | |
| 1476 IndexError is raised. 'propname' must be the name of a property | |
| 1477 of this class or a KeyError is raised. | |
| 1478 | |
|
1780
d2801a2b0a77
Initial implementation (half-baked) at new Tracker instance.
Richard Jones <richard@users.sourceforge.net>
parents:
1751
diff
changeset
|
1479 'cache' exists for backwards compatibility, and is not used. |
| 1165 | 1480 ''' |
| 1481 if propname == 'id': | |
| 1482 return nodeid | |
| 1483 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1484 # 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
|
1485 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
|
1486 |
| 1165 | 1487 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
|
1488 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
|
1489 return d['creation'] |
| 1165 | 1490 else: |
| 1491 return date.Date() | |
| 1492 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
|
1493 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
|
1494 return d['activity'] |
| 1165 | 1495 else: |
| 1496 return date.Date() | |
| 1497 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
|
1498 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
|
1499 return d['creator'] |
| 1165 | 1500 else: |
|
1800
a3b1b1dcf639
Use getuid(), not figure_curuserid()
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1794
diff
changeset
|
1501 return self.db.getuid() |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1502 if propname == 'actor': |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1503 if d.has_key('actor'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1504 return d['actor'] |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1505 else: |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1506 return self.db.getuid() |
| 1165 | 1507 |
| 1508 # get the property (raises KeyErorr if invalid) | |
| 1509 prop = self.properties[propname] | |
| 1510 | |
| 1511 if not d.has_key(propname): | |
| 1512 if default is self._marker: | |
| 1513 if isinstance(prop, Multilink): | |
| 1514 return [] | |
| 1515 else: | |
| 1516 return None | |
| 1517 else: | |
| 1518 return default | |
| 1519 | |
| 1520 # don't pass our list to other code | |
| 1521 if isinstance(prop, Multilink): | |
| 1522 return d[propname][:] | |
| 1523 | |
| 1524 return d[propname] | |
| 1525 | |
| 1526 def set(self, nodeid, **propvalues): | |
| 1527 '''Modify a property on an existing node of this class. | |
| 1528 | |
| 1529 'nodeid' must be the id of an existing node of this class or an | |
| 1530 IndexError is raised. | |
| 1531 | |
| 1532 Each key in 'propvalues' must be the name of a property of this | |
| 1533 class or a KeyError is raised. | |
| 1534 | |
| 1535 All values in 'propvalues' must be acceptable types for their | |
| 1536 corresponding properties or a TypeError is raised. | |
| 1537 | |
| 1538 If the value of the key property is set, it must not collide with | |
| 1539 other key strings or a ValueError is raised. | |
| 1540 | |
| 1541 If the value of a Link or Multilink property contains an invalid | |
| 1542 node id, a ValueError is raised. | |
| 1543 ''' | |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1544 self.fireAuditors('set', nodeid, propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1545 oldvalues = copy.deepcopy(self.db.getnode(self.classname, nodeid)) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1546 propvalues = self.set_inner(nodeid, **propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1547 self.fireReactors('set', nodeid, oldvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1548 return propvalues |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1549 |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1550 def set_inner(self, nodeid, **propvalues): |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1551 ''' Called by set, in-between the audit and react calls. |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1552 ''' |
| 1165 | 1553 if not propvalues: |
| 1554 return propvalues | |
| 1555 | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1556 if propvalues.has_key('creation') or propvalues.has_key('creator') or \ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1557 propvalues.has_key('actor') or propvalues.has_key('activity'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1558 raise KeyError, '"creation", "creator", "actor" and '\ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1559 '"activity" are reserved' |
| 1165 | 1560 |
| 1561 if propvalues.has_key('id'): | |
| 1562 raise KeyError, '"id" is reserved' | |
| 1563 | |
| 1564 if self.db.journaltag is None: | |
| 1565 raise DatabaseError, 'Database open read-only' | |
| 1566 | |
| 1567 node = self.db.getnode(self.classname, nodeid) | |
| 1568 if self.is_retired(nodeid): | |
| 1569 raise IndexError, 'Requested item is retired' | |
| 1570 num_re = re.compile('^\d+$') | |
| 1571 | |
| 1572 # if the journal value is to be different, store it in here | |
| 1573 journalvalues = {} | |
| 1574 | |
| 1575 # remember the add/remove stuff for multilinks, making it easier | |
| 1576 # for the Database layer to do its stuff | |
| 1577 multilink_changes = {} | |
| 1578 | |
| 1579 for propname, value in propvalues.items(): | |
| 1580 # check to make sure we're not duplicating an existing key | |
| 1581 if propname == self.key and node[propname] != value: | |
| 1582 try: | |
| 1583 self.lookup(value) | |
| 1584 except KeyError: | |
| 1585 pass | |
| 1586 else: | |
| 1587 raise ValueError, 'node with key "%s" exists'%value | |
| 1588 | |
| 1589 # this will raise the KeyError if the property isn't valid | |
| 1590 # ... we don't use getprops() here because we only care about | |
| 1591 # 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
|
1592 try: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1593 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
|
1594 except KeyError: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1595 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
|
1596 self.classname, propname) |
| 1165 | 1597 |
| 1598 # if the value's the same as the existing value, no sense in | |
| 1599 # doing anything | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1600 current = node.get(propname, None) |
|
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1601 if value == current: |
| 1165 | 1602 del propvalues[propname] |
| 1603 continue | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1604 journalvalues[propname] = current |
| 1165 | 1605 |
| 1606 # do stuff based on the prop type | |
| 1607 if isinstance(prop, Link): | |
| 1608 link_class = prop.classname | |
| 1609 # if it isn't a number, it's a key | |
| 1610 if value is not None and not isinstance(value, type('')): | |
| 1611 raise ValueError, 'property "%s" link value be a string'%( | |
| 1612 propname) | |
| 1613 if isinstance(value, type('')) and not num_re.match(value): | |
| 1614 try: | |
| 1615 value = self.db.classes[link_class].lookup(value) | |
| 1616 except (TypeError, KeyError): | |
| 1617 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1618 propname, value, prop.classname) | |
| 1619 | |
| 1620 if (value is not None and | |
| 1621 not self.db.getclass(link_class).hasnode(value)): | |
| 1622 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 1623 | |
| 1624 if self.do_journal and prop.do_journal: | |
| 1625 # register the unlink with the old linked node | |
| 1626 if node[propname] is not None: | |
| 1627 self.db.addjournal(link_class, node[propname], 'unlink', | |
| 1628 (self.classname, nodeid, propname)) | |
| 1629 | |
| 1630 # register the link with the newly linked node | |
| 1631 if value is not None: | |
| 1632 self.db.addjournal(link_class, value, 'link', | |
| 1633 (self.classname, nodeid, propname)) | |
| 1634 | |
| 1635 elif isinstance(prop, Multilink): | |
| 1636 if type(value) != type([]): | |
| 1637 raise TypeError, 'new property "%s" not a list of'\ | |
| 1638 ' ids'%propname | |
| 1639 link_class = self.properties[propname].classname | |
| 1640 l = [] | |
| 1641 for entry in value: | |
| 1642 # if it isn't a number, it's a key | |
| 1643 if type(entry) != type(''): | |
| 1644 raise ValueError, 'new property "%s" link value ' \ | |
| 1645 'must be a string'%propname | |
| 1646 if not num_re.match(entry): | |
| 1647 try: | |
| 1648 entry = self.db.classes[link_class].lookup(entry) | |
| 1649 except (TypeError, KeyError): | |
| 1650 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1651 propname, entry, | |
| 1652 self.properties[propname].classname) | |
| 1653 l.append(entry) | |
| 1654 value = l | |
| 1655 propvalues[propname] = value | |
| 1656 | |
| 1657 # figure the journal entry for this property | |
| 1658 add = [] | |
| 1659 remove = [] | |
| 1660 | |
| 1661 # handle removals | |
| 1662 if node.has_key(propname): | |
| 1663 l = node[propname] | |
| 1664 else: | |
| 1665 l = [] | |
| 1666 for id in l[:]: | |
| 1667 if id in value: | |
| 1668 continue | |
| 1669 # register the unlink with the old linked node | |
| 1670 if self.do_journal and self.properties[propname].do_journal: | |
| 1671 self.db.addjournal(link_class, id, 'unlink', | |
| 1672 (self.classname, nodeid, propname)) | |
| 1673 l.remove(id) | |
| 1674 remove.append(id) | |
| 1675 | |
| 1676 # handle additions | |
| 1677 for id in value: | |
| 1678 if not self.db.getclass(link_class).hasnode(id): | |
| 1679 raise IndexError, '%s has no node %s'%(link_class, id) | |
| 1680 if id in l: | |
| 1681 continue | |
| 1682 # register the link with the newly linked node | |
| 1683 if self.do_journal and self.properties[propname].do_journal: | |
| 1684 self.db.addjournal(link_class, id, 'link', | |
| 1685 (self.classname, nodeid, propname)) | |
| 1686 l.append(id) | |
| 1687 add.append(id) | |
| 1688 | |
| 1689 # figure the journal entry | |
| 1690 l = [] | |
| 1691 if add: | |
| 1692 l.append(('+', add)) | |
| 1693 if remove: | |
| 1694 l.append(('-', remove)) | |
| 1695 multilink_changes[propname] = (add, remove) | |
| 1696 if l: | |
| 1697 journalvalues[propname] = tuple(l) | |
| 1698 | |
| 1699 elif isinstance(prop, String): | |
|
1383
f19dde90e473
applied unicode patch
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1365
diff
changeset
|
1700 if value is not None and type(value) != type('') and type(value) != type(u''): |
| 1165 | 1701 raise TypeError, 'new property "%s" not a string'%propname |
|
2893
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
1702 if prop.indexme: |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
1703 self.db.indexer.add_text((self.classname, nodeid, propname), |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
1704 value) |
| 1165 | 1705 |
| 1706 elif isinstance(prop, Password): | |
| 1707 if not isinstance(value, password.Password): | |
| 1708 raise TypeError, 'new property "%s" not a Password'%propname | |
| 1709 propvalues[propname] = value | |
| 1710 | |
| 1711 elif value is not None and isinstance(prop, Date): | |
| 1712 if not isinstance(value, date.Date): | |
| 1713 raise TypeError, 'new property "%s" not a Date'% propname | |
| 1714 propvalues[propname] = value | |
| 1715 | |
| 1716 elif value is not None and isinstance(prop, Interval): | |
| 1717 if not isinstance(value, date.Interval): | |
| 1718 raise TypeError, 'new property "%s" not an '\ | |
| 1719 'Interval'%propname | |
| 1720 propvalues[propname] = value | |
| 1721 | |
| 1722 elif value is not None and isinstance(prop, Number): | |
| 1723 try: | |
| 1724 float(value) | |
| 1725 except ValueError: | |
| 1726 raise TypeError, 'new property "%s" not numeric'%propname | |
| 1727 | |
| 1728 elif value is not None and isinstance(prop, Boolean): | |
| 1729 try: | |
| 1730 int(value) | |
| 1731 except ValueError: | |
| 1732 raise TypeError, 'new property "%s" not boolean'%propname | |
| 1733 | |
| 1734 # nothing to do? | |
| 1735 if not propvalues: | |
| 1736 return propvalues | |
| 1737 | |
| 1738 # 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
|
1739 self.db.setnode(self.classname, nodeid, propvalues, multilink_changes) |
| 1165 | 1740 |
| 1741 if self.do_journal: | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1742 self.db.addjournal(self.classname, nodeid, 'set', journalvalues) |
| 1165 | 1743 |
| 1744 return propvalues | |
| 1745 | |
| 1746 def retire(self, nodeid): | |
| 1747 '''Retire a node. | |
| 1748 | |
| 1749 The properties on the node remain available from the get() method, | |
| 1750 and the node's id is never reused. | |
| 1751 | |
| 1752 Retired nodes are not returned by the find(), list(), or lookup() | |
| 1753 methods, and other nodes may reuse the values of their key properties. | |
| 1754 ''' | |
| 1755 if self.db.journaltag is None: | |
| 1756 raise DatabaseError, 'Database open read-only' | |
| 1757 | |
|
1345
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1758 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
|
1759 |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1760 # 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
|
1761 # conversion (hello, sqlite) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1762 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
|
1763 self.db.arg, self.db.arg) |
| 1165 | 1764 if __debug__: |
| 1765 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
|
1766 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
|
1767 if self.do_journal: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1768 self.db.addjournal(self.classname, nodeid, 'retired', None) |
| 1165 | 1769 |
|
1345
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1770 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
|
1771 |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1772 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
|
1773 '''Restore a retired node. |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1774 |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1775 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
|
1776 ''' |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1777 if self.db.journaltag is None: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1778 raise DatabaseError, 'Database open read-only' |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1779 |
|
1523
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1780 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
|
1781 # 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
|
1782 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
|
1783 try: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1784 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
|
1785 except KeyError: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1786 pass |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1787 else: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1788 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
|
1789 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
|
1790 |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1791 self.fireAuditors('restore', nodeid, None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1792 # 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
|
1793 # conversion (hello, sqlite) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1794 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
|
1795 self.db.arg, self.db.arg) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1796 if __debug__: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1797 print >>hyperdb.DEBUG, 'restore', (self, sql, nodeid) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1798 self.db.cursor.execute(sql, (0, nodeid)) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1799 if self.do_journal: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1800 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
|
1801 |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1802 self.fireReactors('restore', nodeid, None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1803 |
| 1165 | 1804 def is_retired(self, nodeid): |
| 1805 '''Return true if the node is rerired | |
| 1806 ''' | |
| 1807 sql = 'select __retired__ from _%s where id=%s'%(self.classname, | |
| 1808 self.db.arg) | |
| 1809 if __debug__: | |
| 1810 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
|
1811 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
|
1812 return int(self.db.sql_fetchone()[0]) |
| 1165 | 1813 |
| 1814 def destroy(self, nodeid): | |
| 1815 '''Destroy a node. | |
| 1816 | |
| 1817 WARNING: this method should never be used except in extremely rare | |
| 1818 situations where there could never be links to the node being | |
| 1819 deleted | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1820 |
| 1165 | 1821 WARNING: use retire() instead |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1822 |
| 1165 | 1823 WARNING: the properties of this node will not be available ever again |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1824 |
| 1165 | 1825 WARNING: really, use retire() instead |
| 1826 | |
| 1827 Well, I think that's enough warnings. This method exists mostly to | |
| 1828 support the session storage of the cgi interface. | |
| 1829 | |
| 1830 The node is completely removed from the hyperdb, including all journal | |
| 1831 entries. It will no longer be available, and will generally break code | |
| 1832 if there are any references to the node. | |
| 1833 ''' | |
| 1834 if self.db.journaltag is None: | |
| 1835 raise DatabaseError, 'Database open read-only' | |
| 1836 self.db.destroynode(self.classname, nodeid) | |
| 1837 | |
| 1838 def history(self, nodeid): | |
| 1839 '''Retrieve the journal of edits on a particular node. | |
| 1840 | |
| 1841 'nodeid' must be the id of an existing node of this class or an | |
| 1842 IndexError is raised. | |
| 1843 | |
| 1844 The returned list contains tuples of the form | |
| 1845 | |
|
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
|
1846 (nodeid, date, tag, action, params) |
| 1165 | 1847 |
| 1848 'date' is a Timestamp object specifying the time of the change and | |
| 1849 'tag' is the journaltag specified when the database was opened. | |
| 1850 ''' | |
| 1851 if not self.do_journal: | |
| 1852 raise ValueError, 'Journalling is disabled for this class' | |
| 1853 return self.db.getjournal(self.classname, nodeid) | |
| 1854 | |
| 1855 # Locating nodes: | |
| 1856 def hasnode(self, nodeid): | |
| 1857 '''Determine if the given nodeid actually exists | |
| 1858 ''' | |
| 1859 return self.db.hasnode(self.classname, nodeid) | |
| 1860 | |
| 1861 def setkey(self, propname): | |
| 1862 '''Select a String property of this class to be the key property. | |
| 1863 | |
| 1864 'propname' must be the name of a String property of this class or | |
| 1865 None, or a TypeError is raised. The values of the key property on | |
| 1866 all existing nodes must be unique or a ValueError is raised. | |
| 1867 ''' | |
| 1868 prop = self.getprops()[propname] | |
| 1869 if not isinstance(prop, String): | |
| 1870 raise TypeError, 'key properties must be String' | |
| 1871 self.key = propname | |
| 1872 | |
| 1873 def getkey(self): | |
| 1874 '''Return the name of the key property for this class or None.''' | |
| 1875 return self.key | |
| 1876 | |
| 1877 def labelprop(self, default_to_id=0): | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1878 '''Return the property name for a label for the given node. |
| 1165 | 1879 |
| 1880 This method attempts to generate a consistent label for the node. | |
| 1881 It tries the following in order: | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1882 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1883 1. key property |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1884 2. "name" property |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1885 3. "title" property |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1886 4. first property from the sorted property name list |
| 1165 | 1887 ''' |
| 1888 k = self.getkey() | |
| 1889 if k: | |
| 1890 return k | |
| 1891 props = self.getprops() | |
| 1892 if props.has_key('name'): | |
| 1893 return 'name' | |
| 1894 elif props.has_key('title'): | |
| 1895 return 'title' | |
| 1896 if default_to_id: | |
| 1897 return 'id' | |
| 1898 props = props.keys() | |
| 1899 props.sort() | |
| 1900 return props[0] | |
| 1901 | |
| 1902 def lookup(self, keyvalue): | |
| 1903 '''Locate a particular node by its key property and return its id. | |
| 1904 | |
| 1905 If this class has no key property, a TypeError is raised. If the | |
| 1906 'keyvalue' matches one of the values for the key property among | |
| 1907 the nodes in this class, the matching node's id is returned; | |
| 1908 otherwise a KeyError is raised. | |
| 1909 ''' | |
| 1910 if not self.key: | |
| 1911 raise TypeError, 'No key property set for class %s'%self.classname | |
| 1912 | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1913 # 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
|
1914 # sqlite) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1915 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
|
1916 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
|
1917 self.db.sql(sql, (keyvalue, 1)) |
| 1165 | 1918 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1919 # 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
|
1920 row = self.db.sql_fetchone() |
|
735adcbfc665
fix to SQL lookup() and retirement
Richard Jones <richard@users.sourceforge.net>
parents:
1195
diff
changeset
|
1921 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
|
1922 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
|
1923 keyvalue, self.classname) |
| 1165 | 1924 |
| 1925 # return the id | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1926 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1927 return str(row[0]) |
| 1165 | 1928 |
| 1929 def find(self, **propspec): | |
| 1930 '''Get the ids of nodes in this class which link to the given nodes. | |
| 1931 | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1932 '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
|
1933 propname={nodeid:1, } |
| 1165 | 1934 '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
|
1935 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
|
1936 Multilink property, or a TypeError is raised. |
| 1165 | 1937 |
| 1938 Any node in this class whose 'propname' property links to any of the | |
| 1939 nodeids will be returned. Used by the full text indexing, which knows | |
| 1940 that "foo" occurs in msg1, msg3 and file7, so we have hits on these | |
| 1941 issues: | |
| 1942 | |
| 1943 db.issue.find(messages={'1':1,'3':1}, files={'7':1}) | |
| 1944 ''' | |
| 1945 if __debug__: | |
| 1946 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
|
1947 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1948 # shortcut |
| 1165 | 1949 if not propspec: |
| 1950 return [] | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1951 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1952 # validate the args |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1953 props = self.getprops() |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1954 propspec = propspec.items() |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1955 for propname, nodeids in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1956 # check the prop is OK |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1957 prop = props[propname] |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1958 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
|
1959 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
|
1960 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1961 # first, links |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1962 a = self.db.arg |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1963 allvalues = () |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1964 sql = [] |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1965 where = [] |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1966 for prop, values in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1967 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
|
1968 continue |
|
1912
2b0ab61db194
fixes for [SF#818339]
Richard Jones <richard@users.sourceforge.net>
parents:
1911
diff
changeset
|
1969 if type(values) is type({}) and len(values) == 1: |
|
2b0ab61db194
fixes for [SF#818339]
Richard Jones <richard@users.sourceforge.net>
parents:
1911
diff
changeset
|
1970 values = values.keys()[0] |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1971 if type(values) is type(''): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1972 allvalues += (values,) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1973 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
|
1974 elif values is None: |
|
e2a8ce4d2317
Class.find() may now find unset Links [SF#700620]
Richard Jones <richard@users.sourceforge.net>
parents:
1557
diff
changeset
|
1975 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
|
1976 else: |
|
2495
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1977 values = values.keys() |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1978 s = '' |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1979 if None in values: |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1980 values.remove(None) |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1981 s = '_%s is NULL or '%prop |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1982 allvalues += tuple(values) |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1983 s += '_%s in (%s)'%(prop, ','.join([a]*len(values))) |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
1984 where.append('(' + s +')') |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1985 if where: |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1986 allvalues = (1, ) + allvalues |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1987 sql.append('''select id from _%s where __retired__ <> %s |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1988 and %s'''%(self.classname, a, ' and '.join(where))) |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1989 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1990 # now multilinks |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1991 for prop, values in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1992 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
|
1993 continue |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1994 if not values: |
|
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1995 continue |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1996 allvalues += (1, ) |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1997 if type(values) is type(''): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1998 allvalues += (values,) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1999 s = a |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
2000 else: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
2001 allvalues += tuple(values.keys()) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
2002 s = ','.join([a]*len(values)) |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
2003 tn = '%s_%s'%(self.classname, prop) |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2004 sql.append('''select id from _%s, %s where __retired__ <> %s |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2005 and id = %s.nodeid and %s.linkid in (%s)'''%(self.classname, |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2006 tn, a, tn, tn, s)) |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2007 |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2008 if not sql: |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
2009 return [] |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2010 sql = ' union '.join(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
|
2011 self.db.sql(sql, allvalues) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2012 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2013 l = [str(x[0]) for x in self.db.sql_fetchall()] |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2014 if __debug__: |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2015 print >>hyperdb.DEBUG, 'find ... ', l |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2016 return l |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2017 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2018 def stringFind(self, **requirements): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2019 '''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
|
2020 properties in a caseless search. |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2021 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2022 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
|
2023 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2024 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
|
2025 ''' |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2026 where = [] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2027 args = [] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2028 for propname in requirements.keys(): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2029 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
|
2030 if not isinstance(prop, String): |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2031 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
|
2032 where.append(propname) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2033 args.append(requirements[propname].lower()) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2034 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2035 # generate the where clause |
|
1549
a53a7e197360
fixed rdbms email address lookup (case insensitivity)
Richard Jones <richard@users.sourceforge.net>
parents:
1539
diff
changeset
|
2036 s = ' and '.join(['lower(_%s)=%s'%(col, self.db.arg) for col in where]) |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2037 sql = 'select id from _%s where %s and __retired__<>%s'%( |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2038 self.classname, s, self.db.arg) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2039 args.append(1) |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2040 self.db.sql(sql, tuple(args)) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2041 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2042 l = [str(x[0]) for x in self.db.sql_fetchall()] |
| 1165 | 2043 if __debug__: |
| 2044 print >>hyperdb.DEBUG, 'find ... ', l | |
| 2045 return l | |
| 2046 | |
| 2047 def list(self): | |
| 2048 ''' Return a list of the ids of the active nodes in this class. | |
| 2049 ''' | |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2050 return self.getnodeids(retired=0) |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2051 |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2052 def getnodeids(self, retired=None): |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2053 ''' 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
|
2054 |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2055 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
|
2056 retired or non-retired nodes, depending on the flag. |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2057 ''' |
|
1707
3d627e34f18e
sqlite backend now passes all tests under 2.3.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1599
diff
changeset
|
2058 # 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
|
2059 if retired is not None: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2060 if retired: |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2061 args = (0, ) |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2062 else: |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2063 args = (1, ) |
|
1539
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2064 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
|
2065 self.db.arg) |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2066 else: |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2067 args = () |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2068 sql = 'select id from _%s'%self.classname |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2069 if __debug__: |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2070 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
|
2071 self.db.cursor.execute(sql, args) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2072 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2073 ids = [str(x[0]) for x in self.db.cursor.fetchall()] |
|
1539
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2074 return ids |
| 1165 | 2075 |
|
1249
6c24a86a12ae
Fixes for SourceForge tracker bugs.
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
2076 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
|
2077 group=(None,None)): |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2078 '''Return a list of the ids of the active nodes in this class that |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2079 match the 'filter' spec, sorted by the group spec and then the |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2080 sort spec |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2081 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2082 "filterspec" is {propname: value(s)} |
| 1165 | 2083 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2084 "sort" and "group" are (dir, prop) where dir is '+', '-' or None |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2085 and prop is a prop name or None |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2086 |
|
2363
c69b67905043
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2321
diff
changeset
|
2087 "search_matches" is {nodeid: marker} or None |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2088 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2089 The filter must match all properties specificed - but if the |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2090 property value to match is a list, any one of the values in the |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2091 list may match for that property to match. |
| 1165 | 2092 ''' |
|
2579
ead9f926234a
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2537
diff
changeset
|
2093 # we can't match anything if search_matches is empty |
|
1205
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
2094 if search_matches == {}: |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
2095 return [] |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
2096 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2097 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2098 start_t = time.time() |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2099 |
| 1165 | 2100 cn = self.classname |
| 2101 | |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2102 timezone = self.db.getUserTimezone() |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2103 |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2104 # vars to hold the components of the SQL statement |
|
2537
5455dd2ec6d7
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2535
diff
changeset
|
2105 frum = [] # FROM clauses |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2106 loj = [] # LEFT OUTER JOIN clauses |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2107 where = [] # WHERE clauses |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2108 args = [] # *any* positional arguments |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2109 a = self.db.arg |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2110 |
| 1165 | 2111 # figure the WHERE clause from the filterspec |
| 2112 props = self.getprops() | |
|
2256
0b198ed096af
fixes for py2.1 (booleans, sigh)
Richard Jones <richard@users.sourceforge.net>
parents:
2248
diff
changeset
|
2113 mlfilt = 0 # are we joining with Multilink tables? |
| 1165 | 2114 for k, v in filterspec.items(): |
| 2115 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
|
2116 # now do other where clause stuff |
| 1165 | 2117 if isinstance(propclass, Multilink): |
|
2256
0b198ed096af
fixes for py2.1 (booleans, sigh)
Richard Jones <richard@users.sourceforge.net>
parents:
2248
diff
changeset
|
2118 mlfilt = 1 |
| 1165 | 2119 tn = '%s_%s'%(cn, k) |
|
1556
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
2120 if v in ('-1', ['-1']): |
|
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
2121 # 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
|
2122 # corresponding multilink table) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2123 where.append('_%s.id not in (select nodeid from %s)'%(cn, |
|
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2124 tn)) |
|
1556
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
2125 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
|
2126 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
|
2127 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2128 where.append('_%s.id=%s.nodeid and %s.linkid in (%s)'%(cn, |
|
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2129 tn, tn, s)) |
| 1165 | 2130 args = args + v |
| 2131 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
|
2132 frum.append(tn) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2133 where.append('_%s.id=%s.nodeid and %s.linkid=%s'%(cn, tn, |
|
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2134 tn, a)) |
| 1165 | 2135 args.append(v) |
|
1365
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2136 elif k == 'id': |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2137 if isinstance(v, type([])): |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2138 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2139 where.append('_%s.%s in (%s)'%(cn, k, s)) |
|
1365
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2140 args = args + v |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2141 else: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2142 where.append('_%s.%s=%s'%(cn, k, a)) |
|
1365
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2143 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
|
2144 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
|
2145 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
|
2146 v = [v] |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2147 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2148 # 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
|
2149 # 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
|
2150 # 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
|
2151 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
|
2152 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2153 # now add to the where clause |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2154 where.append('(' |
|
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2155 +' or '.join(["_%s._%s LIKE '%s'"%(cn, k, s) for s in v]) |
|
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2156 +')') |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2157 # 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
|
2158 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
|
2159 if isinstance(v, type([])): |
|
2509
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2160 d = {} |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2161 for entry in v: |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2162 if entry == '-1': |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2163 entry = None |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2164 d[entry] = entry |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2165 l = [] |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2166 if d.has_key(None) or not d: |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2167 del d[None] |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2168 l.append('_%s._%s is NULL'%(cn, k)) |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2169 if d: |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2170 v = d.keys() |
|
1295
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
2171 s = ','.join([a for x in v]) |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2172 l.append('(_%s._%s in (%s))'%(cn, k, s)) |
|
1295
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
2173 args = args + v |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2174 if l: |
|
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2175 where.append('(' + ' or '.join(l) +')') |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2176 else: |
|
2509
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2177 if v in ('-1', None): |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2178 v = None |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2179 where.append('_%s._%s is NULL'%(cn, k)) |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2180 else: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2181 where.append('_%s._%s=%s'%(cn, k, a)) |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2182 args.append(v) |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2183 elif isinstance(propclass, Date): |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2184 dc = self.db.hyperdb_to_sql_value[hyperdb.Date] |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2185 if isinstance(v, type([])): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2186 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2187 where.append('_%s._%s in (%s)'%(cn, k, s)) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2188 args = args + [dc(date.Date(v)) for x in v] |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2189 else: |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2190 try: |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2191 # 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
|
2192 date_rng = Range(v, date.Date, offset=timezone) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2193 if date_rng.from_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2194 where.append('_%s._%s >= %s'%(cn, k, a)) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2195 args.append(dc(date_rng.from_value)) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2196 if date_rng.to_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2197 where.append('_%s._%s <= %s'%(cn, k, a)) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2198 args.append(dc(date_rng.to_value)) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2199 except ValueError: |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2200 # 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
|
2201 pass |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2202 elif isinstance(propclass, Interval): |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2203 # filter using the __<prop>_int__ column |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2204 if isinstance(v, type([])): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2205 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2206 where.append('_%s.__%s_int__ in (%s)'%(cn, k, s)) |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2207 args = args + [date.Interval(x).as_seconds() for x in v] |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2208 else: |
|
1596
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2209 try: |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2210 # 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
|
2211 date_rng = Range(v, date.Interval) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2212 if date_rng.from_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2213 where.append('_%s.__%s_int__ >= %s'%(cn, k, a)) |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2214 args.append(date_rng.from_value.as_seconds()) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2215 if date_rng.to_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2216 where.append('_%s.__%s_int__ <= %s'%(cn, k, a)) |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2217 args.append(date_rng.to_value.as_seconds()) |
|
1596
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2218 except ValueError: |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2219 # 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
|
2220 pass |
| 1165 | 2221 else: |
| 2222 if isinstance(v, type([])): | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2223 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2224 where.append('_%s._%s in (%s)'%(cn, k, s)) |
| 1165 | 2225 args = args + v |
| 2226 else: | |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2227 where.append('_%s._%s=%s'%(cn, k, a)) |
| 1165 | 2228 args.append(v) |
| 2229 | |
|
1740
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
2230 # don't match retired nodes |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2231 where.append('_%s.__retired__ <> 1'%cn) |
|
1740
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
2232 |
| 1165 | 2233 # add results of full text search |
| 2234 if search_matches is not None: | |
| 2235 v = search_matches.keys() | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2236 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2237 where.append('_%s.id in (%s)'%(cn, s)) |
| 1165 | 2238 args = args + v |
| 2239 | |
|
2722
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2240 # sanity check: sorting *and* grouping on the same property? |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2241 if group[1] == sort[1]: |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2242 sort = (None, None) |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2243 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2244 # "grouping" is just the first-order sorting in the SQL fetch |
| 1165 | 2245 orderby = [] |
| 2246 ordercols = [] | |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2247 mlsort = [] |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2248 for sortby in group, sort: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2249 sdir, prop = sortby |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2250 if sdir and prop: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2251 if isinstance(props[prop], Multilink): |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2252 mlsort.append(sortby) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2253 continue |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2254 elif isinstance(props[prop], Interval): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2255 # use the int column for sorting |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2256 o = '__'+prop+'_int__' |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2257 ordercols.append(o) |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2258 elif isinstance(props[prop], Link): |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2259 # determine whether the linked Class has an order property |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2260 lcn = props[prop].classname |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2261 link = self.db.classes[lcn] |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2262 o = '_%s._%s'%(cn, prop) |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2263 if link.getprops().has_key('order'): |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2264 tn = '_' + lcn |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2265 loj.append('LEFT OUTER JOIN %s on %s=%s.id'%(tn, |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2266 o, tn)) |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2267 ordercols.append(tn + '._order') |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2268 o = tn + '._order' |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2269 else: |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2270 ordercols.append(o) |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2271 elif prop == 'id': |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2272 o = '_%s.id'%cn |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2273 else: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2274 o = '_%s._%s'%(cn, prop) |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2275 ordercols.append(o) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2276 if sdir == '-': |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2277 o += ' desc' |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2278 orderby.append(o) |
| 1165 | 2279 |
| 2280 # construct the SQL | |
|
2537
5455dd2ec6d7
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2535
diff
changeset
|
2281 frum.append('_'+cn) |
| 1165 | 2282 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
|
2283 if where: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2284 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
|
2285 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2286 where = '' |
|
2240
ad4b717e12b9
Small optimisation to only use "select distinct(id) ..."...
Richard Jones <richard@users.sourceforge.net>
parents:
2237
diff
changeset
|
2287 if mlfilt: |
|
ad4b717e12b9
Small optimisation to only use "select distinct(id) ..."...
Richard Jones <richard@users.sourceforge.net>
parents:
2237
diff
changeset
|
2288 # we're joining tables on the id, so we will get dupes if we |
|
ad4b717e12b9
Small optimisation to only use "select distinct(id) ..."...
Richard Jones <richard@users.sourceforge.net>
parents:
2237
diff
changeset
|
2289 # don't distinct() |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2290 cols = ['distinct(_%s.id)'%cn] |
|
2240
ad4b717e12b9
Small optimisation to only use "select distinct(id) ..."...
Richard Jones <richard@users.sourceforge.net>
parents:
2237
diff
changeset
|
2291 else: |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2292 cols = ['_%s.id'%cn] |
| 1165 | 2293 if orderby: |
| 2294 cols = cols + ordercols | |
| 2295 order = ' order by %s'%(','.join(orderby)) | |
| 2296 else: | |
| 2297 order = '' | |
| 2298 cols = ','.join(cols) | |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2299 loj = ' '.join(loj) |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2300 sql = 'select %s from %s %s %s%s'%(cols, frum, loj, where, order) |
| 1165 | 2301 args = tuple(args) |
| 2302 if __debug__: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2303 print >>hyperdb.DEBUG, 'filter', (self, sql, args) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2304 __traceback_info__ = (sql, args) |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2305 if args: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2306 self.db.cursor.execute(sql, args) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2307 else: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2308 # psycopg doesn't like empty args |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2309 self.db.cursor.execute(sql) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
2310 l = self.db.sql_fetchall() |
| 1165 | 2311 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2312 # return the IDs (the first column) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2313 # XXX numeric ids |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2314 l = [str(row[0]) for row in l] |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2315 |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2316 if not mlsort: |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2317 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2318 self.db.stats['filtering'] += (time.time() - start_t) |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2319 return l |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2320 |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2321 # ergh. someone wants to sort by a multilink. |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2322 r = [] |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2323 for id in l: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2324 m = [] |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2325 for ml in mlsort: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2326 m.append(self.get(id, ml[1])) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2327 r.append((id, m)) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2328 i = 0 |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2329 for sortby in mlsort: |
|
2234
70d21059aa18
fix nested scope bug in multilink sorting in rdbms filtering
Richard Jones <richard@users.sourceforge.net>
parents:
2228
diff
changeset
|
2330 def sortfun(a, b, dir=sortby[i], i=i): |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2331 if dir == '-': |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2332 return cmp(b[1][i], a[1][i]) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2333 else: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2334 return cmp(a[1][i], b[1][i]) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2335 r.sort(sortfun) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2336 i += 1 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2337 r = [i[0] for i in r] |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2338 |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2339 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2340 self.db.stats['filtering'] += (time.time() - start_t) |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2341 |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2342 return r |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2343 |
| 1165 | 2344 def count(self): |
| 2345 '''Get the number of nodes in this class. | |
| 2346 | |
| 2347 If the returned integer is 'numnodes', the ids of all the nodes | |
| 2348 in this class run from 1 to numnodes, and numnodes+1 will be the | |
| 2349 id of the next node to be created in this class. | |
| 2350 ''' | |
| 2351 return self.db.countnodes(self.classname) | |
| 2352 | |
| 2353 # Manipulating properties: | |
| 2354 def getprops(self, protected=1): | |
| 2355 '''Return a dictionary mapping property names to property objects. | |
| 2356 If the "protected" flag is true, we include protected properties - | |
| 2357 those which may not be modified. | |
| 2358 ''' | |
| 2359 d = self.properties.copy() | |
| 2360 if protected: | |
| 2361 d['id'] = String() | |
| 2362 d['creation'] = hyperdb.Date() | |
| 2363 d['activity'] = hyperdb.Date() | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
2364 d['creator'] = hyperdb.Link('user') |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
2365 d['actor'] = hyperdb.Link('user') |
| 1165 | 2366 return d |
| 2367 | |
| 2368 def addprop(self, **properties): | |
| 2369 '''Add properties to this class. | |
| 2370 | |
| 2371 The keyword arguments in 'properties' must map names to property | |
| 2372 objects, or a TypeError is raised. None of the keys in 'properties' | |
| 2373 may collide with the names of existing properties, or a ValueError | |
| 2374 is raised before any properties have been added. | |
| 2375 ''' | |
| 2376 for key in properties.keys(): | |
| 2377 if self.properties.has_key(key): | |
| 2378 raise ValueError, key | |
| 2379 self.properties.update(properties) | |
| 2380 | |
| 2381 def index(self, nodeid): | |
| 2382 '''Add (or refresh) the node to search indexes | |
| 2383 ''' | |
| 2384 # find all the String properties that have indexme | |
| 2385 for prop, propclass in self.getprops().items(): | |
| 2386 if isinstance(propclass, String) and propclass.indexme: | |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2387 self.db.indexer.add_text((self.classname, nodeid, prop), |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2388 str(self.get(nodeid, prop))) |
| 1165 | 2389 |
| 2390 | |
| 2391 # | |
| 2392 # Detector interface | |
| 2393 # | |
| 2394 def audit(self, event, detector): | |
| 2395 '''Register a detector | |
| 2396 ''' | |
| 2397 l = self.auditors[event] | |
| 2398 if detector not in l: | |
| 2399 self.auditors[event].append(detector) | |
| 2400 | |
| 2401 def fireAuditors(self, action, nodeid, newvalues): | |
| 2402 '''Fire all registered auditors. | |
| 2403 ''' | |
| 2404 for audit in self.auditors[action]: | |
| 2405 audit(self.db, self, nodeid, newvalues) | |
| 2406 | |
| 2407 def react(self, event, detector): | |
| 2408 '''Register a detector | |
| 2409 ''' | |
| 2410 l = self.reactors[event] | |
| 2411 if detector not in l: | |
| 2412 self.reactors[event].append(detector) | |
| 2413 | |
| 2414 def fireReactors(self, action, nodeid, oldvalues): | |
| 2415 '''Fire all registered reactors. | |
| 2416 ''' | |
| 2417 for react in self.reactors[action]: | |
| 2418 react(self.db, self, nodeid, oldvalues) | |
| 2419 | |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2420 # |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2421 # import / export support |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2422 # |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2423 def export_list(self, propnames, nodeid): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2424 ''' Export a node - generate a list of CSV-able data in the order |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2425 specified by propnames for the given node. |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2426 ''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2427 properties = self.getprops() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2428 l = [] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2429 for prop in propnames: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2430 proptype = properties[prop] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2431 value = self.get(nodeid, prop) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2432 # "marshal" data where needed |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2433 if value is None: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2434 pass |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2435 elif isinstance(proptype, hyperdb.Date): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2436 value = value.get_tuple() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2437 elif isinstance(proptype, hyperdb.Interval): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2438 value = value.get_tuple() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2439 elif isinstance(proptype, hyperdb.Password): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2440 value = str(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2441 l.append(repr(value)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2442 l.append(repr(self.is_retired(nodeid))) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2443 return l |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2444 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2445 def import_list(self, propnames, proplist): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2446 ''' Import a node - all information including "id" is present and |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2447 should not be sanity checked. Triggers are not triggered. The |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2448 journal should be initialised using the "creator" and "created" |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2449 information. |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2450 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2451 Return the nodeid of the node imported. |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2452 ''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2453 if self.db.journaltag is None: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2454 raise DatabaseError, 'Database open read-only' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2455 properties = self.getprops() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2456 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2457 # make the new node's property map |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2458 d = {} |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2459 retire = 0 |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2460 if not "id" in propnames: |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2461 newid = self.db.newid(self.classname) |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2462 else: |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2463 newid = eval(proplist[propnames.index("id")]) |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2464 for i in range(len(propnames)): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2465 # Use eval to reverse the repr() used to output the CSV |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2466 value = eval(proplist[i]) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2467 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2468 # Figure the property for this column |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2469 propname = propnames[i] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2470 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2471 # "unmarshal" where necessary |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2472 if propname == 'id': |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2473 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2474 elif propname == 'is retired': |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2475 # is the item retired? |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2476 if int(value): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2477 retire = 1 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2478 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2479 elif value is None: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2480 d[propname] = None |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2481 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2482 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2483 prop = properties[propname] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2484 if value is None: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2485 # don't set Nones |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2486 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2487 elif isinstance(prop, hyperdb.Date): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2488 value = date.Date(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2489 elif isinstance(prop, hyperdb.Interval): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2490 value = date.Interval(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2491 elif isinstance(prop, hyperdb.Password): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2492 pwd = password.Password() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2493 pwd.unpack(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2494 value = pwd |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2495 d[propname] = value |
|
2893
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2496 if isinstance(prop, String): |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2497 if type(value) != type('') and type(value) != type(u''): |
|
2893
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2498 raise TypeError, \ |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2499 'new property "%(propname)s" not a string: %(value)r' \ |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2500 % locals() |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2501 if prop.indexme: |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2502 self.db.indexer.add_text((self.classname, newid, propname), |
|
17df4dc4ae51
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2888
diff
changeset
|
2503 value) |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2504 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2505 # get a new id if necessary |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2506 if newid is None: |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2507 newid = self.db.newid(self.classname) |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2508 |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2509 # insert new node or update existing? |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2510 if not self.hasnode(newid): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2511 self.db.addnode(self.classname, newid, d) # insert |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2512 else: |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2513 self.db.setnode(self.classname, newid, d) # update |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2514 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2515 # retire? |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2516 if retire: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2517 # use the arg for __retired__ to cope with any odd database type |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2518 # conversion (hello, sqlite) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2519 sql = 'update _%s set __retired__=%s where id=%s'%(self.classname, |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2520 self.db.arg, self.db.arg) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2521 if __debug__: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2522 print >>hyperdb.DEBUG, 'retire', (self, sql, newid) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2523 self.db.cursor.execute(sql, (1, newid)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2524 return newid |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2525 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2526 def export_journals(self): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2527 '''Export a class's journal - generate a list of lists of |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2528 CSV-able data: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2529 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2530 nodeid, date, user, action, params |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2531 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2532 No heading here - the columns are fixed. |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2533 ''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2534 properties = self.getprops() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2535 r = [] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2536 for nodeid in self.getnodeids(): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2537 for nodeid, date, user, action, params in self.history(nodeid): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2538 date = date.get_tuple() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2539 if action == 'set': |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2540 for propname, value in params.items(): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2541 prop = properties[propname] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2542 # make sure the params are eval()'able |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2543 if value is None: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2544 pass |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2545 elif isinstance(prop, Date): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2546 value = value.get_tuple() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2547 elif isinstance(prop, Interval): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2548 value = value.get_tuple() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2549 elif isinstance(prop, Password): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2550 value = str(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2551 params[propname] = value |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2552 l = [nodeid, date, user, action, params] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2553 r.append(map(repr, l)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2554 return r |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2555 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2556 def import_journals(self, entries): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2557 '''Import a class's journal. |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2558 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2559 Uses setjournal() to set the journal for each item.''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2560 properties = self.getprops() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2561 d = {} |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2562 for l in entries: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2563 l = map(eval, l) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2564 nodeid, jdate, user, action, params = l |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2565 r = d.setdefault(nodeid, []) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2566 if action == 'set': |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2567 for propname, value in params.items(): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2568 prop = properties[propname] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2569 if value is None: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2570 pass |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2571 elif isinstance(prop, Date): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2572 value = date.Date(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2573 elif isinstance(prop, Interval): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2574 value = date.Interval(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2575 elif isinstance(prop, Password): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2576 pwd = password.Password() |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2577 pwd.unpack(value) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2578 value = pwd |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2579 params[propname] = value |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2580 r.append((nodeid, date.Date(jdate), user, action, params)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2581 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2582 for nodeid, l in d.items(): |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2583 self.db.setjournal(self.classname, nodeid, l) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2584 |
|
2598
5077745b0758
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2579
diff
changeset
|
2585 class FileClass(hyperdb.FileClass, Class): |
| 1165 | 2586 '''This class defines a large chunk of data. To support this, it has a |
| 2587 mandatory String property "content" which is typically saved off | |
| 2588 externally to the hyperdb. | |
| 2589 | |
| 2590 The default MIME type of this data is defined by the | |
| 2591 "default_mime_type" class attribute, which may be overridden by each | |
| 2592 node if the class defines a "type" String property. | |
| 2593 ''' | |
| 2594 default_mime_type = 'text/plain' | |
| 2595 | |
| 2596 def create(self, **propvalues): | |
| 2597 ''' snaffle the file propvalue and store in a file | |
| 2598 ''' | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2599 # 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
|
2600 # 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
|
2601 self.fireAuditors('create', None, propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2602 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2603 # now remove the content property so it's not stored in the db |
| 1165 | 2604 content = propvalues['content'] |
| 2605 del propvalues['content'] | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2606 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2607 # do the database create |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2608 newid = self.create_inner(**propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2609 |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2610 # figure the mime type |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2611 mime_type = propvalues.get('type', self.default_mime_type) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2612 |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2613 # and index! |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2614 self.db.indexer.add_text((self.classname, newid, 'content'), content, |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2615 mime_type) |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2616 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2617 # fire reactors |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2618 self.fireReactors('create', newid, None) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2619 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2620 # store off the content as a file |
| 1165 | 2621 self.db.storefile(self.classname, newid, None, content) |
| 2622 return newid | |
| 2623 | |
| 2624 _marker = [] | |
| 2625 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
|
2626 ''' 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
|
2627 |
|
d2801a2b0a77
Initial implementation (half-baked) at new Tracker instance.
Richard Jones <richard@users.sourceforge.net>
parents:
1751
diff
changeset
|
2628 'cache' exists for backwards compatibility, and is not used. |
| 1165 | 2629 ''' |
| 2630 poss_msg = 'Possibly a access right configuration problem.' | |
| 2631 if propname == 'content': | |
| 2632 try: | |
| 2633 return self.db.getfile(self.classname, nodeid, None) | |
| 2634 except IOError, (strerror): | |
| 2635 # BUG: by catching this we donot see an error in the log. | |
| 2636 return 'ERROR reading file: %s%s\n%s\n%s'%( | |
| 2637 self.classname, nodeid, poss_msg, strerror) | |
| 2638 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
|
2639 return Class.get(self, nodeid, propname, default) |
| 1165 | 2640 else: |
|
1780
d2801a2b0a77
Initial implementation (half-baked) at new Tracker instance.
Richard Jones <richard@users.sourceforge.net>
parents:
1751
diff
changeset
|
2641 return Class.get(self, nodeid, propname) |
| 1165 | 2642 |
| 2643 def getprops(self, protected=1): | |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2644 '''In addition to the actual properties on the node, these methods |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2645 provide the "content" property. If the "protected" flag is true, |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2646 we include protected properties - those which may not be |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2647 modified. |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2648 |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2649 Note that the content prop is indexed separately, hence no indexme. |
| 1165 | 2650 ''' |
| 2651 d = Class.getprops(self, protected=protected).copy() | |
| 2652 d['content'] = hyperdb.String() | |
| 2653 return d | |
| 2654 | |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2655 def set(self, itemid, **propvalues): |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2656 ''' Snarf the "content" propvalue and update it in a file |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2657 ''' |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2658 self.fireAuditors('set', itemid, propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2659 oldvalues = copy.deepcopy(self.db.getnode(self.classname, itemid)) |
| 1165 | 2660 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2661 # now remove the content property so it's not stored in the db |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2662 content = None |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2663 if propvalues.has_key('content'): |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2664 content = propvalues['content'] |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2665 del propvalues['content'] |
| 1165 | 2666 |
|
2762
638fc2dab5a1
fixed editing of message contents
Richard Jones <richard@users.sourceforge.net>
parents:
2756
diff
changeset
|
2667 # do the database set |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2668 propvalues = self.set_inner(itemid, **propvalues) |
| 1165 | 2669 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2670 # do content? |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2671 if content: |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2672 # store and index |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2673 self.db.storefile(self.classname, itemid, None, content) |
|
2965
0e1860dcb328
unit test fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2893
diff
changeset
|
2674 mime_type = propvalues.get('type', self.get(itemid, 'type')) |
|
0e1860dcb328
unit test fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2893
diff
changeset
|
2675 if not mime_type: |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2676 mime_type = self.default_mime_type |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2677 self.db.indexer.add_text((self.classname, itemid, 'content'), |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2678 content, mime_type) |
| 1165 | 2679 |
|
2762
638fc2dab5a1
fixed editing of message contents
Richard Jones <richard@users.sourceforge.net>
parents:
2756
diff
changeset
|
2680 propvalues['content'] = content |
|
638fc2dab5a1
fixed editing of message contents
Richard Jones <richard@users.sourceforge.net>
parents:
2756
diff
changeset
|
2681 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2682 # fire reactors |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2683 self.fireReactors('set', itemid, oldvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2684 return propvalues |
| 1165 | 2685 |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2686 def index(self, nodeid): |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2687 '''Add (or refresh) the node to search indexes. |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2688 |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2689 Pass on the content-type property for the content property. |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2690 ''' |
|
2513
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2691 Class.index(self, nodeid) |
|
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2692 try: |
|
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2693 mime_type = self.get(nodeid, 'type', self.default_mime_type) |
|
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2694 except KeyError: |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2695 mime_type = self.default_mime_type |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2696 self.db.indexer.add_text((self.classname, nodeid, 'content'), |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2697 str(self.get(nodeid, 'content')), mime_type) |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2698 |
| 1165 | 2699 # XXX deviation from spec - was called ItemClass |
| 2700 class IssueClass(Class, roundupdb.IssueClass): | |
| 2701 # Overridden methods: | |
| 2702 def __init__(self, db, classname, **properties): | |
| 2703 '''The newly-created class automatically includes the "messages", | |
| 2704 "files", "nosy", and "superseder" properties. If the 'properties' | |
| 2705 dictionary attempts to specify any of these properties or a | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
2706 "creation", "creator", "activity" or "actor" property, a ValueError |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
2707 is raised. |
| 1165 | 2708 ''' |
| 2709 if not properties.has_key('title'): | |
| 2710 properties['title'] = hyperdb.String(indexme='yes') | |
| 2711 if not properties.has_key('messages'): | |
| 2712 properties['messages'] = hyperdb.Multilink("msg") | |
| 2713 if not properties.has_key('files'): | |
| 2714 properties['files'] = hyperdb.Multilink("file") | |
| 2715 if not properties.has_key('nosy'): | |
| 2716 # note: journalling is turned off as it really just wastes | |
| 2717 # space. this behaviour may be overridden in an instance | |
| 2718 properties['nosy'] = hyperdb.Multilink("user", do_journal="no") | |
| 2719 if not properties.has_key('superseder'): | |
| 2720 properties['superseder'] = hyperdb.Multilink(classname) | |
| 2721 Class.__init__(self, db, classname, **properties) | |
| 2722 |
