From: Mike Bayer Date: Fri, 29 Jan 2010 02:01:11 +0000 (+0000) Subject: - inline some code and turn some instance-level defaults into class level X-Git-Tag: rel_0_6beta1~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a04c4dc42cf4c3c35163b59a78f05efe47547dc0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - inline some code and turn some instance-level defaults into class level --- diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 54283581d6..bb6562deaf 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -112,22 +112,20 @@ SERVER_SIDE_CURSOR_RE = re.compile( 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: diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 3d192a9be6..6e4a342195 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -732,7 +732,7 @@ class Connection(Connectable): 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 @@ -798,14 +798,6 @@ class Connection(Connectable): 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.""" @@ -1080,7 +1072,7 @@ class Connection(Connectable): 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 @@ -1161,7 +1153,7 @@ class Connection(Connectable): 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 diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index e2a03d2276..44c9689423 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -224,32 +224,62 @@ class DefaultDialect(base.Dialect): 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], @@ -270,9 +300,10 @@ class DefaultExecutionContext(base.ExecutionContext): 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() @@ -281,38 +312,32 @@ class DefaultExecutionContext(base.ExecutionContext): 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 diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 5edb6e47fe..f0f55ed725 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -3925,7 +3925,8 @@ class _UpdateBase(_Executable, ClauseElement): if m: self._returning = kwargs.pop(k) util.warn_deprecated( - "The %r argument is deprecated. Please use statement.returning(col1, col2, ...)" % k + "The %r argument is deprecated. Please " + "use statement.returning(col1, col2, ...)" % k ) return kwargs @@ -4007,6 +4008,8 @@ class Insert(_ValuesBase): _prefixes = () + kwargs = util.frozendict() + def __init__(self, table, values=None, @@ -4022,8 +4025,9 @@ class Insert(_ValuesBase): self._returning = returning if prefixes: self._prefixes = tuple([_literal_as_text(p) for p in prefixes]) - - self.kwargs = self._process_deprecated_kw(kwargs) + + if kwargs: + self.kwargs = self._process_deprecated_kw(kwargs) def get_children(self, **kwargs): if self.select is not None: diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index b4e9ba0cd0..2f7575d3b0 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -388,9 +388,9 @@ def to_instance(typeobj): if typeobj is None: return NULLTYPE - try: + if util.callable(typeobj): return typeobj() - except TypeError: + else: return typeobj def adapt_type(typeobj, colspecs):