]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- MetaData can bind to an engine either via "url" or "engine" kwargs
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 22 Mar 2007 21:18:23 +0000 (21:18 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 22 Mar 2007 21:18:23 +0000 (21:18 +0000)
to constructor, or by using connect() method.  BoundMetaData is
identical to MetaData except engine_or_url param is required.
DynamicMetaData is the same and provides thread-local connections
be default.

CHANGES
lib/sqlalchemy/schema.py

diff --git a/CHANGES b/CHANGES
index ffcb701f1820d2961e46f451a90e822ff64a1557..4ddd23f506943afe5ab6cbde851f4f07986636e0 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -9,6 +9,12 @@
     
     - slightly better support for bind params as column clauses, either
     via bindparam() or via literal(), i.e. select([literal('foo')])
+
+    - MetaData can bind to an engine either via "url" or "engine" kwargs
+    to constructor, or by using connect() method.  BoundMetaData is 
+    identical to MetaData except engine_or_url param is required.
+    DynamicMetaData is the same and provides thread-local connections 
+    be default.
     
     - exists() becomes useable as a standalone selectable, not just in a 
     WHERE clause, i.e. exists([columns], criterion).select()
index 168a0f90f048c83427b624189368644549a1618e..daaa0e6818176e58524b2cf9934be6976d90eb70 100644 (file)
@@ -1067,13 +1067,53 @@ class Index(SchemaItem):
 class MetaData(SchemaItem):
     """Represent a collection of Tables and their associated schema constructs."""
 
-    def __init__(self, name=None, **kwargs):
+    def __init__(self, name=None, url=None, engine=None, **kwargs):
+        """create a new MetaData object.
+        
+            name
+                optional name for this MetaData instance.
+            
+            url
+                a string or URL instance which will be passed to create_engine(),
+                along with **kwargs - this MetaData will be bound to the resulting
+                engine.
+            
+            engine
+                an Engine instance to which this MetaData will be bound.
+                
+            case_sensitive
+                popped from **kwargs, indicates default case sensitive setting for
+                all contained objects.  defaults to True.
+            
+        """        
+
         self.tables = {}
         self.name = name
+        self._engine = None
         self._set_casing_strategy(name, kwargs)
+        if engine or url:
+            self.connect(engine or url, **kwargs)
 
     def is_bound(self):
-        return False
+        """return True if this MetaData is bound to an Engine."""
+        return self._engine is not None
+
+    def connect(self, engine_or_url, **kwargs):
+        """bind this MetaData to an Engine.
+        
+            engine_or_url
+                a string, URL or Engine instance.  If a string or URL,
+                will be passed to create_engine() along with **kwargs to
+                produce the engine which to connect to.  otherwise connects
+                directly to the given Engine.
+                
+        """
+        
+        from sqlalchemy.engine.url import URL
+        if isinstance(engine_or_url, basestring) or isinstance(engine_or_url, URL):
+            self._engine = sqlalchemy.create_engine(engine_or_url, **kwargs)
+        else:
+            self._engine = engine_or_url
 
     def clear(self):
         self.tables.clear()
@@ -1140,19 +1180,17 @@ class MetaData(SchemaItem):
         return self._engine
 
 class BoundMetaData(MetaData):
-    """Build upon ``MetaData`` to provide the capability to bind to an
-``Engine`` implementation.
+    """``MetaData`` for which the first argument is a required Engine, url string, or URL instance.
+    
     """
 
     def __init__(self, engine_or_url, name=None, **kwargs):
-        super(BoundMetaData, self).__init__(name, **kwargs)
-        if isinstance(engine_or_url, basestring):
-            self._engine = sqlalchemy.create_engine(engine_or_url, **kwargs)
+        from sqlalchemy.engine.url import URL
+        if isinstance(engine_or_url, basestring) or isinstance(engine_or_url, URL):
+            super(BoundMetaData, self).__init__(name=name, url=engine_or_url, **kwargs)
         else:
-            self._engine = engine_or_url
-
-    def is_bound(self):
-        return True
+            super(BoundMetaData, self).__init__(name=name, engine=engine_or_url, **kwargs)
+            
 
 class DynamicMetaData(MetaData):
     """Build upon ``MetaData`` to provide the capability to bind to 
@@ -1161,15 +1199,16 @@ thread-local basis.
     """
 
     def __init__(self, name=None, threadlocal=True, **kwargs):
-        super(DynamicMetaData, self).__init__(name, **kwargs)
         if threadlocal:
             self.context = util.ThreadLocal()
         else:
             self.context = self
         self.__engines = {}
+        super(DynamicMetaData, self).__init__(name=name, **kwargs)
 
     def connect(self, engine_or_url, **kwargs):
-        if isinstance(engine_or_url, str):
+        from sqlalchemy.engine.url import URL
+        if isinstance(engine_or_url, basestring) or isinstance(engine_or_url, URL):
             try:
                 self.context._engine = self.__engines[engine_or_url]
             except KeyError: