001package gudusoft.gsqlparser.catalog.runtime;
002
003import java.util.Collections;
004import java.util.LinkedHashMap;
005import java.util.Map;
006import java.util.Objects;
007
008/**
009 * Configuration handed to {@link CatalogProvider#open(CatalogProviderConfig)}.
010 *
011 * <p>Plan ยง7.2. Carries vendor-agnostic properties (e.g., JDBC URL, OAuth tokens) that the
012 * provider interprets. Property keys are stable per provider; cross-provider keys live on
013 * {@code CatalogLoadOptions}.</p>
014 */
015public final class CatalogProviderConfig {
016
017    private static final CatalogProviderConfig EMPTY = new CatalogProviderConfig(new Builder());
018
019    private final Map<String, Object> properties;
020
021    private CatalogProviderConfig(Builder b) {
022        this.properties = Collections.unmodifiableMap(new LinkedHashMap<String, Object>(b.properties));
023    }
024
025    public static Builder builder() {
026        return new Builder();
027    }
028
029    public static CatalogProviderConfig empty() {
030        return EMPTY;
031    }
032
033    public Map<String, Object> properties() {
034        return properties;
035    }
036
037    /** Convenience accessor; returns {@code null} when the key is absent. */
038    public Object property(String key) {
039        return properties.get(key);
040    }
041
042    @Override
043    public boolean equals(Object o) {
044        if (this == o) return true;
045        if (!(o instanceof CatalogProviderConfig)) return false;
046        return properties.equals(((CatalogProviderConfig) o).properties);
047    }
048
049    @Override
050    public int hashCode() {
051        return Objects.hash(properties);
052    }
053
054    /**
055     * {@inheritDoc}
056     *
057     * <p>Only property <em>keys</em> are included; values are redacted because configs
058     * routinely carry credentials (JDBC passwords, OAuth tokens). Use
059     * {@link #properties()} when raw values are genuinely needed (and avoid logging
060     * the result).</p>
061     */
062    @Override
063    public String toString() {
064        return "CatalogProviderConfig{keys=" + properties.keySet() + '}';
065    }
066
067    public static final class Builder {
068
069        private final Map<String, Object> properties = new LinkedHashMap<String, Object>();
070
071        private Builder() {
072        }
073
074        public Builder property(String key, Object value) {
075            if (key == null) {
076                throw new IllegalArgumentException(
077                    "CatalogProviderConfig property key may not be null");
078            }
079            this.properties.put(key, value);
080            return this;
081        }
082
083        public CatalogProviderConfig build() {
084            return new CatalogProviderConfig(this);
085        }
086    }
087}