001package gudusoft.gsqlparser.catalog.runtime;
002
003import gudusoft.gsqlparser.EDbVendor;
004import gudusoft.gsqlparser.catalog.input.model.IdentifierConfig;
005
006import java.util.Objects;
007
008/**
009 * Runtime-side context for a resolution call: active catalog/schema, search path, and case
010 * sensitivity hint.
011 *
012 * <p>Plan §7.2. {@link #caseSensitive()} is advisory: the actual compare rule is driven by
013 * {@code IdentifierService} via {@code CatalogIdentifierPolicy.areEqual} — this flag is
014 * just a hint when callers want to override the dialect default at the resolution site
015 * (e.g., adapter integration tests).</p>
016 */
017public final class CatalogContext {
018
019    private final EDbVendor vendor;
020    private final String activeCatalog;
021    private final String activeSchema;
022    private final CatalogSearchPath searchPath;
023    private final boolean caseSensitive;
024    private final IdentifierConfig identifierConfig;
025
026    private CatalogContext(Builder b) {
027        if (b.vendor == null) {
028            throw new IllegalArgumentException("CatalogContext.vendor is required");
029        }
030        this.vendor = b.vendor;
031        this.activeCatalog = b.activeCatalog;
032        this.activeSchema = b.activeSchema;
033        this.searchPath = b.searchPath != null ? b.searchPath : CatalogSearchPath.empty();
034        this.caseSensitive = b.caseSensitive;
035        this.identifierConfig = b.identifierConfig != null
036            ? b.identifierConfig : IdentifierConfig.defaultsFor(b.vendor);
037        if (this.identifierConfig.vendor() != b.vendor) {
038            throw new IllegalArgumentException(
039                "CatalogContext: identifierConfig.vendor=" + this.identifierConfig.vendor()
040                    + " does not match vendor=" + b.vendor);
041        }
042    }
043
044    public static Builder builder() {
045        return new Builder();
046    }
047
048    public EDbVendor vendor() {
049        return vendor;
050    }
051
052    public String activeCatalog() {
053        return activeCatalog;
054    }
055
056    public String activeSchema() {
057        return activeSchema;
058    }
059
060    public CatalogSearchPath searchPath() {
061        return searchPath;
062    }
063
064    public boolean caseSensitive() {
065        return caseSensitive;
066    }
067
068    public IdentifierConfig identifierConfig() {
069        return identifierConfig;
070    }
071
072    @Override
073    public boolean equals(Object o) {
074        if (this == o) return true;
075        if (!(o instanceof CatalogContext)) return false;
076        CatalogContext that = (CatalogContext) o;
077        return caseSensitive == that.caseSensitive
078            && vendor == that.vendor
079            && Objects.equals(activeCatalog, that.activeCatalog)
080            && Objects.equals(activeSchema, that.activeSchema)
081            && searchPath.equals(that.searchPath)
082            && identifierConfig.equals(that.identifierConfig);
083    }
084
085    @Override
086    public int hashCode() {
087        return Objects.hash(vendor, activeCatalog, activeSchema, searchPath, caseSensitive,
088            identifierConfig);
089    }
090
091    @Override
092    public String toString() {
093        return "CatalogContext{vendor=" + vendor
094            + ", catalog=" + activeCatalog
095            + ", schema=" + activeSchema
096            + ", searchPath=" + searchPath
097            + ", caseSensitive=" + caseSensitive + '}';
098    }
099
100    public static final class Builder {
101
102        private EDbVendor vendor;
103        private String activeCatalog;
104        private String activeSchema;
105        private CatalogSearchPath searchPath;
106        private boolean caseSensitive;
107        private IdentifierConfig identifierConfig;
108
109        private Builder() {
110        }
111
112        public Builder identifierConfig(IdentifierConfig v) {
113            this.identifierConfig = v;
114            return this;
115        }
116
117        public Builder vendor(EDbVendor v) {
118            this.vendor = v;
119            return this;
120        }
121
122        public Builder activeCatalog(String v) {
123            this.activeCatalog = v;
124            return this;
125        }
126
127        public Builder activeSchema(String v) {
128            this.activeSchema = v;
129            return this;
130        }
131
132        public Builder searchPath(CatalogSearchPath v) {
133            this.searchPath = v;
134            return this;
135        }
136
137        public Builder caseSensitive(boolean v) {
138            this.caseSensitive = v;
139            return this;
140        }
141
142        public CatalogContext build() {
143            return new CatalogContext(this);
144        }
145    }
146}