MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _table_of

Function _table_of

graphify/extractors/sql.py:901–945  ·  view source on GitHub ↗

(table as written, alias) of a `relation`, a bare `from`, or a naked object_reference — the three places this grammar puts a table name. Only `relation` (and the bare `from` of a DELETE) can carry an alias: the bare identifier next to the object_reference is one (`dbo.Orders

(node)

Source from the content-addressed store, hash-verified

899 walk(child)
900
901 def _table_of(node) -> tuple[str | None, str | None]:
902 """(table as written, alias) of a `relation`, a bare `from`, or a naked
903 object_reference — the three places this grammar puts a table name.
904
905 Only `relation` (and the bare `from` of a DELETE) can carry an alias:
906 the bare identifier next to the object_reference is one (`dbo.Orders
907 o`). The identifiers INSIDE an object_reference are its name parts and
908 must not be read as an alias.
909 """
910 has_siblings = node.type in ("relation", "from")
911 obj = node
912 if has_siblings:
913 obj = next((c for c in node.children if c.type == "object_reference"), None)
914 if obj is None:
915 # `DELETE p FROM ...`: only the alias is named at this level.
916 alias_node = next(
917 (c for c in node.children
918 if c.type == "identifier" and not _read(c).startswith("@")),
919 None,
920 )
921 return None, (_read(alias_node) if alias_node is not None else None)
922 elif node.type != "object_reference":
923 return None, None
924
925 name = _read(obj)
926 suffix = ""
927 alias = None
928 if has_siblings:
929 for c in node.children:
930 if c is obj or c.type != "identifier":
931 continue
932 text = _read(c)
933 if text.startswith("@"):
934 suffix = text # dblink suffix, part of the name
935 else:
936 alias = text # `dbo.Orders o`
937 if alias is None and not _is_temp(name) and len(node.children) == 1:
938 # `UPDATE o SET ... FROM x o`: the relation is nothing but an
939 # alias. The DELETE shapes carry more text and stay names.
940 # A single-part bare name only: `dbo.Orders` is a table.
941 if len(obj.children) == 1 and obj.children[0].type == "identifier":
942 return None, name
943 if suffix and name.startswith("@"):
944 return name, alias
945 return f"{name}{suffix}", alias
946
947 def _is_temp(tbl: str) -> bool:
948 """`#t` / `##t` / `@t`: a temp table or a table variable, i.e. session

Callers 1

_write_tableFunction · 0.85

Calls 2

_is_tempFunction · 0.85
_readFunction · 0.70

Tested by

no test coverage detected