annotate roundup/backends/back_gadfly.py @ 1222:bc3bc3248dd1

added Class.find() unit test, fixed implementations
author Richard Jones <richard@users.sourceforge.net>
date Thu, 26 Sep 2002 03:04:24 +0000
parents e0032f4ab334
children 8dd4f736370b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1222
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
1 # $Id: back_gadfly.py,v 1.27 2002-09-26 03:04:24 richard Exp $
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
2 __doc__ = '''
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
3 About Gadfly
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
4 ============
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
5
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
6 Gadfly is a collection of python modules that provides relational
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
7 database functionality entirely implemented in Python. It supports a
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
8 subset of the intergalactic standard RDBMS Structured Query Language
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
9 SQL.
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
10
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
11
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
12 Basic Structure
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
13 ===============
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
14
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
15 We map roundup classes to relational tables. Automatically detect schema
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
16 changes and modify the gadfly table schemas appropriately. Multilinks
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
17 (which represent a many-to-many relationship) are handled through
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
18 intermediate tables.
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
19
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
20 Journals are stored adjunct to the per-class tables.
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
22 Table names and columns have "_" prepended so the names can't
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
23 clash with restricted names (like "order"). Retirement is determined by the
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
24 __retired__ column being true.
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
25
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
26 All columns are defined as VARCHAR, since it really doesn't matter what
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
27 type they're defined as. We stuff all kinds of data in there ;) [as long as
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
28 it's marshallable, gadfly doesn't care]
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
29
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
30
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
31 Additional Instance Requirements
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
32 ================================
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
33
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
34 The instance configuration must specify where the database is. It does this
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
35 with GADFLY_DATABASE, which is used as the arguments to the gadfly.gadfly()
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
36 method:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
37
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
38 Using an on-disk database directly (not a good idea):
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
39 GADFLY_DATABASE = (database name, directory)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
40
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
41 Using a network database (much better idea):
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
42 GADFLY_DATABASE = (policy, password, address, port)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
43
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
44 Because multiple accesses directly to a gadfly database aren't handled, but
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
45 multiple network accesses are, it's strongly advised that the latter setup be
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
46 used.
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
47
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
48 '''
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
49
1222
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
50 # standard python modules
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
51 import sys, os, time, re, errno, weakref, copy
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
52
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
53 # roundup modules
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
54 from roundup import hyperdb, date, password, roundupdb, security
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
55 from roundup.hyperdb import String, Password, Date, Interval, Link, \
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
56 Multilink, DatabaseError, Boolean, Number
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
57
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
58 # basic RDBMS backen implementation
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
59 from roundup.backends import rdbms_common
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
60
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
61 # the all-important gadfly :)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
62 import gadfly
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
63 import gadfly.client
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
64 import gadfly.database
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
65
1222
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
66 class Database(rdbms_common.Database):
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
67 # char to use for positional arguments
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
68 arg = '?'
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
69
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
70 def open_connection(self):
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
71 db = getattr(self.config, 'GADFLY_DATABASE', ('database', self.dir))
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
72 if len(db) == 2:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
73 # ensure files are group readable and writable
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
74 os.umask(0002)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
75 try:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
76 self.conn = gadfly.gadfly(*db)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
77 except IOError, error:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
78 if error.errno != errno.ENOENT:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
79 raise
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
80 self.database_schema = {}
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
81 self.conn = gadfly.gadfly()
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
82 self.conn.startup(*db)
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
83 self.cursor = self.conn.cursor()
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
84 self.cursor.execute('create table schema (schema varchar)')
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
85 self.cursor.execute('create table ids (name varchar, num integer)')
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
86 else:
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
87 self.cursor = self.conn.cursor()
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
88 self.cursor.execute('select schema from schema')
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
89 self.database_schema = self.cursor.fetchone()[0]
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
90 else:
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
91 self.conn = gadfly.client.gfclient(*db)
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
92 self.database_schema = self.load_dbschema()
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
93
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
94 def __repr__(self):
1032
dd51e435fa9e *** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents: 1025
diff changeset
95 return '<roundfly 0x%x>'%id(self)
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
96
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
97 def sql_fetchone(self):
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
98 ''' Fetch a single row. If there's nothing to fetch, return None.
1156
d0da32fbdedc gadfly backend now complete
Richard Jones <richard@users.sourceforge.net>
parents: 1145
diff changeset
99 '''
1145
81941abedb0a nicer error message for invalid class lookup
Richard Jones <richard@users.sourceforge.net>
parents: 1143
diff changeset
100 try:
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
101 return self.cursor.fetchone()
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
102 except gadfly.database.error, message:
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
103 if message == 'no more results':
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
104 return None
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
105 raise
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
106
1195
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
107 def sql_fetchall(self):
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
108 ''' Fetch a single row. If there's nothing to fetch, return [].
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
109 '''
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
110 try:
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
111 return self.cursor.fetchall()
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
112 except gadfly.database.error, message:
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
113 if message == 'no more results':
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
114 return []
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
115 raise
e0032f4ab334 added missing stringFind to sql backends
Richard Jones <richard@users.sourceforge.net>
parents: 1183
diff changeset
116
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
117 def save_dbschema(self, schema):
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
118 ''' Save the schema definition that the database currently implements
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
119 '''
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
120 self.sql('insert into schema values (?)', (self.database_schema,))
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
121
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
122 def load_dbschema(self):
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
123 ''' Load the schema definition that the database currently implements
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
124 '''
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
125 self.cursor.execute('select schema from schema')
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
126 return self.cursor.fetchone()[0]
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
127
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
128 def save_journal(self, classname, cols, nodeid, journaldate,
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
129 journaltag, action, params):
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
130 ''' Save the journal entry to the database
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
131 '''
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
132 # nothing special to do
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
133 entry = (nodeid, journaldate, journaltag, action, params)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
134
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
135 # do the insert
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
136 a = self.arg
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
137 sql = 'insert into %s__journal (%s) values (?,?,?,?,?)'%(classname,
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
138 cols)
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
139 if __debug__:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
140 print >>hyperdb.DEBUG, 'addjournal', (self, sql, entry)
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
141 self.cursor.execute(sql, entry)
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
142
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
143 def load_journal(self, classname, cols, nodeid):
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
144 ''' Load the journal from the database
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
145 '''
972
ca0a542b2d19 That's gadfly done, mostly.
Richard Jones <richard@users.sourceforge.net>
parents: 968
diff changeset
146 # now get the journal entries
1165
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
147 sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname,
14467c765167 sqlite backend!
Richard Jones <richard@users.sourceforge.net>
parents: 1156
diff changeset
148 self.arg)
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
149 if __debug__:
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
150 print >>hyperdb.DEBUG, 'getjournal', (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: 1174
diff changeset
151 self.cursor.execute(sql, (nodeid,))
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
152 res = []
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
153 for nodeid, date_stamp, user, action, params in self.cursor.fetchall():
968
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
154 res.append((nodeid, date.Date(date_stamp), user, action, params))
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
155 return res
07d8a4e296f8 Whee! It's not finished yet, but I can create a new instance...
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
156
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
157 class GadflyClass:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
158 def filter(self, search_matches, filterspec, sort, group):
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
159 ''' Gadfly doesn't have a LIKE predicate :(
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
160 '''
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
161 cn = self.classname
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
162
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
163 # figure the WHERE clause from the filterspec
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
164 props = self.getprops()
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
165 frum = ['_'+cn]
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
166 where = []
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
167 args = []
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
168 a = self.db.arg
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
169 for k, v in filterspec.items():
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
170 propclass = props[k]
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
171 if isinstance(propclass, Multilink):
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
172 tn = '%s_%s'%(cn, k)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
173 frum.append(tn)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
174 if isinstance(v, type([])):
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
175 s = ','.join([a for x in v])
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
176 where.append('id=%s.nodeid and %s.linkid in (%s)'%(tn,tn,s))
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
177 args = args + v
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
178 else:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
179 where.append('id=%s.nodeid and %s.linkid = %s'%(tn, tn, a))
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
180 args.append(v)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
181 else:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
182 if isinstance(v, type([])):
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
183 s = ','.join([a for x in v])
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
184 where.append('_%s in (%s)'%(k, s))
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
185 args = args + v
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
186 else:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
187 where.append('_%s=%s'%(k, a))
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
188 args.append(v)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
189
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
190 # add results of full text search
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
191 if search_matches is not None:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
192 v = search_matches.keys()
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
193 s = ','.join([a for x in v])
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
194 where.append('id in (%s)'%s)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
195 args = args + v
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
196
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
197 # "grouping" is just the first-order sorting in the SQL fetch
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
198 # can modify it...)
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
199 orderby = []
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
200 ordercols = []
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
201 if group[0] is not None and group[1] is not None:
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
202 if group[0] != '-':
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
203 orderby.append('_'+group[1])
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
204 ordercols.append('_'+group[1])
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
205 else:
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
206 orderby.append('_'+group[1]+' desc')
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
207 ordercols.append('_'+group[1])
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
208
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
209 # now add in the sorting
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
210 group = ''
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
211 if sort[0] is not None and sort[1] is not None:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
212 direction, colname = sort
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
213 if direction != '-':
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
214 if colname == 'id':
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
215 orderby.append(colname)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
216 else:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
217 orderby.append('_'+colname)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
218 ordercols.append('_'+colname)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
219 else:
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
220 if colname == 'id':
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
221 orderby.append(colname+' desc')
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
222 ordercols.append(colname)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
223 else:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
224 orderby.append('_'+colname+' desc')
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
225 ordercols.append('_'+colname)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
226
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
227 # construct the SQL
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
228 frum = ','.join(frum)
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
229 if where:
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
230 where = ' where ' + (' and '.join(where))
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
231 else:
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
232 where = ''
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
233 cols = ['id']
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
234 if orderby:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
235 cols = cols + ordercols
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
236 order = ' order by %s'%(','.join(orderby))
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
237 else:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
238 order = ''
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
239 cols = ','.join(cols)
1174
8e318dfaf479 Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents: 1170
diff changeset
240 sql = 'select %s from %s %s%s%s'%(cols, frum, where, group, order)
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
241 args = tuple(args)
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
242 if __debug__:
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
243 print >>hyperdb.DEBUG, 'filter', (self, sql, args)
1183
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
244 self.db.cursor.execute(sql, args)
08a13a84ed43 Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents: 1174
diff changeset
245 l = self.db.cursor.fetchall()
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
246
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
247 # return the IDs
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
248 return [row[0] for row in l]
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
249
1222
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
250 def find(self, **propspec):
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
251 ''' Overload to filter out duplicates in the result
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
252 '''
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
253 d = {}
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
254 for k in rdbms_common.Class.find(self, **propspec):
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
255 d[k] = 1
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
256 return d.keys()
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
257
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
258 class Class(GadflyClass, rdbms_common.Class):
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
259 pass
1222
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
260 class IssueClass(GadflyClass, rdbms_common.IssueClass):
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
261 pass
1222
bc3bc3248dd1 added Class.find() unit test, fixed implementations
Richard Jones <richard@users.sourceforge.net>
parents: 1195
diff changeset
262 class FileClass(GadflyClass, rdbms_common.FileClass):
1170
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
263 pass
af104fa52746 Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents: 1165
diff changeset
264

Roundup Issue Tracker: http://roundup-tracker.org/