diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-06-26 16:15:19 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-07-08 11:05:11 -0400 |
commit | 91f376692d472a5bf0c4b4033816250ec1ce3ab6 (patch) | |
tree | 31f7f72cbe981eb73ed0ba11808d4fb5ae6b7d51 /lib/sqlalchemy/orm/session.py | |
parent | 3dc9a4a2392d033f9d1bd79dd6b6ecea6281a61c (diff) | |
download | sqlalchemy-91f376692d472a5bf0c4b4033816250ec1ce3ab6.tar.gz |
Add future=True to create_engine/Session; unify select()
Several weeks of using the future_select() construct
has led to the proposal there be just one select() construct
again which features the new join() method, and otherwise accepts
both the 1.x and 2.x argument styles. This would make
migration simpler and reduce confusion.
However, confusion may be increased by the fact that select().join()
is different Current thinking is we may be better off
with a few hard behavioral changes to old and relatively unknown APIs
rather than trying to play both sides within two extremely similar
but subtly different APIs. At the moment, the .join() thing seems
to be the only behavioral change that occurs without the user
taking any explicit steps. Session.execute() will still
behave the old way as we are adding a future flag.
This change also adds the "future" flag to Session() and
session.execute(), so that interpretation of the incoming statement,
as well as that the new style result is returned, does not
occur for existing applications unless they add the use
of this flag.
The change in general is moving the "removed in 2.0" system
further along where we want the test suite to fully pass
even if the SQLALCHEMY_WARN_20 flag is set.
Get many tests to pass when SQLALCHEMY_WARN_20 is set; this
should be ongoing after this patch merges.
Improve the RemovedIn20 warning; these are all deprecated
"since" 1.4, so ensure that's what the messages read.
Make sure the inforamtion link is on all warnings.
Add deprecation warnings for parameters present and
add warnings to all FromClause.select() types of methods.
Fixes: #5379
Fixes: #5284
Change-Id: I765a0b912b3dcd0e995426427d8bb7997cbffd51
References: #5159
Diffstat (limited to 'lib/sqlalchemy/orm/session.py')
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index abc990f7b..f4f7374e4 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -118,6 +118,7 @@ class ORMExecuteState(util.MemoizedSlots): "_compile_state_cls", "_starting_event_idx", "_events_todo", + "_future", ) def __init__( @@ -129,6 +130,7 @@ class ORMExecuteState(util.MemoizedSlots): bind_arguments, compile_state_cls, events_todo, + future, ): self.session = session self.statement = statement @@ -137,6 +139,7 @@ class ORMExecuteState(util.MemoizedSlots): self.bind_arguments = bind_arguments self._compile_state_cls = compile_state_cls self._events_todo = list(events_todo) + self._future = future def _remaining_events(self): return self._events_todo[self._starting_event_idx + 1 :] @@ -212,6 +215,7 @@ class ORMExecuteState(util.MemoizedSlots): _execution_options, _bind_arguments, _parent_execute_state=self, + future=self._future, ) @property @@ -924,6 +928,7 @@ class Session(_SessionClassMethods): self, bind=None, autoflush=True, + future=False, expire_on_commit=True, autocommit=False, twophase=False, @@ -1039,6 +1044,26 @@ class Session(_SessionClassMethods): so that all attribute/object access subsequent to a completed transaction will load from the most recent database state. + :param future: if True, use 2.0 style behavior for the + :meth:`_orm.Session.execute` method. This includes that the + :class:`_engine.Result` object returned will return new-style + tuple rows, as well as that Core constructs such as + :class:`_sql.Select`, + :class:`_sql.Update` and :class:`_sql.Delete` will be interpreted + in an ORM context if they are made against ORM entities rather than + plain :class:`.Table` metadata objects. + + The "future" flag is also available on a per-execution basis + using the :paramref:`_orm.Session.execute.future` flag. + + .. versionadded:: 1.4 + + .. seealso:: + + :ref:`migration_20_toplevel` + + :ref:`migration_20_result_rows` + :param info: optional dictionary of arbitrary data to be associated with this :class:`.Session`. Is available via the :attr:`.Session.info` attribute. Note the dictionary is copied at @@ -1071,6 +1096,7 @@ class Session(_SessionClassMethods): self._flushing = False self._warn_on_events = False self._transaction = None + self.future = future self.hash_key = _new_sessionid() self.autoflush = autoflush self.autocommit = autocommit @@ -1387,6 +1413,7 @@ class Session(_SessionClassMethods): params=None, execution_options=util.immutabledict(), bind_arguments=None, + future=False, _parent_execute_state=None, _add_event=None, **kw @@ -1493,6 +1520,14 @@ class Session(_SessionClassMethods): Contents of this dictionary are passed to the :meth:`.Session.get_bind` method. + :param future: + Use future style execution for this statement. This is + the same effect as the :paramref:`_orm.Session.future` flag, + except at the level of this single statement execution. See + that flag for details. + + .. versionadded:: 1.4 + :param mapper: deprecated; use the bind_arguments dictionary @@ -1518,15 +1553,18 @@ class Session(_SessionClassMethods): """ statement = coercions.expect(roles.CoerceTextStatementRole, statement) + future = future or self.future + if not bind_arguments: bind_arguments = kw elif kw: bind_arguments.update(kw) - if ( + if future and ( statement._propagate_attrs.get("compile_state_plugin", None) == "orm" ): + # note that even without "future" mode, we need compile_state_cls = CompileState._get_plugin_class_for_plugin( statement, "orm" ) @@ -1547,7 +1585,7 @@ class Session(_SessionClassMethods): ) else: bind_arguments.setdefault("clause", statement) - if statement._is_future: + if future: execution_options = util.immutabledict().merge_with( execution_options, {"future_result": True} ) @@ -1568,6 +1606,7 @@ class Session(_SessionClassMethods): bind_arguments, compile_state_cls, events_todo, + future, ) for idx, fn in enumerate(events_todo): orm_exec_state._starting_event_idx = idx |