Commits

Mike Bayer  committed 51f5709

tableimpl and columnimpl proxy to actual impl objects per engine

  • Participants
  • Parent commits 7c541ef

Comments (0)

Files changed (2)

File lib/sqlalchemy/ext/proxy.py

 from sqlalchemy.engine import create_engine
 from sqlalchemy.types import TypeEngine
 
-import thread
+import thread, weakref
 
 class ProxyEngine(object):
     """
         raise AttributeError('No connection established in ProxyEngine: '
                              ' no access to %s' % attr)
 
+        
 class ProxyColumnImpl(sql.ColumnImpl):
     """Proxy column; defers engine access to ProxyEngine
     """
     def __init__(self, engine, column):
         sql.ColumnImpl.__init__(self, column)
         self._engine = engine
-
+        self.impls = weakref.WeakKeyDictionary()
+    def _get_impl(self):
+        e = self.engine
+        try:
+            return self.impls[e]
+        except KeyError:
+            impl = e.columnimpl(self.column)
+            self.impls[e] = impl
+    def __getattr__(self, key):
+        return getattr(self._get_impl(), key)
     engine = property(lambda self: self._engine.engine)
 
 class ProxyTableImpl(sql.TableImpl):
     def __init__(self, engine, table):
         sql.TableImpl.__init__(self, table)
         self._engine = engine
+        self.impls = weakref.WeakKeyDictionary()
+    def _get_impl(self):
+        e = self.engine
+        try:
+            return self.impls[e]
+        except KeyError:
+            impl = e.tableimpl(self.table)
+            self.impls[e] = impl
+            return impl
+    def __getattr__(self, key):
+        return getattr(self._get_impl(), key)
 
     engine = property(lambda self: self._engine.engine)
 

File test/proxy_engine.py

 
         engine.connect(testbase.db_uri)
         dogs.create()
+        try:
+            spot = Dog()
+            spot.breed = 'beagle'
+            spot.name = 'Spot'
 
-        spot = Dog()
-        spot.breed = 'beagle'
-        spot.name = 'Spot'
-
-        rover = Dog()
-        rover.breed = 'spaniel'
-        rover.name = 'Rover'
+            rover = Dog()
+            rover.breed = 'spaniel'
+            rover.name = 'Rover'
         
-        objectstore.commit()
+            objectstore.commit()
         
-        assert spot.dog_id > 0, "Spot did not get an id"
-        assert rover.dog_id != spot.dog_id
-        
+            assert spot.dog_id > 0, "Spot did not get an id"
+            assert rover.dog_id != spot.dog_id
+        finally:
+            dogs.drop()
+            
     def  test_type_proxy_schema_gen(self):
         from sqlalchemy.databases.postgres import PGSchemaGenerator
 
         # answer
         engine.connect('postgres://database=test&port=5432&host=127.0.0.1&user=scott&password=tiger')
 
-        sg = PGSchemaGenerator(engine.proxy())
+        sg = PGSchemaGenerator(engine)
         id_spec = sg.get_column_specification(lizards.c.id)
         assert id_spec == 'id SERIAL NOT NULL PRIMARY KEY'