diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-02-11 21:04:48 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-02-11 21:04:48 +0000 |
commit | 86c0504fa6fc95dc2ac644d1a37dc86a0e502fb1 (patch) | |
tree | add3e118a4148e69f02e2399fa2cf1e4e5426912 | |
parent | 280274812261868e8f665f706cd27e06eaff4302 (diff) | |
download | sqlalchemy-86c0504fa6fc95dc2ac644d1a37dc86a0e502fb1.tar.gz |
tableimpl and columnimpl proxy to actual impl objects per engine
-rw-r--r-- | lib/sqlalchemy/ext/proxy.py | 25 | ||||
-rw-r--r-- | test/proxy_engine.py | 26 |
2 files changed, 37 insertions, 14 deletions
diff --git a/lib/sqlalchemy/ext/proxy.py b/lib/sqlalchemy/ext/proxy.py index 6139164bf..4db001653 100644 --- a/lib/sqlalchemy/ext/proxy.py +++ b/lib/sqlalchemy/ext/proxy.py @@ -7,7 +7,7 @@ from sqlalchemy import sql from sqlalchemy.engine import create_engine from sqlalchemy.types import TypeEngine -import thread +import thread, weakref class ProxyEngine(object): """ @@ -84,13 +84,23 @@ 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): @@ -99,6 +109,17 @@ 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) diff --git a/test/proxy_engine.py b/test/proxy_engine.py index 639334716..e235a2221 100644 --- a/test/proxy_engine.py +++ b/test/proxy_engine.py @@ -241,20 +241,22 @@ class ProxyEngineTest2(PersistTest): 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' - - objectstore.commit() + rover = Dog() + rover.breed = 'spaniel' + rover.name = 'Rover' - assert spot.dog_id > 0, "Spot did not get an id" - assert rover.dog_id != spot.dog_id + objectstore.commit() + 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 @@ -268,7 +270,7 @@ class ProxyEngineTest2(PersistTest): # 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' |