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

Function _write_table

graphify/extractors/sql.py:952–1008  ·  view source on GitHub ↗

The table a DELETE/UPDATE/INSERT/MERGE writes, as written. A session object (`#t`, `##t`, `@t`) is returned as-is; the caller drops it, since `_is_temp` has to answer that for the earlier shapes too.

(write_node)

Source from the content-addressed store, hash-verified

950 return tbl.lstrip("[").startswith(("#", "@"))
951
952 def _write_table(write_node) -> str | None:
953 """The table a DELETE/UPDATE/INSERT/MERGE writes, as written.
954
955 A session object (`#t`, `##t`, `@t`) is returned as-is; the caller drops
956 it, since `_is_temp` has to answer that for the earlier shapes too.
957 """
958 if write_node.type == "insert" and not any(
959 c.type == "keyword_into" for c in write_node.children
960 ):
961 return None
962 # 0.3.13 splits `INSERT INTO ##shared` into an ERROR `##` followed by
963 # an object_reference `shared`; the marker is a sibling, not in the name.
964 first = next(
965 (c for c in write_node.children if c.type in ("object_reference", "relation")),
966 None,
967 )
968 if first is not None:
969 marker = first.prev_sibling
970 while marker is not None and marker.type.startswith("keyword_"):
971 marker = marker.prev_sibling
972 if marker is not None and _read(marker).strip() in ("#", "##", "@"):
973 return f"#{_read(first).strip()}"
974 target = next(
975 (c for c in write_node.children
976 if c.type in ("relation", "object_reference", "from")),
977 None,
978 )
979 tbl, alias = _table_of(target) if target is not None else (None, None)
980 if tbl is not None:
981 return tbl
982 if target is None:
983 # `DELETE FROM dbo.T ...`: `delete` holds nothing but its keyword;
984 # the table is in the sibling FROM.
985 stmt = write_node.parent
986 for clause in (c for c in (stmt.children if stmt else []) if c.type == "from"):
987 for rel in clause.children:
988 if rel.type in ("object_reference", "relation"):
989 return _table_of(rel)[0]
990 return None
991 # Only an alias was named (`UPDATE o SET ... FROM x o`); the table is
992 # the FROM relation that declares it. The FROM lives inside `update`,
993 # or beside `delete` when the statement is a DELETE.
994 stmt = write_node.parent
995 if stmt is None:
996 return None
997 scopes = [write_node, stmt]
998 for scope in scopes:
999 for clause in (c for c in scope.children if c.type in ("from", "join")):
1000 rels = (
1001 clause.children if clause.type == "from"
1002 else [cc for cc in clause.children if cc.type == "relation"]
1003 )
1004 for rel in rels:
1005 r_tbl, r_alias = _table_of(rel)
1006 if r_tbl and alias and r_alias and r_alias.lower() == alias.lower():
1007 return r_tbl
1008 return None
1009

Callers 1

_walk_writesFunction · 0.85

Calls 2

_table_ofFunction · 0.85
_readFunction · 0.70

Tested by

no test coverage detected