SQLAlchemy 2.1.0b2__cp313-cp313t-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. sqlalchemy/__init__.py +298 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +171 -0
  4. sqlalchemy/connectors/asyncio.py +476 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/dialects/__init__.py +62 -0
  7. sqlalchemy/dialects/_typing.py +30 -0
  8. sqlalchemy/dialects/mssql/__init__.py +89 -0
  9. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  10. sqlalchemy/dialects/mssql/base.py +4166 -0
  11. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  12. sqlalchemy/dialects/mssql/json.py +140 -0
  13. sqlalchemy/dialects/mssql/mssqlpython.py +220 -0
  14. sqlalchemy/dialects/mssql/provision.py +196 -0
  15. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  16. sqlalchemy/dialects/mssql/pyodbc.py +698 -0
  17. sqlalchemy/dialects/mysql/__init__.py +106 -0
  18. sqlalchemy/dialects/mysql/_mariadb_shim.py +312 -0
  19. sqlalchemy/dialects/mysql/aiomysql.py +226 -0
  20. sqlalchemy/dialects/mysql/asyncmy.py +214 -0
  21. sqlalchemy/dialects/mysql/base.py +3877 -0
  22. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  23. sqlalchemy/dialects/mysql/dml.py +279 -0
  24. sqlalchemy/dialects/mysql/enumerated.py +277 -0
  25. sqlalchemy/dialects/mysql/expression.py +146 -0
  26. sqlalchemy/dialects/mysql/json.py +92 -0
  27. sqlalchemy/dialects/mysql/mariadb.py +67 -0
  28. sqlalchemy/dialects/mysql/mariadbconnector.py +330 -0
  29. sqlalchemy/dialects/mysql/mysqlconnector.py +296 -0
  30. sqlalchemy/dialects/mysql/mysqldb.py +312 -0
  31. sqlalchemy/dialects/mysql/provision.py +153 -0
  32. sqlalchemy/dialects/mysql/pymysql.py +157 -0
  33. sqlalchemy/dialects/mysql/pyodbc.py +156 -0
  34. sqlalchemy/dialects/mysql/reflection.py +724 -0
  35. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  36. sqlalchemy/dialects/mysql/types.py +845 -0
  37. sqlalchemy/dialects/oracle/__init__.py +85 -0
  38. sqlalchemy/dialects/oracle/base.py +3977 -0
  39. sqlalchemy/dialects/oracle/cx_oracle.py +1601 -0
  40. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  41. sqlalchemy/dialects/oracle/json.py +158 -0
  42. sqlalchemy/dialects/oracle/oracledb.py +909 -0
  43. sqlalchemy/dialects/oracle/provision.py +288 -0
  44. sqlalchemy/dialects/oracle/types.py +367 -0
  45. sqlalchemy/dialects/oracle/vector.py +368 -0
  46. sqlalchemy/dialects/postgresql/__init__.py +171 -0
  47. sqlalchemy/dialects/postgresql/_psycopg_common.py +229 -0
  48. sqlalchemy/dialects/postgresql/array.py +534 -0
  49. sqlalchemy/dialects/postgresql/asyncpg.py +1323 -0
  50. sqlalchemy/dialects/postgresql/base.py +5789 -0
  51. sqlalchemy/dialects/postgresql/bitstring.py +327 -0
  52. sqlalchemy/dialects/postgresql/dml.py +360 -0
  53. sqlalchemy/dialects/postgresql/ext.py +593 -0
  54. sqlalchemy/dialects/postgresql/hstore.py +423 -0
  55. sqlalchemy/dialects/postgresql/json.py +408 -0
  56. sqlalchemy/dialects/postgresql/named_types.py +521 -0
  57. sqlalchemy/dialects/postgresql/operators.py +130 -0
  58. sqlalchemy/dialects/postgresql/pg8000.py +670 -0
  59. sqlalchemy/dialects/postgresql/pg_catalog.py +344 -0
  60. sqlalchemy/dialects/postgresql/provision.py +184 -0
  61. sqlalchemy/dialects/postgresql/psycopg.py +799 -0
  62. sqlalchemy/dialects/postgresql/psycopg2.py +860 -0
  63. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  64. sqlalchemy/dialects/postgresql/ranges.py +1002 -0
  65. sqlalchemy/dialects/postgresql/types.py +388 -0
  66. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  67. sqlalchemy/dialects/sqlite/aiosqlite.py +321 -0
  68. sqlalchemy/dialects/sqlite/base.py +3063 -0
  69. sqlalchemy/dialects/sqlite/dml.py +279 -0
  70. sqlalchemy/dialects/sqlite/json.py +100 -0
  71. sqlalchemy/dialects/sqlite/provision.py +229 -0
  72. sqlalchemy/dialects/sqlite/pysqlcipher.py +161 -0
  73. sqlalchemy/dialects/sqlite/pysqlite.py +754 -0
  74. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  75. sqlalchemy/engine/__init__.py +62 -0
  76. sqlalchemy/engine/_processors_cy.cp313t-win_arm64.pyd +0 -0
  77. sqlalchemy/engine/_processors_cy.py +92 -0
  78. sqlalchemy/engine/_result_cy.cp313t-win_arm64.pyd +0 -0
  79. sqlalchemy/engine/_result_cy.py +633 -0
  80. sqlalchemy/engine/_row_cy.cp313t-win_arm64.pyd +0 -0
  81. sqlalchemy/engine/_row_cy.py +232 -0
  82. sqlalchemy/engine/_util_cy.cp313t-win_arm64.pyd +0 -0
  83. sqlalchemy/engine/_util_cy.py +136 -0
  84. sqlalchemy/engine/base.py +3354 -0
  85. sqlalchemy/engine/characteristics.py +155 -0
  86. sqlalchemy/engine/create.py +877 -0
  87. sqlalchemy/engine/cursor.py +2421 -0
  88. sqlalchemy/engine/default.py +2402 -0
  89. sqlalchemy/engine/events.py +965 -0
  90. sqlalchemy/engine/interfaces.py +3495 -0
  91. sqlalchemy/engine/mock.py +134 -0
  92. sqlalchemy/engine/processors.py +82 -0
  93. sqlalchemy/engine/reflection.py +2100 -0
  94. sqlalchemy/engine/result.py +1966 -0
  95. sqlalchemy/engine/row.py +397 -0
  96. sqlalchemy/engine/strategies.py +16 -0
  97. sqlalchemy/engine/url.py +922 -0
  98. sqlalchemy/engine/util.py +156 -0
  99. sqlalchemy/event/__init__.py +26 -0
  100. sqlalchemy/event/api.py +220 -0
  101. sqlalchemy/event/attr.py +674 -0
  102. sqlalchemy/event/base.py +472 -0
  103. sqlalchemy/event/legacy.py +258 -0
  104. sqlalchemy/event/registry.py +390 -0
  105. sqlalchemy/events.py +17 -0
  106. sqlalchemy/exc.py +922 -0
  107. sqlalchemy/ext/__init__.py +11 -0
  108. sqlalchemy/ext/associationproxy.py +2072 -0
  109. sqlalchemy/ext/asyncio/__init__.py +29 -0
  110. sqlalchemy/ext/asyncio/base.py +281 -0
  111. sqlalchemy/ext/asyncio/engine.py +1487 -0
  112. sqlalchemy/ext/asyncio/exc.py +21 -0
  113. sqlalchemy/ext/asyncio/result.py +994 -0
  114. sqlalchemy/ext/asyncio/scoping.py +1679 -0
  115. sqlalchemy/ext/asyncio/session.py +2007 -0
  116. sqlalchemy/ext/automap.py +1701 -0
  117. sqlalchemy/ext/baked.py +559 -0
  118. sqlalchemy/ext/compiler.py +600 -0
  119. sqlalchemy/ext/declarative/__init__.py +65 -0
  120. sqlalchemy/ext/declarative/extensions.py +560 -0
  121. sqlalchemy/ext/horizontal_shard.py +481 -0
  122. sqlalchemy/ext/hybrid.py +1877 -0
  123. sqlalchemy/ext/indexable.py +364 -0
  124. sqlalchemy/ext/instrumentation.py +450 -0
  125. sqlalchemy/ext/mutable.py +1081 -0
  126. sqlalchemy/ext/orderinglist.py +439 -0
  127. sqlalchemy/ext/serializer.py +185 -0
  128. sqlalchemy/future/__init__.py +16 -0
  129. sqlalchemy/future/engine.py +15 -0
  130. sqlalchemy/inspection.py +174 -0
  131. sqlalchemy/log.py +283 -0
  132. sqlalchemy/orm/__init__.py +176 -0
  133. sqlalchemy/orm/_orm_constructors.py +2694 -0
  134. sqlalchemy/orm/_typing.py +179 -0
  135. sqlalchemy/orm/attributes.py +2868 -0
  136. sqlalchemy/orm/base.py +976 -0
  137. sqlalchemy/orm/bulk_persistence.py +2152 -0
  138. sqlalchemy/orm/clsregistry.py +582 -0
  139. sqlalchemy/orm/collections.py +1568 -0
  140. sqlalchemy/orm/context.py +3471 -0
  141. sqlalchemy/orm/decl_api.py +2280 -0
  142. sqlalchemy/orm/decl_base.py +2309 -0
  143. sqlalchemy/orm/dependency.py +1306 -0
  144. sqlalchemy/orm/descriptor_props.py +1183 -0
  145. sqlalchemy/orm/dynamic.py +307 -0
  146. sqlalchemy/orm/evaluator.py +379 -0
  147. sqlalchemy/orm/events.py +3386 -0
  148. sqlalchemy/orm/exc.py +237 -0
  149. sqlalchemy/orm/identity.py +302 -0
  150. sqlalchemy/orm/instrumentation.py +746 -0
  151. sqlalchemy/orm/interfaces.py +1589 -0
  152. sqlalchemy/orm/loading.py +1684 -0
  153. sqlalchemy/orm/mapped_collection.py +557 -0
  154. sqlalchemy/orm/mapper.py +4411 -0
  155. sqlalchemy/orm/path_registry.py +829 -0
  156. sqlalchemy/orm/persistence.py +1789 -0
  157. sqlalchemy/orm/properties.py +973 -0
  158. sqlalchemy/orm/query.py +3528 -0
  159. sqlalchemy/orm/relationships.py +3570 -0
  160. sqlalchemy/orm/scoping.py +2232 -0
  161. sqlalchemy/orm/session.py +5403 -0
  162. sqlalchemy/orm/state.py +1175 -0
  163. sqlalchemy/orm/state_changes.py +196 -0
  164. sqlalchemy/orm/strategies.py +3492 -0
  165. sqlalchemy/orm/strategy_options.py +2562 -0
  166. sqlalchemy/orm/sync.py +164 -0
  167. sqlalchemy/orm/unitofwork.py +798 -0
  168. sqlalchemy/orm/util.py +2438 -0
  169. sqlalchemy/orm/writeonly.py +694 -0
  170. sqlalchemy/pool/__init__.py +41 -0
  171. sqlalchemy/pool/base.py +1522 -0
  172. sqlalchemy/pool/events.py +375 -0
  173. sqlalchemy/pool/impl.py +582 -0
  174. sqlalchemy/py.typed +0 -0
  175. sqlalchemy/schema.py +74 -0
  176. sqlalchemy/sql/__init__.py +156 -0
  177. sqlalchemy/sql/_annotated_cols.py +397 -0
  178. sqlalchemy/sql/_dml_constructors.py +132 -0
  179. sqlalchemy/sql/_elements_constructors.py +2164 -0
  180. sqlalchemy/sql/_orm_types.py +20 -0
  181. sqlalchemy/sql/_selectable_constructors.py +840 -0
  182. sqlalchemy/sql/_typing.py +487 -0
  183. sqlalchemy/sql/_util_cy.cp313t-win_arm64.pyd +0 -0
  184. sqlalchemy/sql/_util_cy.py +127 -0
  185. sqlalchemy/sql/annotation.py +590 -0
  186. sqlalchemy/sql/base.py +2699 -0
  187. sqlalchemy/sql/cache_key.py +1066 -0
  188. sqlalchemy/sql/coercions.py +1373 -0
  189. sqlalchemy/sql/compiler.py +8327 -0
  190. sqlalchemy/sql/crud.py +1815 -0
  191. sqlalchemy/sql/ddl.py +1928 -0
  192. sqlalchemy/sql/default_comparator.py +654 -0
  193. sqlalchemy/sql/dml.py +1977 -0
  194. sqlalchemy/sql/elements.py +6033 -0
  195. sqlalchemy/sql/events.py +458 -0
  196. sqlalchemy/sql/expression.py +172 -0
  197. sqlalchemy/sql/functions.py +2305 -0
  198. sqlalchemy/sql/lambdas.py +1443 -0
  199. sqlalchemy/sql/naming.py +209 -0
  200. sqlalchemy/sql/operators.py +2897 -0
  201. sqlalchemy/sql/roles.py +332 -0
  202. sqlalchemy/sql/schema.py +6703 -0
  203. sqlalchemy/sql/selectable.py +7553 -0
  204. sqlalchemy/sql/sqltypes.py +4093 -0
  205. sqlalchemy/sql/traversals.py +1042 -0
  206. sqlalchemy/sql/type_api.py +2446 -0
  207. sqlalchemy/sql/util.py +1495 -0
  208. sqlalchemy/sql/visitors.py +1157 -0
  209. sqlalchemy/testing/__init__.py +96 -0
  210. sqlalchemy/testing/assertions.py +1007 -0
  211. sqlalchemy/testing/assertsql.py +519 -0
  212. sqlalchemy/testing/asyncio.py +128 -0
  213. sqlalchemy/testing/config.py +440 -0
  214. sqlalchemy/testing/engines.py +483 -0
  215. sqlalchemy/testing/entities.py +117 -0
  216. sqlalchemy/testing/exclusions.py +476 -0
  217. sqlalchemy/testing/fixtures/__init__.py +30 -0
  218. sqlalchemy/testing/fixtures/base.py +384 -0
  219. sqlalchemy/testing/fixtures/mypy.py +247 -0
  220. sqlalchemy/testing/fixtures/orm.py +227 -0
  221. sqlalchemy/testing/fixtures/sql.py +538 -0
  222. sqlalchemy/testing/pickleable.py +155 -0
  223. sqlalchemy/testing/plugin/__init__.py +6 -0
  224. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  225. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  226. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  227. sqlalchemy/testing/profiling.py +329 -0
  228. sqlalchemy/testing/provision.py +613 -0
  229. sqlalchemy/testing/requirements.py +1978 -0
  230. sqlalchemy/testing/schema.py +198 -0
  231. sqlalchemy/testing/suite/__init__.py +19 -0
  232. sqlalchemy/testing/suite/test_cte.py +237 -0
  233. sqlalchemy/testing/suite/test_ddl.py +420 -0
  234. sqlalchemy/testing/suite/test_dialect.py +776 -0
  235. sqlalchemy/testing/suite/test_insert.py +630 -0
  236. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  237. sqlalchemy/testing/suite/test_results.py +660 -0
  238. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  239. sqlalchemy/testing/suite/test_select.py +2112 -0
  240. sqlalchemy/testing/suite/test_sequence.py +317 -0
  241. sqlalchemy/testing/suite/test_table_via_select.py +686 -0
  242. sqlalchemy/testing/suite/test_types.py +2271 -0
  243. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  244. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  245. sqlalchemy/testing/util.py +535 -0
  246. sqlalchemy/testing/warnings.py +52 -0
  247. sqlalchemy/types.py +76 -0
  248. sqlalchemy/util/__init__.py +158 -0
  249. sqlalchemy/util/_collections.py +688 -0
  250. sqlalchemy/util/_collections_cy.cp313t-win_arm64.pyd +0 -0
  251. sqlalchemy/util/_collections_cy.pxd +8 -0
  252. sqlalchemy/util/_collections_cy.py +516 -0
  253. sqlalchemy/util/_has_cython.py +46 -0
  254. sqlalchemy/util/_immutabledict_cy.cp313t-win_arm64.pyd +0 -0
  255. sqlalchemy/util/_immutabledict_cy.py +240 -0
  256. sqlalchemy/util/compat.py +299 -0
  257. sqlalchemy/util/concurrency.py +322 -0
  258. sqlalchemy/util/cython.py +79 -0
  259. sqlalchemy/util/deprecations.py +401 -0
  260. sqlalchemy/util/langhelpers.py +2320 -0
  261. sqlalchemy/util/preloaded.py +152 -0
  262. sqlalchemy/util/queue.py +304 -0
  263. sqlalchemy/util/tool_support.py +201 -0
  264. sqlalchemy/util/topological.py +120 -0
  265. sqlalchemy/util/typing.py +711 -0
  266. sqlalchemy-2.1.0b2.dist-info/METADATA +269 -0
  267. sqlalchemy-2.1.0b2.dist-info/RECORD +270 -0
  268. sqlalchemy-2.1.0b2.dist-info/WHEEL +5 -0
  269. sqlalchemy-2.1.0b2.dist-info/licenses/LICENSE +19 -0
  270. sqlalchemy-2.1.0b2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1684 @@
