From: jeff Date: Sat, 25 Feb 2006 17:50:46 +0000 (+0000) Subject: Refactored ProxyEngine into BaseProxyEngine and ProxyEngine. Also added an AutoConnec... X-Git-Tag: rel_0_1_3~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdf9292241e22eb464c366bcf69c0212b3ff4c41;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Refactored ProxyEngine into BaseProxyEngine and ProxyEngine. Also added an AutoConnectProxyEngine to late bind to a particular dburi. I ran the proxy_engine test, however, I don't have postgresql installed so not all tests worked. Also, I don't have an WSGI package installed to run the wsgi tests. --- diff --git a/lib/sqlalchemy/ext/proxy.py b/lib/sqlalchemy/ext/proxy.py index 4f1f9e4010..dc02606086 100644 --- a/lib/sqlalchemy/ext/proxy.py +++ b/lib/sqlalchemy/ext/proxy.py @@ -9,7 +9,72 @@ from sqlalchemy.types import TypeEngine import thread, weakref -class ProxyEngine(object): +class BaseProxyEngine(object): + ''' + Basis for all proxy engines + ''' + def __init__(self): + self.tables = {} + + def get_engine(self): + raise NotImplementedError + + def set_engine(self, engine): + raise NotImplementedError + + engine = property(get_engine, set_engine) + + def hash_key(self): + return "%s(%s)" % (self.__class__.__name__, id(self)) + + def oid_column_name(self): + # NOTE: setting up mappers fails unless the proxy engine returns + # something for oid column name, and the call happens too early + # to proxy, so effecticely no oids are allowed when using + # proxy engine + e= self.get_engine() + if e is None: + return None + return e.oid_column_name() + + def type_descriptor(self, typeobj): + """Proxy point: return a ProxyTypeEngine + """ + return ProxyTypeEngine(self, typeobj) + + def __getattr__(self, attr): + # call get_engine() to give subclasses a chance to change + # connection establishment behavior + e= self.get_engine() + if e is not None: + return getattr(e, attr) + raise AttributeError('No connection established in ProxyEngine: ' + ' no access to %s' % attr) + +class AutoConnectEngine(BaseProxyEngine): + ''' + An SQLEngine proxy that automatically connects when necessary. + ''' + + def __init__(self, dburi, opts=None, **kwargs): + BaseProxyEngine.__init__(self) + self.dburi= dburi + self.opts= opts + self.kwargs= kwargs + self._engine= None + + def get_engine(self): + if self._engine is None: + self._engine= create_engine( self.dburi, self.opts, **self.kwargs ) + return self._engine + + def set_engine(self, engine): + raise NotImplementedError + + engine = property(get_engine, set_engine) + + +class ProxyEngine(BaseProxyEngine): """ SQLEngine proxy. Supports lazy and late initialization by delegating to a real engine (set with connect()), and using proxy @@ -17,11 +82,11 @@ class ProxyEngine(object): """ def __init__(self): + BaseProxyEngine.__init__(self) # create the local storage for uri->engine map and current engine self.storage = local() self.storage.connection = {} self.storage.engine = None - self.tables = {} def connect(self, uri, opts=None, **kwargs): """Establish connection to a real engine. @@ -49,31 +114,6 @@ class ProxyEngine(object): engine = property(get_engine, set_engine) - def hash_key(self): - return "%s(%s)" % (self.__class__.__name__, id(self)) - - def oid_column_name(self): - # NOTE: setting up mappers fails unless the proxy engine returns - # something for oid column name, and the call happens too early - # to proxy, so effecticely no oids are allowed when using - # proxy engine - if self.storage.engine is None: - return None - return self.get_engine().oid_column_name() - - - def type_descriptor(self, typeobj): - """Proxy point: return a ProxyTypeEngine - """ - return ProxyTypeEngine(self, typeobj) - - def __getattr__(self, attr): - # call get_engine() to give subclasses a chance to change - # connection establishment behavior - if self.get_engine() is not None: - return getattr(self.engine, attr) - raise AttributeError('No connection established in ProxyEngine: ' - ' no access to %s' % attr) class ProxyType(object): """ProxyType base class; used by ProxyTypeEngine to construct proxying