MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _optimized_get_statement

Method _optimized_get_statement

lib/sqlalchemy/orm/mapper.py:3605–3705  ·  view source on GitHub ↗

assemble a WHERE clause which retrieves a given state by primary key, using a minimized set of tables. Applies to a joined-table inheritance mapper where the requested attribute names are only present on joined tables, not the base table. The WHERE clause attempts t

(self, state, attribute_names)

Source from the content-addressed store, hash-verified

3603 )
3604
3605 def _optimized_get_statement(self, state, attribute_names):
3606 """assemble a WHERE clause which retrieves a given state by primary
3607 key, using a minimized set of tables.
3608
3609 Applies to a joined-table inheritance mapper where the
3610 requested attribute names are only present on joined tables,
3611 not the base table. The WHERE clause attempts to include
3612 only those tables to minimize joins.
3613
3614 """
3615 props = self._props
3616
3617 col_attribute_names = set(attribute_names).intersection(
3618 state.mapper.column_attrs.keys()
3619 )
3620 tables: Set[FromClause] = set(
3621 chain(
3622 *[
3623 sql_util.find_tables(c, check_columns=True)
3624 for key in col_attribute_names
3625 for c in props[key].columns
3626 ]
3627 )
3628 )
3629
3630 if self.base_mapper.local_table in tables:
3631 return None
3632
3633 def visit_binary(binary):
3634 leftcol = binary.left
3635 rightcol = binary.right
3636 if leftcol is None or rightcol is None:
3637 return
3638
3639 if leftcol.table not in tables:
3640 leftval = self._get_committed_state_attr_by_column(
3641 state,
3642 state.dict,
3643 leftcol,
3644 passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
3645 )
3646 if leftval in orm_util._none_set:
3647 raise _OptGetColumnsNotAvailable()
3648 binary.left = sql.bindparam(
3649 None, leftval, type_=binary.right.type
3650 )
3651 elif rightcol.table not in tables:
3652 rightval = self._get_committed_state_attr_by_column(
3653 state,
3654 state.dict,
3655 rightcol,
3656 passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
3657 )
3658 if rightval in orm_util._none_set:
3659 raise _OptGetColumnsNotAvailable()
3660 binary.right = sql.bindparam(
3661 None, rightval, type_=binary.right.type
3662 )

Callers 2

_load_scalar_attributesFunction · 0.80

Calls 10

iterate_to_rootMethod · 0.95
intersectionMethod · 0.45
keysMethod · 0.45
appendMethod · 0.45
addMethod · 0.45
and_Method · 0.45
extendMethod · 0.45
set_label_styleMethod · 0.45
whereMethod · 0.45
selectMethod · 0.45

Tested by 1