Skip to content

Commit 6930dfc

Browse files
committed
Convert execution to move through Session
This patch replaces the ORM execution flow with a single pathway through Session.execute() for all queries, including Core and ORM. Currently included is full support for ORM Query, Query.from_statement(), select(), as well as the baked query and horizontal shard systems. Initial changes have also been made to the dogpile caching example, which like baked query makes use of a new ORM-specific execution hook that replaces the use of both QueryEvents.before_compile() as well as Query._execute_and_instances() as the central ORM interception hooks. select() and Query() constructs alike can be passed to Session.execute() where they will return ORM results in a Results object. This API is currently used internally by Query. Full support for Session.execute()->results to behave in a fully 2.0 fashion will be in later changesets. bulk update/delete with ORM support will also be delivered via the update() and delete() constructs, however these have not yet been adapted to the new system and may follow in a subsequent update. Performance is also beginning to lag as of this commit and some previous ones. It is hoped that a few central functions such as the coercions functions can be rewritten in C to re-gain performance. Additionally, query caching is now available and some subsequent patches will attempt to cache more of the per-execution work from the ORM layer, e.g. column getters and adapters. This patch also contains initial "turn on" of the caching system enginewide via the query_cache_size parameter to create_engine(). Still defaulting at zero for "no caching". The caching system still needs adjustments in order to gain adequate performance. Change-Id: I047a7ebb26aa85dc01f6789fac2bff561dcd555d
1 parent dce8c7a commit 6930dfc

71 files changed

Lines changed: 3208 additions & 1524 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/build/orm/extensions/baked.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ cache the **return results** from the database. A technique that demonstrates
2020
the caching of the SQL calls and result sets themselves is available in
2121
:ref:`examples_caching`.
2222

23+
.. deprecated:: 1.4 SQLAlchemy 1.4 and 2.0 feature an all-new direct query
24+
caching system that removes the need for the :class:`.BakedQuery` system.
25+
Caching is now built in to all Core and ORM queries using the
26+
:paramref:`.create_engine.query_cache_size` parameter.
27+
2328

2429
.. versionadded:: 1.0.0
2530

doc/build/orm/session_api.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. currentmodule:: sqlalchemy.orm.session
1+
.. currentmodule:: sqlalchemy.orm
22

33
Session API
44
===========
@@ -10,11 +10,45 @@ Session and sessionmaker()
1010
:members:
1111
:inherited-members:
1212

13-
.. autoclass:: sqlalchemy.orm.session.Session
13+
.. autoclass:: ORMExecuteState
14+
:members:
15+
16+
17+
.. attribute:: session
18+
19+
The :class:`_orm.Session` in use.
20+
21+
.. attribute:: statement
22+
23+
The SQL statement being invoked. For an ORM selection as would
24+
be retrieved from :class:`_orm.Query`, this is an instance of
25+
:class:`_future.select` that was generated from the ORM query.
26+
27+
.. attribute:: parameters
28+
29+
Dictionary of parameters that was passed to :meth:`_orm.Session.execute`.
30+
31+
.. attribute:: execution_options
32+
33+
Dictionary of execution options passed to :meth:`_orm.Session.execute`.
34+
Note that this dictionary does not include execution options that may
35+
be associated with the statement itself, or with any underlying
36+
:class:`_engine.Connection` that may be used to invoke this statement.
37+
38+
.. attribute:: bind_arguments
39+
40+
The dictionary passed as the
41+
:paramref:`_orm.Session.execute.bind_arguments` dictionary. This
42+
dictionary may be used by extensions to :class:`_orm.Session` to pass
43+
arguments that will assist in determining amongst a set of database
44+
connections which one should be used to invoke this statement.
45+
46+
47+
.. autoclass:: Session
1448
:members:
1549
:inherited-members:
1650

17-
.. autoclass:: sqlalchemy.orm.session.SessionTransaction
51+
.. autoclass:: SessionTransaction
1852
:members:
1953

2054
Session Utilities

examples/dogpile_caching/advanced.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .caching_query import FromCache
77
from .caching_query import RelationshipCache
8+
from .environment import cache
89
from .environment import Session
910
from .model import cache_address_bits
1011
from .model import Person
@@ -48,7 +49,8 @@ def load_name_range(start, end, invalidate=False):
4849

4950
# if requested, invalidate the cache on current criterion.
5051
if invalidate:
51-
q.invalidate()
52+
cache.invalidate(q, {}, FromCache("default", "name_range"))
53+
cache.invalidate(q, {}, RelationshipCache(Person.addresses, "default"))
5254

5355
return q.all()
5456

0 commit comments

Comments
 (0)