001package gudusoft.gsqlparser.ir.semantic.catalog;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.List;
006
007/**
008 * Slice 76 — minimal table descriptor inside a {@link Catalog}.
009 *
010 * <p>Carries a name (possibly qualified, e.g. {@code "schema.table"}; the
011 * exact qualifier rules are delegated to {@code TSQLEnv.addTable} via the
012 * analyzer's internal bridge) and an ordered list of
013 * {@link CatalogColumn}s.
014 *
015 * <p>This class is immutable; construct it via {@link #builder(String)}.
016 */
017public final class CatalogTable {
018
019    private final String name;
020    private final List<CatalogColumn> columns;
021
022    private CatalogTable(String name, List<CatalogColumn> columns) {
023        this.name = name;
024        this.columns = Collections.unmodifiableList(columns);
025    }
026
027    public static Builder builder(String name) {
028        return new Builder(name);
029    }
030
031    public String getName() {
032        return name;
033    }
034
035    /**
036     * @return an unmodifiable view of the columns in declaration order.
037     */
038    public List<CatalogColumn> getColumns() {
039        return columns;
040    }
041
042    @Override
043    public String toString() {
044        return "CatalogTable{name=" + name + ", columns=" + columns + "}";
045    }
046
047    /**
048     * Mutable builder for {@link CatalogTable}. Single-use:
049     * {@link #build()} returns one {@code CatalogTable} and the builder
050     * should not be reused after that.
051     */
052    public static final class Builder {
053        private final String name;
054        private final List<CatalogColumn> columns = new ArrayList<>();
055
056        private Builder(String name) {
057            if (name == null) {
058                throw new IllegalArgumentException("table name must not be null");
059            }
060            if (name.isEmpty()) {
061                throw new IllegalArgumentException("table name must not be empty");
062            }
063            this.name = name;
064        }
065
066        /** Convenience overload — equivalent to {@code addColumn(new CatalogColumn(name))}. */
067        public Builder addColumn(String columnName) {
068            return addColumn(new CatalogColumn(columnName));
069        }
070
071        public Builder addColumn(CatalogColumn column) {
072            if (column == null) {
073                throw new IllegalArgumentException("column must not be null");
074            }
075            columns.add(column);
076            return this;
077        }
078
079        public CatalogTable build() {
080            return new CatalogTable(name, new ArrayList<>(columns));
081        }
082    }
083}