1
+ # orm/loading.py
2
+ # Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
3
+ # <see AUTHORS file>
4
+ #
5
+ # This module is part of SQLAlchemy and is released under
6
+ # the MIT License: https://www.opensource.org/licenses/mit-license.php
7
+ # mypy: ignore-errors
8
+
9
+
10
+ """private module containing functions used to convert database
11
+ rows into object instances and associated state.
12
+
13
+ the functions here are called primarily by Query, Mapper,
14
+ as well as some of the attribute loading strategies.
15
+
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from typing import Any
21
+ from typing import Dict
22
+ from typing import Iterable
23
+ from typing import List
24
+ from typing import Mapping
25
+ from typing import Optional
26
+ from typing import Sequence
27
+ from typing import Tuple
28
+ from typing import TYPE_CHECKING
29
+ from typing import TypeVar
30
+ from typing import Union
31
+
32
+ from . import attributes
33
+ from . import exc as orm_exc
34
+ from . import path_registry
35
+ from .base import _DEFER_FOR_STATE
36
+ from .base import _RAISE_FOR_STATE
37
+ from .base import _SET_DEFERRED_EXPIRED
38
+ from .base import PassiveFlag
39
+ from .context import _ORMCompileState
40
+ from .context import FromStatement
41
+ from .context import QueryContext
42
+ from .strategies import _SelectInLoader
43
+ from .util import _none_set
44
+ from .util import state_str
45
+ from .. import exc as sa_exc
46
+ from .. import util
47
+ from ..engine import result_tuple
48
+ from ..engine.result import ChunkedIteratorResult
49
+ from ..engine.result import FrozenResult
50
+ from ..engine.result import SimpleResultMetaData
51
+ from ..sql import select
52
+ from ..sql import util as sql_util
53
+ from ..sql.selectable import ForUpdateArg
54
+ from ..sql.selectable import SelectState
55
+ from ..util import EMPTY_DICT
56
+ from ..util.typing import TupleAny
57
+ from ..util.typing import Unpack
58
+
59
+ if TYPE_CHECKING:
60
+ from ._typing import _IdentityKeyType
61
+ from .base import LoaderCallableStatus
62
+ from .interfaces import ORMOption
63
+ from .mapper import Mapper
64
+ from .query import Query
65
+ from .session import Session
66
+ from .state import InstanceState
67
+ from ..engine.cursor import CursorResult
68
+ from ..engine.interfaces import _ExecuteOptions
69
+ from ..engine.result import Result
70
+ from ..sql import Select
71
+
72
+ _T = TypeVar("_T", bound=Any)
73
+ _O = TypeVar("_O", bound=object)
74
+ _new_runid = util.counter()
75
+
76
+
77
+ _PopulatorDict = Dict[str, List[Tuple[str, Any]]]
78
+
79
+
80
+ def instances(
81
+ cursor: CursorResult[Unpack[TupleAny]], context: QueryContext
82
+ ) -> Result[Unpack[TupleAny]]:
83
+ """Return a :class:`.Result` given an ORM query context.
84
+
85
+ :param cursor: a :class:`.CursorResult`, generated by a statement
86
+ which came from :class:`.ORMCompileState`
87
+
88
+ :param context: a :class:`.QueryContext` object
89
+
90
+ :return: a :class:`.Result` object representing ORM results
91
+
92
+ .. versionchanged:: 1.4 The instances() function now uses
93
+ :class:`.Result` objects and has an all new interface.
94
+
95
+ """
96
+
97
+ context.runid = _new_runid()
98
+
99
+ if context.top_level_context:
100
+ is_top_level = False
101
+ context.post_load_paths = context.top_level_context.post_load_paths
102
+ else:
103
+ is_top_level = True
104
+ context.post_load_paths = {}
105
+
106
+ compile_state = context.compile_state
107
+ filtered = compile_state._has_mapper_entities
108
+ single_entity = (
109
+ not context.load_options._only_return_tuples
110
+ and len(compile_state._entities) == 1
111
+ and compile_state._entities[0].supports_single_entity
112
+ )
113
+
114
+ try:
115
+ (process, labels, extra) = list(
116
+ zip(
117
+ *[
118
+ query_entity.row_processor(context, cursor)
119
+ for query_entity in context.compile_state._entities
120
+ ]
121
+ )
122
+ )
123
+
124
+ if context.yield_per and (
125
+ context.loaders_require_buffering
126
+ or context.loaders_require_uniquing
127
+ ):
128
+ raise sa_exc.InvalidRequestError(
129
+ "Can't use yield_per with eager loaders that require uniquing "
130
+ "or row buffering, e.g. joinedload() against collections "
131
+ "or subqueryload(). Consider the selectinload() strategy "
132
+ "for better flexibility in loading objects."
133
+ )
134
+
135
+ except Exception:
136
+ with util.safe_reraise():
137
+ cursor.close()
138
+
139
+ def _no_unique(entry):
140
+ raise sa_exc.InvalidRequestError(
141
+ "Can't use the ORM yield_per feature in conjunction with unique()"
142
+ )
143
+
144
+ def _not_hashable(datatype, *, legacy=False, uncertain=False):
145
+ if not legacy:
146
+
147
+ def go(obj):
148
+ if uncertain:
149
+ try:
150
+ return hash(obj)
151
+ except:
152
+ pass
153
+
154
+ raise sa_exc.InvalidRequestError(
155
+ "Can't apply uniqueness to row tuple containing value of "
156
+ f"""type {datatype!r}; {
157
+ 'the values returned appear to be'
158
+ if uncertain
159
+ else 'this datatype produces'
160
+ } non-hashable values"""
161
+ )
162
+
163
+ return go
164
+ elif not uncertain:
165
+ return id
166
+ else:
167
+ _use_id = False
168
+
169
+ def go(obj):
170
+ nonlocal _use_id
171
+
172
+ if not _use_id:
173
+ try:
174
+ return hash(obj)
175
+ except:
176
+ pass
177
+
178
+ # in #10459, we considered using a warning here, however
179
+ # as legacy query uses result.unique() in all cases, this
180
+ # would lead to too many warning cases.
181
+ _use_id = True
182
+
183
+ return id(obj)
184
+
185
+ return go
186
+
187
+ unique_filters = [
188
+ (
189
+ _no_unique
190
+ if context.yield_per
191
+ else (
192
+ _not_hashable(
193
+ ent.column.type, # type: ignore
194
+ legacy=context.load_options._legacy_uniquing,
195
+ uncertain=ent._null_column_type,
196
+ )
197
+ if (
198
+ not ent.use_id_for_hash
199
+ and (ent._non_hashable_value or ent._null_column_type)
200
+ )
201
+ else id if ent.use_id_for_hash else None
202
+ )
203
+ )
204
+ for ent in context.compile_state._entities
205
+ ]
206
+
207
+ row_metadata = SimpleResultMetaData(
208
+ labels, extra, _unique_filters=unique_filters
209
+ )
210
+
211
+ def chunks(size): # type: ignore
212
+ while True:
213
+ yield_per = size
214
+
215
+ context.partials = {}
216
+
217
+ if yield_per:
218
+ fetch = cursor.fetchmany(yield_per)
219
+
220
+ if not fetch:
221
+ break
222
+ else:
223
+ fetch = cursor._raw_all_rows()
224
+
225
+ if single_entity:
226
+ proc = process[0]
227
+ rows = [proc(row) for row in fetch]
228
+ else:
229
+ rows = [
230
+ tuple([proc(row) for proc in process]) for row in fetch
231
+ ]
232
+
233
+ # if we are the originating load from a query, meaning we
234
+ # aren't being called as a result of a nested "post load",
235
+ # iterate through all the collected post loaders and fire them
236
+ # off. Previously this used to work recursively, however that
237
+ # prevented deeply nested structures from being loadable
238
+ if is_top_level:
239
+ if yield_per:
240
+ # if using yield per, memoize the state of the
241
+ # collection so that it can be restored
242
+ top_level_post_loads = list(
243
+ context.post_load_paths.items()
244
+ )
245
+
246
+ while context.post_load_paths:
247
+ post_loads = list(context.post_load_paths.items())
248
+ context.post_load_paths.clear()
249
+ for path, post_load in post_loads:
250
+ post_load.invoke(context, path)
251
+
252
+ if yield_per:
253
+ context.post_load_paths.clear()
254
+ context.post_load_paths.update(top_level_post_loads)
255
+
256
+ yield rows
257
+
258
+ if not yield_per:
259
+ break
260
+
261
+ if context.execution_options.get("prebuffer_rows", False):
262
+ # this is a bit of a hack at the moment.
263
+ # I would rather have some option in the result to pre-buffer
264
+ # internally.
265
+ _prebuffered = list(chunks(None))
266
+
267
+ def chunks(size):
268
+ return iter(_prebuffered)
269
+
270
+ result = ChunkedIteratorResult(
271
+ row_metadata,
272
+ chunks,
273
+ source_supports_scalars=single_entity,
274
+ raw=cursor,
275
+ dynamic_yield_per=cursor.context._is_server_side,
276
+ )
277
+
278
+ # filtered and single_entity are used to indicate to legacy Query that the
279
+ # query has ORM entities, so legacy deduping and scalars should be called
280
+ # on the result.
281
+ result._attributes = result._attributes.union(
282
+ dict(filtered=filtered, is_single_entity=single_entity)
283
+ )
284
+
285
+ # multi_row_eager_loaders OTOH is specific to joinedload.
286
+ if context.compile_state.multi_row_eager_loaders:
287
+
288
+ def require_unique(obj):
289
+ raise sa_exc.InvalidRequestError(
290
+ "The unique() method must be invoked on this Result, "
291
+ "as it contains results that include joined eager loads "
292
+ "against collections"
293
+ )
294
+
295
+ result._unique_filter_state = (set(), require_unique)
296
+
297
+ if context.yield_per:
298
+ result.yield_per(context.yield_per)
299
+
300
+ return result
301
+
302
+
303
+ @util.preload_module("sqlalchemy.orm.context")
304
+ def merge_frozen_result(session, statement, frozen_result, load=True):
305
+ """Merge a :class:`_engine.FrozenResult` back into a :class:`_orm.Session`,
306
+ returning a new :class:`_engine.Result` object with :term:`persistent`
307
+ objects.
308
+
309
+ See the section :ref:`do_orm_execute_re_executing` for an example.
310
+
311
+ .. seealso::
312
+
313
+ :ref:`do_orm_execute_re_executing`
314
+
315
+ :meth:`_engine.Result.freeze`
316
+
317
+ :class:`_engine.FrozenResult`
318
+
319
+ """
320
+ querycontext = util.preloaded.orm_context
321
+
322
+ if load:
323
+ # flush current contents if we expect to load data
324
+ session._autoflush()
325
+
326
+ ctx = querycontext._ORMSelectCompileState._create_entities_collection(
327
+ statement, legacy=False
328
+ )
329
+
330
+ with session.no_autoflush:
331
+ mapped_entities = [
332
+ i
333
+ for i, e in enumerate(ctx._entities)
334
+ if isinstance(e, querycontext._MapperEntity)
335
+ ]
336
+ keys = [ent._label_name for ent in ctx._entities]
337
+
338
+ keyed_tuple = result_tuple(
339
+ keys, [ent._extra_entities for ent in ctx._entities]
340
+ )
341
+
342
+ result = []
343
+ for newrow in frozen_result._rewrite_rows():
344
+ for i in mapped_entities:
345
+ if newrow[i] is not None:
346
+ newrow[i] = session._merge(
347
+ attributes.instance_state(newrow[i]),
348
+ attributes.instance_dict(newrow[i]),
349
+ load=load,
350
+ _recursive={},
351
+ _resolve_conflict_map={},
352
+ )
353
+
354
+ result.append(keyed_tuple(newrow))
355
+
356
+ return frozen_result.with_new_rows(result)
357
+
358
+
359
+ @util.became_legacy_20(
360
+ ":func:`_orm.merge_result`",
361
+ alternative="The function as well as the method on :class:`_orm.Query` "
362
+ "is superseded by the :func:`_orm.merge_frozen_result` function.",
363
+ )
364
+ @util.preload_module("sqlalchemy.orm.context")
365
+ def merge_result(
366
+ query: Query[Any],
367
+ iterator: Union[FrozenResult, Iterable[Sequence[Any]], Iterable[object]],
368
+ load: bool = True,
369
+ ) -> Union[FrozenResult, Iterable[Any]]:
370
+ """Merge a result into the given :class:`.Query` object's Session.
371
+
372
+ See :meth:`_orm.Query.merge_result` for top-level documentation on this
373
+ function.
374
+
375
+ """
376
+
377
+ querycontext = util.preloaded.orm_context
378
+
379
+ session = query.session
380
+ if load:
381
+ # flush current contents if we expect to load data
382
+ session._autoflush()
383
+
384
+ # TODO: need test coverage and documentation for the FrozenResult
385
+ # use case.
386
+ if isinstance(iterator, FrozenResult):
387
+ frozen_result = iterator
388
+ iterator = iter(frozen_result.data)
389
+ else:
390
+ frozen_result = None
391
+
392
+ ctx = querycontext._ORMSelectCompileState._create_entities_collection(
393
+ query, legacy=True
394
+ )
395
+
396
+ autoflush = session.autoflush
397
+ try:
398
+ session.autoflush = False
399
+ single_entity = not frozen_result and len(ctx._entities) == 1
400
+
401
+ if single_entity:
402
+ if isinstance(ctx._entities[0], querycontext._MapperEntity):
403
+ result = [
404
+ session._merge(
405
+ attributes.instance_state(instance),
406
+ attributes.instance_dict(instance),
407
+ load=load,
408
+ _recursive={},
409
+ _resolve_conflict_map={},
410
+ )
411
+ for instance in iterator
412
+ ]
413
+ else:
414
+ result = list(iterator)
415
+ else:
416
+ mapped_entities = [
417
+ i
418
+ for i, e in enumerate(ctx._entities)
419
+ if isinstance(e, querycontext._MapperEntity)
420
+ ]
421
+ result = []
422
+ keys = [ent._label_name for ent in ctx._entities]
423
+
424
+ keyed_tuple = result_tuple(
425
+ keys, [ent._extra_entities for ent in ctx._entities]
426
+ )
427
+
428
+ for row in iterator:
429
+ newrow = list(row)
430
+ for i in mapped_entities:
431
+ if newrow[i] is not None:
432
+ newrow[i] = session._merge(
433
+ attributes.instance_state(newrow[i]),
434
+ attributes.instance_dict(newrow[i]),
435
+ load=load,
436
+ _recursive={},
437
+ _resolve_conflict_map={},
438
+ )
439
+ result.append(keyed_tuple(newrow))
440
+
441
+ if frozen_result:
442
+ return frozen_result.with_new_rows(result)
443
+ else:
444
+ return iter(result)
445
+ finally:
446
+ session.autoflush = autoflush
447
+
448
+
449
+ def get_from_identity(
450
+ session: Session,
451
+ mapper: Mapper[_O],
452
+ key: _IdentityKeyType[_O],
453
+ passive: PassiveFlag,
454
+ ) -> Union[LoaderCallableStatus, Optional[_O]]:
455
+ """Look up the given key in the given session's identity map,
456
+ check the object for expired state if found.
457
+
458
+ """
459
+ instance = session.identity_map.get(key)
460
+ if instance is not None:
461
+ state = attributes.instance_state(instance)
462
+
463
+ if mapper.inherits and not state.mapper.isa(mapper):
464
+ return attributes.PASSIVE_CLASS_MISMATCH
465
+
466
+ # expired - ensure it still exists
467
+ if state.expired:
468
+ if not passive & attributes.SQL_OK:
469
+ # TODO: no coverage here
470
+ return attributes.PASSIVE_NO_RESULT
471
+ elif not passive & attributes.RELATED_OBJECT_OK:
472
+ # this mode is used within a flush and the instance's
473
+ # expired state will be checked soon enough, if necessary.
474
+ # also used by immediateloader for a mutually-dependent
475
+ # o2m->m2m load, :ticket:`6301`
476
+ return instance
477
+ try:
478
+ state._load_expired(state, passive)
479
+ except orm_exc.ObjectDeletedError:
480
+ session._remove_newly_deleted([state])
481
+ return None
482
+ return instance
483
+ else:
484
+ return None
485
+
486
+
487
+ def _load_on_ident(
488
+ session: Session,
489
+ statement: Union[Select, FromStatement],
490
+ key: Optional[_IdentityKeyType],
491
+ *,
492
+ load_options: Optional[Sequence[ORMOption]] = None,
493
+ refresh_state: Optional[InstanceState[Any]] = None,
494
+ with_for_update: Optional[ForUpdateArg] = None,
495
+ only_load_props: Optional[Iterable[str]] = None,
496
+ no_autoflush: bool = False,
497
+ bind_arguments: Mapping[str, Any] = util.EMPTY_DICT,
498
+ execution_options: _ExecuteOptions = util.EMPTY_DICT,
499
+ require_pk_cols: bool = False,
500
+ is_user_refresh: bool = False,
501
+ ):
502
+ """Load the given identity key from the database."""
503
+ if key is not None:
504
+ ident = key[1]
505
+ identity_token = key[2]
506
+ else:
507
+ ident = identity_token = None
508
+
509
+ return _load_on_pk_identity(
510
+ session,
511
+ statement,
512
+ ident,
513
+ load_options=load_options,
514
+ refresh_state=refresh_state,
515
+ with_for_update=with_for_update,
516
+ only_load_props=only_load_props,
517
+ identity_token=identity_token,
518
+ no_autoflush=no_autoflush,
519
+ bind_arguments=bind_arguments,
520
+ execution_options=execution_options,
521
+ require_pk_cols=require_pk_cols,
522
+ is_user_refresh=is_user_refresh,
523
+ )
524
+
525
+
526
+ def _load_on_pk_identity(
527
+ session: Session,
528
+ statement: Union[Select, FromStatement],
529
+ primary_key_identity: Optional[Tuple[Any, ...]],
530
+ *,
531
+ load_options: Optional[Sequence[ORMOption]] = None,
532
+ refresh_state: Optional[InstanceState[Any]] = None,
533
+ with_for_update: Optional[ForUpdateArg] = None,
534
+ only_load_props: Optional[Iterable[str]] = None,
535
+ identity_token: Optional[Any] = None,
536
+ no_autoflush: bool = False,
537
+ bind_arguments: Mapping[str, Any] = util.EMPTY_DICT,
538
+ execution_options: _ExecuteOptions = util.EMPTY_DICT,
539
+ require_pk_cols: bool = False,
540
+ is_user_refresh: bool = False,
541
+ ):
542
+ """Load the given primary key identity from the database."""
543
+
544
+ query = statement
545
+ q = query._clone()
546
+
547
+ assert not q._is_lambda_element
548
+
549
+ if load_options is None:
550
+ load_options = QueryContext.default_load_options
551
+
552
+ if (
553
+ statement._compile_options
554
+ is SelectState.default_select_compile_options
555
+ ):
556
+ compile_options = _ORMCompileState.default_compile_options
557
+ else:
558
+ compile_options = statement._compile_options
559
+
560
+ if primary_key_identity is not None:
561
+ mapper = query._propagate_attrs["plugin_subject"]
562
+
563
+ (_get_clause, _get_params) = mapper._get_clause
564
+
565
+ # None present in ident - turn those comparisons
566
+ # into "IS NULL"
567
+ if None in primary_key_identity:
568
+ nones = {
569
+ _get_params[col].key
570
+ for col, value in zip(mapper.primary_key, primary_key_identity)
571
+ if value is None
572
+ }
573
+
574
+ _get_clause = sql_util.adapt_criterion_to_null(_get_clause, nones)
575
+
576
+ if len(nones) == len(primary_key_identity):
577
+ util.warn(
578
+ "fully NULL primary key identity cannot load any "
579
+ "object. This condition may raise an error in a future "
580
+ "release."
581
+ )
582
+
583
+ q._where_criteria = (_get_clause,)
584
+
585
+ params = {
586
+ _get_params[primary_key].key: id_val
587
+ for id_val, primary_key in zip(
588
+ primary_key_identity, mapper.primary_key
589
+ )
590
+ }
591
+ else:
592
+ params = None
593
+
594
+ if with_for_update is not None:
595
+ version_check = True
596
+ q._for_update_arg = with_for_update
597
+ elif query._for_update_arg is not None:
598
+ version_check = True
599
+ q._for_update_arg = query._for_update_arg
600
+ else:
601
+ version_check = False
602
+
603
+ if require_pk_cols and only_load_props:
604
+ if not refresh_state:
605
+ raise sa_exc.ArgumentError(
606
+ "refresh_state is required when require_pk_cols is present"
607
+ )
608
+
609
+ refresh_state_prokeys = refresh_state.mapper._primary_key_propkeys
610
+ has_changes = {
611
+ key
612
+ for key in refresh_state_prokeys.difference(only_load_props)
613
+ if refresh_state.attrs[key].history.has_changes()
614
+ }
615
+ if has_changes:
616
+ # raise if pending pk changes are present.
617
+ # technically, this could be limited to the case where we have
618
+ # relationships in the only_load_props collection to be refreshed
619
+ # also (and only ones that have a secondary eager loader, at that).
620
+ # however, the error is in place across the board so that behavior
621
+ # here is easier to predict. The use case it prevents is one
622
+ # of mutating PK attrs, leaving them unflushed,
623
+ # calling session.refresh(), and expecting those attrs to remain
624
+ # still unflushed. It seems likely someone doing all those
625
+ # things would be better off having the PK attributes flushed
626
+ # to the database before tinkering like that (session.refresh() is
627
+ # tinkering).
628
+ raise sa_exc.InvalidRequestError(
629
+ f"Please flush pending primary key changes on "
630
+ "attributes "
631
+ f"{has_changes} for mapper {refresh_state.mapper} before "
632
+ "proceeding with a refresh"
633
+ )
634
+
635
+ # overall, the ORM has no internal flow right now for "dont load the
636
+ # primary row of an object at all, but fire off
637
+ # selectinload/subqueryload/immediateload for some relationships".
638
+ # It would probably be a pretty big effort to add such a flow. So
639
+ # here, the case for #8703 is introduced; user asks to refresh some
640
+ # relationship attributes only which are
641
+ # selectinload/subqueryload/immediateload/ etc. (not joinedload).
642
+ # ORM complains there's no columns in the primary row to load.
643
+ # So here, we just add the PK cols if that
644
+ # case is detected, so that there is a SELECT emitted for the primary
645
+ # row.
646
+ #
647
+ # Let's just state right up front, for this one little case,
648
+ # the ORM here is adding a whole extra SELECT just to satisfy
649
+ # limitations in the internal flow. This is really not a thing
650
+ # SQLAlchemy finds itself doing like, ever, obviously, we are
651
+ # constantly working to *remove* SELECTs we don't need. We
652
+ # rationalize this for now based on 1. session.refresh() is not
653
+ # commonly used 2. session.refresh() with only relationship attrs is
654
+ # even less commonly used 3. the SELECT in question is very low
655
+ # latency.
656
+ #
657
+ # to add the flow to not include the SELECT, the quickest way
658
+ # might be to just manufacture a single-row result set to send off to
659
+ # instances(), but we'd have to weave that into context.py and all
660
+ # that. For 2.0.0, we have enough big changes to navigate for now.
661
+ #
662
+ mp = refresh_state.mapper._props
663
+ for p in only_load_props:
664
+ if mp[p]._is_relationship:
665
+ only_load_props = refresh_state_prokeys.union(only_load_props)
666
+ break
667
+
668
+ if refresh_state and refresh_state.load_options:
669
+ compile_options += {"_current_path": refresh_state.load_path.parent}
670
+ q = q.options(*refresh_state.load_options)
671
+
672
+ new_compile_options, load_options = _set_get_options(
673
+ compile_options,
674
+ load_options,
675
+ version_check=version_check,
676
+ only_load_props=only_load_props,
677
+ refresh_state=refresh_state,
678
+ identity_token=identity_token,
679
+ is_user_refresh=is_user_refresh,
680
+ )
681
+
682
+ q._compile_options = new_compile_options
683
+ q._order_by = None
684
+
685
+ if no_autoflush:
686
+ load_options += {"_autoflush": False}
687
+
688
+ execution_options = util.EMPTY_DICT.merge_with(
689
+ execution_options,
690
+ util.immutabledict(_sa_orm_load_options=load_options),
691
+ )
692
+ result = (
693
+ session.execute(
694
+ q,
695
+ params=params,
696
+ execution_options=execution_options,
697
+ bind_arguments=bind_arguments,
698
+ )
699
+ .unique()
700
+ .scalars()
701
+ )
702
+
703
+ try:
704
+ return result.one()
705
+ except orm_exc.NoResultFound:
706
+ return None
707
+
708
+
709
+ def _set_get_options(
710
+ compile_opt,
711
+ load_opt,
712
+ populate_existing=None,
713
+ version_check=None,
714
+ only_load_props=None,
715
+ refresh_state=None,
716
+ identity_token=None,
717
+ is_user_refresh=None,
718
+ ):
719
+ compile_options = {}
720
+ load_options = {}
721
+ if version_check:
722
+ load_options["_version_check"] = version_check
723
+ if populate_existing:
724
+ load_options["_populate_existing"] = populate_existing
725
+ if refresh_state:
726
+ load_options["_refresh_state"] = refresh_state
727
+ compile_options["_for_refresh_state"] = True
728
+ if only_load_props:
729
+ compile_options["_only_load_props"] = frozenset(only_load_props)
730
+ if identity_token:
731
+ load_options["_identity_token"] = identity_token
732
+
733
+ if is_user_refresh:
734
+ load_options["_is_user_refresh"] = is_user_refresh
735
+ if load_options:
736
+ load_opt += load_options
737
+ if compile_options:
738
+ compile_opt += compile_options
739
+
740
+ return compile_opt, load_opt
741
+
742
+
743
+ def _setup_entity_query(
744
+ compile_state,
745
+ mapper,
746
+ query_entity,
747
+ path,
748
+ adapter,
749
+ column_collection,
750
+ with_polymorphic=None,
751
+ only_load_props=None,
752
+ polymorphic_discriminator=None,
753
+ **kw,
754
+ ):
755
+ if with_polymorphic:
756
+ poly_properties = mapper._iterate_polymorphic_properties(
757
+ with_polymorphic
758
+ )
759
+ else:
760
+ poly_properties = mapper._polymorphic_properties
761
+
762
+ quick_populators = {}
763
+
764
+ path.set(compile_state.attributes, "memoized_setups", quick_populators)
765
+
766
+ # for the lead entities in the path, e.g. not eager loads, and
767
+ # assuming a user-passed aliased class, e.g. not a from_self() or any
768
+ # implicit aliasing, don't add columns to the SELECT that aren't
769
+ # in the thing that's aliased.
770
+ check_for_adapt = adapter and len(path) == 1 and path[-1].is_aliased_class
771
+
772
+ for value in poly_properties:
773
+ if only_load_props and value.key not in only_load_props:
774
+ continue
775
+ value.setup(
776
+ compile_state,
777
+ query_entity,
778
+ path,
779
+ adapter,
780
+ only_load_props=only_load_props,
781
+ column_collection=column_collection,
782
+ memoized_populators=quick_populators,
783
+ check_for_adapt=check_for_adapt,
784
+ **kw,
785
+ )
786
+
787
+ if (
788
+ polymorphic_discriminator is not None
789
+ and polymorphic_discriminator is not mapper.polymorphic_on
790
+ ):
791
+ if adapter:
792
+ pd = adapter.columns[polymorphic_discriminator]
793
+ else:
794
+ pd = polymorphic_discriminator
795
+ column_collection.append(pd)
796
+
797
+
798
+ def _warn_for_runid_changed(state):
799
+ util.warn(
800
+ "Loading context for %s has changed within a load/refresh "
801
+ "handler, suggesting a row refresh operation took place. If this "
802
+ "event handler is expected to be "
803
+ "emitting row refresh operations within an existing load or refresh "
804
+ "operation, set restore_load_context=True when establishing the "
805
+ "listener to ensure the context remains unchanged when the event "
806
+ "handler completes." % (state_str(state),)
807
+ )
808
+
809
+
810
+ def _instance_processor(
811
+ query_entity,
812
+ mapper,
813
+ context,
814
+ result,
815
+ path,
816
+ adapter,
817
+ only_load_props=None,
818
+ refresh_state=None,
819
+ polymorphic_discriminator=None,
820
+ _polymorphic_from=None,
821
+ ):
822
+ """Produce a mapper level row processor callable
823
+ which processes rows into mapped instances."""
824
+
825
+ # note that this method, most of which exists in a closure
826
+ # called _instance(), resists being broken out, as
827
+ # attempts to do so tend to add significant function
828
+ # call overhead. _instance() is the most
829
+ # performance-critical section in the whole ORM.
830
+
831
+ identity_class = mapper._identity_class
832
+ compile_state = context.compile_state
833
+
834
+ # look for "row getter" functions that have been assigned along
835
+ # with the compile state that were cached from a previous load.
836
+ # these are operator.itemgetter() objects that each will extract a
837
+ # particular column from each row.
838
+
839
+ getter_key = ("getters", mapper)
840
+ getters = path.get(compile_state.attributes, getter_key, None)
841
+
842
+ if getters is None:
843
+ # no getters, so go through a list of attributes we are loading for,
844
+ # and the ones that are column based will have already put information
845
+ # for us in another collection "memoized_setups", which represents the
846
+ # output of the LoaderStrategy.setup_query() method. We can just as
847
+ # easily call LoaderStrategy.create_row_processor for each, but by
848
+ # getting it all at once from setup_query we save another method call
849
+ # per attribute.
850
+ props = mapper._prop_set
851
+ if only_load_props is not None:
852
+ props = props.intersection(
853
+ mapper._props[k] for k in only_load_props
854
+ )
855
+
856
+ quick_populators = path.get(
857
+ context.attributes, "memoized_setups", EMPTY_DICT
858
+ )
859
+
860
+ todo = []
861
+ cached_populators = {
862
+ "new": [],
863
+ "quick": [],
864
+ "deferred": [],
865
+ "expire": [],
866
+ "existing": [],
867
+ "eager": [],
868
+ }
869
+
870
+ if refresh_state is None:
871
+ # we can also get the "primary key" tuple getter function
872
+ pk_cols = mapper.primary_key
873
+
874
+ if adapter:
875
+ pk_cols = [adapter.columns[c] for c in pk_cols]
876
+ primary_key_getter = result._tuple_getter(pk_cols)
877
+ else:
878
+ primary_key_getter = None
879
+
880
+ getters = {
881
+ "cached_populators": cached_populators,
882
+ "todo": todo,
883
+ "primary_key_getter": primary_key_getter,
884
+ }
885
+ for prop in props:
886
+ if prop in quick_populators:
887
+ # this is an inlined path just for column-based attributes.
888
+ col = quick_populators[prop]
889
+ if col is _DEFER_FOR_STATE:
890
+ cached_populators["new"].append(
891
+ (prop.key, prop._deferred_column_loader)
892
+ )
893
+ elif col is _SET_DEFERRED_EXPIRED:
894
+ # note that in this path, we are no longer
895
+ # searching in the result to see if the column might
896
+ # be present in some unexpected way.
897
+ cached_populators["expire"].append((prop.key, False))
898
+ elif col is _RAISE_FOR_STATE:
899
+ cached_populators["new"].append(
900
+ (prop.key, prop._raise_column_loader)
901
+ )
902
+ else:
903
+ getter = None
904
+ if adapter:
905
+ # this logic had been removed for all 1.4 releases
906
+ # up until 1.4.18; the adapter here is particularly
907
+ # the compound eager adapter which isn't accommodated
908
+ # in the quick_populators right now. The "fallback"
909
+ # logic below instead took over in many more cases
910
+ # until issue #6596 was identified.
911
+
912
+ # note there is still an issue where this codepath
913
+ # produces no "getter" for cases where a joined-inh
914
+ # mapping includes a labeled column property, meaning
915
+ # KeyError is caught internally and we fall back to
916
+ # _getter(col), which works anyway. The adapter
917
+ # here for joined inh without any aliasing might not
918
+ # be useful. Tests which see this include
919
+ # test.orm.inheritance.test_basic ->
920
+ # EagerTargetingTest.test_adapt_stringency
921
+ # OptimizedLoadTest.test_column_expression_joined
922
+ # PolymorphicOnNotLocalTest.test_polymorphic_on_column_prop # noqa: E501
923
+ #
924
+
925
+ adapted_col = adapter.columns[col]
926
+ if adapted_col is not None:
927
+ getter = result._getter(adapted_col, False)
928
+ if not getter:
929
+ getter = result._getter(col, False)
930
+ if getter:
931
+ cached_populators["quick"].append((prop.key, getter))
932
+ else:
933
+ # fall back to the ColumnProperty itself, which
934
+ # will iterate through all of its columns
935
+ # to see if one fits
936
+ prop.create_row_processor(
937
+ context,
938
+ query_entity,
939
+ path,
940
+ mapper,
941
+ result,
942
+ adapter,
943
+ cached_populators,
944
+ )
945
+ else:
946
+ # loader strategies like subqueryload, selectinload,
947
+ # joinedload, basically relationships, these need to interact
948
+ # with the context each time to work correctly.
949
+ todo.append(prop)
950
+
951
+ path.set(compile_state.attributes, getter_key, getters)
952
+
953
+ cached_populators = getters["cached_populators"]
954
+
955
+ populators = {key: list(value) for key, value in cached_populators.items()}
956
+ for prop in getters["todo"]:
957
+ prop.create_row_processor(
958
+ context, query_entity, path, mapper, result, adapter, populators
959
+ )
960
+
961
+ propagated_loader_options = context.propagated_loader_options
962
+ load_path = (
963
+ context.compile_state.current_path + path
964
+ if context.compile_state.current_path.path
965
+ else path
966
+ )
967
+
968
+ session_identity_map = context.session.identity_map
969
+
970
+ populate_existing = context.populate_existing or mapper.always_refresh
971
+ load_evt = bool(mapper.class_manager.dispatch.load)
972
+ refresh_evt = bool(mapper.class_manager.dispatch.refresh)
973
+ persistent_evt = bool(context.session.dispatch.loaded_as_persistent)
974
+ if persistent_evt:
975
+ loaded_as_persistent = context.session.dispatch.loaded_as_persistent
976
+ instance_state = attributes.instance_state
977
+ instance_dict = attributes.instance_dict
978
+ session_id = context.session.hash_key
979
+ runid = context.runid
980
+ identity_token = context.identity_token
981
+
982
+ version_check = context.version_check
983
+ if version_check:
984
+ version_id_col = mapper.version_id_col
985
+ if version_id_col is not None:
986
+ if adapter:
987
+ version_id_col = adapter.columns[version_id_col]
988
+ version_id_getter = result._getter(version_id_col)
989
+ else:
990
+ version_id_getter = None
991
+
992
+ if not refresh_state and _polymorphic_from is not None:
993
+ key = ("loader", path.path)
994
+
995
+ if key in context.attributes and context.attributes[key].strategy == (
996
+ ("selectinload_polymorphic", True),
997
+ ):
998
+ option_entities = context.attributes[key].local_opts["entities"]
999
+ else:
1000
+ option_entities = None
1001
+ selectin_load_via = mapper._should_selectin_load(
1002
+ option_entities,
1003
+ _polymorphic_from,
1004
+ )
1005
+
1006
+ if selectin_load_via and selectin_load_via is not _polymorphic_from:
1007
+ # only_load_props goes w/ refresh_state only, and in a refresh
1008
+ # we are a single row query for the exact entity; polymorphic
1009
+ # loading does not apply
1010
+ assert only_load_props is None
1011
+
1012
+ if selectin_load_via.is_mapper:
1013
+ _load_supers = []
1014
+ _endmost_mapper = selectin_load_via
1015
+ while (
1016
+ _endmost_mapper
1017
+ and _endmost_mapper is not _polymorphic_from
1018
+ ):
1019
+ _load_supers.append(_endmost_mapper)
1020
+ _endmost_mapper = _endmost_mapper.inherits
1021
+ else:
1022
+ _load_supers = [selectin_load_via]
1023
+
1024
+ for _selectinload_entity in _load_supers:
1025
+ if _PostLoad.path_exists(
1026
+ context, load_path, _selectinload_entity
1027
+ ):
1028
+ continue
1029
+ callable_ = _load_subclass_via_in(
1030
+ context,
1031
+ path,
1032
+ _selectinload_entity,
1033
+ _polymorphic_from,
1034
+ option_entities,
1035
+ )
1036
+ _PostLoad.callable_for_path(
1037
+ context,
1038
+ load_path,
1039
+ _selectinload_entity.mapper,
1040
+ _selectinload_entity,
1041
+ callable_,
1042
+ _selectinload_entity,
1043
+ )
1044
+
1045
+ post_load = _PostLoad.for_context(context, load_path, only_load_props)
1046
+
1047
+ if refresh_state:
1048
+ refresh_identity_key = refresh_state.key
1049
+ if refresh_identity_key is None:
1050
+ # super-rare condition; a refresh is being called
1051
+ # on a non-instance-key instance; this is meant to only
1052
+ # occur within a flush()
1053
+ refresh_identity_key = mapper._identity_key_from_state(
1054
+ refresh_state
1055
+ )
1056
+ else:
1057
+ refresh_identity_key = None
1058
+
1059
+ primary_key_getter = getters["primary_key_getter"]
1060
+
1061
+ if mapper.allow_partial_pks:
1062
+ is_not_primary_key = _none_set.issuperset
1063
+ else:
1064
+ is_not_primary_key = _none_set.intersection
1065
+
1066
+ def _instance(row):
1067
+ # determine the state that we'll be populating
1068
+ if refresh_identity_key:
1069
+ # fixed state that we're refreshing
1070
+ state = refresh_state
1071
+ instance = state.obj()
1072
+ dict_ = instance_dict(instance)
1073
+ isnew = state.runid != runid
1074
+ currentload = True
1075
+ loaded_instance = False
1076
+ else:
1077
+ # look at the row, see if that identity is in the
1078
+ # session, or we have to create a new one
1079
+ identitykey = (
1080
+ identity_class,
1081
+ primary_key_getter(row),
1082
+ identity_token,
1083
+ )
1084
+
1085
+ instance = session_identity_map.get(identitykey)
1086
+
1087
+ if instance is not None:
1088
+ # existing instance
1089
+ state = instance_state(instance)
1090
+ dict_ = instance_dict(instance)
1091
+
1092
+ isnew = state.runid != runid
1093
+ currentload = not isnew
1094
+ loaded_instance = False
1095
+
1096
+ if version_check and version_id_getter and not currentload:
1097
+ _validate_version_id(
1098
+ mapper, state, dict_, row, version_id_getter
1099
+ )
1100
+
1101
+ else:
1102
+ # create a new instance
1103
+
1104
+ # check for non-NULL values in the primary key columns,
1105
+ # else no entity is returned for the row
1106
+ if is_not_primary_key(identitykey[1]):
1107
+ return None
1108
+
1109
+ isnew = True
1110
+ currentload = True
1111
+ loaded_instance = True
1112
+
1113
+ instance = mapper.class_manager.new_instance()
1114
+
1115
+ dict_ = instance_dict(instance)
1116
+ state = instance_state(instance)
1117
+ state.key = identitykey
1118
+ state.identity_token = identity_token
1119
+
1120
+ # attach instance to session.
1121
+ state.session_id = session_id
1122
+ session_identity_map._add_unpresent(state, identitykey)
1123
+
1124
+ effective_populate_existing = populate_existing
1125
+ if refresh_state is state:
1126
+ effective_populate_existing = True
1127
+
1128
+ # populate. this looks at whether this state is new
1129
+ # for this load or was existing, and whether or not this
1130
+ # row is the first row with this identity.
1131
+ if currentload or effective_populate_existing:
1132
+ # full population routines. Objects here are either
1133
+ # just created, or we are doing a populate_existing
1134
+
1135
+ # be conservative about setting load_path when populate_existing
1136
+ # is in effect; want to maintain options from the original
1137
+ # load. see test_expire->test_refresh_maintains_deferred_options
1138
+ if isnew and (
1139
+ propagated_loader_options or not effective_populate_existing
1140
+ ):
1141
+ state.load_options = propagated_loader_options
1142
+ state.load_path = load_path
1143
+
1144
+ _populate_full(
1145
+ context,
1146
+ row,
1147
+ state,
1148
+ dict_,
1149
+ isnew,
1150
+ load_path,
1151
+ loaded_instance,
1152
+ effective_populate_existing,
1153
+ populators,
1154
+ )
1155
+
1156
+ if isnew:
1157
+ # state.runid should be equal to context.runid / runid
1158
+ # here, however for event checks we are being more conservative
1159
+ # and checking against existing run id
1160
+ # assert state.runid == runid
1161
+
1162
+ existing_runid = state.runid
1163
+
1164
+ if loaded_instance:
1165
+ if load_evt:
1166
+ state.manager.dispatch.load(state, context)
1167
+ if state.runid != existing_runid:
1168
+ _warn_for_runid_changed(state)
1169
+ if persistent_evt:
1170
+ loaded_as_persistent(context.session, state)
1171
+ if state.runid != existing_runid:
1172
+ _warn_for_runid_changed(state)
1173
+ elif refresh_evt:
1174
+ state.manager.dispatch.refresh(
1175
+ state, context, only_load_props
1176
+ )
1177
+ if state.runid != runid:
1178
+ _warn_for_runid_changed(state)
1179
+
1180
+ if effective_populate_existing or state.modified:
1181
+ if refresh_state and only_load_props:
1182
+ state._commit(dict_, only_load_props)
1183
+ else:
1184
+ state._commit_all(dict_, session_identity_map)
1185
+
1186
+ if post_load:
1187
+ post_load.add_state(state, True)
1188
+
1189
+ else:
1190
+ # partial population routines, for objects that were already
1191
+ # in the Session, but a row matches them; apply eager loaders
1192
+ # on existing objects, etc.
1193
+ unloaded = state.unloaded
1194
+ isnew = state not in context.partials
1195
+
1196
+ if not isnew or unloaded or populators["eager"]:
1197
+ # state is having a partial set of its attributes
1198
+ # refreshed. Populate those attributes,
1199
+ # and add to the "context.partials" collection.
1200
+
1201
+ to_load = _populate_partial(
1202
+ context,
1203
+ row,
1204
+ state,
1205
+ dict_,
1206
+ isnew,
1207
+ load_path,
1208
+ unloaded,
1209
+ populators,
1210
+ )
1211
+
1212
+ if isnew:
1213
+ if refresh_evt:
1214
+ existing_runid = state.runid
1215
+ state.manager.dispatch.refresh(state, context, to_load)
1216
+ if state.runid != existing_runid:
1217
+ _warn_for_runid_changed(state)
1218
+
1219
+ state._commit(dict_, to_load)
1220
+
1221
+ if post_load and context.invoke_all_eagers:
1222
+ post_load.add_state(state, False)
1223
+
1224
+ return instance
1225
+
1226
+ if mapper.polymorphic_map and not _polymorphic_from and not refresh_state:
1227
+ # if we are doing polymorphic, dispatch to a different _instance()
1228
+ # method specific to the subclass mapper
1229
+ def ensure_no_pk(row):
1230
+ identitykey = (
1231
+ identity_class,
1232
+ primary_key_getter(row),
1233
+ identity_token,
1234
+ )
1235
+ if not is_not_primary_key(identitykey[1]):
1236
+ return identitykey
1237
+ else:
1238
+ return None
1239
+
1240
+ _instance = _decorate_polymorphic_switch(
1241
+ _instance,
1242
+ context,
1243
+ query_entity,
1244
+ mapper,
1245
+ result,
1246
+ path,
1247
+ polymorphic_discriminator,
1248
+ adapter,
1249
+ ensure_no_pk,
1250
+ )
1251
+
1252
+ return _instance
1253
+
1254
+
1255
+ def _load_subclass_via_in(
1256
+ context, path, entity, polymorphic_from, option_entities
1257
+ ):
1258
+ mapper = entity.mapper
1259
+
1260
+ # TODO: polymorphic_from seems to be a Mapper in all cases.
1261
+ # this is likely not needed, but as we dont have typing in loading.py
1262
+ # yet, err on the safe side
1263
+ polymorphic_from_mapper = polymorphic_from.mapper
1264
+ not_against_basemost = polymorphic_from_mapper.inherits is not None
1265
+
1266
+ zero_idx = len(mapper.base_mapper.primary_key) == 1
1267
+
1268
+ if entity.is_aliased_class or not_against_basemost:
1269
+ q, enable_opt, disable_opt = mapper._subclass_load_via_in(
1270
+ entity, polymorphic_from
1271
+ )
1272
+ else:
1273
+ q, enable_opt, disable_opt = mapper._subclass_load_via_in_mapper
1274
+
1275
+ def do_load(context, path, states, load_only, effective_entity):
1276
+ if not option_entities:
1277
+ # filter out states for those that would have selectinloaded
1278
+ # from another loader
1279
+ # TODO: we are currently ignoring the case where the
1280
+ # "selectin_polymorphic" option is used, as this is much more
1281
+ # complex / specific / very uncommon API use
1282
+ states = [
1283
+ (s, v)
1284
+ for s, v in states
1285
+ if s.mapper._would_selectin_load_only_from_given_mapper(mapper)
1286
+ ]
1287
+
1288
+ if not states:
1289
+ return
1290
+
1291
+ orig_query = context.query
1292
+
1293
+ if path.parent:
1294
+ enable_opt_lcl = enable_opt._prepend_path(path)
1295
+ disable_opt_lcl = disable_opt._prepend_path(path)
1296
+ else:
1297
+ enable_opt_lcl = enable_opt
1298
+ disable_opt_lcl = disable_opt
1299
+ options = (
1300
+ (enable_opt_lcl,) + orig_query._with_options + (disable_opt_lcl,)
1301
+ )
1302
+
1303
+ q2 = q.options(*options)
1304
+
1305
+ q2._compile_options = context.compile_state.default_compile_options
1306
+ q2._compile_options += {"_current_path": path.parent}
1307
+
1308
+ if context.populate_existing:
1309
+ q2 = q2.execution_options(populate_existing=True)
1310
+
1311
+ while states:
1312
+ chunk = states[0 : _SelectInLoader._chunksize]
1313
+ states = states[_SelectInLoader._chunksize :]
1314
+ context.session.execute(
1315
+ q2,
1316
+ dict(
1317
+ primary_keys=[
1318
+ state.key[1][0] if zero_idx else state.key[1]
1319
+ for state, load_attrs in chunk
1320
+ ]
1321
+ ),
1322
+ ).unique().scalars().all()
1323
+
1324
+ return do_load
1325
+
1326
+
1327
+ def _populate_full(
1328
+ context,
1329
+ row,
1330
+ state,
1331
+ dict_,
1332
+ isnew,
1333
+ load_path,
1334
+ loaded_instance,
1335
+ populate_existing,
1336
+ populators,
1337
+ ):
1338
+ if isnew:
1339
+ # first time we are seeing a row with this identity.
1340
+ state.runid = context.runid
1341
+
1342
+ for key, getter in populators["quick"]:
1343
+ dict_[key] = getter(row)
1344
+ if populate_existing:
1345
+ for key, set_callable in populators["expire"]:
1346
+ dict_.pop(key, None)
1347
+ if set_callable:
1348
+ state.expired_attributes.add(key)
1349
+ else:
1350
+ for key, set_callable in populators["expire"]:
1351
+ if set_callable:
1352
+ state.expired_attributes.add(key)
1353
+
1354
+ for key, populator in populators["new"]:
1355
+ populator(state, dict_, row)
1356
+
1357
+ elif load_path != state.load_path:
1358
+ # new load path, e.g. object is present in more than one
1359
+ # column position in a series of rows
1360
+ state.load_path = load_path
1361
+
1362
+ # if we have data, and the data isn't in the dict, OK, let's put
1363
+ # it in.
1364
+ for key, getter in populators["quick"]:
1365
+ if key not in dict_:
1366
+ dict_[key] = getter(row)
1367
+
1368
+ # otherwise treat like an "already seen" row
1369
+ for key, populator in populators["existing"]:
1370
+ populator(state, dict_, row)
1371
+ # TODO: allow "existing" populator to know this is
1372
+ # a new path for the state:
1373
+ # populator(state, dict_, row, new_path=True)
1374
+
1375
+ else:
1376
+ # have already seen rows with this identity in this same path.
1377
+ for key, populator in populators["existing"]:
1378
+ populator(state, dict_, row)
1379
+
1380
+ # TODO: same path
1381
+ # populator(state, dict_, row, new_path=False)
1382
+
1383
+
1384
+ def _populate_partial(
1385
+ context, row, state, dict_, isnew, load_path, unloaded, populators
1386
+ ):
1387
+ if not isnew:
1388
+ if unloaded:
1389
+ # extra pass, see #8166
1390
+ for key, getter in populators["quick"]:
1391
+ if key in unloaded:
1392
+ dict_[key] = getter(row)
1393
+
1394
+ to_load = context.partials[state]
1395
+ for key, populator in populators["existing"]:
1396
+ if key in to_load:
1397
+ populator(state, dict_, row)
1398
+ else:
1399
+ to_load = unloaded
1400
+ context.partials[state] = to_load
1401
+
1402
+ for key, getter in populators["quick"]:
1403
+ if key in to_load:
1404
+ dict_[key] = getter(row)
1405
+ for key, set_callable in populators["expire"]:
1406
+ if key in to_load:
1407
+ dict_.pop(key, None)
1408
+ if set_callable:
1409
+ state.expired_attributes.add(key)
1410
+ for key, populator in populators["new"]:
1411
+ if key in to_load:
1412
+ populator(state, dict_, row)
1413
+
1414
+ for key, populator in populators["eager"]:
1415
+ if key not in unloaded:
1416
+ populator(state, dict_, row)
1417
+
1418
+ return to_load
1419
+
1420
+
1421
+ def _validate_version_id(mapper, state, dict_, row, getter):
1422
+ if mapper._get_state_attr_by_column(
1423
+ state, dict_, mapper.version_id_col
1424
+ ) != getter(row):
1425
+ raise orm_exc.StaleDataError(
1426
+ "Instance '%s' has version id '%s' which "
1427
+ "does not match database-loaded version id '%s'."
1428
+ % (
1429
+ state_str(state),
1430
+ mapper._get_state_attr_by_column(
1431
+ state, dict_, mapper.version_id_col
1432
+ ),
1433
+ getter(row),
1434
+ )
1435
+ )
1436
+
1437
+
1438
+ def _decorate_polymorphic_switch(
1439
+ instance_fn,
1440
+ context,
1441
+ query_entity,
1442
+ mapper,
1443
+ result,
1444
+ path,
1445
+ polymorphic_discriminator,
1446
+ adapter,
1447
+ ensure_no_pk,
1448
+ ):
1449
+ if polymorphic_discriminator is not None:
1450
+ polymorphic_on = polymorphic_discriminator
1451
+ else:
1452
+ polymorphic_on = mapper.polymorphic_on
1453
+ if polymorphic_on is None:
1454
+ return instance_fn
1455
+
1456
+ if adapter:
1457
+ polymorphic_on = adapter.columns[polymorphic_on]
1458
+
1459
+ def configure_subclass_mapper(discriminator):
1460
+ try:
1461
+ sub_mapper = mapper.polymorphic_map[discriminator]
1462
+ except KeyError:
1463
+ raise AssertionError(
1464
+ "No such polymorphic_identity %r is defined" % discriminator
1465
+ )
1466
+ else:
1467
+ if sub_mapper is mapper:
1468
+ return None
1469
+ elif not sub_mapper.isa(mapper):
1470
+ return False
1471
+
1472
+ return _instance_processor(
1473
+ query_entity,
1474
+ sub_mapper,
1475
+ context,
1476
+ result,
1477
+ path,
1478
+ adapter,
1479
+ _polymorphic_from=mapper,
1480
+ )
1481
+
1482
+ polymorphic_instances = util.PopulateDict(configure_subclass_mapper)
1483
+
1484
+ getter = result._getter(polymorphic_on)
1485
+
1486
+ def polymorphic_instance(row):
1487
+ discriminator = getter(row)
1488
+ if discriminator is not None:
1489
+ _instance = polymorphic_instances[discriminator]
1490
+ if _instance:
1491
+ return _instance(row)
1492
+ elif _instance is False:
1493
+ identitykey = ensure_no_pk(row)
1494
+
1495
+ if identitykey:
1496
+ raise sa_exc.InvalidRequestError(
1497
+ "Row with identity key %s can't be loaded into an "
1498
+ "object; the polymorphic discriminator column '%s' "
1499
+ "refers to %s, which is not a sub-mapper of "
1500
+ "the requested %s"
1501
+ % (
1502
+ identitykey,
1503
+ polymorphic_on,
1504
+ mapper.polymorphic_map[discriminator],
1505
+ mapper,
1506
+ )
1507
+ )
1508
+ else:
1509
+ return None
1510
+ else:
1511
+ return instance_fn(row)
1512
+ else:
1513
+ identitykey = ensure_no_pk(row)
1514
+
1515
+ if identitykey:
1516
+ raise sa_exc.InvalidRequestError(
1517
+ "Row with identity key %s can't be loaded into an "
1518
+ "object; the polymorphic discriminator column '%s' is "
1519
+ "NULL" % (identitykey, polymorphic_on)
1520
+ )
1521
+ else:
1522
+ return None
1523
+
1524
+ return polymorphic_instance
1525
+
1526
+
1527
+ class _PostLoad:
1528
+ """Track loaders and states for "post load" operations."""
1529
+
1530
+ __slots__ = "loaders", "states", "load_keys"
1531
+
1532
+ def __init__(self):
1533
+ self.loaders = {}
1534
+ self.states = util.OrderedDict()
1535
+ self.load_keys = None
1536
+
1537
+ def add_state(self, state, overwrite):
1538
+ # the states for a polymorphic load here are all shared
1539
+ # within a single PostLoad object among multiple subtypes.
1540
+ # Filtering of callables on a per-subclass basis needs to be done at
1541
+ # the invocation level
1542
+ self.states[state] = overwrite
1543
+
1544
+ def invoke(self, context, path):
1545
+ if not self.states:
1546
+ return
1547
+ path = path_registry.PathRegistry.coerce(path)
1548
+ for (
1549
+ effective_context,
1550
+ token,
1551
+ limit_to_mapper,
1552
+ loader,
1553
+ arg,
1554
+ kw,
1555
+ ) in self.loaders.values():
1556
+ states = [
1557
+ (state, overwrite)
1558
+ for state, overwrite in self.states.items()
1559
+ if state.manager.mapper.isa(limit_to_mapper)
1560
+ ]
1561
+ if states:
1562
+ loader(
1563
+ effective_context, path, states, self.load_keys, *arg, **kw
1564
+ )
1565
+ self.states.clear()
1566
+
1567
+ @classmethod
1568
+ def for_context(cls, context, path, only_load_props):
1569
+ pl = context.post_load_paths.get(path.path)
1570
+ if pl is not None and only_load_props:
1571
+ pl.load_keys = only_load_props
1572
+ return pl
1573
+
1574
+ @classmethod
1575
+ def path_exists(self, context, path, key):
1576
+ return (
1577
+ path.path in context.post_load_paths
1578
+ and key in context.post_load_paths[path.path].loaders
1579
+ )
1580
+
1581
+ @classmethod
1582
+ def callable_for_path(
1583
+ cls, context, path, limit_to_mapper, token, loader_callable, *arg, **kw
1584
+ ):
1585
+ if path.path in context.post_load_paths:
1586
+ pl = context.post_load_paths[path.path]
1587
+ else:
1588
+ pl = context.post_load_paths[path.path] = _PostLoad()
1589
+ pl.loaders[token] = (
1590
+ context,
1591
+ token,
1592
+ limit_to_mapper,
1593
+ loader_callable,
1594
+ arg,
1595
+ kw,
1596
+ )
1597
+
1598
+
1599
+ def _load_scalar_attributes(mapper, state, attribute_names, passive):
1600
+ """initiate a column-based attribute refresh operation."""
1601
+
1602
+ # assert mapper is _state_mapper(state)
1603
+ session = state.session
1604
+ if not session:
1605
+ raise orm_exc.DetachedInstanceError(
1606
+ "Instance %s is not bound to a Session; "
1607
+ "attribute refresh operation cannot proceed" % (state_str(state))
1608
+ )
1609
+
1610
+ no_autoflush = bool(passive & attributes.NO_AUTOFLUSH)
1611
+
1612
+ # in the case of inheritance, particularly concrete and abstract
1613
+ # concrete inheritance, the class manager might have some keys
1614
+ # of attributes on the superclass that we didn't actually map.
1615
+ # These could be mapped as "concrete, don't load" or could be completely
1616
+ # excluded from the mapping and we know nothing about them. Filter them
1617
+ # here to prevent them from coming through.
1618
+ if attribute_names:
1619
+ attribute_names = attribute_names.intersection(mapper.attrs.keys())
1620
+
1621
+ if mapper.inherits and not mapper.concrete:
1622
+ # load based on committed attributes in the object, formed into
1623
+ # a truncated SELECT that only includes relevant tables. does not
1624
+ # currently use state.key
1625
+ statement = mapper._optimized_get_statement(state, attribute_names)
1626
+ if statement is not None:
1627
+ # undefer() isn't needed here because statement has the
1628
+ # columns needed already, this implicitly undefers that column
1629
+ stmt = FromStatement(mapper, statement)
1630
+
1631
+ return _load_on_ident(
1632
+ session,
1633
+ stmt,
1634
+ None,
1635
+ only_load_props=attribute_names,
1636
+ refresh_state=state,
1637
+ no_autoflush=no_autoflush,
1638
+ )
1639
+
1640
+ # normal load, use state.key as the identity to SELECT
1641
+ has_key = bool(state.key)
1642
+
1643
+ if has_key:
1644
+ identity_key = state.key
1645
+ else:
1646
+ # this codepath is rare - only valid when inside a flush, and the
1647
+ # object is becoming persistent but hasn't yet been assigned
1648
+ # an identity_key.
1649
+ # check here to ensure we have the attrs we need.
1650
+ pk_attrs = [
1651
+ mapper._columntoproperty[col].key for col in mapper.primary_key
1652
+ ]
1653
+ if state.expired_attributes.intersection(pk_attrs):
1654
+ raise sa_exc.InvalidRequestError(
1655
+ "Instance %s cannot be refreshed - it's not "
1656
+ " persistent and does not "
1657
+ "contain a full primary key." % state_str(state)
1658
+ )
1659
+ identity_key = mapper._identity_key_from_state(state)
1660
+
1661
+ if (
1662
+ _none_set.issubset(identity_key) and not mapper.allow_partial_pks
1663
+ ) or _none_set.issuperset(identity_key):
1664
+ util.warn_limited(
1665
+ "Instance %s to be refreshed doesn't "
1666
+ "contain a full primary key - can't be refreshed "
1667
+ "(and shouldn't be expired, either).",
1668
+ state_str(state),
1669
+ )
1670
+ return
1671
+
1672
+ result = _load_on_ident(
1673
+ session,
1674
+ select(mapper),
1675
+ identity_key,
1676
+ refresh_state=state,
1677
+ only_load_props=attribute_names,
1678
+ no_autoflush=no_autoflush,
1679
+ )
1680
+
1681
+ # if instance is pending, a refresh operation
1682
+ # may not complete (even if PK attributes are assigned)
1683
+ if has_key and result is None:
1684
+ raise orm_exc.ObjectDeletedError(state)