001package gudusoft.gsqlparser.dlineage.symbol;
002
003import gudusoft.gsqlparser.TSourceToken;
004import gudusoft.gsqlparser.nodes.TObjectName;
005
006/** The written names of local symbols, read from the parse, not cut out of rendered text. */
007public final class LocalSymbolNames {
008
009    private LocalSymbolNames() {
010    }
011
012    /**
013     * The base of a variable reference as the parser split it: {@code rec} of {@code rec.field}, the whole
014     * {@code "rec.one"} of a delimited name holding a dot; without a leading {@code :}. Falls back to the reference's
015     * text when the parser gave it no parts.
016     */
017    public static String baseName(TObjectName name) {
018        if (name == null) {
019            return null;
020        }
021        for (TSourceToken token : new TSourceToken[] {name.getServerToken(), name.getDatabaseToken(),
022                name.getSchemaToken(), name.getPackageToken(), name.getObjectToken(), name.getPartToken()}) {
023            if (token != null && token.toString() != null && !token.toString().isEmpty()) {
024                return strip(token.toString());
025            }
026        }
027        return strip(name.toString());
028    }
029
030    private static String strip(String text) {
031        return text != null && text.startsWith(":") ? text.substring(1) : text;
032    }
033}