[Sqlalchemy-commits] [1076] sqlalchemy/trunk: engine argument on tables optional
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-03-02 00:38:30
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1076] sqlalchemy/trunk: engine argument on tables optional</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1076</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-03-01 18:38:16 -0600 (Wed, 01 Mar 2006)</dd> </dl> <h3>Log Message</h3> <pre>engine argument on tables optional test suite uses BaseProxyEngine as a base for the tester engine documented global proxy engine</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunkCHANGES">sqlalchemy/trunk/CHANGES</a></li> <li><a href="#sqlalchemytrunkdocbuildcontentdbenginemyt">sqlalchemy/trunk/doc/build/content/dbengine.myt</a></li> <li><a href="#sqlalchemytrunklibsqlalchemy__init__py">sqlalchemy/trunk/lib/sqlalchemy/__init__.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyschemapy">sqlalchemy/trunk/lib/sqlalchemy/schema.py</a></li> <li><a href="#sqlalchemytrunktesttestbasepy">sqlalchemy/trunk/test/testbase.py</a></li> <li><a href="#sqlalchemytrunktesttesttypespy">sqlalchemy/trunk/test/testtypes.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunkCHANGES"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/CHANGES (1075 => 1076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/CHANGES 2006-03-02 00:30:49 UTC (rev 1075) +++ sqlalchemy/trunk/CHANGES 2006-03-02 00:38:16 UTC (rev 1076) </span><span class="lines">@@ -27,6 +27,9 @@ </span><span class="cx"> result set side. </span><span class="cx"> - postgres maintains a list of ANSI functions that must have no parenthesis so </span><span class="cx"> function calls with no arguments work consistently </span><ins>+- 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". </ins><span class="cx"> 0.1.2 </span><span class="cx"> - fixed a recursive call in schema that was somehow running 994 times then returning </span><span class="cx"> normally. broke nothing, slowed down everything. thanks to jpellerin for finding this. </span></span></pre></div> <a id="sqlalchemytrunkdocbuildcontentdbenginemyt"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/doc/build/content/dbengine.myt (1075 => 1076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/doc/build/content/dbengine.myt 2006-03-02 00:30:49 UTC (rev 1075) +++ sqlalchemy/trunk/doc/build/content/dbengine.myt 2006-03-02 00:38:16 UTC (rev 1076) </span><span class="lines">@@ -141,8 +141,28 @@ </span><span class="cx"> engine.connect(environ['db_uri']) </span><span class="cx"> # now you have a real db connection and can select, insert, etc. </span><span class="cx"> </&> </span><ins>+ + <&|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') </ins><span class="cx"> </span><ins>+ # create the table in the selected database + users.create() </ins><span class="cx"> </&> </span><ins>+ + </&> + </&> </ins><span class="cx"> <&|doclib.myt:item, name="transactions", description="Transactions" &> </span><span class="cx"> <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> </span><span class="cx"> </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemy__init__py"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/__init__.py (1075 => 1076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/__init__.py 2006-03-02 00:30:49 UTC (rev 1075) +++ sqlalchemy/trunk/lib/sqlalchemy/__init__.py 2006-03-02 00:38:16 UTC (rev 1076) </span><span class="lines">@@ -15,3 +15,7 @@ </span><span class="cx"> import sqlalchemy.schema </span><span class="cx"> import sqlalchemy.ext.proxy </span><span class="cx"> sqlalchemy.schema.default_engine = sqlalchemy.ext.proxy.ProxyEngine() </span><ins>+ +def global_connect(*args, **kwargs): + sqlalchemy.schema.default_engine.connect(*args, **kwargs) + </ins><span class="cx">\ No newline at end of file </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyschemapy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/schema.py (1075 => 1076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/schema.py 2006-03-02 00:30:49 UTC (rev 1075) +++ sqlalchemy/trunk/lib/sqlalchemy/schema.py 2006-03-02 00:38:16 UTC (rev 1076) </span><span class="lines">@@ -47,6 +47,9 @@ </span><span class="cx"> """a metaclass used by the Table object to provide singleton behavior.""" </span><span class="cx"> def __call__(self, name, engine=None, *args, **kwargs): </span><span class="cx"> try: </span><ins>+ if not isinstance(engine, SchemaEngine): + args = [engine] + list(args) + engine = None </ins><span class="cx"> if engine is None: </span><span class="cx"> engine = default_engine </span><span class="cx"> name = str(name) # in case of incoming unicode </span></span></pre></div> <a id="sqlalchemytrunktesttestbasepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/testbase.py (1075 => 1076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/testbase.py 2006-03-02 00:30:49 UTC (rev 1075) +++ sqlalchemy/trunk/test/testbase.py 2006-03-02 00:38:16 UTC (rev 1076) </span><span class="lines">@@ -1,7 +1,8 @@ </span><span class="cx"> import unittest </span><span class="cx"> import StringIO </span><span class="cx"> import sqlalchemy.engine as engine </span><del>-import sqlalchemy.ext.proxy </del><ins>+import sqlalchemy.ext.proxy as proxy +import sqlalchemy.schema as schema </ins><span class="cx"> import re, sys </span><span class="cx"> </span><span class="cx"> echo = True </span><span class="lines">@@ -47,7 +48,7 @@ </span><span class="cx"> raise "Could not create engine. specify --db <sqlite|sqlite_file|postgres|mysql|oracle> to test runner." </span><span class="cx"> </span><span class="cx"> if PROXY: </span><del>- db = sqlalchemy.ext.proxy.ProxyEngine(echo=echo, default_ordering=True) </del><ins>+ db = proxy.ProxyEngine(echo=echo, default_ordering=True) </ins><span class="cx"> db.connect(db_uri) </span><span class="cx"> else: </span><span class="cx"> db = engine.create_engine(db_uri, echo=echo, default_ordering=True) </span><span class="lines">@@ -103,17 +104,21 @@ </span><span class="cx"> finally: </span><span class="cx"> self.assert_(db.sql_count == count, "desired statement count %d does not match %d" % (count, db.sql_count)) </span><span class="cx"> </span><del>-class EngineAssert(object): </del><ins>+class EngineAssert(proxy.BaseProxyEngine): </ins><span class="cx"> """decorates a SQLEngine object to match the incoming queries against a set of assertions.""" </span><span class="cx"> def __init__(self, engine): </span><del>- self.engine = engine </del><ins>+ self._engine = engine </ins><span class="cx"> self.realexec = engine.post_exec </span><span class="cx"> self.realexec.im_self.post_exec = self.post_exec </span><span class="cx"> self.logger = engine.logger </span><span class="cx"> self.set_assert_list(None, None) </span><span class="cx"> self.sql_count = 0 </span><del>- def __getattr__(self, key): - return getattr(self.engine, key) </del><ins>+ def get_engine(self): + return self._engine + def set_engine(self, e): + self._engine = e +# def __getattr__(self, key): + # return getattr(self.engine, key) </ins><span class="cx"> def set_assert_list(self, unittest, list): </span><span class="cx"> self.unittest = unittest </span><span class="cx"> self.assert_list = list </span></span></pre></div> <a id="sqlalchemytrunktesttesttypespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/testtypes.py (1075 => 1076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/testtypes.py 2006-03-02 00:30:49 UTC (rev 1075) +++ sqlalchemy/trunk/test/testtypes.py 2006-03-02 00:38:16 UTC (rev 1076) </span><span class="lines">@@ -67,7 +67,7 @@ </span><span class="cx"> 'float_column': 'float_column NUMERIC(25, 2)' </span><span class="cx"> } </span><span class="cx"> </span><del>- if not db.engine.__module__.endswith('sqlite'): </del><ins>+ if not db.name=='sqlite': </ins><span class="cx"> expectedResults['float_column'] = 'float_column FLOAT(25)' </span><span class="cx"> </span><span class="cx"> print db.engine.__module__ </span></span></pre> </div> </div> </body> </html> |