MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _load_on_pk_identity

Method _load_on_pk_identity

lib/sqlalchemy/ext/baked.py:490–555  ·  view source on GitHub ↗

Load the given primary key identity from the database.

(self, session, query, primary_key_identity, **kw)

Source from the content-addressed store, hash-verified

488 return query._get_impl(ident, self._load_on_pk_identity)
489
490 def _load_on_pk_identity(self, session, query, primary_key_identity, **kw):
491 """Load the given primary key identity from the database."""
492
493 mapper = query._raw_columns[0]._annotations["parententity"]
494
495 _get_clause, _get_params = mapper._get_clause
496
497 def setup(query):
498 _lcl_get_clause = _get_clause
499 q = query._clone()
500 q._get_condition()
501 q._order_by = None
502
503 # None present in ident - turn those comparisons
504 # into "IS NULL"
505 if None in primary_key_identity:
506 nones = {
507 _get_params[col].key
508 for col, value in zip(
509 mapper.primary_key, primary_key_identity
510 )
511 if value is None
512 }
513 _lcl_get_clause = sql_util.adapt_criterion_to_null(
514 _lcl_get_clause, nones
515 )
516
517 # TODO: can mapper._get_clause be pre-adapted?
518 q._where_criteria = (
519 sql_util._deep_annotate(_lcl_get_clause, {"_orm_adapt": True}),
520 )
521
522 for fn in self._post_criteria:
523 q = fn(q)
524 return q
525
526 # cache the query against a key that includes
527 # which positions in the primary key are NULL
528 # (remember, we can map to an OUTER JOIN)
529 bq = self.bq
530
531 # add the clause we got from mapper._get_clause to the cache
532 # key so that if a race causes multiple calls to _get_clause,
533 # we've cached on ours
534 bq = bq._clone()
535 bq._cache_key += (_get_clause,)
536
537 bq = bq.with_criteria(
538 setup, tuple(elem is None for elem in primary_key_identity)
539 )
540
541 params = {
542 _get_params[primary_key].key: id_val
543 for id_val, primary_key in zip(
544 primary_key_identity, mapper.primary_key
545 )
546 }
547

Callers 1

_emit_lazyloadMethod · 0.80

Calls 4

with_criteriaMethod · 0.80
for_sessionMethod · 0.80
_cloneMethod · 0.45
paramsMethod · 0.45

Tested by

no test coverage detected