Mercurial > p > roundup > code
annotate roundup/backends/rdbms_common.py @ 2756:b654a33346a6 maint-0.7 0.7.8
merge from HEAD, pre-release stuff
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 14 Oct 2004 22:28:02 +0000 |
| parents | 1e70e58cf763 |
| children | 638fc2dab5a1 |
| rev | line source |
|---|---|
|
2756
b654a33346a6
merge from HEAD, pre-release stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2734
diff
changeset
|
1 # $Id: rdbms_common.py,v 1.98.2.24 2004-10-14 22:28:02 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) |
| 1165 | 697 |
| 698 # | |
| 699 # Nodes | |
| 700 # | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
701 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
702 hyperdb_to_sql_value = { |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
703 hyperdb.String : str, |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
704 # fractional seconds by default |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
705 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
|
706 hyperdb.Link : int, |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
707 hyperdb.Interval : str, |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
708 hyperdb.Password : str, |
|
2166
cd42c3c7173a
MySQL and Postgresql use BOOL/BOOLEAN for Boolean types
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
709 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
|
710 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
|
711 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
|
712 } |
| 1165 | 713 def addnode(self, classname, nodeid, node): |
| 714 ''' Add the specified node to its class's db. | |
| 715 ''' | |
| 716 if __debug__: | |
| 717 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
|
718 |
|
96cd422532ef
bye bye gadfly - you served your purpose well [SF#701127]
Richard Jones <richard@users.sourceforge.net>
parents:
1523
diff
changeset
|
719 # determine the column definitions and multilink tables |
| 1165 | 720 cl = self.classes[classname] |
| 721 cols, mls = self.determine_columns(cl.properties.items()) | |
| 722 | |
|
1189
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
723 # 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
|
724 values = node.copy() |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
725 if not values.has_key('creator'): |
|
1189
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
726 # add in the "calculated" properties (dupe so we don't affect |
|
23b8d1e87fe3
import fixes
Richard Jones <richard@users.sourceforge.net>
parents:
1187
diff
changeset
|
727 # 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
|
728 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
|
729 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
|
730 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
731 cl = self.classes[classname] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
732 props = cl.getprops(protected=1) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
733 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
|
734 |
| 1165 | 735 # 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
|
736 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
|
737 if not values.has_key(col): |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
738 if isinstance(prop, Multilink): |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
739 values[col] = [] |
|
1496
e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
Richard Jones <richard@users.sourceforge.net>
parents:
1492
diff
changeset
|
740 else: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
741 values[col] = None |
| 1165 | 742 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
743 # 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
|
744 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
745 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
746 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
747 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
748 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
749 # figure the values to insert |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
750 vals = [] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
751 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
|
752 # this is somewhat dodgy.... |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
753 if col.endswith('_int__'): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
754 # XXX eugh, this test suxxors |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
755 value = values[col[2:-6]] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
756 # 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
|
757 if value is not None: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
758 vals.append(value.as_seconds()) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
759 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
760 vals.append(value) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
761 continue |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
762 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
763 prop = props[col[1:]] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
764 value = values[col[1:]] |
|
2482
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
765 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
|
766 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
|
767 vals.append(value) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
768 vals.append(nodeid) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
769 vals = tuple(vals) |
| 1165 | 770 |
| 771 # 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
|
772 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
|
773 cols = ','.join([col for col,dt in cols]) + ',id' |
| 1165 | 774 |
| 775 # perform the inserts | |
| 776 sql = 'insert into _%s (%s) values (%s)'%(classname, cols, s) | |
| 777 if __debug__: | |
| 778 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
|
779 self.cursor.execute(sql, vals) |
| 1165 | 780 |
| 781 # insert the multilink rows | |
| 782 for col in mls: | |
| 783 t = '%s_%s'%(classname, col) | |
| 784 for entry in node[col]: | |
| 785 sql = 'insert into %s (linkid, nodeid) values (%s,%s)'%(t, | |
| 786 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
|
787 self.sql(sql, (entry, nodeid)) |
| 1165 | 788 |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
789 def setnode(self, classname, nodeid, values, multilink_changes={}): |
| 1165 | 790 ''' Change the specified node. |
| 791 ''' | |
| 792 if __debug__: | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
793 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
|
794 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
795 # 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
|
796 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
797 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
798 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
799 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
800 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
801 # 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
|
802 values = values.copy() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
803 values['activity'] = date.Date() |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
804 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
|
805 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
806 cl = self.classes[classname] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
807 props = cl.getprops() |
| 1165 | 808 |
| 809 cols = [] | |
| 810 mls = [] | |
| 811 # 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
|
812 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
|
813 prop = props[col] |
| 1165 | 814 if isinstance(prop, Multilink): |
| 815 mls.append(col) | |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
816 elif isinstance(prop, Interval): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
817 # 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
|
818 cols.append(col) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
819 # 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
|
820 cols.append('_' +col + '_int__') |
| 1165 | 821 else: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
822 cols.append(col) |
| 1165 | 823 cols.sort() |
| 824 | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
825 # figure the values to insert |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
826 vals = [] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
827 for col in cols: |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
828 if col.endswith('_int__'): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
829 # XXX eugh, this test suxxors |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
830 # 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
|
831 col = col[1:-6] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
832 prop = props[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
833 value = values[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
834 if value is None: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
835 vals.append(None) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
836 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
837 vals.append(value.as_seconds()) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
838 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
839 prop = props[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
840 value = values[col] |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
841 if value is None: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
842 e = None |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
843 else: |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
844 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
|
845 vals.append(e) |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
846 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
847 vals.append(int(nodeid)) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
848 vals = tuple(vals) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
849 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
850 # 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
|
851 if cols: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
852 # 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
|
853 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
|
854 cols = ','.join(cols) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
855 |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
856 # perform the update |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
857 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
|
858 if __debug__: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
859 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
|
860 self.cursor.execute(sql, vals) |
| 1165 | 861 |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
862 # 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
|
863 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
|
864 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
|
865 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
|
866 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
|
867 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
868 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
|
869 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
870 # 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
|
871 # 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
|
872 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
|
873 (nodeid,)) |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
874 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
875 # 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
|
876 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
|
877 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
|
878 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
|
879 # 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
|
880 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
|
881 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
882 # we have multilink changes to apply |
| 1165 | 883 for col, (add, remove) in multilink_changes.items(): |
| 884 tn = '%s_%s'%(classname, col) | |
| 885 if add: | |
| 886 sql = 'insert into %s (nodeid, linkid) values (%s,%s)'%(tn, | |
| 887 self.arg, self.arg) | |
| 888 for addid in add: | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
889 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
890 self.sql(sql, (int(nodeid), int(addid))) |
| 1165 | 891 if remove: |
| 892 sql = 'delete from %s where nodeid=%s and linkid=%s'%(tn, | |
| 893 self.arg, self.arg) | |
| 894 for removeid in remove: | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
895 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
896 self.sql(sql, (int(nodeid), int(removeid))) |
| 1165 | 897 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
898 sql_to_hyperdb_value = { |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
899 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
|
900 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
|
901 # 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
|
902 hyperdb.Link : str, |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
903 hyperdb.Interval : date.Interval, |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
904 hyperdb.Password : lambda x: password.Password(encrypted=x), |
|
2482
a15f91a10e45
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2428
diff
changeset
|
905 hyperdb.Boolean : _bool_cvt, |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
906 hyperdb.Number : _num_cvt, |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
907 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
|
908 } |
| 1165 | 909 def getnode(self, classname, nodeid): |
| 910 ''' Get a node from the database. | |
| 911 ''' | |
| 912 if __debug__: | |
| 913 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
|
914 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
915 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
916 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
917 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
918 # 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
|
919 self.cache_lru.remove(key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
920 self.cache_lru.insert(0, key) |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
921 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
922 self.stats['cache_hits'] += 1 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
923 # return the cached information |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
924 return self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
925 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
926 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
927 self.stats['cache_misses'] += 1 |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
928 start_t = time.time() |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
929 |
| 1165 | 930 # figure the columns we're fetching |
| 931 cl = self.classes[classname] | |
| 932 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
|
933 scols = ','.join([col for col,dt in cols]) |
| 1165 | 934 |
| 935 # perform the basic property fetch | |
| 936 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
|
937 self.sql(sql, (nodeid,)) |
| 1165 | 938 |
|
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
|
939 values = self.sql_fetchone() |
| 1165 | 940 if values is None: |
| 941 raise IndexError, 'no such %s node %s'%(classname, nodeid) | |
| 942 | |
| 943 # make up the node | |
| 944 node = {} | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
945 props = cl.getprops(protected=1) |
| 1165 | 946 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
|
947 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
|
948 if name.endswith('_int__'): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
949 # XXX eugh, this test suxxors |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
950 # 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
|
951 continue |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
952 value = values[col] |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
953 if value is not None: |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
954 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
|
955 node[name] = value |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
956 |
| 1165 | 957 |
| 958 # now the multilinks | |
| 959 for col in mls: | |
| 960 # get the link ids | |
| 961 sql = 'select linkid from %s_%s where nodeid=%s'%(classname, col, | |
| 962 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
|
963 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 964 # 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
|
965 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
966 node[col] = [str(x[0]) for x in self.cursor.fetchall()] |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
967 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
968 # save off in the cache |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
969 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
970 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
|
971 # update the LRU |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
972 self.cache_lru.insert(0, key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
973 if len(self.cache_lru) > ROW_CACHE_SIZE: |
|
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
974 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
|
975 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
976 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
977 self.stats['get_items'] += (time.time() - start_t) |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
978 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
979 return node |
| 1165 | 980 |
| 981 def destroynode(self, classname, nodeid): | |
| 982 '''Remove a node from the database. Called exclusively by the | |
| 983 destroy() method on Class. | |
| 984 ''' | |
| 985 if __debug__: | |
| 986 print >>hyperdb.DEBUG, 'destroynode', (self, classname, nodeid) | |
| 987 | |
| 988 # make sure the node exists | |
| 989 if not self.hasnode(classname, nodeid): | |
| 990 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 991 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
992 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
993 if self.cache.has_key((classname, nodeid)): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
994 del self.cache[(classname, nodeid)] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
995 |
| 1165 | 996 # see if there's any obvious commit actions that we should get rid of |
| 997 for entry in self.transactions[:]: | |
| 998 if entry[1][:2] == (classname, nodeid): | |
| 999 self.transactions.remove(entry) | |
| 1000 | |
| 1001 # now do the SQL | |
| 1002 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
|
1003 self.sql(sql, (nodeid,)) |
| 1165 | 1004 |
| 1005 # remove from multilnks | |
| 1006 cl = self.getclass(classname) | |
| 1007 x, mls = self.determine_columns(cl.properties.items()) | |
| 1008 for col in mls: | |
| 1009 # get the link ids | |
| 1010 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
|
1011 self.sql(sql, (nodeid,)) |
| 1165 | 1012 |
| 1013 # remove journal entries | |
| 1014 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
|
1015 self.sql(sql, (nodeid,)) |
| 1165 | 1016 |
| 1017 def hasnode(self, classname, nodeid): | |
| 1018 ''' Determine if the database has a given node. | |
| 1019 ''' | |
| 1020 sql = 'select count(*) from _%s where id=%s'%(classname, self.arg) | |
| 1021 if __debug__: | |
| 1022 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
|
1023 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
|
1024 return int(self.cursor.fetchone()[0]) |
| 1165 | 1025 |
| 1026 def countnodes(self, classname): | |
| 1027 ''' Count the number of nodes that exist for a particular Class. | |
| 1028 ''' | |
| 1029 sql = 'select count(*) from _%s'%classname | |
| 1030 if __debug__: | |
| 1031 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
|
1032 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
|
1033 return self.cursor.fetchone()[0] |
| 1165 | 1034 |
| 1035 def addjournal(self, classname, nodeid, action, params, creator=None, | |
| 1036 creation=None): | |
| 1037 ''' Journal the Action | |
| 1038 'action' may be: | |
| 1039 | |
| 1040 'create' or 'set' -- 'params' is a dictionary of property values | |
| 1041 'link' or 'unlink' -- 'params' is (classname, nodeid, propname) | |
| 1042 'retire' -- 'params' is None | |
| 1043 ''' | |
| 1044 # handle supply of the special journalling parameters (usually | |
| 1045 # supplied on importing an existing database) | |
| 1046 if creator: | |
| 1047 journaltag = creator | |
| 1048 else: | |
|
1800
a3b1b1dcf639
Use getuid(), not figure_curuserid()
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1794
diff
changeset
|
1049 journaltag = self.getuid() |
| 1165 | 1050 if creation: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1051 journaldate = creation |
| 1165 | 1052 else: |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1053 journaldate = date.Date() |
| 1165 | 1054 |
| 1055 # 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
|
1056 cols = 'nodeid,date,tag,action,params' |
| 1165 | 1057 |
| 1058 if __debug__: | |
| 1059 print >>hyperdb.DEBUG, 'addjournal', (nodeid, journaldate, | |
| 1060 journaltag, action, params) | |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1061 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1062 # make the journalled data marshallable |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1063 if isinstance(params, type({})): |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1064 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
|
1065 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1066 params = repr(params) |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1067 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1068 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
|
1069 journaldate = dc(journaldate) |
| 1165 | 1070 |
|
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
|
1071 self.save_journal(classname, cols, nodeid, journaldate, |
| 1165 | 1072 journaltag, action, params) |
| 1073 | |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1074 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
|
1075 '''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
|
1076 # 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
|
1077 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
|
1078 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
|
1079 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1080 # 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
|
1081 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
|
1082 |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1083 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
|
1084 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
|
1085 if __debug__: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
1086 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
|
1087 journaltag, action, params) |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1088 |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1089 # make the journalled data marshallable |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1090 if isinstance(params, type({})): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1091 self._journal_marshal(params, classname) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1092 params = repr(params) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1093 |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1094 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
|
1095 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
|
1096 |
|
2274
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1097 def _journal_marshal(self, params, classname): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1098 '''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
|
1099 eval'able values.''' |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1100 properties = self.getclass(classname).getprops() |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1101 for param, value in params.items(): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1102 if not value: |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1103 continue |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1104 property = properties[param] |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1105 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
|
1106 if isinstance(property, Password): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1107 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1108 elif isinstance(property, Date): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1109 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1110 elif isinstance(property, Interval): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1111 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1112 elif isinstance(property, Boolean): |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1113 params[param] = cvt(value) |
|
86bb9c804b41
fix RDBMS import (thanks Tamer Fahmy)
Richard Jones <richard@users.sourceforge.net>
parents:
2256
diff
changeset
|
1114 |
| 1165 | 1115 def getjournal(self, classname, nodeid): |
| 1116 ''' get the journal for id | |
| 1117 ''' | |
| 1118 # make sure the node exists | |
| 1119 if not self.hasnode(classname, nodeid): | |
| 1120 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 1121 | |
| 1122 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
|
1123 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
|
1124 |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1125 # now unmarshal the data |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1126 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
|
1127 res = [] |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1128 properties = self.getclass(classname).getprops() |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1129 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
|
1130 params = eval(params) |
|
2248
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1131 if isinstance(params, type({})): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1132 for param, value in params.items(): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1133 if not value: |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1134 continue |
|
2732
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1135 property = properties.get(param, None) |
|
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1136 if property is None: |
|
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1137 # deleted property |
|
0d8b3b5f40ea
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2728
diff
changeset
|
1138 continue |
|
2248
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1139 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
|
1140 if isinstance(property, Password): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1141 params[param] = cvt(value) |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1142 elif isinstance(property, Date): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1143 params[param] = cvt(value) |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1144 elif isinstance(property, Interval): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1145 params[param] = cvt(value) |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1146 elif isinstance(property, Boolean): |
|
cd7e6d6288c6
fixed rego from email address [SF#947414]
Richard Jones <richard@users.sourceforge.net>
parents:
2244
diff
changeset
|
1147 params[param] = cvt(value) |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1148 # XXX numeric ids |
|
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1149 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
|
1150 return res |
| 1165 | 1151 |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1152 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
|
1153 journaltag, action, params): |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1154 ''' 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
|
1155 ''' |
|
2244
ac4f295499a4
fixed journal marshalling in RDBMS backends [SF#943627]
Richard Jones <richard@users.sourceforge.net>
parents:
2240
diff
changeset
|
1156 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
|
1157 |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1158 # do the insert |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1159 a = self.arg |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1160 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
|
1161 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
|
1162 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
|
1163 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
|
1164 self.cursor.execute(sql, entry) |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1165 |
|
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
|
1166 def load_journal(self, classname, cols, nodeid): |
| 1165 | 1167 ''' Load the journal from the database |
| 1168 ''' | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1169 # now get the journal entries |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1170 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
|
1171 cols, classname, self.arg) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1172 if __debug__: |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1173 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
|
1174 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
|
1175 return self.cursor.fetchall() |
| 1165 | 1176 |
| 1177 def pack(self, pack_before): | |
| 1178 ''' Delete all journal entries except "create" before 'pack_before'. | |
| 1179 ''' | |
|
2417
fe722c32ce0c
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2380
diff
changeset
|
1180 date_stamp = self.hyperdb_to_sql_value[Date](pack_before) |
| 1165 | 1181 |
| 1182 # do the delete | |
| 1183 for classname in self.classes.keys(): | |
| 1184 sql = "delete from %s__journal where date<%s and "\ | |
| 1185 "action<>'create'"%(classname, self.arg) | |
| 1186 if __debug__: | |
| 1187 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
|
1188 self.cursor.execute(sql, (date_stamp,)) |
| 1165 | 1189 |
| 1190 def sql_commit(self): | |
| 1191 ''' Actually commit to the database. | |
| 1192 ''' | |
|
2075
b1704ba7be41
make mysql / postgresql work again. beginnings of otk/session store in rdbmses
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
1193 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
|
1194 print >>hyperdb.DEBUG, '+++ commit database connection +++' |
| 1165 | 1195 self.conn.commit() |
| 1196 | |
| 1197 def commit(self): | |
| 1198 ''' Commit the current transactions. | |
| 1199 | |
| 1200 Save all data changed since the database was opened or since the | |
| 1201 last commit() or rollback(). | |
| 1202 ''' | |
| 1203 if __debug__: | |
| 1204 print >>hyperdb.DEBUG, 'commit', (self,) | |
| 1205 | |
| 1206 # commit the database | |
| 1207 self.sql_commit() | |
| 1208 | |
| 1209 # now, do all the other transaction stuff | |
| 1210 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
|
1211 method(*args) |
| 1165 | 1212 |
| 1213 # save the indexer state | |
| 1214 self.indexer.save_index() | |
| 1215 | |
| 1216 # clear out the transactions | |
| 1217 self.transactions = [] | |
| 1218 | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1219 def sql_rollback(self): |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1220 self.conn.rollback() |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1221 |
| 1165 | 1222 def rollback(self): |
| 1223 ''' Reverse all actions from the current transaction. | |
| 1224 | |
| 1225 Undo all the changes made since the database was opened or the last | |
| 1226 commit() or rollback() was performed. | |
| 1227 ''' | |
| 1228 if __debug__: | |
| 1229 print >>hyperdb.DEBUG, 'rollback', (self,) | |
| 1230 | |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1231 self.sql_rollback() |
| 1165 | 1232 |
| 1233 # roll back "other" transaction stuff | |
| 1234 for method, args in self.transactions: | |
| 1235 # delete temporary files | |
| 1236 if method == self.doStoreFile: | |
| 1237 self.rollbackStoreFile(*args) | |
| 1238 self.transactions = [] | |
| 1239 | |
|
1492
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
1240 # clear the cache |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
1241 self.clearCache() |
|
2fc7d4a8c9e7
fixed sqlite rollback/caching bug [SF#689383]
Richard Jones <richard@users.sourceforge.net>
parents:
1484
diff
changeset
|
1242 |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1243 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
|
1244 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
|
1245 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
|
1246 self.conn.close() |
|
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1247 |
| 1165 | 1248 def close(self): |
| 1249 ''' Close off the connection. | |
| 1250 ''' | |
|
2093
3f6024ab2c7a
That's the last of the RDBMS migration steps done! Yay!
Richard Jones <richard@users.sourceforge.net>
parents:
2089
diff
changeset
|
1251 self.indexer.close() |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
1252 self.sql_close() |
| 1165 | 1253 |
| 1254 # | |
| 1255 # The base Class class | |
| 1256 # | |
| 1257 class Class(hyperdb.Class): | |
| 1258 ''' The handle to a particular class of nodes in a hyperdatabase. | |
| 1259 | |
| 1260 All methods except __repr__ and getnode must be implemented by a | |
| 1261 concrete backend Class. | |
| 1262 ''' | |
| 1263 | |
| 1264 def __init__(self, db, classname, **properties): | |
| 1265 '''Create a new class with a given name and property specification. | |
| 1266 | |
| 1267 'classname' must not collide with the name of an existing class, | |
| 1268 or a ValueError is raised. The keyword arguments in 'properties' | |
| 1269 must map names to property objects, or a TypeError is raised. | |
| 1270 ''' | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1271 for name in 'creation activity creator actor'.split(): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1272 if properties.has_key(name): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1273 raise ValueError, '"creation", "activity", "creator" and '\ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1274 '"actor" are reserved' |
| 1165 | 1275 |
| 1276 self.classname = classname | |
| 1277 self.properties = properties | |
| 1278 self.db = weakref.proxy(db) # use a weak ref to avoid circularity | |
| 1279 self.key = '' | |
| 1280 | |
| 1281 # should we journal changes (default yes) | |
| 1282 self.do_journal = 1 | |
| 1283 | |
| 1284 # do the db-related init stuff | |
| 1285 db.addclass(self) | |
| 1286 | |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1287 self.auditors = {'create': [], 'set': [], 'retire': [], 'restore': []} |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1288 self.reactors = {'create': [], 'set': [], 'retire': [], 'restore': []} |
| 1165 | 1289 |
| 1290 def schema(self): | |
| 1291 ''' A dumpable version of the schema that we can store in the | |
| 1292 database | |
| 1293 ''' | |
| 1294 return (self.key, [(x, repr(y)) for x,y in self.properties.items()]) | |
| 1295 | |
| 1296 def enableJournalling(self): | |
| 1297 '''Turn journalling on for this class | |
| 1298 ''' | |
| 1299 self.do_journal = 1 | |
| 1300 | |
| 1301 def disableJournalling(self): | |
| 1302 '''Turn journalling off for this class | |
| 1303 ''' | |
| 1304 self.do_journal = 0 | |
| 1305 | |
| 1306 # Editing nodes: | |
| 1307 def create(self, **propvalues): | |
| 1308 ''' Create a new node of this class and return its id. | |
| 1309 | |
| 1310 The keyword arguments in 'propvalues' map property names to values. | |
| 1311 | |
| 1312 The values of arguments must be acceptable for the types of their | |
| 1313 corresponding properties or a TypeError is raised. | |
| 1314 | |
| 1315 If this class has a key property, it must be present and its value | |
| 1316 must not collide with other key strings or a ValueError is raised. | |
| 1317 | |
| 1318 Any other properties on this class that are missing from the | |
| 1319 'propvalues' dictionary are set to None. | |
| 1320 | |
| 1321 If an id in a link or multilink property does not refer to a valid | |
| 1322 node, an IndexError is raised. | |
| 1323 ''' | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1324 self.fireAuditors('create', None, propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1325 newid = self.create_inner(**propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1326 self.fireReactors('create', newid, None) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1327 return newid |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1328 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1329 def create_inner(self, **propvalues): |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
1330 ''' 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
|
1331 ''' |
| 1165 | 1332 if propvalues.has_key('id'): |
| 1333 raise KeyError, '"id" is reserved' | |
| 1334 | |
| 1335 if self.db.journaltag is None: | |
| 1336 raise DatabaseError, 'Database open read-only' | |
| 1337 | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1338 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
|
1339 propvalues.has_key('creation') or propvalues.has_key('activity'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1340 raise KeyError, '"creator", "actor", "creation" and '\ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1341 '"activity" are reserved' |
| 1165 | 1342 |
| 1343 # new node's id | |
| 1344 newid = self.db.newid(self.classname) | |
| 1345 | |
| 1346 # validate propvalues | |
| 1347 num_re = re.compile('^\d+$') | |
| 1348 for key, value in propvalues.items(): | |
| 1349 if key == self.key: | |
| 1350 try: | |
| 1351 self.lookup(value) | |
| 1352 except KeyError: | |
| 1353 pass | |
| 1354 else: | |
| 1355 raise ValueError, 'node with key "%s" exists'%value | |
| 1356 | |
| 1357 # try to handle this property | |
| 1358 try: | |
| 1359 prop = self.properties[key] | |
| 1360 except KeyError: | |
| 1361 raise KeyError, '"%s" has no property "%s"'%(self.classname, | |
| 1362 key) | |
| 1363 | |
| 1364 if value is not None and isinstance(prop, Link): | |
| 1365 if type(value) != type(''): | |
| 1366 raise ValueError, 'link value must be String' | |
| 1367 link_class = self.properties[key].classname | |
| 1368 # if it isn't a number, it's a key | |
| 1369 if not num_re.match(value): | |
| 1370 try: | |
| 1371 value = self.db.classes[link_class].lookup(value) | |
| 1372 except (TypeError, KeyError): | |
| 1373 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1374 key, value, link_class) | |
| 1375 elif not self.db.getclass(link_class).hasnode(value): | |
| 1376 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 1377 | |
| 1378 # save off the value | |
| 1379 propvalues[key] = value | |
| 1380 | |
| 1381 # register the link with the newly linked node | |
| 1382 if self.do_journal and self.properties[key].do_journal: | |
| 1383 self.db.addjournal(link_class, value, 'link', | |
| 1384 (self.classname, newid, key)) | |
| 1385 | |
| 1386 elif isinstance(prop, Multilink): | |
| 1387 if type(value) != type([]): | |
| 1388 raise TypeError, 'new property "%s" not a list of ids'%key | |
| 1389 | |
| 1390 # clean up and validate the list of links | |
| 1391 link_class = self.properties[key].classname | |
| 1392 l = [] | |
| 1393 for entry in value: | |
| 1394 if type(entry) != type(''): | |
| 1395 raise ValueError, '"%s" multilink value (%r) '\ | |
| 1396 'must contain Strings'%(key, value) | |
| 1397 # if it isn't a number, it's a key | |
| 1398 if not num_re.match(entry): | |
| 1399 try: | |
| 1400 entry = self.db.classes[link_class].lookup(entry) | |
| 1401 except (TypeError, KeyError): | |
| 1402 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1403 key, entry, self.properties[key].classname) | |
| 1404 l.append(entry) | |
| 1405 value = l | |
| 1406 propvalues[key] = value | |
| 1407 | |
| 1408 # handle additions | |
| 1409 for nodeid in value: | |
| 1410 if not self.db.getclass(link_class).hasnode(nodeid): | |
| 1411 raise IndexError, '%s has no node %s'%(link_class, | |
| 1412 nodeid) | |
| 1413 # register the link with the newly linked node | |
| 1414 if self.do_journal and self.properties[key].do_journal: | |
| 1415 self.db.addjournal(link_class, nodeid, 'link', | |
| 1416 (self.classname, newid, key)) | |
| 1417 | |
| 1418 elif isinstance(prop, String): | |
|
1383
f19dde90e473
applied unicode patch
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1365
diff
changeset
|
1419 if type(value) != type('') and type(value) != type(u''): |
| 1165 | 1420 raise TypeError, 'new property "%s" not a string'%key |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1421 self.db.indexer.add_text((self.classname, newid, key), value) |
| 1165 | 1422 |
| 1423 elif isinstance(prop, Password): | |
| 1424 if not isinstance(value, password.Password): | |
| 1425 raise TypeError, 'new property "%s" not a Password'%key | |
| 1426 | |
| 1427 elif isinstance(prop, Date): | |
| 1428 if value is not None and not isinstance(value, date.Date): | |
| 1429 raise TypeError, 'new property "%s" not a Date'%key | |
| 1430 | |
| 1431 elif isinstance(prop, Interval): | |
| 1432 if value is not None and not isinstance(value, date.Interval): | |
| 1433 raise TypeError, 'new property "%s" not an Interval'%key | |
| 1434 | |
| 1435 elif value is not None and isinstance(prop, Number): | |
| 1436 try: | |
| 1437 float(value) | |
| 1438 except ValueError: | |
| 1439 raise TypeError, 'new property "%s" not numeric'%key | |
| 1440 | |
| 1441 elif value is not None and isinstance(prop, Boolean): | |
| 1442 try: | |
| 1443 int(value) | |
| 1444 except ValueError: | |
| 1445 raise TypeError, 'new property "%s" not boolean'%key | |
| 1446 | |
| 1447 # make sure there's data where there needs to be | |
| 1448 for key, prop in self.properties.items(): | |
| 1449 if propvalues.has_key(key): | |
| 1450 continue | |
| 1451 if key == self.key: | |
| 1452 raise ValueError, 'key property "%s" is required'%key | |
| 1453 if isinstance(prop, Multilink): | |
| 1454 propvalues[key] = [] | |
| 1455 else: | |
| 1456 propvalues[key] = None | |
| 1457 | |
| 1458 # done | |
| 1459 self.db.addnode(self.classname, newid, propvalues) | |
| 1460 if self.do_journal: | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1461 self.db.addjournal(self.classname, newid, 'create', {}) |
| 1165 | 1462 |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1463 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1464 return str(newid) |
| 1165 | 1465 |
| 1466 _marker = [] | |
| 1467 def get(self, nodeid, propname, default=_marker, cache=1): | |
| 1468 '''Get the value of a property on an existing node of this class. | |
| 1469 | |
| 1470 'nodeid' must be the id of an existing node of this class or an | |
| 1471 IndexError is raised. 'propname' must be the name of a property | |
| 1472 of this class or a KeyError is raised. | |
| 1473 | |
|
1780
d2801a2b0a77
Initial implementation (half-baked) at new Tracker instance.
Richard Jones <richard@users.sourceforge.net>
parents:
1751
diff
changeset
|
1474 'cache' exists for backwards compatibility, and is not used. |
| 1165 | 1475 ''' |
| 1476 if propname == 'id': | |
| 1477 return nodeid | |
| 1478 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1479 # 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
|
1480 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
|
1481 |
| 1165 | 1482 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
|
1483 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
|
1484 return d['creation'] |
| 1165 | 1485 else: |
| 1486 return date.Date() | |
| 1487 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
|
1488 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
|
1489 return d['activity'] |
| 1165 | 1490 else: |
| 1491 return date.Date() | |
| 1492 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
|
1493 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
|
1494 return d['creator'] |
| 1165 | 1495 else: |
|
1800
a3b1b1dcf639
Use getuid(), not figure_curuserid()
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1794
diff
changeset
|
1496 return self.db.getuid() |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1497 if propname == 'actor': |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1498 if d.has_key('actor'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1499 return d['actor'] |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1500 else: |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1501 return self.db.getuid() |
| 1165 | 1502 |
| 1503 # get the property (raises KeyErorr if invalid) | |
| 1504 prop = self.properties[propname] | |
| 1505 | |
| 1506 if not d.has_key(propname): | |
| 1507 if default is self._marker: | |
| 1508 if isinstance(prop, Multilink): | |
| 1509 return [] | |
| 1510 else: | |
| 1511 return None | |
| 1512 else: | |
| 1513 return default | |
| 1514 | |
| 1515 # don't pass our list to other code | |
| 1516 if isinstance(prop, Multilink): | |
| 1517 return d[propname][:] | |
| 1518 | |
| 1519 return d[propname] | |
| 1520 | |
| 1521 def set(self, nodeid, **propvalues): | |
| 1522 '''Modify a property on an existing node of this class. | |
| 1523 | |
| 1524 'nodeid' must be the id of an existing node of this class or an | |
| 1525 IndexError is raised. | |
| 1526 | |
| 1527 Each key in 'propvalues' must be the name of a property of this | |
| 1528 class or a KeyError is raised. | |
| 1529 | |
| 1530 All values in 'propvalues' must be acceptable types for their | |
| 1531 corresponding properties or a TypeError is raised. | |
| 1532 | |
| 1533 If the value of the key property is set, it must not collide with | |
| 1534 other key strings or a ValueError is raised. | |
| 1535 | |
| 1536 If the value of a Link or Multilink property contains an invalid | |
| 1537 node id, a ValueError is raised. | |
| 1538 ''' | |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1539 self.fireAuditors('set', nodeid, propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1540 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
|
1541 propvalues = self.set_inner(nodeid, **propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1542 self.fireReactors('set', nodeid, oldvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1543 return propvalues |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1544 |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1545 def set_inner(self, nodeid, **propvalues): |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1546 ''' 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
|
1547 ''' |
| 1165 | 1548 if not propvalues: |
| 1549 return propvalues | |
| 1550 | |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1551 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
|
1552 propvalues.has_key('actor') or propvalues.has_key('activity'): |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1553 raise KeyError, '"creation", "creator", "actor" and '\ |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
1554 '"activity" are reserved' |
| 1165 | 1555 |
| 1556 if propvalues.has_key('id'): | |
| 1557 raise KeyError, '"id" is reserved' | |
| 1558 | |
| 1559 if self.db.journaltag is None: | |
| 1560 raise DatabaseError, 'Database open read-only' | |
| 1561 | |
| 1562 node = self.db.getnode(self.classname, nodeid) | |
| 1563 if self.is_retired(nodeid): | |
| 1564 raise IndexError, 'Requested item is retired' | |
| 1565 num_re = re.compile('^\d+$') | |
| 1566 | |
| 1567 # if the journal value is to be different, store it in here | |
| 1568 journalvalues = {} | |
| 1569 | |
| 1570 # remember the add/remove stuff for multilinks, making it easier | |
| 1571 # for the Database layer to do its stuff | |
| 1572 multilink_changes = {} | |
| 1573 | |
| 1574 for propname, value in propvalues.items(): | |
| 1575 # check to make sure we're not duplicating an existing key | |
| 1576 if propname == self.key and node[propname] != value: | |
| 1577 try: | |
| 1578 self.lookup(value) | |
| 1579 except KeyError: | |
| 1580 pass | |
| 1581 else: | |
| 1582 raise ValueError, 'node with key "%s" exists'%value | |
| 1583 | |
| 1584 # this will raise the KeyError if the property isn't valid | |
| 1585 # ... we don't use getprops() here because we only care about | |
| 1586 # 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
|
1587 try: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1588 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
|
1589 except KeyError: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1590 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
|
1591 self.classname, propname) |
| 1165 | 1592 |
| 1593 # if the value's the same as the existing value, no sense in | |
| 1594 # doing anything | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1595 current = node.get(propname, None) |
|
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1596 if value == current: |
| 1165 | 1597 del propvalues[propname] |
| 1598 continue | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1599 journalvalues[propname] = current |
| 1165 | 1600 |
| 1601 # do stuff based on the prop type | |
| 1602 if isinstance(prop, Link): | |
| 1603 link_class = prop.classname | |
| 1604 # if it isn't a number, it's a key | |
| 1605 if value is not None and not isinstance(value, type('')): | |
| 1606 raise ValueError, 'property "%s" link value be a string'%( | |
| 1607 propname) | |
| 1608 if isinstance(value, type('')) and not num_re.match(value): | |
| 1609 try: | |
| 1610 value = self.db.classes[link_class].lookup(value) | |
| 1611 except (TypeError, KeyError): | |
| 1612 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1613 propname, value, prop.classname) | |
| 1614 | |
| 1615 if (value is not None and | |
| 1616 not self.db.getclass(link_class).hasnode(value)): | |
| 1617 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 1618 | |
| 1619 if self.do_journal and prop.do_journal: | |
| 1620 # register the unlink with the old linked node | |
| 1621 if node[propname] is not None: | |
| 1622 self.db.addjournal(link_class, node[propname], 'unlink', | |
| 1623 (self.classname, nodeid, propname)) | |
| 1624 | |
| 1625 # register the link with the newly linked node | |
| 1626 if value is not None: | |
| 1627 self.db.addjournal(link_class, value, 'link', | |
| 1628 (self.classname, nodeid, propname)) | |
| 1629 | |
| 1630 elif isinstance(prop, Multilink): | |
| 1631 if type(value) != type([]): | |
| 1632 raise TypeError, 'new property "%s" not a list of'\ | |
| 1633 ' ids'%propname | |
| 1634 link_class = self.properties[propname].classname | |
| 1635 l = [] | |
| 1636 for entry in value: | |
| 1637 # if it isn't a number, it's a key | |
| 1638 if type(entry) != type(''): | |
| 1639 raise ValueError, 'new property "%s" link value ' \ | |
| 1640 'must be a string'%propname | |
| 1641 if not num_re.match(entry): | |
| 1642 try: | |
| 1643 entry = self.db.classes[link_class].lookup(entry) | |
| 1644 except (TypeError, KeyError): | |
| 1645 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1646 propname, entry, | |
| 1647 self.properties[propname].classname) | |
| 1648 l.append(entry) | |
| 1649 value = l | |
| 1650 propvalues[propname] = value | |
| 1651 | |
| 1652 # figure the journal entry for this property | |
| 1653 add = [] | |
| 1654 remove = [] | |
| 1655 | |
| 1656 # handle removals | |
| 1657 if node.has_key(propname): | |
| 1658 l = node[propname] | |
| 1659 else: | |
| 1660 l = [] | |
| 1661 for id in l[:]: | |
| 1662 if id in value: | |
| 1663 continue | |
| 1664 # register the unlink with the old linked node | |
| 1665 if self.do_journal and self.properties[propname].do_journal: | |
| 1666 self.db.addjournal(link_class, id, 'unlink', | |
| 1667 (self.classname, nodeid, propname)) | |
| 1668 l.remove(id) | |
| 1669 remove.append(id) | |
| 1670 | |
| 1671 # handle additions | |
| 1672 for id in value: | |
| 1673 if not self.db.getclass(link_class).hasnode(id): | |
| 1674 raise IndexError, '%s has no node %s'%(link_class, id) | |
| 1675 if id in l: | |
| 1676 continue | |
| 1677 # register the link with the newly linked node | |
| 1678 if self.do_journal and self.properties[propname].do_journal: | |
| 1679 self.db.addjournal(link_class, id, 'link', | |
| 1680 (self.classname, nodeid, propname)) | |
| 1681 l.append(id) | |
| 1682 add.append(id) | |
| 1683 | |
| 1684 # figure the journal entry | |
| 1685 l = [] | |
| 1686 if add: | |
| 1687 l.append(('+', add)) | |
| 1688 if remove: | |
| 1689 l.append(('-', remove)) | |
| 1690 multilink_changes[propname] = (add, remove) | |
| 1691 if l: | |
| 1692 journalvalues[propname] = tuple(l) | |
| 1693 | |
| 1694 elif isinstance(prop, String): | |
|
1383
f19dde90e473
applied unicode patch
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1365
diff
changeset
|
1695 if value is not None and type(value) != type('') and type(value) != type(u''): |
| 1165 | 1696 raise TypeError, 'new property "%s" not a string'%propname |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1697 self.db.indexer.add_text((self.classname, nodeid, propname), |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
1698 value) |
| 1165 | 1699 |
| 1700 elif isinstance(prop, Password): | |
| 1701 if not isinstance(value, password.Password): | |
| 1702 raise TypeError, 'new property "%s" not a Password'%propname | |
| 1703 propvalues[propname] = value | |
| 1704 | |
| 1705 elif value is not None and isinstance(prop, Date): | |
| 1706 if not isinstance(value, date.Date): | |
| 1707 raise TypeError, 'new property "%s" not a Date'% propname | |
| 1708 propvalues[propname] = value | |
| 1709 | |
| 1710 elif value is not None and isinstance(prop, Interval): | |
| 1711 if not isinstance(value, date.Interval): | |
| 1712 raise TypeError, 'new property "%s" not an '\ | |
| 1713 'Interval'%propname | |
| 1714 propvalues[propname] = value | |
| 1715 | |
| 1716 elif value is not None and isinstance(prop, Number): | |
| 1717 try: | |
| 1718 float(value) | |
| 1719 except ValueError: | |
| 1720 raise TypeError, 'new property "%s" not numeric'%propname | |
| 1721 | |
| 1722 elif value is not None and isinstance(prop, Boolean): | |
| 1723 try: | |
| 1724 int(value) | |
| 1725 except ValueError: | |
| 1726 raise TypeError, 'new property "%s" not boolean'%propname | |
| 1727 | |
| 1728 # nothing to do? | |
| 1729 if not propvalues: | |
| 1730 return propvalues | |
| 1731 | |
| 1732 # 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
|
1733 self.db.setnode(self.classname, nodeid, propvalues, multilink_changes) |
| 1165 | 1734 |
| 1735 if self.do_journal: | |
|
1304
61ad556cfc8d
working toward 0.5.2 release
Richard Jones <richard@users.sourceforge.net>
parents:
1295
diff
changeset
|
1736 self.db.addjournal(self.classname, nodeid, 'set', journalvalues) |
| 1165 | 1737 |
| 1738 return propvalues | |
| 1739 | |
| 1740 def retire(self, nodeid): | |
| 1741 '''Retire a node. | |
| 1742 | |
| 1743 The properties on the node remain available from the get() method, | |
| 1744 and the node's id is never reused. | |
| 1745 | |
| 1746 Retired nodes are not returned by the find(), list(), or lookup() | |
| 1747 methods, and other nodes may reuse the values of their key properties. | |
| 1748 ''' | |
| 1749 if self.db.journaltag is None: | |
| 1750 raise DatabaseError, 'Database open read-only' | |
| 1751 | |
|
1345
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1752 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
|
1753 |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1754 # 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
|
1755 # conversion (hello, sqlite) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1756 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
|
1757 self.db.arg, self.db.arg) |
| 1165 | 1758 if __debug__: |
| 1759 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
|
1760 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
|
1761 if self.do_journal: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1762 self.db.addjournal(self.classname, nodeid, 'retired', None) |
| 1165 | 1763 |
|
1345
618aa9c37d65
fire auditors and reactors in rdbms retire (thanks Sheila King)
Richard Jones <richard@users.sourceforge.net>
parents:
1333
diff
changeset
|
1764 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
|
1765 |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1766 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
|
1767 '''Restore a retired node. |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1768 |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1769 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
|
1770 ''' |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1771 if self.db.journaltag is None: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1772 raise DatabaseError, 'Database open read-only' |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1773 |
|
1523
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1774 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
|
1775 # 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
|
1776 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
|
1777 try: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1778 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
|
1779 except KeyError: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1780 pass |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1781 else: |
|
63aa7be52d2c
checked to make sure that the restored item doesn't clash...
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1519
diff
changeset
|
1782 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
|
1783 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
|
1784 |
|
1519
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1785 self.fireAuditors('restore', nodeid, None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1786 # 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
|
1787 # conversion (hello, sqlite) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1788 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
|
1789 self.db.arg, self.db.arg) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1790 if __debug__: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1791 print >>hyperdb.DEBUG, 'restore', (self, sql, nodeid) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1792 self.db.cursor.execute(sql, (0, nodeid)) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1793 if self.do_journal: |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1794 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
|
1795 |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1796 self.fireReactors('restore', nodeid, None) |
|
6fede2aa6a12
added ability to restore retired nodes
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1515
diff
changeset
|
1797 |
| 1165 | 1798 def is_retired(self, nodeid): |
| 1799 '''Return true if the node is rerired | |
| 1800 ''' | |
| 1801 sql = 'select __retired__ from _%s where id=%s'%(self.classname, | |
| 1802 self.db.arg) | |
| 1803 if __debug__: | |
| 1804 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
|
1805 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
|
1806 return int(self.db.sql_fetchone()[0]) |
| 1165 | 1807 |
| 1808 def destroy(self, nodeid): | |
| 1809 '''Destroy a node. | |
| 1810 | |
| 1811 WARNING: this method should never be used except in extremely rare | |
| 1812 situations where there could never be links to the node being | |
| 1813 deleted | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1814 |
| 1165 | 1815 WARNING: use retire() instead |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1816 |
| 1165 | 1817 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
|
1818 |
| 1165 | 1819 WARNING: really, use retire() instead |
| 1820 | |
| 1821 Well, I think that's enough warnings. This method exists mostly to | |
| 1822 support the session storage of the cgi interface. | |
| 1823 | |
| 1824 The node is completely removed from the hyperdb, including all journal | |
| 1825 entries. It will no longer be available, and will generally break code | |
| 1826 if there are any references to the node. | |
| 1827 ''' | |
| 1828 if self.db.journaltag is None: | |
| 1829 raise DatabaseError, 'Database open read-only' | |
| 1830 self.db.destroynode(self.classname, nodeid) | |
| 1831 | |
| 1832 def history(self, nodeid): | |
| 1833 '''Retrieve the journal of edits on a particular node. | |
| 1834 | |
| 1835 'nodeid' must be the id of an existing node of this class or an | |
| 1836 IndexError is raised. | |
| 1837 | |
| 1838 The returned list contains tuples of the form | |
| 1839 | |
|
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
|
1840 (nodeid, date, tag, action, params) |
| 1165 | 1841 |
| 1842 'date' is a Timestamp object specifying the time of the change and | |
| 1843 'tag' is the journaltag specified when the database was opened. | |
| 1844 ''' | |
| 1845 if not self.do_journal: | |
| 1846 raise ValueError, 'Journalling is disabled for this class' | |
| 1847 return self.db.getjournal(self.classname, nodeid) | |
| 1848 | |
| 1849 # Locating nodes: | |
| 1850 def hasnode(self, nodeid): | |
| 1851 '''Determine if the given nodeid actually exists | |
| 1852 ''' | |
| 1853 return self.db.hasnode(self.classname, nodeid) | |
| 1854 | |
| 1855 def setkey(self, propname): | |
| 1856 '''Select a String property of this class to be the key property. | |
| 1857 | |
| 1858 'propname' must be the name of a String property of this class or | |
| 1859 None, or a TypeError is raised. The values of the key property on | |
| 1860 all existing nodes must be unique or a ValueError is raised. | |
| 1861 ''' | |
| 1862 prop = self.getprops()[propname] | |
| 1863 if not isinstance(prop, String): | |
| 1864 raise TypeError, 'key properties must be String' | |
| 1865 self.key = propname | |
| 1866 | |
| 1867 def getkey(self): | |
| 1868 '''Return the name of the key property for this class or None.''' | |
| 1869 return self.key | |
| 1870 | |
| 1871 def labelprop(self, default_to_id=0): | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1872 '''Return the property name for a label for the given node. |
| 1165 | 1873 |
| 1874 This method attempts to generate a consistent label for the node. | |
| 1875 It tries the following in order: | |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1876 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1877 1. key property |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1878 2. "name" property |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1879 3. "title" property |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
1880 4. first property from the sorted property name list |
| 1165 | 1881 ''' |
| 1882 k = self.getkey() | |
| 1883 if k: | |
| 1884 return k | |
| 1885 props = self.getprops() | |
| 1886 if props.has_key('name'): | |
| 1887 return 'name' | |
| 1888 elif props.has_key('title'): | |
| 1889 return 'title' | |
| 1890 if default_to_id: | |
| 1891 return 'id' | |
| 1892 props = props.keys() | |
| 1893 props.sort() | |
| 1894 return props[0] | |
| 1895 | |
| 1896 def lookup(self, keyvalue): | |
| 1897 '''Locate a particular node by its key property and return its id. | |
| 1898 | |
| 1899 If this class has no key property, a TypeError is raised. If the | |
| 1900 'keyvalue' matches one of the values for the key property among | |
| 1901 the nodes in this class, the matching node's id is returned; | |
| 1902 otherwise a KeyError is raised. | |
| 1903 ''' | |
| 1904 if not self.key: | |
| 1905 raise TypeError, 'No key property set for class %s'%self.classname | |
| 1906 | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1907 # 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
|
1908 # sqlite) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1909 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
|
1910 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
|
1911 self.db.sql(sql, (keyvalue, 1)) |
| 1165 | 1912 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1913 # 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
|
1914 row = self.db.sql_fetchone() |
|
735adcbfc665
fix to SQL lookup() and retirement
Richard Jones <richard@users.sourceforge.net>
parents:
1195
diff
changeset
|
1915 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
|
1916 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
|
1917 keyvalue, self.classname) |
| 1165 | 1918 |
| 1919 # return the id | |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1920 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
1921 return str(row[0]) |
| 1165 | 1922 |
| 1923 def find(self, **propspec): | |
| 1924 '''Get the ids of nodes in this class which link to the given nodes. | |
| 1925 | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1926 '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
|
1927 propname={nodeid:1, } |
| 1165 | 1928 '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
|
1929 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
|
1930 Multilink property, or a TypeError is raised. |
| 1165 | 1931 |
| 1932 Any node in this class whose 'propname' property links to any of the | |
| 1933 nodeids will be returned. Used by the full text indexing, which knows | |
| 1934 that "foo" occurs in msg1, msg3 and file7, so we have hits on these | |
| 1935 issues: | |
| 1936 | |
| 1937 db.issue.find(messages={'1':1,'3':1}, files={'7':1}) | |
| 1938 ''' | |
| 1939 if __debug__: | |
| 1940 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
|
1941 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1942 # shortcut |
| 1165 | 1943 if not propspec: |
| 1944 return [] | |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1945 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1946 # validate the args |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1947 props = self.getprops() |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1948 propspec = propspec.items() |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1949 for propname, nodeids in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1950 # check the prop is OK |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1951 prop = props[propname] |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1952 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
|
1953 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
|
1954 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1955 # first, links |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1956 a = self.db.arg |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1957 allvalues = () |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1958 sql = [] |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1959 where = [] |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1960 for prop, values in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1961 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
|
1962 continue |
|
1912
2b0ab61db194
fixes for [SF#818339]
Richard Jones <richard@users.sourceforge.net>
parents:
1911
diff
changeset
|
1963 if type(values) is type({}) and len(values) == 1: |
|
2b0ab61db194
fixes for [SF#818339]
Richard Jones <richard@users.sourceforge.net>
parents:
1911
diff
changeset
|
1964 values = values.keys()[0] |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1965 if type(values) is type(''): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1966 allvalues += (values,) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1967 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
|
1968 elif values is None: |
|
e2a8ce4d2317
Class.find() may now find unset Links [SF#700620]
Richard Jones <richard@users.sourceforge.net>
parents:
1557
diff
changeset
|
1969 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
|
1970 else: |
|
2495
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1971 values = values.keys() |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1972 s = '' |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1973 if None in values: |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1974 values.remove(None) |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1975 s = '_%s is NULL or '%prop |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1976 allvalues += tuple(values) |
|
8ff455218ec2
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2482
diff
changeset
|
1977 s += '_%s in (%s)'%(prop, ','.join([a]*len(values))) |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
1978 where.append('(' + s +')') |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1979 if where: |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1980 allvalues = (1, ) + allvalues |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1981 sql.append('''select id from _%s where __retired__ <> %s |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1982 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
|
1983 |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1984 # now multilinks |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1985 for prop, values in propspec: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1986 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
|
1987 continue |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1988 if not values: |
|
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1989 continue |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1990 allvalues += (1, ) |
|
1222
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1991 if type(values) is type(''): |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1992 allvalues += (values,) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1993 s = a |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1994 else: |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1995 allvalues += tuple(values.keys()) |
|
bc3bc3248dd1
added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents:
1212
diff
changeset
|
1996 s = ','.join([a]*len(values)) |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
1997 tn = '%s_%s'%(self.classname, prop) |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1998 sql.append('''select id from _%s, %s where __retired__ <> %s |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
1999 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
|
2000 tn, a, tn, tn, s)) |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2001 |
|
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2002 if not sql: |
|
1988
5660b89f8903
more compliance testing, this time for find()
Richard Jones <richard@users.sourceforge.net>
parents:
1986
diff
changeset
|
2003 return [] |
|
2734
1e70e58cf763
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2732
diff
changeset
|
2004 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
|
2005 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
|
2006 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2007 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
|
2008 if __debug__: |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2009 print >>hyperdb.DEBUG, 'find ... ', l |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2010 return l |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2011 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2012 def stringFind(self, **requirements): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2013 '''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
|
2014 properties in a caseless search. |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2015 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2016 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
|
2017 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2018 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
|
2019 ''' |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2020 where = [] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2021 args = [] |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2022 for propname in requirements.keys(): |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2023 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
|
2024 if not isinstance(prop, String): |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2025 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
|
2026 where.append(propname) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2027 args.append(requirements[propname].lower()) |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2028 |
|
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2029 # generate the where clause |
|
1549
a53a7e197360
fixed rdbms email address lookup (case insensitivity)
Richard Jones <richard@users.sourceforge.net>
parents:
1539
diff
changeset
|
2030 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
|
2031 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
|
2032 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
|
2033 args.append(1) |
|
1195
e0032f4ab334
added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents:
1189
diff
changeset
|
2034 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
|
2035 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2036 l = [str(x[0]) for x in self.db.sql_fetchall()] |
| 1165 | 2037 if __debug__: |
| 2038 print >>hyperdb.DEBUG, 'find ... ', l | |
| 2039 return l | |
| 2040 | |
| 2041 def list(self): | |
| 2042 ''' Return a list of the ids of the active nodes in this class. | |
| 2043 ''' | |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2044 return self.getnodeids(retired=0) |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2045 |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2046 def getnodeids(self, retired=None): |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2047 ''' 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
|
2048 |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2049 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
|
2050 retired or non-retired nodes, depending on the flag. |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2051 ''' |
|
1707
3d627e34f18e
sqlite backend now passes all tests under 2.3.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1599
diff
changeset
|
2052 # 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
|
2053 if retired is not None: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2054 if retired: |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2055 args = (0, ) |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2056 else: |
|
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1707
diff
changeset
|
2057 args = (1, ) |
|
1539
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2058 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
|
2059 self.db.arg) |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2060 else: |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2061 args = () |
|
800b5896e14a
fixed rdbms export - getnodeids in particular with NULL values
Richard Jones <richard@users.sourceforge.net>
parents:
1528
diff
changeset
|
2062 sql = 'select id from _%s'%self.classname |
|
1484
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2063 if __debug__: |
|
b3f2484babce
fixes to import/export
Richard Jones <richard@users.sourceforge.net>
parents:
1479
diff
changeset
|
2064 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
|
2065 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
|
2066 # XXX numeric ids |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2067 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
|
2068 return ids |
| 1165 | 2069 |
|
1249
6c24a86a12ae
Fixes for SourceForge tracker bugs.
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
2070 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
|
2071 group=(None,None)): |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2072 '''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
|
2073 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
|
2074 sort spec |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2075 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2076 "filterspec" is {propname: value(s)} |
| 1165 | 2077 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2078 "sort" and "group" are (dir, prop) where dir is '+', '-' or None |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2079 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
|
2080 |
|
2363
c69b67905043
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2321
diff
changeset
|
2081 "search_matches" is {nodeid: marker} or None |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2082 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2083 The filter must match all properties specificed - but if the |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1988
diff
changeset
|
2084 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
|
2085 list may match for that property to match. |
| 1165 | 2086 ''' |
|
2579
ead9f926234a
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2537
diff
changeset
|
2087 # 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
|
2088 if search_matches == {}: |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
2089 return [] |
|
0df08772f166
fix to SQL filtering
Richard Jones <richard@users.sourceforge.net>
parents:
1203
diff
changeset
|
2090 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2091 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2092 start_t = time.time() |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2093 |
| 1165 | 2094 cn = self.classname |
| 2095 | |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2096 timezone = self.db.getUserTimezone() |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2097 |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2098 # vars to hold the components of the SQL statement |
|
2537
5455dd2ec6d7
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2535
diff
changeset
|
2099 frum = [] # FROM clauses |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2100 loj = [] # LEFT OUTER JOIN clauses |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2101 where = [] # WHERE clauses |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2102 args = [] # *any* positional arguments |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2103 a = self.db.arg |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2104 |
| 1165 | 2105 # figure the WHERE clause from the filterspec |
| 2106 props = self.getprops() | |
|
2256
0b198ed096af
fixes for py2.1 (booleans, sigh)
Richard Jones <richard@users.sourceforge.net>
parents:
2248
diff
changeset
|
2107 mlfilt = 0 # are we joining with Multilink tables? |
| 1165 | 2108 for k, v in filterspec.items(): |
| 2109 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
|
2110 # now do other where clause stuff |
| 1165 | 2111 if isinstance(propclass, Multilink): |
|
2256
0b198ed096af
fixes for py2.1 (booleans, sigh)
Richard Jones <richard@users.sourceforge.net>
parents:
2248
diff
changeset
|
2112 mlfilt = 1 |
| 1165 | 2113 tn = '%s_%s'%(cn, k) |
|
1556
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
2114 if v in ('-1', ['-1']): |
|
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
2115 # 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
|
2116 # corresponding multilink table) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2117 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
|
2118 tn)) |
|
1556
9e89f5394f6c
handle myriad of argument types to filter()
Richard Jones <richard@users.sourceforge.net>
parents:
1555
diff
changeset
|
2119 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
|
2120 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
|
2121 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2122 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
|
2123 tn, tn, s)) |
| 1165 | 2124 args = args + v |
| 2125 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
|
2126 frum.append(tn) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2127 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
|
2128 tn, a)) |
| 1165 | 2129 args.append(v) |
|
1365
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2130 elif k == 'id': |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2131 if isinstance(v, type([])): |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2132 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2133 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
|
2134 args = args + v |
|
4884fb0860f9
fixed rdbms searching by ID [SF#666615]
Richard Jones <richard@users.sourceforge.net>
parents:
1351
diff
changeset
|
2135 else: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2136 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
|
2137 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
|
2138 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
|
2139 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
|
2140 v = [v] |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2141 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2142 # 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
|
2143 # 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
|
2144 # 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
|
2145 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
|
2146 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2147 # now add to the where clause |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2148 where.append('(' |
|
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2149 +' 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
|
2150 +')') |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2151 # 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
|
2152 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
|
2153 if isinstance(v, type([])): |
|
2509
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2154 d = {} |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2155 for entry in v: |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2156 if entry == '-1': |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2157 entry = None |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2158 d[entry] = entry |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2159 l = [] |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2160 if d.has_key(None) or not d: |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2161 del d[None] |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2162 l.append('_%s._%s is NULL'%(cn, k)) |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2163 if d: |
|
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2164 v = d.keys() |
|
1295
9c3459cb8ab6
work-around for sqlite bug
Richard Jones <richard@users.sourceforge.net>
parents:
1252
diff
changeset
|
2165 s = ','.join([a for x in v]) |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2166 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
|
2167 args = args + v |
|
2535
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2168 if l: |
|
50da6d3bac46
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2513
diff
changeset
|
2169 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
|
2170 else: |
|
2509
ea887c804103
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2506
diff
changeset
|
2171 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
|
2172 v = None |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2173 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
|
2174 else: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2175 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
|
2176 args.append(v) |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2177 elif isinstance(propclass, Date): |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2093
diff
changeset
|
2178 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
|
2179 if isinstance(v, type([])): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2180 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2181 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
|
2182 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
|
2183 else: |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2184 try: |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2185 # 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
|
2186 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
|
2187 if date_rng.from_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2188 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
|
2189 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
|
2190 if date_rng.to_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2191 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
|
2192 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
|
2193 except ValueError: |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1496
diff
changeset
|
2194 # 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
|
2195 pass |
|
1351
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2196 elif isinstance(propclass, Interval): |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2197 # 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
|
2198 if isinstance(v, type([])): |
|
d1bfb479e527
fixed searching on date / interval fields [SF#658157]
Richard Jones <richard@users.sourceforge.net>
parents:
1345
diff
changeset
|
2199 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2200 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
|
2201 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
|
2202 else: |
|
1596
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2203 try: |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2204 # 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
|
2205 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
|
2206 if date_rng.from_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2207 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
|
2208 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
|
2209 if date_rng.to_value: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2210 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
|
2211 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
|
2212 except ValueError: |
|
33a0d94c7658
searching on ranges of intervals is implemented
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1578
diff
changeset
|
2213 # 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
|
2214 pass |
| 1165 | 2215 else: |
| 2216 if isinstance(v, type([])): | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2217 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2218 where.append('_%s._%s in (%s)'%(cn, k, s)) |
| 1165 | 2219 args = args + v |
| 2220 else: | |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2221 where.append('_%s._%s=%s'%(cn, k, a)) |
| 1165 | 2222 args.append(v) |
| 2223 | |
|
1740
5ca448ff8052
don't have RDBMS backends list retired nodes [SF#767319]
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
2224 # don't match retired nodes |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2225 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
|
2226 |
| 1165 | 2227 # add results of full text search |
| 2228 if search_matches is not None: | |
| 2229 v = search_matches.keys() | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2230 s = ','.join([a for x in v]) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2231 where.append('_%s.id in (%s)'%(cn, s)) |
| 1165 | 2232 args = args + v |
| 2233 | |
|
2722
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2234 # sanity check: sorting *and* grouping on the same property? |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2235 if group[1] == sort[1]: |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2236 sort = (None, None) |
|
8140fb128088
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2604
diff
changeset
|
2237 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2238 # "grouping" is just the first-order sorting in the SQL fetch |
| 1165 | 2239 orderby = [] |
| 2240 ordercols = [] | |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2241 mlsort = [] |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2242 for sortby in group, sort: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2243 sdir, prop = sortby |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2244 if sdir and prop: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2245 if isinstance(props[prop], Multilink): |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2246 mlsort.append(sortby) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2247 continue |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2248 elif isinstance(props[prop], Interval): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2249 # 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
|
2250 o = '__'+prop+'_int__' |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2251 ordercols.append(o) |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2252 elif isinstance(props[prop], Link): |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2253 # 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
|
2254 lcn = props[prop].classname |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2255 link = self.db.classes[lcn] |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2256 o = '_%s._%s'%(cn, prop) |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2257 if link.getprops().has_key('order'): |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2258 tn = '_' + lcn |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2259 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
|
2260 o, tn)) |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2261 ordercols.append(tn + '._order') |
|
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2262 o = tn + '._order' |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2263 else: |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2264 ordercols.append(o) |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2265 elif prop == 'id': |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2266 o = '_%s.id'%cn |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2267 else: |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2268 o = '_%s._%s'%(cn, prop) |
|
2185
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2269 ordercols.append(o) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2270 if sdir == '-': |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2271 o += ' desc' |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2272 orderby.append(o) |
| 1165 | 2273 |
| 2274 # construct the SQL | |
|
2537
5455dd2ec6d7
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2535
diff
changeset
|
2275 frum.append('_'+cn) |
| 1165 | 2276 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
|
2277 if where: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2278 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
|
2279 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2280 where = '' |
|
2240
ad4b717e12b9
Small optimisation to only use "select distinct(id) ..."...
Richard Jones <richard@users.sourceforge.net>
parents:
2237
diff
changeset
|
2281 if mlfilt: |
|
ad4b717e12b9
Small optimisation to only use "select distinct(id) ..."...
Richard Jones <richard@users.sourceforge.net>
parents:
2237
diff
changeset
|
2282 # 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
|
2283 # don't distinct() |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2284 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
|
2285 else: |
|
2317
4fd7a1805544
fix anydbm sort/group direction handling...
Richard Jones <richard@users.sourceforge.net>
parents:
2274
diff
changeset
|
2286 cols = ['_%s.id'%cn] |
| 1165 | 2287 if orderby: |
| 2288 cols = cols + ordercols | |
| 2289 order = ' order by %s'%(','.join(orderby)) | |
| 2290 else: | |
| 2291 order = '' | |
| 2292 cols = ','.join(cols) | |
|
2380
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2293 loj = ' '.join(loj) |
|
1ccfcfeca61b
backport from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2363
diff
changeset
|
2294 sql = 'select %s from %s %s %s%s'%(cols, frum, loj, where, order) |
| 1165 | 2295 args = tuple(args) |
| 2296 if __debug__: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2297 print >>hyperdb.DEBUG, 'filter', (self, sql, args) |
|
2321
c075edcc8153
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2317
diff
changeset
|
2298 __traceback_info__ = (sql, args) |
|
1906
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2299 if args: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2300 self.db.cursor.execute(sql, args) |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2301 else: |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2302 # psycopg doesn't like empty args |
|
f255363e6d97
PostgreSQL backend lands.
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
2303 self.db.cursor.execute(sql) |
|
1911
f5c804379c85
fixed ZRoundup - mostly changes to classic template
Richard Jones <richard@users.sourceforge.net>
parents:
1906
diff
changeset
|
2304 l = self.db.sql_fetchall() |
| 1165 | 2305 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
2306 # 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
|
2307 # XXX numeric ids |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2308 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
|
2309 |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2310 if not mlsort: |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2311 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2312 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
|
2313 return l |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2314 |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2315 # 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
|
2316 r = [] |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2317 for id in l: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2318 m = [] |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2319 for ml in mlsort: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2320 m.append(self.get(id, ml[1])) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2321 r.append((id, m)) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2322 i = 0 |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2323 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
|
2324 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
|
2325 if dir == '-': |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2326 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
|
2327 else: |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2328 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
|
2329 r.sort(sortfun) |
|
c52a931879c4
sort/group by multilink in RDBMS
Richard Jones <richard@users.sourceforge.net>
parents:
2175
diff
changeset
|
2330 i += 1 |
|
2237
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2331 r = [i[0] for i in r] |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2332 |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2333 if __debug__: |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2334 self.db.stats['filtering'] += (time.time() - start_t) |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2335 |
|
f624fc20f8fe
added capturing of stats
Richard Jones <richard@users.sourceforge.net>
parents:
2234
diff
changeset
|
2336 return r |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
2337 |
| 1165 | 2338 def count(self): |
| 2339 '''Get the number of nodes in this class. | |
| 2340 | |
| 2341 If the returned integer is 'numnodes', the ids of all the nodes | |
| 2342 in this class run from 1 to numnodes, and numnodes+1 will be the | |
| 2343 id of the next node to be created in this class. | |
| 2344 ''' | |
| 2345 return self.db.countnodes(self.classname) | |
| 2346 | |
| 2347 # Manipulating properties: | |
| 2348 def getprops(self, protected=1): | |
| 2349 '''Return a dictionary mapping property names to property objects. | |
| 2350 If the "protected" flag is true, we include protected properties - | |
| 2351 those which may not be modified. | |
| 2352 ''' | |
| 2353 d = self.properties.copy() | |
| 2354 if protected: | |
| 2355 d['id'] = String() | |
| 2356 d['creation'] = hyperdb.Date() | |
| 2357 d['activity'] = hyperdb.Date() | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
2358 d['creator'] = hyperdb.Link('user') |
|
2077
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
2359 d['actor'] = hyperdb.Link('user') |
| 1165 | 2360 return d |
| 2361 | |
| 2362 def addprop(self, **properties): | |
| 2363 '''Add properties to this class. | |
| 2364 | |
| 2365 The keyword arguments in 'properties' must map names to property | |
| 2366 objects, or a TypeError is raised. None of the keys in 'properties' | |
| 2367 may collide with the names of existing properties, or a ValueError | |
| 2368 is raised before any properties have been added. | |
| 2369 ''' | |
| 2370 for key in properties.keys(): | |
| 2371 if self.properties.has_key(key): | |
| 2372 raise ValueError, key | |
| 2373 self.properties.update(properties) | |
| 2374 | |
| 2375 def index(self, nodeid): | |
| 2376 '''Add (or refresh) the node to search indexes | |
| 2377 ''' | |
| 2378 # find all the String properties that have indexme | |
| 2379 for prop, propclass in self.getprops().items(): | |
| 2380 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
|
2381 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
|
2382 str(self.get(nodeid, prop))) |
| 1165 | 2383 |
| 2384 | |
| 2385 # | |
| 2386 # Detector interface | |
| 2387 # | |
| 2388 def audit(self, event, detector): | |
| 2389 '''Register a detector | |
| 2390 ''' | |
| 2391 l = self.auditors[event] | |
| 2392 if detector not in l: | |
| 2393 self.auditors[event].append(detector) | |
| 2394 | |
| 2395 def fireAuditors(self, action, nodeid, newvalues): | |
| 2396 '''Fire all registered auditors. | |
| 2397 ''' | |
| 2398 for audit in self.auditors[action]: | |
| 2399 audit(self.db, self, nodeid, newvalues) | |
| 2400 | |
| 2401 def react(self, event, detector): | |
| 2402 '''Register a detector | |
| 2403 ''' | |
| 2404 l = self.reactors[event] | |
| 2405 if detector not in l: | |
| 2406 self.reactors[event].append(detector) | |
| 2407 | |
| 2408 def fireReactors(self, action, nodeid, oldvalues): | |
| 2409 '''Fire all registered reactors. | |
| 2410 ''' | |
| 2411 for react in self.reactors[action]: | |
| 2412 react(self.db, self, nodeid, oldvalues) | |
| 2413 | |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2414 # |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2415 # 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
|
2416 # |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2417 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
|
2418 ''' 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
|
2419 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
|
2420 ''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2421 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
|
2422 l = [] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2423 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
|
2424 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
|
2425 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
|
2426 # "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
|
2427 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
|
2428 pass |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2429 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
|
2430 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
|
2431 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
|
2432 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
|
2433 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
|
2434 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
|
2435 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
|
2436 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
|
2437 return l |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2438 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2439 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
|
2440 ''' 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
|
2441 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
|
2442 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
|
2443 information. |
|
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 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
|
2446 ''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2447 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
|
2448 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
|
2449 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
|
2450 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2451 # 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
|
2452 d = {} |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2453 retire = 0 |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2454 if not "id" in propnames: |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2455 newid = self.db.newid(self.classname) |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2456 else: |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2457 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
|
2458 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
|
2459 # 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
|
2460 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
|
2461 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2462 # 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
|
2463 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
|
2464 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2465 # "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
|
2466 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
|
2467 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2468 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
|
2469 # 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
|
2470 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
|
2471 retire = 1 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2472 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2473 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
|
2474 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
|
2475 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2476 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2477 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
|
2478 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
|
2479 # 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
|
2480 continue |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2481 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
|
2482 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
|
2483 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
|
2484 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
|
2485 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
|
2486 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
|
2487 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
|
2488 value = pwd |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2489 d[propname] = value |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2490 if isinstance(prop, String) and prop.indexme: |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2491 if type(value) != type('') and type(value) != type(u''): |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2492 raise TypeError, 'new property "%s" not a string'%key |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2493 self.db.indexer.add_text((self.classname, newid, propname), |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2494 value) |
|
2175
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2495 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2496 # 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
|
2497 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
|
2498 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
|
2499 |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2500 # 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
|
2501 if not self.hasnode(newid): |
|
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2502 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
|
2503 else: |
|
2217
98d3bf8ffb19
store Intervals as two columns (and other fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2196
diff
changeset
|
2504 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
|
2505 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2506 # retire? |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2507 if retire: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2508 # 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
|
2509 # 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
|
2510 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
|
2511 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
|
2512 if __debug__: |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2513 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
|
2514 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
|
2515 return newid |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2516 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2517 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
|
2518 '''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
|
2519 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
|
2520 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2521 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
|
2522 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2523 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
|
2524 ''' |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2525 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
|
2526 r = [] |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2527 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
|
2528 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
|
2529 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
|
2530 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
|
2531 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
|
2532 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
|
2533 # 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
|
2534 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
|
2535 pass |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2536 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
|
2537 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
|
2538 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
|
2539 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
|
2540 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
|
2541 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
|
2542 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
|
2543 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
|
2544 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
|
2545 return r |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2546 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2547 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
|
2548 '''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
|
2549 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2550 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
|
2551 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
|
2552 d = {} |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2553 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
|
2554 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
|
2555 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
|
2556 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
|
2557 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
|
2558 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
|
2559 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
|
2560 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
|
2561 pass |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2562 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
|
2563 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
|
2564 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
|
2565 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
|
2566 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
|
2567 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
|
2568 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
|
2569 value = pwd |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2570 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
|
2571 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
|
2572 |
|
723098a10677
Export and import now include journals (incompatible with export < 0.7)
Richard Jones <richard@users.sourceforge.net>
parents:
2166
diff
changeset
|
2573 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
|
2574 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
|
2575 |
|
2598
5077745b0758
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2579
diff
changeset
|
2576 class FileClass(hyperdb.FileClass, Class): |
| 1165 | 2577 '''This class defines a large chunk of data. To support this, it has a |
| 2578 mandatory String property "content" which is typically saved off | |
| 2579 externally to the hyperdb. | |
| 2580 | |
| 2581 The default MIME type of this data is defined by the | |
| 2582 "default_mime_type" class attribute, which may be overridden by each | |
| 2583 node if the class defines a "type" String property. | |
| 2584 ''' | |
| 2585 default_mime_type = 'text/plain' | |
| 2586 | |
| 2587 def create(self, **propvalues): | |
| 2588 ''' snaffle the file propvalue and store in a file | |
| 2589 ''' | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2590 # 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
|
2591 # 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
|
2592 self.fireAuditors('create', None, propvalues) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2593 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2594 # now remove the content property so it's not stored in the db |
| 1165 | 2595 content = propvalues['content'] |
| 2596 del propvalues['content'] | |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2597 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2598 # do the database create |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2599 newid = self.create_inner(**propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2600 |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2601 # figure the mime type |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2602 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
|
2603 |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2604 # and index! |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2605 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
|
2606 mime_type) |
|
1431
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2607 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2608 # fire reactors |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2609 self.fireReactors('create', newid, None) |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2610 |
|
c70068162e64
Altered Class.create() and FileClass.create() methods...
Richard Jones <richard@users.sourceforge.net>
parents:
1417
diff
changeset
|
2611 # store off the content as a file |
| 1165 | 2612 self.db.storefile(self.classname, newid, None, content) |
| 2613 return newid | |
| 2614 | |
| 2615 _marker = [] | |
| 2616 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
|
2617 ''' 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
|
2618 |
|
d2801a2b0a77
Initial implementation (half-baked) at new Tracker instance.
Richard Jones <richard@users.sourceforge.net>
parents:
1751
diff
changeset
|
2619 'cache' exists for backwards compatibility, and is not used. |
| 1165 | 2620 ''' |
| 2621 poss_msg = 'Possibly a access right configuration problem.' | |
| 2622 if propname == 'content': | |
| 2623 try: | |
| 2624 return self.db.getfile(self.classname, nodeid, None) | |
| 2625 except IOError, (strerror): | |
| 2626 # BUG: by catching this we donot see an error in the log. | |
| 2627 return 'ERROR reading file: %s%s\n%s\n%s'%( | |
| 2628 self.classname, nodeid, poss_msg, strerror) | |
| 2629 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
|
2630 return Class.get(self, nodeid, propname, default) |
| 1165 | 2631 else: |
|
1780
d2801a2b0a77
Initial implementation (half-baked) at new Tracker instance.
Richard Jones <richard@users.sourceforge.net>
parents:
1751
diff
changeset
|
2632 return Class.get(self, nodeid, propname) |
| 1165 | 2633 |
| 2634 def getprops(self, protected=1): | |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2635 '''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
|
2636 provide the "content" property. If the "protected" flag is true, |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2637 we include protected properties - those which may not be |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2638 modified. |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2639 |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2640 Note that the content prop is indexed separately, hence no indexme. |
| 1165 | 2641 ''' |
| 2642 d = Class.getprops(self, protected=protected).copy() | |
| 2643 d['content'] = hyperdb.String() | |
| 2644 return d | |
| 2645 | |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2646 def set(self, itemid, **propvalues): |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2647 ''' 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
|
2648 ''' |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2649 self.fireAuditors('set', itemid, propvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2650 oldvalues = copy.deepcopy(self.db.getnode(self.classname, itemid)) |
| 1165 | 2651 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2652 # 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
|
2653 content = None |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2654 if propvalues.has_key('content'): |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2655 content = propvalues['content'] |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2656 del propvalues['content'] |
| 1165 | 2657 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2658 # do the database create |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2659 propvalues = self.set_inner(itemid, **propvalues) |
| 1165 | 2660 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2661 # do content? |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2662 if content: |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2663 # store and index |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2664 self.db.storefile(self.classname, itemid, None, content) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2665 mime_type = propvalues.get('type', self.get(itemid, 'type')) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2666 if not mime_type: |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2667 mime_type = self.default_mime_type |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2668 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
|
2669 content, mime_type) |
| 1165 | 2670 |
|
2089
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2671 # fire reactors |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2672 self.fireReactors('set', itemid, oldvalues) |
|
93f03c6714d8
A few big changes in this commit:
Richard Jones <richard@users.sourceforge.net>
parents:
2082
diff
changeset
|
2673 return propvalues |
| 1165 | 2674 |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2675 def index(self, nodeid): |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2676 '''Add (or refresh) the node to search indexes. |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2677 |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2678 Pass on the content-type property for the content property. |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2679 ''' |
|
2513
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2680 Class.index(self, nodeid) |
|
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2681 try: |
|
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2682 mime_type = self.get(nodeid, 'type', self.default_mime_type) |
|
7e823f8938e9
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2509
diff
changeset
|
2683 except KeyError: |
|
2506
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2684 mime_type = self.default_mime_type |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2685 self.db.indexer.add_text((self.classname, nodeid, 'content'), |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2686 str(self.get(nodeid, 'content')), mime_type) |
|
f89a84f881f0
merge from HEAD
Richard Jones <richard@users.sourceforge.net>
parents:
2499
diff
changeset
|
2687 |
| 1165 | 2688 # XXX deviation from spec - was called ItemClass |
| 2689 class IssueClass(Class, roundupdb.IssueClass): | |
| 2690 # Overridden methods: | |
| 2691 def __init__(self, db, classname, **properties): | |
| 2692 '''The newly-created class automatically includes the "messages", | |
| 2693 "files", "nosy", and "superseder" properties. If the 'properties' | |
| 2694 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
|
2695 "creation", "creator", "activity" or "actor" property, a ValueError |
|
3e0961d6d44d
Added the "actor" property.
Richard Jones <richard@users.sourceforge.net>
parents:
2076
diff
changeset
|
2696 is raised. |
| 1165 | 2697 ''' |
| 2698 if not properties.has_key('title'): | |
| 2699 properties['title'] = hyperdb.String(indexme='yes') | |
| 2700 if not properties.has_key('messages'): | |
| 2701 properties['messages'] = hyperdb.Multilink("msg") | |
| 2702 if not properties.has_key('files'): | |
| 2703 properties['files'] = hyperdb.Multilink("file") | |
| 2704 if not properties.has_key('nosy'): | |
| 2705 # note: journalling is turned off as it really just wastes | |
| 2706 # space. this behaviour may be overridden in an instance | |
| 2707 properties['nosy'] = hyperdb.Multilink("user", do_journal="no") | |
| 2708 if not properties.has_key('superseder'): | |
| 2709 properties['superseder'] = hyperdb.Multilink(classname) | |
| 2710 Class.__init__(self, db, classname, **properties) | |
| 2711 |
