001package gudusoft.gsqlparser.analyzer.v2.callgraph;
002
003import gudusoft.gsqlparser.ir.bound.ERoutineKind;
004import gudusoft.gsqlparser.ir.common.SourceAnchor;
005
006import java.util.ArrayList;
007import java.util.Collections;
008import java.util.List;
009
010/**
011 * A node in the call graph representing a single routine (procedure, function, trigger, etc.).
012 */
013public class CallGraphNode {
014
015    private final String routineId;
016    private final String routineName;
017    private final String packageName;
018    private final ERoutineKind routineKind;
019    private final SourceAnchor declarationAnchor;
020    private final List<TableAccess> tableAccesses;
021
022    public CallGraphNode(String routineId, String routineName, String packageName,
023                         ERoutineKind routineKind, SourceAnchor declarationAnchor) {
024        this.routineId = routineId;
025        this.routineName = routineName;
026        this.packageName = packageName;
027        this.routineKind = routineKind;
028        this.declarationAnchor = declarationAnchor;
029        this.tableAccesses = new ArrayList<TableAccess>();
030    }
031
032    public String getRoutineId() { return routineId; }
033    public String getRoutineName() { return routineName; }
034    public String getPackageName() { return packageName; }
035    public ERoutineKind getRoutineKind() { return routineKind; }
036    public SourceAnchor getDeclarationAnchor() { return declarationAnchor; }
037
038    public List<TableAccess> getTableAccesses() {
039        return Collections.unmodifiableList(tableAccesses);
040    }
041
042    public void addTableAccess(TableAccess access) {
043        // Deduplicate: skip if same normalized table name and access kind already exists
044        String normalizedNew = TableAccessExtractor.normalizeTableName(access.getTableName());
045        for (TableAccess existing : tableAccesses) {
046            if (existing.getAccessKind() == access.getAccessKind()
047                    && TableAccessExtractor.normalizeTableName(existing.getTableName()).equals(normalizedNew)) {
048                return;
049            }
050        }
051        tableAccesses.add(access);
052    }
053
054    @Override
055    public String toString() {
056        return "CallGraphNode{" + routineId + ", tables=" + tableAccesses.size() + "}";
057    }
058}