MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _copy_internals

Method _copy_internals

lib/sqlalchemy/sql/selectable.py:5853–5909  ·  view source on GitHub ↗
(
        self, clone: _CloneCallableType = _clone, **kw: Any
    )

Source from the content-addressed store, hash-verified

5851 return False
5852
5853 def _copy_internals(
5854 self, clone: _CloneCallableType = _clone, **kw: Any
5855 ) -> None:
5856 # Select() object has been cloned and probably adapted by the
5857 # given clone function. Apply the cloning function to internal
5858 # objects
5859
5860 # 1. keep a dictionary of the froms we've cloned, and what
5861 # they've become. This allows us to ensure the same cloned from
5862 # is used when other items such as columns are "cloned"
5863
5864 all_the_froms = set(
5865 itertools.chain(
5866 _from_objects(*self._raw_columns),
5867 _from_objects(*self._where_criteria),
5868 _from_objects(*[elem[0] for elem in self._setup_joins]),
5869 )
5870 )
5871
5872 # do a clone for the froms we've gathered. what is important here
5873 # is if any of the things we are selecting from, like tables,
5874 # were converted into Join objects. if so, these need to be
5875 # added to _from_obj explicitly, because otherwise they won't be
5876 # part of the new state, as they don't associate themselves with
5877 # their columns.
5878 new_froms = {f: clone(f, **kw) for f in all_the_froms}
5879
5880 # 2. copy FROM collections, adding in joins that we've created.
5881 existing_from_obj = [clone(f, **kw) for f in self._from_obj]
5882 add_froms = (
5883 {f for f in new_froms.values() if isinstance(f, Join)}
5884 .difference(all_the_froms)
5885 .difference(existing_from_obj)
5886 )
5887
5888 self._from_obj = tuple(existing_from_obj) + tuple(add_froms)
5889
5890 # 3. clone everything else, making sure we use columns
5891 # corresponding to the froms we just made.
5892 def replace(
5893 obj: Union[BinaryExpression[Any], ColumnClause[Any]],
5894 **kw: Any,
5895 ) -> Optional[KeyedColumnElement[Any]]:
5896 if isinstance(obj, ColumnClause) and obj.table in new_froms:
5897 newelem = new_froms[obj.table].corresponding_column(obj)
5898 return newelem
5899 return None
5900
5901 kw["replace"] = replace
5902
5903 # copy everything else. for table-ish things like correlate,
5904 # correlate_except, setup_joins, these clone normally. For
5905 # column-expression oriented things like raw_columns, where_criteria,
5906 # order by, we get this from the new froms.
5907 super()._copy_internals(clone=clone, omit_attrs=("_from_obj",), **kw)
5908
5909 self._reset_memoizations()
5910

Callers

nothing calls this directly

Calls 7

_from_objectsFunction · 0.85
_reset_memoizationsMethod · 0.80
cloneFunction · 0.70
chainMethod · 0.45
differenceMethod · 0.45
valuesMethod · 0.45
_copy_internalsMethod · 0.45

Tested by

no test coverage detected