Code

simple LRU cache for SQL databases
[roundup.git] / roundup / backends / back_gadfly.py
index 8bd2346584722737e6264df01d44efecd00146d2..a4e8a76127bfed5736578975753643dc8ec4ce7c 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: back_gadfly.py,v 1.22 2002-09-18 05:07:47 richard Exp $
+# $Id: back_gadfly.py,v 1.23 2002-09-19 02:37:41 richard Exp $
 __doc__ = '''
 About Gadfly
 ============
@@ -136,3 +136,120 @@ class Database(Database):
             res.append((nodeid, date.Date(date_stamp), user, action, params))
         return res
 
+class GadflyClass:
+    def filter(self, search_matches, filterspec, sort, group):
+        ''' Gadfly doesn't have a LIKE predicate :(
+        '''
+        cn = self.classname
+
+        # figure the WHERE clause from the filterspec
+        props = self.getprops()
+        frum = ['_'+cn]
+        where = []
+        args = []
+        a = self.db.arg
+        for k, v in filterspec.items():
+            propclass = props[k]
+            if isinstance(propclass, Multilink):
+                tn = '%s_%s'%(cn, k)
+                frum.append(tn)
+                if isinstance(v, type([])):
+                    s = ','.join([self.arg for x in v])
+                    where.append('id=%s.nodeid and %s.linkid in (%s)'%(tn,tn,s))
+                    args = args + v
+                else:
+                    where.append('id=%s.nodeid and %s.linkid = %s'%(tn, tn, a))
+                    args.append(v)
+            else:
+                if isinstance(v, type([])):
+                    s = ','.join([a for x in v])
+                    where.append('_%s in (%s)'%(k, s))
+                    args = args + v
+                else:
+                    where.append('_%s=%s'%(k, a))
+                    args.append(v)
+
+        # add results of full text search
+        if search_matches is not None:
+            v = search_matches.keys()
+            s = ','.join([a for x in v])
+            where.append('id in (%s)'%s)
+            args = args + v
+
+        # figure the order by clause
+        orderby = []
+        ordercols = []
+        if sort[0] is not None and sort[1] is not None:
+            direction, colname = sort
+            if direction != '-':
+                if colname == 'activity':
+                    orderby.append('activity')
+                    ordercols.append('max(%s__journal.date) as activity'%cn)
+                    frum.append('%s__journal'%cn)
+                    where.append('%s__journal.nodeid = _%s.id'%(cn, cn))
+                elif colname == 'id':
+                    orderby.append(colname)
+                    ordercols.append(colname)
+                else:
+                    orderby.append('_'+colname)
+                    ordercols.append('_'+colname)
+            else:
+                if colname == 'activity':
+                    orderby.append('activity desc')
+                    ordercols.append('max(%s__journal.date) as activity'%cn)
+                    frum.append('%s__journal'%cn)
+                    where.append('%s__journal.nodeid = _%s.id'%(cn, cn))
+                elif colname == 'id':
+                    orderby.append(colname+' desc')
+                    ordercols.append(colname)
+                else:
+                    orderby.append('_'+colname+' desc')
+                    ordercols.append('_'+colname)
+
+        # figure the group by clause
+        groupby = []
+        groupcols = []
+        if group[0] is not None and group[1] is not None:
+            if group[0] != '-':
+                groupby.append('_'+group[1])
+                groupcols.append('_'+group[1])
+            else:
+                groupby.append('_'+group[1]+' desc')
+                groupcols.append('_'+group[1])
+
+        # construct the SQL
+        frum = ','.join(frum)
+        where = ' and '.join(where)
+        cols = []
+        if orderby:
+            cols = cols + ordercols
+            order = ' order by %s'%(','.join(orderby))
+        else:
+            order = ''
+        if 0: #groupby:
+            cols = cols + groupcols
+            group = ' group by %s'%(','.join(groupby))
+        else:
+            group = ''
+        if 'id' not in cols:
+            cols.append('id')
+        cols = ','.join(cols)
+        sql = 'select %s from %s where %s%s%s'%(cols, frum, where, order,
+            group)
+        args = tuple(args)
+        if __debug__:
+            print >>hyperdb.DEBUG, 'filter', (self, sql, args)
+        cursor = self.db.conn.cursor()
+        cursor.execute(sql, args)
+        l = cursor.fetchall()
+
+        # return the IDs
+        return [row[0] for row in l]
+
+class Class(GadflyClass, Class):
+    pass
+class IssueClass(GadflyClass, IssueClass):
+    pass
+class FileClass(GadflyClass, FileClass):
+    pass
+