Mike Bayer avatar Mike Bayer committed bd26d49

- 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.

Comments (0)

Files changed (2)

     
     - 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()

lib/sqlalchemy/schema.py

 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()
         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 
     """
 
     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:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.