]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
engine argument on tables optional
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 2 Mar 2006 00:38:16 +0000 (00:38 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 2 Mar 2006 00:38:16 +0000 (00:38 +0000)
test suite uses BaseProxyEngine as a base for the tester engine
documented global proxy engine

CHANGES
doc/build/content/dbengine.myt
lib/sqlalchemy/__init__.py
lib/sqlalchemy/schema.py
test/testbase.py
test/testtypes.py

diff --git a/CHANGES b/CHANGES
index 5a749f13103b01f42774db606608b177f98bfb58..6724ff1116c1d635a221e11cbcd659a2f8364d0b 100644 (file)
--- 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.
index 838ac2d0383a6ff025fc43a6c99dfead15bbb6a0..4ce3d1b00b0a6668ab0c5f412aab47037a6a75fd 100644 (file)
         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" &>
+    <p>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 <span class="codeline">global_connect</span> function.</p>
+    <&|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" &>
     <p>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.</p>
index 5efbf6652b0f1cd76cc24045dff14940e56d19e4..9f7b99feab8db0adda5daa507f6e9ec601da4bef 100644 (file)
@@ -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
index 6d43d8e9d07a1e19e696fa17213c3db24876bd1e..a11a1539e82287d87e588dabf7f935d0bb1f0899 100644 (file)
@@ -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
index 2a9094f5d0acfa3f5579453e476b66efa74540e6..bddb940bec22e069dec6c4fd08b199d00f787de0 100644 (file)
@@ -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 <sqlite|sqlite_file|postgres|mysql|oracle> 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
index 3f25d696f45d5b92845edb1b32ebb0b301fe0d70..4bee6a3949f3270583f17446dc55c5438be2feac 100644 (file)
@@ -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__