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)
| 3627 | ) |
| 3628 | |
| 3629 | def _optimized_get_statement(self, state, attribute_names): |
| 3630 | """assemble a WHERE clause which retrieves a given state by primary |
| 3631 | key, using a minimized set of tables. |
| 3632 | |
| 3633 | Applies to a joined-table inheritance mapper where the |
| 3634 | requested attribute names are only present on joined tables, |
| 3635 | not the base table. The WHERE clause attempts to include |
| 3636 | only those tables to minimize joins. |
| 3637 | |
| 3638 | """ |
| 3639 | props = self._props |
| 3640 | |
| 3641 | col_attribute_names = set(attribute_names).intersection( |
| 3642 | state.mapper.column_attrs.keys() |
| 3643 | ) |
| 3644 | tables: Set[FromClause] = set( |
| 3645 | chain( |
| 3646 | *[ |
| 3647 | sql_util.find_tables(c, check_columns=True) |
| 3648 | for key in col_attribute_names |
| 3649 | for c in props[key].columns |
| 3650 | ] |
| 3651 | ) |
| 3652 | ) |
| 3653 | |
| 3654 | if self.base_mapper.local_table in tables: |
| 3655 | return None |
| 3656 | |
| 3657 | def visit_binary(binary): |
| 3658 | leftcol = binary.left |
| 3659 | rightcol = binary.right |
| 3660 | if leftcol is None or rightcol is None: |
| 3661 | return |
| 3662 | |
| 3663 | if leftcol.table not in tables: |
| 3664 | leftval = self._get_committed_state_attr_by_column( |
| 3665 | state, |
| 3666 | state.dict, |
| 3667 | leftcol, |
| 3668 | passive=PassiveFlag.PASSIVE_NO_INITIALIZE, |
| 3669 | ) |
| 3670 | if leftval in orm_util._none_set: |
| 3671 | raise _OptGetColumnsNotAvailable() |
| 3672 | binary.left = sql.bindparam( |
| 3673 | None, leftval, type_=binary.right.type |
| 3674 | ) |
| 3675 | elif rightcol.table not in tables: |
| 3676 | rightval = self._get_committed_state_attr_by_column( |
| 3677 | state, |
| 3678 | state.dict, |
| 3679 | rightcol, |
| 3680 | passive=PassiveFlag.PASSIVE_NO_INITIALIZE, |
| 3681 | ) |
| 3682 | if rightval in orm_util._none_set: |
| 3683 | raise _OptGetColumnsNotAvailable() |
| 3684 | binary.right = sql.bindparam( |
| 3685 | None, rightval, type_=binary.right.type |
| 3686 | ) |