Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 475

Show
Ignore:
Timestamp:
08/30/07 16:50:59
Author:
fumanchu
Message:

Improved counting for db SM's.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/sandboxes.py

    r474 r475  
    394394                    self.view(dejavu.Query(cls, (attr,), expr, **kwargs))]) 
    395395     
    396     def count(self, cls, expr): 
     396    def count(self, cls, expr=None): 
    397397        """Number of Units of the given cls which match the given expr.""" 
    398398        if cls.identifiers: 
  • trunk/storage/__init__.py

    r474 r475  
    429429        return [x for x in self.xview(query, distinct=distinct)] 
    430430     
    431     def count(self, cls, expr): 
     431    def count(self, cls, expr=None): 
    432432        """Number of Units of the given cls which match the given expr.""" 
    433433        if cls.identifiers: 
  • trunk/storage/db.py

    r474 r475  
    3434 
    3535import geniusql 
    36 from geniusql import logic 
     36from geniusql import logic, logicfuncs 
    3737 
    3838import dejavu 
     
    261261            for row in data: 
    262262                yield tuple(row) 
     263     
     264    def count(self, cls, expr=None): 
     265        """Number of Units of the given cls which match the given expr.""" 
     266        if cls.identifiers: 
     267            uniq = cls.identifiers 
     268        else: 
     269            uniq = cls._properties.keys() 
     270        # TODO: handle multiple args to count() 
     271        counter = lambda x: [logicfuncs.count(getattr(x, uniq[0]))] 
     272         
     273        query = dejavu.Query(cls, counter, expr) 
     274         
     275        if self.logflags & logflags.VIEW: 
     276            self.log(logflags.VIEW.message(query, False)) 
     277         
     278        data = self.select(query) 
     279        if data.statement.imperfect: 
     280            # ^%$#@! There's no way to handle imperfect queries without 
     281            # creating all involved Units, which defeats the performance 
     282            # benefits of view. 
     283            clsname = self.__class__.__name__ 
     284            warnings.warn("The requested query cannot produce perfect SQL " 
     285                          "with a %s datasource. It may take an absurdly " 
     286                          "long time to run, since each unit must be fully-" 
     287                          "formed. %s" % (clsname, query), StorageWarning) 
     288            return storage.StorageManager.count(self, cls, expr) 
     289        else: 
     290            return data.scalar() 
    263291     
    264292    def multirecall(self, relation, expr): 
  • trunk/test/zoo_fixture.py

    r474 r475  
    572572            escapees = box.view((Animal, ['Legs'], f), distinct=True) 
    573573            self.assertEqual(escapees, [(4, ), ]) 
     574             
     575            # count should use an aggregate function when using DB's 
     576            fourlegged = root.count(Animal, lambda x: x.Legs == 4) 
     577            self.assertEqual(fourlegged, 4) 
     578            topics = root.count(Exhibit) 
     579            self.assertEqual(topics, 2) 
    574580             
    575581            # range should return a sorted list