From: Mike Bayer Date: Thu, 2 Mar 2006 00:38:16 +0000 (+0000) Subject: engine argument on tables optional X-Git-Tag: rel_0_1_3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=413dcdf0f4cc860ad2f4613c8cf02f38bb59c426;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git engine argument on tables optional test suite uses BaseProxyEngine as a base for the tester engine documented global proxy engine --- diff --git a/CHANGES b/CHANGES index 5a749f1310..6724ff1116 100644 --- a/CHANGES +++ b/CHANGES @@ -27,6 +27,9 @@ as Unicode types, with raw-byte/utf-8 translation on the bind parameter and result set side. - postgres maintains a list of ANSI functions that must have no parenthesis so function calls with no arguments work consistently +- tables can be created with no engine specified. this will default their engine +to a module-scoped "default engine" which is a ProxyEngine. this engine can +be connected via the function "global_connect". 0.1.2 - fixed a recursive call in schema that was somehow running 994 times then returning normally. broke nothing, slowed down everything. thanks to jpellerin for finding this. diff --git a/doc/build/content/dbengine.myt b/doc/build/content/dbengine.myt index 838ac2d038..4ce3d1b00b 100644 --- a/doc/build/content/dbengine.myt +++ b/doc/build/content/dbengine.myt @@ -141,7 +141,27 @@ engine.connect(environ['db_uri']) # now you have a real db connection and can select, insert, etc. + + <&|doclib.myt:item, name="defaultproxy", description="Using the Global Proxy" &> +

There is an instance of ProxyEngine available within the schema package as "default_engine". You can construct Table objects and not specify the engine parameter, and they will connect to this engine by default. To connect the default_engine, use the global_connect function.

+ <&|formatting.myt:code&> + # define the tables and mappers + from sqlalchemy import * + + # specify a table with no explicit engine + users = Table('users', + Column('user_id', Integer, primary_key=True), + Column('user_name', String) + ) + + # connect the global proxy engine + global_connect('sqlite://filename=foo.db') + # create the table in the selected database + users.create() + + + <&|doclib.myt:item, name="transactions", description="Transactions" &>

A SQLEngine also provides an interface to the transactional capabilities of the underlying DBAPI connection object, as well as the connection object itself. Note that when using the object-relational-mapping package, described in a later section, basic transactional operation is handled for you automatically by its "Unit of Work" system; the methods described here will usually apply just to literal SQL update/delete/insert operations or those performed via the SQL construction library.

diff --git a/lib/sqlalchemy/__init__.py b/lib/sqlalchemy/__init__.py index 5efbf6652b..9f7b99feab 100644 --- a/lib/sqlalchemy/__init__.py +++ b/lib/sqlalchemy/__init__.py @@ -15,3 +15,7 @@ from mapping import * import sqlalchemy.schema import sqlalchemy.ext.proxy sqlalchemy.schema.default_engine = sqlalchemy.ext.proxy.ProxyEngine() + +def global_connect(*args, **kwargs): + sqlalchemy.schema.default_engine.connect(*args, **kwargs) + \ No newline at end of file diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 6d43d8e9d0..a11a1539e8 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -47,6 +47,9 @@ class TableSingleton(type): """a metaclass used by the Table object to provide singleton behavior.""" def __call__(self, name, engine=None, *args, **kwargs): try: + if not isinstance(engine, SchemaEngine): + args = [engine] + list(args) + engine = None if engine is None: engine = default_engine name = str(name) # in case of incoming unicode diff --git a/test/testbase.py b/test/testbase.py index 2a9094f5d0..bddb940bec 100644 --- a/test/testbase.py +++ b/test/testbase.py @@ -1,7 +1,8 @@ import unittest import StringIO import sqlalchemy.engine as engine -import sqlalchemy.ext.proxy +import sqlalchemy.ext.proxy as proxy +import sqlalchemy.schema as schema import re, sys echo = True @@ -47,7 +48,7 @@ def parse_argv(): raise "Could not create engine. specify --db to test runner." if PROXY: - db = sqlalchemy.ext.proxy.ProxyEngine(echo=echo, default_ordering=True) + db = proxy.ProxyEngine(echo=echo, default_ordering=True) db.connect(db_uri) else: db = engine.create_engine(db_uri, echo=echo, default_ordering=True) @@ -103,17 +104,21 @@ class AssertMixin(PersistTest): finally: self.assert_(db.sql_count == count, "desired statement count %d does not match %d" % (count, db.sql_count)) -class EngineAssert(object): +class EngineAssert(proxy.BaseProxyEngine): """decorates a SQLEngine object to match the incoming queries against a set of assertions.""" def __init__(self, engine): - self.engine = engine + self._engine = engine self.realexec = engine.post_exec self.realexec.im_self.post_exec = self.post_exec self.logger = engine.logger self.set_assert_list(None, None) self.sql_count = 0 - def __getattr__(self, key): - return getattr(self.engine, key) + def get_engine(self): + return self._engine + def set_engine(self, e): + self._engine = e +# def __getattr__(self, key): + # return getattr(self.engine, key) def set_assert_list(self, unittest, list): self.unittest = unittest self.assert_list = list diff --git a/test/testtypes.py b/test/testtypes.py index 3f25d696f4..4bee6a3949 100644 --- a/test/testtypes.py +++ b/test/testtypes.py @@ -67,7 +67,7 @@ class ColumnsTest(AssertMixin): 'float_column': 'float_column NUMERIC(25, 2)' } - if not db.engine.__module__.endswith('sqlite'): + if not db.name=='sqlite': expectedResults['float_column'] = 'float_column FLOAT(25)' print db.engine.__module__