From: Mike Bayer Date: Fri, 2 Jun 2006 22:34:54 +0000 (+0000) Subject: restored global_connect() function, default table metadata X-Git-Tag: rel_0_2_2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cfcf47338d56cffd1731d93202aa10ce0fec76c4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git restored global_connect() function, default table metadata --- diff --git a/CHANGES b/CHANGES index 3e65c50ef8..331348b303 100644 --- 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 diff --git a/doc/build/content/metadata.txt b/doc/build/content/metadata.txt index 1eda5b171c..006971f6e3 100644 --- a/doc/build/content/metadata.txt +++ b/doc/build/content/metadata.txt @@ -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`: diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 8b0c9f0b36..31b3259902 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -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) diff --git a/test/query.py b/test/query.py index cf9cd0fc5e..2148aae672 100644 --- a/test/query.py +++ b/test/query.py @@ -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