[Sqlalchemy-commits] [1041] sqlalchemy/trunk/lib/sqlalchemy/ext/proxy.py: Refactored ProxyEngine int
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-02-25 17:50:57
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1041] sqlalchemy/trunk/lib/sqlalchemy/ext/proxy.py: Refactored ProxyEngine into BaseProxyEngine and ProxyEngine.</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1041</dd> <dt>Author</dt> <dd>jeff</dd> <dt>Date</dt> <dd>2006-02-25 11:50:46 -0600 (Sat, 25 Feb 2006)</dd> </dl> <h3>Log Message</h3> <pre>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.</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunklibsqlalchemyextproxypy">sqlalchemy/trunk/lib/sqlalchemy/ext/proxy.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunklibsqlalchemyextproxypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/ext/proxy.py (1040 => 1041)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/ext/proxy.py 2006-02-25 07:22:01 UTC (rev 1040) +++ sqlalchemy/trunk/lib/sqlalchemy/ext/proxy.py 2006-02-25 17:50:46 UTC (rev 1041) </span><span class="lines">@@ -9,7 +9,72 @@ </span><span class="cx"> </span><span class="cx"> import thread, weakref </span><span class="cx"> </span><del>-class ProxyEngine(object): </del><ins>+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): </ins><span class="cx"> """ </span><span class="cx"> SQLEngine proxy. Supports lazy and late initialization by </span><span class="cx"> delegating to a real engine (set with connect()), and using proxy </span><span class="lines">@@ -17,11 +82,11 @@ </span><span class="cx"> """ </span><span class="cx"> </span><span class="cx"> def __init__(self): </span><ins>+ BaseProxyEngine.__init__(self) </ins><span class="cx"> # create the local storage for uri->engine map and current engine </span><span class="cx"> self.storage = local() </span><span class="cx"> self.storage.connection = {} </span><span class="cx"> self.storage.engine = None </span><del>- self.tables = {} </del><span class="cx"> </span><span class="cx"> def connect(self, uri, opts=None, **kwargs): </span><span class="cx"> """Establish connection to a real engine. </span><span class="lines">@@ -49,32 +114,7 @@ </span><span class="cx"> </span><span class="cx"> engine = property(get_engine, set_engine) </span><span class="cx"> </span><del>- def hash_key(self): - return "%s(%s)" % (self.__class__.__name__, id(self)) </del><span class="cx"> </span><del>- 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) - </del><span class="cx"> class ProxyType(object): </span><span class="cx"> """ProxyType base class; used by ProxyTypeEngine to construct proxying </span><span class="cx"> types </span></span></pre> </div> </div> </body> </html> |