[Sqlalchemy-commits] [1438] sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py: factored all i
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-05-11 20:09:24
|
<!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>[1438] sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py: factored all instnace variables in Conneciton to be private.</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1438</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-05-11 15:09:13 -0500 (Thu, 11 May 2006)</dd> </dl> <h3>Log Message</h3> <pre>factored all instnace variables in Conneciton to be private.</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemalibsqlalchemyenginebasepy">sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemalibsqlalchemyenginebasepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py (1437 => 1438)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py 2006-05-11 19:55:30 UTC (rev 1437) +++ sqlalchemy/branches/schema/lib/sqlalchemy/engine/base.py 2006-05-11 20:09:13 UTC (rev 1438) </span><span class="lines">@@ -157,10 +157,12 @@ </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="cx"> def __init__(self, engine, connection=None, close_with_result=False): </span><del>- self.engine = engine - self.connection = connection or engine.raw_connection() - self.transaction = None - self.close_with_result = close_with_result </del><ins>+ self.__engine = engine + self.__connection = connection or engine.raw_connection() + self.__transaction = None + self.__close_with_result = close_with_result + engine = property(lambda s:s.__engine, doc="The Engine with which this Connection is associated (read only)") + should_close_with_result = property(lambda s:s.__close_with_result, doc="Indicates if this Connection should be closed when a corresponding ResultProxy is closed; this is essentially an auto-release mode.") </ins><span class="cx"> def _create_transaction(self, parent): </span><span class="cx"> return Transaction(self, parent) </span><span class="cx"> def connect(self): </span><span class="lines">@@ -170,21 +172,33 @@ </span><span class="cx"> """contextual_connect() is implemented to return self so that an incoming Engine or Connection object can be treated similarly.""" </span><span class="cx"> return self </span><span class="cx"> def begin(self): </span><del>- if self.transaction is None: - self.transaction = self._create_transaction(None) - return self.transaction </del><ins>+ if self.__transaction is None: + self.__transaction = self._create_transaction(None) + return self.__transaction </ins><span class="cx"> else: </span><del>- return self._create_transaction(self.transaction) </del><ins>+ return self._create_transaction(self.__transaction) + def _begin_impl(self): + if self.__engine.echo: + self.__engine.log("BEGIN") + self.__engine.dialect.do_begin(self.__connection) + def _rollback_impl(self): + if self.__engine.echo: + self.__engine.log("ROLLBACK") + self.__engine.dialect.do_rollback(self.__connection) + def _commit_impl(self): + if self.__engine.echo: + self.__engine.log("COMMIT") + self.__engine.dialect.do_commit(self.__connection) </ins><span class="cx"> def _autocommit(self, statement): </span><span class="cx"> """when no Transaction is present, this is called after executions to provide "autocommit" behavior.""" </span><span class="cx"> # TODO: have the dialect determine if autocommit can be set on the connection directly without this </span><span class="cx"> # extra step </span><del>- if self.transaction is None and re.match(r'UPDATE|INSERT|CREATE|DELETE|DROP', statement.lstrip().upper()): - self.engine.dialect.do_commit(self.connection) </del><ins>+ if self.__transaction is None and re.match(r'UPDATE|INSERT|CREATE|DELETE|DROP', statement.lstrip().upper()): + self._commit_impl() </ins><span class="cx"> def close(self): </span><del>- if self.connection is not None: - self.connection.close() - self.connection = None </del><ins>+ if self.__connection is not None: + self.__connection.close() + self.__connection = None </ins><span class="cx"> def scalar(self, object, parameters, **kwargs): </span><span class="cx"> row = self.execute(object, parameters, **kwargs).fetchone() </span><span class="cx"> if row is not None: </span><span class="lines">@@ -194,10 +208,10 @@ </span><span class="cx"> def execute(self, object, *multiparams, **params): </span><span class="cx"> return Connection.executors[type(object).__mro__[-2]](self, object, *multiparams, **params) </span><span class="cx"> def execute_default(self, default, **kwargs): </span><del>- return default.accept_schema_visitor(self.engine.dialect.defaultrunner(self.engine, self.proxy, **kwargs)) </del><ins>+ return default.accept_schema_visitor(self.__engine.dialect.defaultrunner(self.__engine, self.proxy, **kwargs)) </ins><span class="cx"> def execute_text(self, statement, parameters=None): </span><span class="cx"> cursor = self._execute_raw(statement, parameters) </span><del>- return ResultProxy(self.engine, self, cursor) </del><ins>+ return ResultProxy(self.__engine, self, cursor) </ins><span class="cx"> def _params_to_listofdicts(self, *multiparams, **params): </span><span class="cx"> if len(multiparams) == 0: </span><span class="cx"> return [params] </span><span class="lines">@@ -216,10 +230,10 @@ </span><span class="cx"> param = multiparams[0] </span><span class="cx"> else: </span><span class="cx"> param = params </span><del>- return self.execute_compiled(elem.compile(engine=self.engine, parameters=param), *multiparams, **params) </del><ins>+ return self.execute_compiled(elem.compile(engine=self.__engine, parameters=param), *multiparams, **params) </ins><span class="cx"> def execute_compiled(self, compiled, *multiparams, **params): </span><span class="cx"> """executes a sql.Compiled object.""" </span><del>- cursor = self.connection.cursor() </del><ins>+ cursor = self.__connection.cursor() </ins><span class="cx"> parameters = [compiled.get_params(**m) for m in self._params_to_listofdicts(*multiparams, **params)] </span><span class="cx"> if len(parameters) == 1: </span><span class="cx"> parameters = parameters[0] </span><span class="lines">@@ -227,14 +241,14 @@ </span><span class="cx"> if statement is None: </span><span class="cx"> return cursor </span><span class="cx"> </span><del>- parameters = self.engine.dialect.convert_compiled_params(parameters) </del><ins>+ parameters = self.__engine.dialect.convert_compiled_params(parameters) </ins><span class="cx"> self._execute_raw(statement, parameters, cursor=cursor, context=context) </span><span class="cx"> return cursor </span><del>- context = self.engine.dialect.create_execution_context() - context.pre_exec(self.engine, proxy, compiled, parameters) </del><ins>+ context = self.__engine.dialect.create_execution_context() + context.pre_exec(self.__engine, proxy, compiled, parameters) </ins><span class="cx"> proxy(str(compiled), parameters) </span><del>- context.post_exec(self.engine, proxy, compiled, parameters) - return ResultProxy(self.engine, self, cursor, context, typemap=compiled.typemap) </del><ins>+ context.post_exec(self.__engine, proxy, compiled, parameters) + return ResultProxy(self.__engine, self, cursor, context, typemap=compiled.typemap) </ins><span class="cx"> </span><span class="cx"> # poor man's multimethod/generic function thingy </span><span class="cx"> executors = { </span><span class="lines">@@ -246,24 +260,24 @@ </span><span class="cx"> </span><span class="cx"> def create(self, entity, **kwargs): </span><span class="cx"> """creates a table or index given an appropriate schema object.""" </span><del>- return self.engine.create(entity, connection=self, **kwargs) </del><ins>+ return self.__engine.create(entity, connection=self, **kwargs) </ins><span class="cx"> def drop(self, entity, **kwargs): </span><span class="cx"> """drops a table or index given an appropriate schema object.""" </span><del>- return self.engine.drop(entity, connection=self, **kwargs) </del><ins>+ return self.__engine.drop(entity, connection=self, **kwargs) </ins><span class="cx"> def reflecttable(self, table, **kwargs): </span><span class="cx"> """reflects the columns in the given table from the database.""" </span><del>- return self.engine.reflecttable(table, connection=self, **kwargs) </del><ins>+ return self.__engine.reflecttable(table, connection=self, **kwargs) </ins><span class="cx"> def default_schema_name(self): </span><del>- return self.engine.dialect.get_default_schema_name(self) </del><ins>+ return self.__engine.dialect.get_default_schema_name(self) </ins><span class="cx"> def run_callable(self, callable_): </span><span class="cx"> callable_(self) </span><span class="cx"> def _execute_raw(self, statement, parameters=None, cursor=None, echo=None, context=None, **kwargs): </span><span class="cx"> if cursor is None: </span><del>- cursor = self.connection.cursor() </del><ins>+ cursor = self.__connection.cursor() </ins><span class="cx"> try: </span><del>- if echo is True or self.engine.echo is not False: - self.engine.log(statement) - self.engine.log(repr(parameters)) </del><ins>+ if echo is True or self.__engine.echo is not False: + self.__engine.log(statement) + self.__engine.log(repr(parameters)) </ins><span class="cx"> if parameters is not None and isinstance(parameters, list) and len(parameters) > 0 and (isinstance(parameters[0], list) or isinstance(parameters[0], dict)): </span><span class="cx"> self._executemany(cursor, statement, parameters, context=context) </span><span class="cx"> else: </span><span class="lines">@@ -275,23 +289,23 @@ </span><span class="cx"> </span><span class="cx"> def _execute(self, c, statement, parameters, context=None): </span><span class="cx"> if parameters is None: </span><del>- if self.engine.dialect.positional: </del><ins>+ if self.__engine.dialect.positional: </ins><span class="cx"> parameters = () </span><span class="cx"> else: </span><span class="cx"> parameters = {} </span><span class="cx"> try: </span><del>- self.engine.dialect.do_execute(c, statement, parameters, context=context) </del><ins>+ self.__engine.dialect.do_execute(c, statement, parameters, context=context) </ins><span class="cx"> except Exception, e: </span><del>- self.engine.dialect.do_rollback(self.connection) - if self.close_with_result: </del><ins>+ self._rollback_impl() + if self.__close_with_result: </ins><span class="cx"> self.close() </span><span class="cx"> raise exceptions.SQLError(statement, parameters, e) </span><span class="cx"> def _executemany(self, c, statement, parameters, context=None): </span><span class="cx"> try: </span><del>- self.engine.dialect.do_executemany(c, statement, parameters, context=context) </del><ins>+ self.__engine.dialect.do_executemany(c, statement, parameters, context=context) </ins><span class="cx"> except Exception, e: </span><del>- self.engine.dialect.do_rollback(self.connection) - if self.close_with_result: </del><ins>+ self.__engine.dialect.do_rollback(self.__connection) + if self.__close_with_result: </ins><span class="cx"> self.close() </span><span class="cx"> raise exceptions.SQLError(statement, parameters, e) </span><span class="cx"> def proxy(self, statement=None, parameters=None): </span><span class="lines">@@ -300,7 +314,7 @@ </span><span class="cx"> This callable is a generic version of a connection/cursor-specific callable that </span><span class="cx"> is produced within the execute_compiled method, and is used for objects that require </span><span class="cx"> this style of proxy when outside of an execute_compiled method, primarily the DefaultRunner.""" </span><del>- parameters = self.engine.dialect.convert_compiled_params(parameters) </del><ins>+ parameters = self.__engine.dialect.convert_compiled_params(parameters) </ins><span class="cx"> return self._execute_raw(statement, parameters) </span><span class="cx"> </span><span class="cx"> class Transaction(object): </span><span class="lines">@@ -310,16 +324,12 @@ </span><span class="cx"> self.parent = parent or self </span><span class="cx"> self.is_active = True </span><span class="cx"> if self.parent is self: </span><del>- self.connection.engine.dialect.do_begin(self.connection.connection) - if self.connection.engine.echo: - self.connection.engine.log("BEGIN") </del><ins>+ self.connection._begin_impl() </ins><span class="cx"> def rollback(self): </span><span class="cx"> if not self.parent.is_active: </span><span class="cx"> raise exceptions.InvalidRequestError("This transaction is inactive") </span><span class="cx"> if self.parent is self: </span><del>- if self.connection.engine.echo: - self.connection.engine.log("ROLLBACK") - self.connection.engine.dialect.do_rollback(self.connection.connection) </del><ins>+ self.connection._rollback_impl() </ins><span class="cx"> self.is_active = False </span><span class="cx"> else: </span><span class="cx"> self.parent.rollback() </span><span class="lines">@@ -327,9 +337,7 @@ </span><span class="cx"> if not self.parent.is_active: </span><span class="cx"> raise exceptions.InvalidRequestError("This transaction is inactive") </span><span class="cx"> if self.parent is self: </span><del>- if self.connection.engine.echo: - self.connection.engine.log("COMMIT") - self.connection.engine.dialect.do_commit(self.connection.connection) </del><ins>+ self.connection._commit_impl() </ins><span class="cx"> self.is_active = False </span><span class="cx"> </span><span class="cx"> class ComposedSQLEngine(sql.Engine): </span><span class="lines">@@ -493,7 +501,7 @@ </span><span class="cx"> def close(self): </span><span class="cx"> if not self.closed: </span><span class="cx"> self.closed = True </span><del>- if self.connection.close_with_result and self.dialect.supports_autoclose_results: </del><ins>+ if self.connection.should_close_with_result and self.dialect.supports_autoclose_results: </ins><span class="cx"> self.connection.close() </span><span class="cx"> def _get_col(self, row, key): </span><span class="cx"> if isinstance(key, sql.ColumnElement): </span></span></pre> </div> </div> </body> </html> |