class PostgreSQL_psycopg2ExecutionContext(PGExecutionContext):
def create_cursor(self):
# TODO: coverage for server side cursors + select.for_update()
- stream_results_option = self.execution_options.get('stream_results')
- is_server_side = (
- # Enabled for this statement ...
- (stream_results_option or
- # ... or enabled for all statements
- (self.dialect.server_side_cursors and
- # ... and not explicitly disabled for this one.
- (stream_results_option or stream_results_option is None))
- ) and (
- # But don't use SS-cursors when autocommit is on ...
- (not self.should_autocommit and
- self.compiled and isinstance(self.compiled.statement, expression.Selectable))
- or (
- # ... or if it's not even a SELECT.
- (not self.compiled or isinstance(self.compiled.statement, expression._TextClause))
- and self.statement and SERVER_SIDE_CURSOR_RE.match(self.statement))))
+
+ if self.dialect.server_side_cursors:
+ is_server_side = \
+ self.execution_options.get('stream_results', True) and (
+ (self.compiled and isinstance(self.compiled.statement, expression.Selectable) \
+ or \
+ (
+ (not self.compiled or
+ isinstance(self.compiled.statement, expression._TextClause))
+ and self.statement and SERVER_SIDE_CURSOR_RE.match(self.statement))
+ )
+ )
+ else:
+ is_server_side = self.execution_options.get('stream_results', False)
self.__is_server_side = is_server_side
if is_server_side:
self.engine = engine
self.__connection = connection or engine.raw_connection()
self.__transaction = None
- self.__close_with_result = close_with_result
+ self.should_close_with_result = close_with_result
self.__savepoint_seq = 0
self.__branch = _branch
self.__invalid = False
return self.__connection
raise exc.InvalidRequestError("This Connection is closed")
- @property
- def should_close_with_result(self):
- """Indicates if this Connection should be closed when a corresponding
- ResultProxy is closed; this is essentially an auto-release mode.
- """
-
- return self.__close_with_result
-
@property
def info(self):
"""A collection of per-DB-API connection instance properties."""
def _execute_default(self, default, multiparams, params):
ctx = self.__create_execution_context()
ret = ctx._exec_default(default)
- if self.__close_with_result:
+ if self.should_close_with_result:
self.close()
return ret
if cursor:
cursor.close()
self._autorollback()
- if self.__close_with_result:
+ if self.should_close_with_result:
self.close()
# Py3K
#raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect) from e
class DefaultExecutionContext(base.ExecutionContext):
+ execution_options = util.frozendict()
+ isinsert = False
+ isupdate = False
+ isdelete = False
+ executemany = False
+ result_map = None
+ compiled = None
+ statement = None
- def __init__(self, dialect, connection, compiled_sql=None, compiled_ddl=None, statement=None, parameters=None):
+ def __init__(self,
+ dialect,
+ connection,
+ compiled_sql=None,
+ compiled_ddl=None,
+ statement=None,
+ parameters=None):
+
self.dialect = dialect
self._connection = self.root_connection = connection
self.engine = connection.engine
if compiled_ddl is not None:
self.compiled = compiled = compiled_ddl
+
+ if compiled.statement._execution_options:
+ self.execution_options = compiled.statement._execution_options
+ if connection._execution_options:
+ self.execution_options = self.execution_options.union(
+ connection._execution_options
+ )
+
if not dialect.supports_unicode_statements:
self.statement = unicode(compiled).encode(self.dialect.encoding)
else:
self.statement = unicode(compiled)
- self.isinsert = self.isupdate = self.isdelete = self.executemany = False
- self.result_map = None
+
self.cursor = self.create_cursor()
self.compiled_parameters = []
self.parameters = [self._default_params]
+
elif compiled_sql is not None:
self.compiled = compiled = compiled_sql
+ if not compiled.can_execute:
+ raise exc.ArgumentError("Not an executable clause: %s" % compiled)
+
+ if compiled.statement._execution_options:
+ self.execution_options = compiled.statement._execution_options
+ if connection._execution_options:
+ self.execution_options = self.execution_options.union(
+ connection._execution_options
+ )
+
# compiled clauseelement. process bind params, process table defaults,
# track collections used by ResultProxy to target and process results
- if not compiled.can_execute:
- raise exc.ArgumentError("Not an executable clause: %s" % compiled)
-
self.processors = dict(
(key, value) for key, value in
( (compiled.bind_names[bindparam],
if not parameters:
self.compiled_parameters = [compiled.construct_params()]
- self.executemany = False
else:
- self.compiled_parameters = [compiled.construct_params(m, _group_number=grp) for grp,m in enumerate(parameters)]
+ self.compiled_parameters = [compiled.construct_params(m, _group_number=grp) for
+ grp,m in enumerate(parameters)]
+
self.executemany = len(parameters) > 1
self.cursor = self.create_cursor()
self.parameters = self.__convert_compiled_params(self.compiled_parameters)
elif statement is not None:
# plain text statement
- self.result_map = self.compiled = None
+ if connection._execution_options:
+ self.execution_options = self.execution_options.union(connection._execution_options)
self.parameters = self.__encode_param_keys(parameters)
self.executemany = len(parameters) > 1
if isinstance(statement, unicode) and not dialect.supports_unicode_statements:
self.statement = statement.encode(self.dialect.encoding)
else:
self.statement = statement
- self.isinsert = self.isupdate = self.isdelete = False
self.cursor = self.create_cursor()
else:
# no statement. used for standalone ColumnDefault execution.
- self.statement = self.compiled = None
- self.isinsert = self.isupdate = self.isdelete = self.executemany = False
+ if connection._execution_options:
+ self.execution_options = self.execution_options.union(connection._execution_options)
self.cursor = self.create_cursor()
-
- @util.memoized_property
- def execution_options(self):
- if self.compiled:
- return self.compiled.statement._execution_options.union(
- self._connection._execution_options)
- else:
- return self._connection._execution_options
@util.memoized_property
def should_autocommit(self):
- autocommit = self.execution_options.get('autocommit', expression.PARSE_AUTOCOMMIT)
+ autocommit = self.execution_options.get('autocommit',
+ not self.compiled and
+ self.statement and
+ expression.PARSE_AUTOCOMMIT
+ or False)
+
if autocommit is expression.PARSE_AUTOCOMMIT:
- if self.statement:
- return self.should_autocommit_text(self.statement)
- else:
- return False
+ return self.should_autocommit_text(self.statement)
else:
return autocommit