]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
restored global_connect() function, default table metadata
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 2 Jun 2006 22:34:54 +0000 (22:34 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 2 Jun 2006 22:34:54 +0000 (22:34 +0000)
CHANGES
doc/build/content/metadata.txt
lib/sqlalchemy/schema.py
test/query.py

diff --git a/CHANGES b/CHANGES
index 3e65c50ef88c3aa55206ce1ec3877b53302c588b..331348b30383e8cfcdb95be83716fa047c142d61 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -16,7 +16,9 @@ first column instead of "1" for criterion, also uses label "rowcount"
 instead of "count".  
 - got rudimental "mapping to multiple tables" functionality cleaned up, 
 more correctly documented
-
+- restored global_connect() function, attaches to a DynamicMetaData
+instance called "default_metadata".  leaving MetaData arg to Table
+out will use the default metadata.
 0.2.1
 - "pool" argument to create_engine() properly propigates
 - fixes to URL, raises exception if not parsed, does not pass blank
index 1eda5b171c2d19a201b21c342f82bc639ea5c020..006971f6e38f0b19914af3b84959cf4cb815a3f5 100644 (file)
@@ -119,6 +119,28 @@ Another form of `MetaData` exists which allows connecting to any number of engin
 
 `DynamicMetaData` is ideal for applications that need to use the same set of `Tables` for many different database connections in the same process, such as a CherryPy web application which handles multiple application instances in one process.
 
+#### Using the global Metadata object
+
+Some users prefer to create `Table` objects without specifying a `MetaData` object, having Tables scoped on an application-wide basis.  For them the `default_metadata` object and the `global_connect()` function is supplied.  `default_metadata` is simply an instance of `DynamicMetaData` that exists within the `sqlalchemy` namespace, and `global_connect()` is a synonym for `default_metadata.connect()`.  Defining a `Table` that has no `MetaData` argument will automatically use this default metadata as follows:
+
+    {python}
+    from sqlalchemy import *
+
+    # a Table with just a name and its Columns
+    mytable = Table('mytable', 
+        Column('col1', Integer, primary_key=True),
+        Column('col2', String(40))
+        )
+
+    # connect all the "anonymous" tables to a postgres uri in the current thread    
+    global_connect('postgres://foo:bar@lala/test')
+
+    # create all tables in the default metadata
+    default_metadata.create_all()
+
+    # the table is bound
+    mytable.insert().execute(col1=5, col2='some value')
+    
 #### Reflecting Tables
 
 Once you have a `BoundMetaData` or a connected `DynamicMetaData`, you can create `Table` objects without specifying their columns, just their names, using `autoload=True`:
index 8b0c9f0b368d5367d20f66941460d4116e5ee18d..31b3259902c58845500065507fbe9cd63706acf1 100644 (file)
@@ -57,6 +57,15 @@ class TableSingleton(type):
                 if not hasattr(engine, '_legacy_metadata'):
                     engine._legacy_metadata = BoundMetaData(engine)
                 metadata = engine._legacy_metadata
+            elif metadata is not None and not isinstance(metadata, MetaData):
+                # they left MetaData out, so assume its another SchemaItem, add it to *args
+                args = list(args)
+                args.insert(0, metadata)
+                metadata = None
+                
+            if metadata is None:
+                metadata = default_metadata
+                
             name = str(name)    # in case of incoming unicode
             schema = kwargs.get('schema', None)
             autoload = kwargs.pop('autoload', False)
index cf9cd0fc5e6f8c5d82109b53553a7ce151e9d2ef..2148aae672b67ad6314c2de6187cd1a491034d02 100644 (file)
@@ -50,7 +50,22 @@ class QueryTest(PersistTest):
         for row in r:
             l.append(row)
         self.assert_(len(l) == 3)
-        
+    
+    def test_global_metadata(self):
+        t1 = Table('table1', Column('col1', Integer, primary_key=True),
+            Column('col2', String(20)))
+        t2 = Table('table2', Column('col1', Integer, primary_key=True),
+            Column('col2', String(20)))
+   
+        assert t1.c.col1
+        global_connect(testbase.db)
+        default_metadata.create_all()
+        try:
+            assert t1.count().scalar() == 0
+        finally:
+            default_metadata.drop_all()
+            default_metadata.clear()
     def testpassiveoverride(self):
         """primarily for postgres, tests that when we get a primary key column back 
         from reflecting a table which has a default value on it, we pre-execute