Code

better detection of unset required props
[roundup.git] / roundup / backends / rdbms_common.py
index f3d6821b83e6b01afcd20a1e41e7f6362e66d05d..b0eb498b0c0df99ae5fdefe13c49f18edca702d2 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: rdbms_common.py,v 1.12 2002-09-23 07:15:32 richard Exp $
+# $Id: rdbms_common.py,v 1.15 2002-09-24 01:59:28 richard Exp $
 
 # standard python modules
 import sys, os, time, re, errno, weakref, copy
@@ -462,10 +462,13 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
         cl = self.classes[classname]
         cols, mls = self.determine_columns(cl.properties.items())
 
-        # add the special props
-        node = node.copy()
-        node['creation'] = node['activity'] = date.Date()
-        node['creator'] = self.curuserid
+        # we'll be supplied these props if we're doing an import
+        if not node.has_key('creator'):
+            # add in the "calculated" properties (dupe so we don't affect
+            # calling code's node assumptions)
+            node = node.copy()
+            node['creation'] = node['activity'] = date.Date()
+            node['creator'] = self.curuserid
 
         # default the non-multilink columns
         for col, prop in cl.properties.items():
@@ -1144,18 +1147,22 @@ class Class(hyperdb.Class):
                 value = pwd
             d[propname] = value
 
+        # add the node and journal
+        self.db.addnode(self.classname, newid, d)
+
         # extract the extraneous journalling gumpf and nuke it
         if d.has_key('creator'):
             creator = d['creator']
             del d['creator']
+        else:
+            creator = None
         if d.has_key('creation'):
             creation = d['creation']
             del d['creation']
+        else:
+            creation = None
         if d.has_key('activity'):
             del d['activity']
-
-        # add the node and journal
-        self.db.addnode(self.classname, newid, d)
         self.db.addjournal(self.classname, newid, 'create', d, creator,
             creation)
         return newid
@@ -1605,12 +1612,33 @@ class Class(hyperdb.Class):
                 self.classname, prop, ','.join([a for x in values.keys()])))
         sql = '\nintersect\n'.join(tables)
         self.db.sql(sql, allvalues)
-        try:
-            l = [x[0] for x in self.db.cursor.fetchall()]
-        except gadfly.database.error, message:
-            if message == 'no more results':
-                l = []
-            raise
+        l = [x[0] for x in self.db.sql_fetchall()]
+        if __debug__:
+            print >>hyperdb.DEBUG, 'find ... ', l
+        return l
+
+    def stringFind(self, **requirements):
+        '''Locate a particular node by matching a set of its String
+        properties in a caseless search.
+
+        If the property is not a String property, a TypeError is raised.
+        
+        The return is a list of the id of all nodes that match.
+        '''
+        where = []
+        args = []
+        for propname in requirements.keys():
+            prop = self.properties[propname]
+            if isinstance(not prop, String):
+                raise TypeError, "'%s' not a String property"%propname
+            where.append(propname)
+            args.append(requirements[propname].lower())
+
+        # generate the where clause
+        s = ' and '.join(['_%s=%s'%(col, self.db.arg) for col in where])
+        sql = 'select id from _%s where %s'%(self.classname, s)
+        self.db.sql(sql, tuple(args))
+        l = [x[0] for x in self.db.sql_fetchall()]
         if __debug__:
             print >>hyperdb.DEBUG, 'find ... ', l
         return l