MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / DedupeColumnCollection

Class DedupeColumnCollection

lib/sqlalchemy/sql/base.py:2303–2521  ·  view source on GitHub ↗

A :class:`_expression.ColumnCollection` that maintains deduplicating behavior. This is useful by schema level objects such as :class:`_schema.Table` and :class:`.PrimaryKeyConstraint`. The collection includes more sophisticated mutator methods as well to suit schema objects which

Source from the content-addressed store, hash-verified

2301
2302
2303class DedupeColumnCollection(WriteableColumnCollection[str, _NAMEDCOL]):
2304 """A :class:`_expression.ColumnCollection`
2305 that maintains deduplicating behavior.
2306
2307 This is useful by schema level objects such as :class:`_schema.Table` and
2308 :class:`.PrimaryKeyConstraint`. The collection includes more
2309 sophisticated mutator methods as well to suit schema objects which
2310 require mutable column collections.
2311
2312 .. versionadded:: 1.4
2313
2314 """
2315
2316 def add( # type: ignore[override]
2317 self,
2318 column: _NAMEDCOL,
2319 key: Optional[str] = None,
2320 *,
2321 index: Optional[int] = None,
2322 ) -> None:
2323 if key is not None and column.key != key:
2324 raise exc.ArgumentError(
2325 "DedupeColumnCollection requires columns be under "
2326 "the same key as their .key"
2327 )
2328 key = column.key
2329
2330 if key is None:
2331 raise exc.ArgumentError(
2332 "Can't add unnamed column to column collection"
2333 )
2334
2335 if key in self._index:
2336 existing = self._index[key][1]
2337
2338 if existing is column:
2339 return
2340
2341 self.replace(column, index=index)
2342
2343 # pop out memoized proxy_set as this
2344 # operation may very well be occurring
2345 # in a _make_proxy operation
2346 util.memoized_property.reset(column, "proxy_set")
2347 else:
2348 self._append_new_column(key, column, index=index)
2349
2350 def _append_new_column(
2351 self, key: str, named_column: _NAMEDCOL, *, index: Optional[int] = None
2352 ) -> None:
2353 collection_length = len(self._collection)
2354
2355 if index is None:
2356 l = collection_length
2357 else:
2358 if index < 0:
2359 index = max(0, collection_length + index)
2360 l = index

Calls

no outgoing calls