Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

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

Changeset 439

Show
Ignore:
Timestamp:
04/30/07 22:54:09
Author:
fumanchu
Message:

New insert_into methods on arena, db.

Files:

Legend:

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

    r438 r439  
    274274                    self._registered_classes[cls] = new_store 
    275275     
    276     def view(self, cls, attrs, expr=None, **kwargs): 
    277         """Yield tuples of attrs for the given cls which match the expr. 
    278          
    279         cls: The Unit subclass for which to yield property tuples. 
     276    def view(self, relation, attrs, expr=None, **kwargs): 
     277        """Yield tuples of attrs for the given relation which match the expr. 
     278         
     279        relation: The Unit class or Join instance for which to yield 
     280            property tuples. 
    280281        attrs: a sequence of strings; each should be the name of 
    281282            a UnitProperty on the given cls. 
     
    304305         
    305306        if self.logflags & logflags.VIEW: 
    306             self.log("VIEW %s [%s]: %s" % (cls.__name__, attrs, expr)) 
    307          
    308         for row in self.storage(cls).view(cls, attrs, expr): 
     307            self.log("VIEW %s [%s]: %s" % (relation, attrs, expr)) 
     308         
     309        store = self._single_store(relation) 
     310        for row in store.view(relation, attrs, expr): 
    309311            yield row 
    310312     
     
    421423            self.log("VIEW %s [%s]: %s" % (relation, attributes, restriction)) 
    422424         
     425        for row in self._single_store(relation).multiview(relation, attributes, restriction): 
     426            yield row 
     427     
     428    def _single_store(self, relation): 
     429        """Return the store for the given relation (or raise ValueError).""" 
    423430        if hasattr(relation, "class1"): 
    424431            stores = [self.storage(cls) for cls in relation] 
    425             firststore = stores[0] 
     432            store = stores[0] 
    426433            for s in stores: 
    427                 if s is not firststore: 
    428                     raise ValueError(u"multiview does not support multiple" 
     434                if s is not store: 
     435                    raise ValueError(u"This operation does not support multiple" 
    429436                                     u" classes in disparate stores.") 
    430437        else: 
    431             firststore = self.storage(relation) 
    432          
    433         for row in firststore.multiview(relation, attributes, restriction): 
    434             yield row 
     438            store = self.storage(relation) 
     439        return store 
     440     
     441    def insert_into(self, name, relation, attributes, restriction=None, 
     442                    distinct=False): 
     443        """INSERT matching data INTO a new class and return the class.""" 
     444        store = self._single_store(relation) 
     445        return store.insert_into(name, relation, attributes, 
     446                                 restriction, distinct) 
    435447 
    436448 
  • trunk/storage/db.py

    r438 r439  
    111111            relation = self.schema[classes.__name__] 
    112112        return self.db.select(relation, attrs, restriction, distinct) 
     113     
     114    def insert_into(self, name, classes, attrs, restriction, distinct=False): 
     115        """INSERT matching data INTO a new class and return the class.""" 
     116        if isinstance(classes, dejavu.UnitJoin): 
     117            relation = self.tablejoin(classes) 
     118        else: 
     119            relation = self.schema[classes.__name__] 
     120        newtable = self.db.insert_into(name, relation, attrs, 
     121                                       restriction, distinct) 
     122        return Modeler(self.schema).make_class(name) 
    113123     
    114124    def recall(self, cls, expr=None):