[Sqlalchemy-commits] [1441] sqlalchemy/branches/schema/doc/build/content: created "Connectable" inte
Brought to you by:
zzzeek
<!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>[1441] sqlalchemy/branches/schema/doc/build/content: created "Connectable" interface to implement the "connection_or_engine" parameter used in Session</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1441</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-05-11 16:01:16 -0500 (Thu, 11 May 2006)</dd> </dl> <h3>Log Message</h3> <pre>created "Connectable" interface to implement the "connection_or_engine" parameter used in Session</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemadocbuildcontentunitofworktxt">sqlalchemy/branches/schema/doc/build/content/unitofwork.txt</a></li> <li><a href="#sqlalchemybranchesschemalibsqlalchemyenginebasepy">sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py</a></li> <li><a href="#sqlalchemybranchesschemalibsqlalchemyormsessionpy">sqlalchemy/branches/schema/lib/sqlalchemy/orm/session.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemadocbuildcontentunitofworktxt"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/doc/build/content/unitofwork.txt (1440 => 1441)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/doc/build/content/unitofwork.txt 2006-05-11 20:42:22 UTC (rev 1440) +++ sqlalchemy/branches/schema/doc/build/content/unitofwork.txt 2006-05-11 21:01:16 UTC (rev 1441) </span><span class="lines">@@ -300,7 +300,7 @@ </span><span class="cx"> sess.bind_mapper(mymapper, sqlite_connection) # bind mymapper operations to a single SQLite connection </span><span class="cx"> sess.bind_table(email_addresses_table, engine2) # bind operations with the email_addresses_table to mysql </span><span class="cx"> </span><del>-Normally, when a `Session` is created via `create_session()` with no arguments, the Session has no awareness of individual `Engines`, and when mappers use the `Session` to retrieve connections, the underlying `MetaData` each `Table` is associated with is expected to be "bound" to an `Engine`, else no engine can be located and an exception is raised. A second form of `create_session()` takes the argument `bind_to=engine_or_connection`, where all SQL operations performed by this `Session` use the single `Engine` or `Connection` passed to the constructor. With `bind_mapper()` and `bind_table()`, the operations of individual mapper and/or tables are bound to distinct engines or connections, thereby overriding not only the engine which may be "bound" to the underlying `MetaData`, but also the `Engine` or `Connection` which may have been passed to the `create_session()` function. Configurations which interact with multiple explicit database con! nections at one time must use either or both of these methods in order to associate `Session` operations with the appropriate connection resource. </del><ins>+Normally, when a `Session` is created via `create_session()` with no arguments, the Session has no awareness of individual `Engines`, and when mappers use the `Session` to retrieve connections, the underlying `MetaData` each `Table` is associated with is expected to be "bound" to an `Engine`, else no engine can be located and an exception is raised. A second form of `create_session()` takes the argument `bind_to=engine_or_connection`, where all SQL operations performed by this `Session` use the single `Engine` or `Connection` (collectively known as a `Connectable`) passed to the constructor. With `bind_mapper()` and `bind_table()`, the operations of individual mapper and/or tables are bound to distinct engines or connections, thereby overriding not only the engine which may be "bound" to the underlying `MetaData`, but also the `Engine` or `Connection` which may have been passed to the `create_session()` function. Configurations which inter! act with multiple explicit database connections at one time must use either or both of these methods in order to associate `Session` operations with the appropriate connection resource. </ins><span class="cx"> </span><span class="cx"> Binding a `Mapper` to a resource takes precedence over a `Table` bind, meaning if mapper A is associated with table B, and the Session binds mapper A to connection X and table B to connection Y, an operation with mapper A will use connection X, not connection Y. </span><span class="cx"> </span></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemyenginebasepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py (1440 => 1441)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py 2006-05-11 20:42:22 UTC (rev 1440) +++ sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py 2006-05-11 21:01:16 UTC (rev 1441) </span><span class="lines">@@ -152,7 +152,16 @@ </span><span class="cx"> indicating that the database inserted data beyond that which we gave it. this value is thread-local.""" </span><span class="cx"> raise NotImplementedError() </span><span class="cx"> </span><del>-class Connection(object): </del><ins>+class Connectable(object): + """interface for an object that can provide an Engine and a Connection object which correponds to that Engine.""" + def contextual_connect(self): + """returns a Connection object which may be part of an ongoing context.""" + raise NotImplementedError() + def _not_impl(self): + raise NotImplementedError() + engine = property(_not_impl, doc="returns the Engine which this Connectable is associated with.") + +class Connection(Connectable): </ins><span class="cx"> """represents a single DBAPI connection returned from the underlying connection pool. Provides </span><span class="cx"> execution support for string-based SQL statements as well as ClauseElement, Compiled and DefaultGenerator objects. </span><span class="cx"> provides a begin method to return Transaction objects.""" </span><span class="lines">@@ -341,7 +350,7 @@ </span><span class="cx"> self.__connection._commit_impl() </span><span class="cx"> self.__is_active = False </span><span class="cx"> </span><del>-class ComposedSQLEngine(sql.Engine): </del><ins>+class ComposedSQLEngine(sql.Engine, Connectable): </ins><span class="cx"> """ </span><span class="cx"> Connects a ConnectionProvider, a Dialect and a CompilerFactory together to </span><span class="cx"> provide a default implementation of SchemaEngine. </span></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemyormsessionpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/orm/session.py (1440 => 1441)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/orm/session.py 2006-05-11 20:42:22 UTC (rev 1440) +++ sqlalchemy/branches/schema/lib/sqlalchemy/orm/session.py 2006-05-11 21:01:16 UTC (rev 1441) </span><span class="lines">@@ -24,25 +24,21 @@ </span><span class="cx"> return self.get_or_add(engine) </span><span class="cx"> def _begin(self): </span><span class="cx"> return SessionTransaction(self.session, self) </span><del>- def add(self, connection_or_engine): - if self.connections.has_key(connection_or_engine.engine): </del><ins>+ def add(self, connectable): + if self.connections.has_key(connectable.engine): </ins><span class="cx"> raise exceptions.InvalidRequestError("Session already has a Connection associated for the given Connection's Engine") </span><del>- return self.get_or_add(connection_or_engine) - def get_or_add(self, connection_or_engine): </del><ins>+ return self.get_or_add(connectable) + def get_or_add(self, connectable): </ins><span class="cx"> # we reference the 'engine' attribute on the given object, which in the case of </span><span class="cx"> # Connection, ProxyEngine, Engine, ComposedSQLEngine, whatever, should return the original </span><span class="cx"> # "Engine" object that is handling the connection. </span><del>- if self.connections.has_key(connection_or_engine.engine): - return self.connections[connection_or_engine.engine][0] - if isinstance(connection_or_engine, sqlalchemy.engine.base.Connection): - e = connection_or_engine.engine - c = connection_or_engine - else: - e = connection_or_engine - c = connection_or_engine.contextual_connect() - if not self.connections.has_key(e.engine): - self.connections[e.engine] = (c, c.begin()) - return self.connections[e.engine][0] </del><ins>+ if self.connections.has_key(connectable.engine): + return self.connections[connectable.engine][0] + e = connectable.engine + c = connectable.contextual_connect() + if not self.connections.has_key(e): + self.connections[e] = (c, c.begin()) + return self.connections[e][0] </ins><span class="cx"> def commit(self): </span><span class="cx"> if self.parent is not None: </span><span class="cx"> return </span></span></pre> </div> </div> </body> </html> |