001package gudusoft.gsqlparser.resolver2.result;
002
003import gudusoft.gsqlparser.ETableSource;
004import gudusoft.gsqlparser.nodes.TTable;
005import gudusoft.gsqlparser.nodes.TObjectName;
006import gudusoft.gsqlparser.nodes.TColumnDefinition;
007import gudusoft.gsqlparser.resolver2.model.ColumnSource;
008
009/**
010 * Utility methods for resolution results.
011 * Provides convenience methods for checking and formatting resolution results.
012 */
013public final class ResolutionUtils {
014
015    private ResolutionUtils() {} // Prevent instantiation
016
017    // ==================== Column-related methods ====================
018
019    /**
020     * Check if a column is resolved
021     */
022    public static boolean isResolved(TObjectName column) {
023        return column != null && column.getSourceTable() != null;
024    }
025
026    /**
027     * Get the resolution confidence of a column
028     */
029    public static double getConfidence(TObjectName column) {
030        if (column == null) return 0.0;
031        ColumnSource cs = column.getColumnSource();
032        if (cs != null) {
033            return cs.getConfidence();
034        }
035        return isResolved(column) ? 1.0 : 0.0;
036    }
037
038    /**
039     * Check if it is a calculated column
040     */
041    public static boolean isCalculatedColumn(TObjectName column) {
042        if (column == null) return false;
043        ColumnSource cs = column.getColumnSource();
044        return cs != null && cs.isCalculatedColumn();
045    }
046
047    /**
048     * Check if it is a column alias
049     */
050    public static boolean isColumnAlias(TObjectName column) {
051        if (column == null) return false;
052        ColumnSource cs = column.getColumnSource();
053        return cs != null && cs.isColumnAlias();
054    }
055
056    /**
057     * Check if it is a star column
058     */
059    public static boolean isStarColumn(TObjectName column) {
060        if (column == null) return false;
061        String name = column.getColumnNameOnly();
062        return "*".equals(name) || (name != null && name.endsWith("*"));
063    }
064
065    /**
066     * Get the display name of a column (table.column format)
067     */
068    public static String getDisplayName(TObjectName column) {
069        if (column == null) return "";
070
071        TTable sourceTable = column.getSourceTable();
072        if (sourceTable != null) {
073            String tableName = getTableDisplayName(sourceTable);
074            return tableName + "." + column.getColumnNameOnly();
075        }
076        return column.getColumnNameOnly();
077    }
078
079    /**
080     * Get the data type of a column (if column definition exists)
081     */
082    public static String getDataType(TObjectName column) {
083        if (column == null) return null;
084        TColumnDefinition colDef = column.getLinkedColumnDef();
085        if (colDef != null && colDef.getDatatype() != null) {
086            return colDef.getDatatype().getDataTypeName();
087        }
088        return null;
089    }
090
091    // ==================== Table-related methods ====================
092
093    /**
094     * Check if it is a physical table
095     */
096    public static boolean isPhysicalTable(TTable table) {
097        if (table == null) return false;
098        return table.getTableType() == ETableSource.objectname && !table.isCTEName();
099    }
100
101    /**
102     * Check if it is a CTE reference
103     */
104    public static boolean isCTE(TTable table) {
105        return table != null && table.isCTEName();
106    }
107
108    /**
109     * Check if it is a subquery
110     */
111    public static boolean isSubquery(TTable table) {
112        return table != null && table.getTableType() == ETableSource.subquery;
113    }
114
115    /**
116     * Check if it is a function table
117     */
118    public static boolean isFunction(TTable table) {
119        return table != null && table.getTableType() == ETableSource.function;
120    }
121
122    /**
123     * Get the display name of a table
124     * Format: schema.table, alias(subquery), cte_name(CTE)
125     */
126    public static String getTableDisplayName(TTable table) {
127        if (table == null) return "";
128
129        ETableSource type = table.getTableType();
130
131        if (type == ETableSource.subquery) {
132            String alias = table.getAliasName();
133            return "(subquery" + (alias != null ? ", alias:" + alias : "") + ")";
134        } else if (table.isCTEName()) {
135            return table.getName() + "(CTE)";
136        } else if (type == ETableSource.unnest) {
137            return table.getAliasName() + "(unnest table)";
138        } else if (type == ETableSource.function) {
139            return "(table-valued function:" + table.getName() + ")";
140        }
141
142        // Physical table: return full name
143        if (table.getTableName() != null) {
144            return table.getTableName().toString();
145        }
146        return table.getName();
147    }
148
149    /**
150     * Get the full table name (including schema)
151     */
152    public static String getFullTableName(TTable table) {
153        if (table == null) return "";
154        if (table.getTableName() != null) {
155            return table.getTableName().toString();
156        }
157        return table.getName();
158    }
159}