MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _gen_tq_label

Method _gen_tq_label

lib/sqlalchemy/sql/elements.py:5491–5547  ·  view source on GitHub ↗

generate table-qualified label for a table-bound column this is <tablename>_<columnname>. used primarily for LABEL_STYLE_TABLENAME_PLUS_COL as well as the .columns collection on a Join object.

(
        self, name: str, dedupe_on_key: bool = True
    )

Source from the content-addressed store, hash-verified

5489 return other.proxy_set.intersection(self.proxy_set)
5490
5491 def _gen_tq_label(
5492 self, name: str, dedupe_on_key: bool = True
5493 ) -> Optional[str]:
5494 """generate table-qualified label
5495
5496 for a table-bound column this is <tablename>_<columnname>.
5497
5498 used primarily for LABEL_STYLE_TABLENAME_PLUS_COL
5499 as well as the .columns collection on a Join object.
5500
5501 """
5502 label: str
5503 t = self.table
5504 if self.is_literal:
5505 return None
5506 elif t is not None and is_named_from_clause(t):
5507 if has_schema_attr(t) and t.schema:
5508 label = (
5509 t.schema.replace(".", "_") + "_" + t.name + ("_" + name)
5510 )
5511 else:
5512 assert not TYPE_CHECKING or isinstance(t, NamedFromClause)
5513 label = t.name + ("_" + name)
5514
5515 # propagate name quoting rules for labels.
5516 if is_quoted_name(name) and name.quote is not None:
5517 if is_quoted_name(label):
5518 label.quote = name.quote
5519 else:
5520 label = quoted_name(label, name.quote)
5521 elif is_quoted_name(t.name) and t.name.quote is not None:
5522 # can't get this situation to occur, so let's
5523 # assert false on it for now
5524 assert not isinstance(label, quoted_name)
5525 label = quoted_name(label, t.name.quote)
5526
5527 if dedupe_on_key:
5528 # ensure the label name doesn't conflict with that of an
5529 # existing column. note that this implies that any Column
5530 # must **not** set up its _label before its parent table has
5531 # all of its other Column objects set up. There are several
5532 # tables in the test suite which will fail otherwise; example:
5533 # table "owner" has columns "name" and "owner_name". Therefore
5534 # column owner.name cannot use the label "owner_name", it has
5535 # to be "owner_name_1".
5536 if label in t.c:
5537 _label = label
5538 counter = 1
5539 while _label in t.c:
5540 _label = label + f"_{counter}"
5541 counter += 1
5542 label = _label
5543
5544 return coercions.expect(roles.TruncatedLabelRole, label)
5545
5546 else:
5547 return name
5548

Callers 1

_ddl_labelMethod · 0.95

Calls 5

is_named_from_clauseFunction · 0.85
has_schema_attrFunction · 0.85
is_quoted_nameFunction · 0.85
quoted_nameClass · 0.85
replaceMethod · 0.45

Tested by

no test coverage detected