MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _copy_internals

Method _copy_internals

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

Source from the content-addressed store, hash-verified

6106 return False
6107
6108 def _copy_internals(
6109 self, clone: _CloneCallableType = _clone, **kw: Any
6110 ) -> None:
6111 # Select() object has been cloned and probably adapted by the
6112 # given clone function. Apply the cloning function to internal
6113 # objects
6114
6115 # 1. keep a dictionary of the froms we've cloned, and what
6116 # they've become. This allows us to ensure the same cloned from
6117 # is used when other items such as columns are "cloned"
6118
6119 all_the_froms = set(
6120 itertools.chain(
6121 _from_objects(*self._raw_columns),
6122 _from_objects(*self._where_criteria),
6123 _from_objects(*[elem[0] for elem in self._setup_joins]),
6124 )
6125 )
6126
6127 # do a clone for the froms we've gathered. what is important here
6128 # is if any of the things we are selecting from, like tables,
6129 # were converted into Join objects. if so, these need to be
6130 # added to _from_obj explicitly, because otherwise they won't be
6131 # part of the new state, as they don't associate themselves with
6132 # their columns.
6133 new_froms = {f: clone(f, **kw) for f in all_the_froms}
6134
6135 # 2. copy FROM collections, adding in joins that we've created.
6136 existing_from_obj = [clone(f, **kw) for f in self._from_obj]
6137 add_froms = (
6138 {f for f in new_froms.values() if isinstance(f, Join)}
6139 .difference(all_the_froms)
6140 .difference(existing_from_obj)
6141 )
6142
6143 self._from_obj = tuple(existing_from_obj) + tuple(add_froms)
6144
6145 # 3. clone everything else, making sure we use columns
6146 # corresponding to the froms we just made.
6147 def replace(
6148 obj: Union[BinaryExpression[Any], ColumnClause[Any]],
6149 **kw: Any,
6150 ) -> Optional[KeyedColumnElement[Any]]:
6151 if isinstance(obj, ColumnClause) and obj.table in new_froms:
6152 newelem = new_froms[obj.table].corresponding_column(obj)
6153 return newelem
6154 return None
6155
6156 kw["replace"] = replace
6157
6158 # copy everything else. for table-ish things like correlate,
6159 # correlate_except, setup_joins, these clone normally. For
6160 # column-expression oriented things like raw_columns, where_criteria,
6161 # order by, we get this from the new froms.
6162 super()._copy_internals(clone=clone, omit_attrs=("_from_obj",), **kw)
6163
6164 self._reset_memoizations()
6165

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