Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

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

Changeset 304

Show
Ignore:
Timestamp:
09/21/06 06:57:07
Author:
fumanchu
Message:

Further fix for #4 (transaction support). New start, rollback, commit attributes on all StorageManager? subclasses. New implicit flag on geniusql.Database objects.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/storage/__init__.py

    r303 r304  
    9595        raise NotImplementedError("%s has no rename_property method." 
    9696                                  % self.__class__) 
     97     
     98    #                            Transactions                             # 
     99     
     100    start = None 
     101    rollback = None 
     102    commit = None 
     103 
    97104 
    98105 
  • trunk/storage/db.py

    r303 r304  
    716716         
    717717        return "\n".join(code) 
    718  
    719  
     718     
     719    def start(self): 
     720        """Start a transaction. Not needed if self.implicit is True.""" 
     721        self.db.start() 
     722     
     723    def rollback(self): 
     724        """Roll back the current transaction.""" 
     725        self.db.rollback() 
     726     
     727    def commit(self): 
     728        """Commit the current transaction.""" 
     729        self.db.commit() 
     730 
  • trunk/storage/geniusql.py

    r303 r304  
    14241424     
    14251425    def fetch(self, query, conn=None): 
    1426         """fetch(query, conn=None) -> rowdata, columns
     1426        """Return rowdata, columns (name, type) for the given query
    14271427         
    14281428        query should be a SQL query in string format 
     
    14571457     
    14581458    transaction_key = threading._get_ident 
    1459      
    1460     def get_transaction(self): 
    1461         """Return the connection for the current transaction, or a new one. 
    1462          
    1463         Uses the current thread id as a transaction key. 
     1459    implicit = False 
     1460     
     1461    def get_transaction(self, new=False): 
     1462        """Return the (possibly new) connection for the current transaction. 
     1463         
     1464        If self.implicit is True, a new connection will be associated with 
     1465        self.transaction_key(); repeated calls to this method will return 
     1466        the same connection. The default transaction key is the current 
     1467        thread ID. 
     1468         
     1469        If self.implicit is False, this method returns self.connection(), 
     1470        unless a transaction is already in progress. 
    14641471        """ 
    14651472        key = self.transaction_key() 
     
    14691476                raise conn 
    14701477        else: 
    1471             conn = self.transactions[key] = self.connection() 
    1472             self.start() 
     1478            conn = self.connection() 
     1479            if self.implicit or new: 
     1480                self.transactions[key] = conn 
     1481                if not new: 
     1482                    self.start() 
    14731483        return conn 
    14741484     
    14751485    def start(self): 
    1476         """Start a transaction.""" 
    1477         self.execute("START TRANSACTION;", self.get_transaction()) 
     1486        """Start a transaction. Not needed if self.implicit is True.""" 
     1487        self.execute("START TRANSACTION;", self.get_transaction(new=True)) 
    14781488     
    14791489    def rollback(self): 
    1480         """Roll back the current transaction.""" 
    1481         self.execute("ROLLBACK;", self.get_transaction()) 
     1490        """Roll back the current transaction (error if not in transaction).""" 
    14821491        key = self.transaction_key() 
     1492        self.execute("ROLLBACK;", self.transactions[key]) 
    14831493        del self.transactions[key] 
    14841494     
    14851495    def commit(self): 
    1486         """Commit the current transaction.""" 
    1487         self.execute("COMMIT;", self.get_transaction()) 
     1496        """Commit the current transaction, if any.""" 
    14881497        key = self.transaction_key() 
    1489         del self.transactions[key] 
     1498        if key in self.transactions: 
     1499            self.execute("COMMIT;", self.transactions[key]) 
     1500            del self.transactions[key] 
    14901501     
    14911502    # Change this to 'error' if you don't want autocommit on schema ops. 
     
    14931504     
    14941505    def lock(self, msg=None): 
    1495         """Deny transactions (required for some operations). 
     1506        """Deny transactions during schema operations (DDL statements). 
    14961507         
    14971508        Any code which calls this should also call 'unlock' in a try/finally: 
  • trunk/storage/storeado.py

    r303 r304  
    513513    def start(self): 
    514514        """Start a transaction.""" 
    515         self.execute("BEGIN TRANSACTION;", self.get_transaction()) 
     515        self.execute("BEGIN TRANSACTION;", self.get_transaction(new=True)) 
    516516     
    517517    def execute(self, query, conn=None): 
  • trunk/storage/storesqlite.py

    r303 r304  
    606606        """Start a transaction.""" 
    607607        self.acquire() 
    608         self.execute("BEGIN;", self.get_transaction()) 
     608        self.execute("BEGIN;", self.get_transaction(new=True)) 
     609     
    609610     
    610611    def rollback(self): 
    611612        """Roll back the current transaction.""" 
    612         db.Database.rollback(self) 
    613         self.release() 
     613        key = self.transaction_key() 
     614        if key in self.transactions: 
     615            self.execute("ROLLBACK;", self.transactions[key]) 
     616            del self.transactions[key] 
     617            self.release() 
    614618     
    615619    def commit(self): 
    616620        """Commit the current transaction.""" 
    617         db.Database.commit(self) 
    618         self.release() 
     621        key = self.transaction_key() 
     622        if key in self.transactions: 
     623            self.execute("COMMIT;", self.transactions[key]) 
     624            del self.transactions[key] 
     625            self.release() 
    619626     
    620627    def execute(self, query, conn=None): 
  • trunk/test/zoo_fixture.py

    r303 r304  
    654654         
    655655        # Test transaction rollback 
    656         box = arena.new_sandbox() 
    657         SDZ = box.unit(Zoo, Name='San Diego Zoo') 
    658         SDZ.Name = 'The One and Only San Diego Zoo' 
    659         SDZ.Founded = datetime.date(2039, 9, 13) 
    660         box.rollback() 
    661         SDZ = box.unit(Zoo, Name='San Diego Zoo') 
    662         self.assertEqual(SDZ.Name, 'San Diego Zoo') 
    663         self.assertEqual(SDZ.Founded, datetime.date(1835, 9, 13)) 
    664         box.flush_all() 
     656        if arena.storage(Zoo).rollback: 
     657            box = arena.new_sandbox() 
     658            SDZ = box.unit(Zoo, Name='San Diego Zoo') 
     659            SDZ.Name = 'The One and Only San Diego Zoo' 
     660            SDZ.Founded = datetime.date(2039, 9, 13) 
     661            box.rollback() 
     662            SDZ = box.unit(Zoo, Name='San Diego Zoo') 
     663            self.assertEqual(SDZ.Name, 'San Diego Zoo') 
     664            self.assertEqual(SDZ.Founded, datetime.date(1835, 9, 13)) 
     665            box.flush_all() 
    665666     
    666667    def test_Multithreading(self):