[Checkins] SVN: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/ reorganized postgres wrappers

Andreas Jung andreas at andreas-jung.com
Mon Mar 19 06:28:10 EDT 2007


Log message for revision 73333:
  reorganized postgres wrappers
  

Changed:
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/base.py
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/postgres.py

-=-
Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/base.py
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/base.py	2007-03-19 10:27:30 UTC (rev 73332)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/base.py	2007-03-19 10:28:10 UTC (rev 73333)
@@ -6,7 +6,7 @@
 # and ZOPYX Ltd. & Co. KG, Tuebingen, Germany
 ##########################################################################
 
-
+import sys
 import threading
 
 import sqlalchemy
@@ -21,13 +21,9 @@
 from z3c.sqlalchemy.mapper import MapperFactory, LazyMapperCollection
 
 import transaction
+from transaction.interfaces import IDataManager
 
 
-_cache = threading.local() # module-level cache 
-
-marker = object
-
-
 class BaseWrapper(object):
 
     implements(ISQLAlchemyWrapper)
@@ -101,3 +97,72 @@
     def _createEngine(self):
         return sqlalchemy.create_engine(self.dsn)
 
+
+_cache = threading.local() # module-level cache 
+
+
+class DataManager(object):
+    """ Wraps session into transaction context of Zope """
+
+    implements(IDataManager)
+
+    def __init__(self, session):
+        self.session = session
+
+    def abort(self, trans):
+        pass
+
+    def tpc_begin(self, trans):
+        pass
+
+    def commit(self, trans):
+        self.session.flush()
+
+    def tpc_vote(self, trans):
+        pass
+
+    def tpc_finish(self, trans):
+        pass
+
+    def tpc_abort(self, trans):
+        pass
+
+    def sortKey(self):
+        return str(id(self))
+
+
+class ZopeBaseWrapper(BaseWrapper):
+    """ A wrapper to be used from within Zope. It connects
+        the session with the transaction management of Zope.
+    """
+
+    @property
+    def session(self):
+
+        if not hasattr(_cache, 'last_transaction'):
+            _cache.last_transaction = None
+            _cache.last_session = None
+
+        # get current transaction
+        txn = transaction.get()
+        txn_str = str(txn)
+
+        # return cached session if we are within the same transaction
+        # and same thread
+        if txn_str == _cache.last_transaction:
+            return _cache.last_session
+
+        # no cached session, let's create a new one
+        session = sqlalchemy.create_session(self._engine)
+                                          
+        # register a DataManager with the current transaction
+        DM = DataManager(session)
+        txn.join(DM)
+
+        # update thread-local cache
+        _cache.last_transaction = txn_str
+        _cache.last_session = session
+
+        # return the session
+        return session 
+

Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/postgres.py
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/postgres.py	2007-03-19 10:27:30 UTC (rev 73332)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/postgres.py	2007-03-19 10:28:10 UTC (rev 73333)
@@ -15,14 +15,15 @@
 from zope.interface import implements
 
 from z3c.sqlalchemy.interfaces import ISQLAlchemyWrapper
-from z3c.sqlalchemy.base import BaseWrapper
+from z3c.sqlalchemy.base import BaseWrapper, ZopeBaseWrapper
 
 from zope_mixin import ZopeMixin
 
 _cache = threading.local() # module-level cache 
 
 
-class PythonPostgresWrapper(BaseWrapper):
+class PostgresMixin(object):
+    """ Mixin class for Postgres aspects """
 
     implements(ISQLAlchemyWrapper)
 
@@ -68,7 +69,12 @@
         return _cache.ref_mapping
 
 
-class ZopePostgresWrapper(PythonPostgresWrapper, ZopeMixin):
+class PythonPostgresWrapper(BaseWrapper, PostgresMixin):
+    """ Wrapper to be used with Python with extended
+        Postgres functionality.
+    """
+
+class ZopePostgresWrapper(ZopeBaseWrapper, PostgresMixin):
     """ A wrapper to be used from within Zope. It connects
         the session with the transaction management of Zope.
     """



More information about the Checkins mailing list