summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/proxy.py
diff options
context:
space:
mode:
authorjeff <none@none>2006-02-25 17:50:46 +0000
committerjeff <none@none>2006-02-25 17:50:46 +0000
commitfdf9292241e22eb464c366bcf69c0212b3ff4c41 (patch)
treec216239a5108a5a190755195c5804c878e234633 /lib/sqlalchemy/ext/proxy.py
parent4b08fe4dd4ab301ac3c60c47468bc7b316c78a12 (diff)
downloadsqlalchemy-fdf9292241e22eb464c366bcf69c0212b3ff4c41.tar.gz
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.
Diffstat (limited to 'lib/sqlalchemy/ext/proxy.py')
-rw-r--r--lib/sqlalchemy/ext/proxy.py94
1 files changed, 67 insertions, 27 deletions
diff --git a/lib/sqlalchemy/ext/proxy.py b/lib/sqlalchemy/ext/proxy.py
index 4f1f9e401..dc0260608 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