try:
return self.__connection
except AttributeError:
- if self.__invalid:
- if self.__transaction is not None:
- raise exc.InvalidRequestError(
- "Can't reconnect until invalid "
- "transaction is rolled back")
- self.__connection = self.engine.raw_connection()
- self.__invalid = False
- return self.__connection
- raise exc.ResourceClosedError("This Connection is closed")
-
+ return self._revalidate_connection()
+
+ def _revalidate_connection(self):
+ if self.__invalid:
+ if self.__transaction is not None:
+ raise exc.InvalidRequestError(
+ "Can't reconnect until invalid "
+ "transaction is rolled back")
+ self.__connection = self.engine.raw_connection()
+ self.__invalid = False
+ return self.__connection
+ raise exc.ResourceClosedError("This Connection is closed")
+
@property
def _connection_is_valid(self):
# use getattr() for is_valid to support exceptions raised in
"""Execute a schema.ColumnDefault object."""
try:
+ try:
+ conn = self.__connection
+ except AttributeError:
+ conn = self._revalidate_connection()
+
dialect = self.dialect
ctx = dialect.execution_ctx_cls._init_default(
- dialect, self)
+ dialect, self, conn)
except Exception, e:
self._handle_dbapi_exception(e, None, None, None, None)
raise
a :class:`.ResultProxy`."""
try:
- context = constructor(dialect, self, *args)
+ try:
+ conn = self.__connection
+ except AttributeError:
+ conn = self._revalidate_connection()
+
+ context = constructor(dialect, self, conn, *args)
except Exception, e:
self._handle_dbapi_exception(e,
statement, parameters,
result_map = None
compiled = None
statement = None
+ _is_implicit_returning = False
+ _is_explicit_returning = False
@classmethod
- def _init_ddl(cls, dialect, connection, compiled_ddl):
+ def _init_ddl(cls, dialect, connection, dbapi_connection, compiled_ddl):
"""Initialize execution context for a DDLElement construct."""
self = cls.__new__(cls)
self.dialect = dialect
- self._connection = self.root_connection = connection
+ self.root_connection = connection
+ self._dbapi_connection = dbapi_connection
self.engine = connection.engine
self.compiled = compiled = compiled_ddl
return self
@classmethod
- def _init_compiled(cls, dialect, connection, compiled, parameters):
+ def _init_compiled(cls, dialect, connection, dbapi_connection, compiled, parameters):
"""Initialize execution context for a Compiled construct."""
self = cls.__new__(cls)
self.dialect = dialect
- self._connection = self.root_connection = connection
+ self.root_connection = connection
+ self._dbapi_connection = dbapi_connection
self.engine = connection.engine
self.compiled = compiled
self.isinsert = compiled.isinsert
self.isupdate = compiled.isupdate
self.isdelete = compiled.isdelete
+
+ if self.isinsert or self.isupdate or self.isdelete:
+ self._is_explicit_returning = compiled.statement._returning
+ self._is_implicit_returning = compiled.returning and \
+ not compiled.statement._returning
if not parameters:
self.compiled_parameters = [compiled.construct_params()]
return self
@classmethod
- def _init_statement(cls, dialect, connection, statement, parameters):
+ def _init_statement(cls, dialect, connection, dbapi_connection, statement, parameters):
"""Initialize execution context for a string SQL statement."""
self = cls.__new__(cls)
self.dialect = dialect
- self._connection = self.root_connection = connection
+ self.root_connection = connection
+ self._dbapi_connection = dbapi_connection
self.engine = connection.engine
# plain text statement
return self
@classmethod
- def _init_default(cls, dialect, connection):
+ def _init_default(cls, dialect, connection, dbapi_connection):
"""Initialize execution context for a ColumnDefault construct."""
self = cls.__new__(cls)
self.dialect = dialect
- self._connection = self.root_connection = connection
+ self.root_connection = connection
+ self._dbapi_connection = dbapi_connection
self.engine = connection.engine
self.execution_options = connection._execution_options
self.cursor = self.create_cursor()
else:
return autocommit
- @util.memoized_property
- def _is_explicit_returning(self):
- return self.compiled and \
- getattr(self.compiled.statement, '_returning', False)
-
- @util.memoized_property
- def _is_implicit_returning(self):
- return self.compiled and \
- bool(self.compiled.returning) and \
- not self.compiled.statement._returning
-
def _execute_scalar(self, stmt):
"""Execute a string statement on the current cursor, returning a
scalar result.
"""
- conn = self._connection
+ conn = self.root_connection
if isinstance(stmt, unicode) and \
not self.dialect.supports_unicode_statements:
stmt = stmt.encode(self.dialect.encoding)
@property
def connection(self):
- return self._connection._branch()
+ return self.root_connection._branch()
def should_autocommit_text(self, statement):
return AUTOCOMMIT_REGEXP.match(statement)
def create_cursor(self):
- return self._connection.connection.cursor()
+ return self._dbapi_connection.cursor()
def pre_exec(self):
pass
def post_insert(self):
if self.dialect.postfetch_lastrowid and \
- (not len(self.inserted_primary_key) or \
+ (not self.inserted_primary_key or \
None in self.inserted_primary_key):
table = self.compiled.statement.table
try:
self.cursor.setinputsizes(*inputsizes)
except Exception, e:
- self._connection._handle_dbapi_exception(e, None, None, None, self)
+ self.root_connection._handle_dbapi_exception(e, None, None, None, self)
raise
else:
inputsizes = {}
try:
self.cursor.setinputsizes(**inputsizes)
except Exception, e:
- self._connection._handle_dbapi_exception(e, None, None, None, self)
+ self.root_connection._handle_dbapi_exception(e, None, None, None, self)
raise
def _exec_default(self, default):
if params:
pd = {}
for bindparam, name in self.bind_names.iteritems():
- for paramname in (bindparam.key, name):
- if paramname in params:
- pd[name] = params[paramname]
- break
- else:
- if bindparam.required:
- if _group_number:
- raise exc.InvalidRequestError(
- "A value is required for bind parameter %r, "
- "in parameter group %d" %
- (bindparam.key, _group_number))
- else:
- raise exc.InvalidRequestError(
- "A value is required for bind parameter %r"
- % bindparam.key)
- elif bindparam.callable:
- pd[name] = bindparam.callable()
+ if bindparam.key in params:
+ pd[name] = params[bindparam.key]
+ elif name in params:
+ pd[name] = params[name]
+ elif bindparam.required:
+ if _group_number:
+ raise exc.InvalidRequestError(
+ "A value is required for bind parameter %r, "
+ "in parameter group %d" %
+ (bindparam.key, _group_number))
else:
- pd[name] = bindparam.value
+ raise exc.InvalidRequestError(
+ "A value is required for bind parameter %r"
+ % bindparam.key)
+ elif bindparam.callable:
+ pd[name] = bindparam.callable()
+ else:
+ pd[name] = bindparam.value
return pd
else:
pd = {}