From 86c0504fa6fc95dc2ac644d1a37dc86a0e502fb1 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 11 Feb 2006 21:04:48 +0000 Subject: [PATCH] tableimpl and columnimpl proxy to actual impl objects per engine --- lib/sqlalchemy/ext/proxy.py | 25 +++++++++++++++++++++++-- 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 6139164bf1..4db001653b 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 6393347164..e235a22212 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' -- 2.47.2