001package gudusoft.gsqlparser.sqlenv.compat;
002
003import gudusoft.gsqlparser.EDbVendor;
004import gudusoft.gsqlparser.catalog.input.CatalogLoadOptions;
005import gudusoft.gsqlparser.catalog.input.CatalogLoaders;
006import gudusoft.gsqlparser.catalog.input.CatalogLoadingMode;
007import gudusoft.gsqlparser.catalog.runtime.CatalogProvider;
008import gudusoft.gsqlparser.catalog.runtime.CatalogProviderConfig;
009import gudusoft.gsqlparser.catalog.runtime.CatalogRuntime;
010import gudusoft.gsqlparser.sqlenv.TSQLEnv;
011
012/**
013 * Convenience facade aliasing {@link CatalogRuntimeToSQLEnvBridge} for the lazy-mode
014 * use case. Plan §13.3.
015 *
016 * <p>Two flavors:</p>
017 * <ul>
018 *   <li>{@link #from(CatalogRuntime)} — wrap an already-built runtime as a
019 *       {@code TSQLEnv} that the legacy parser can consume.</li>
020 *   <li>{@link #from(CatalogProvider, CatalogProviderConfig)} — open the provider, build
021 *       a {@code CatalogRuntime} in {@link CatalogLoadingMode#LAZY LAZY} mode, and wrap.
022 *       Vendor is read from the provider through a one-shot empty snapshot to keep the
023 *       facade ergonomic for callers that don't already have a runtime in hand.</li>
024 * </ul>
025 *
026 * <p>Both flavors carry no {@link CatalogLoadOptions}; callers that need defaults
027 * (active catalog/schema, fetch caps, identifier overrides) should construct the
028 * {@link CatalogRuntimeToSQLEnvBridge} directly with their {@link CatalogLoadOptions}
029 * instead.</p>
030 */
031public final class LazyCatalogSqlEnv {
032
033    private LazyCatalogSqlEnv() {
034        // Static utility — no instances.
035    }
036
037    /** Wrap an existing runtime as a {@link TSQLEnv}. The runtime owns its provider. */
038    public static TSQLEnv from(CatalogRuntime runtime) {
039        if (runtime == null) {
040            throw new IllegalArgumentException("LazyCatalogSqlEnv.from: runtime must not be null");
041        }
042        return new CatalogRuntimeToSQLEnvBridge(runtime, /* options */ null);
043    }
044
045    /**
046     * Wrap an existing runtime as a {@link TSQLEnv} with the supplied options applied
047     * (defaults / identifier config / fetch caps). Convenience for callers that already
048     * have a {@link CatalogLoadOptions} in hand.
049     */
050    public static TSQLEnv from(CatalogRuntime runtime, CatalogLoadOptions options) {
051        if (runtime == null) {
052            throw new IllegalArgumentException("LazyCatalogSqlEnv.from: runtime must not be null");
053        }
054        return new CatalogRuntimeToSQLEnvBridge(runtime, options);
055    }
056
057    /**
058     * Open the provider, build a runtime in {@link CatalogLoadingMode#LAZY LAZY} mode at
059     * vendor {@code generic}, and wrap. Mirrors
060     * {@link CatalogLoaders#fromProvider(CatalogProvider, CatalogProviderConfig)} —
061     * see that method's Javadoc for why the default vendor is generic. Most callers want
062     * {@link #from(CatalogProvider, CatalogProviderConfig, EDbVendor)} instead.
063     */
064    public static TSQLEnv from(CatalogProvider provider, CatalogProviderConfig config) {
065        if (provider == null) {
066            throw new IllegalArgumentException("LazyCatalogSqlEnv.from: provider must not be null");
067        }
068        return new CatalogRuntimeToSQLEnvBridge(
069            CatalogLoaders.fromProvider(provider, config), /* options */ null);
070    }
071
072    /**
073     * Open the provider, build a runtime in LAZY mode for the given vendor, and wrap.
074     * The vendor-explicit form is preferred when the caller knows it: identifier
075     * folding and resolver candidate-expansion both rely on the runtime's vendor.
076     */
077    public static TSQLEnv from(CatalogProvider provider, CatalogProviderConfig config,
078                               EDbVendor vendor) {
079        if (provider == null) {
080            throw new IllegalArgumentException("LazyCatalogSqlEnv.from: provider must not be null");
081        }
082        if (vendor == null) {
083            throw new IllegalArgumentException("LazyCatalogSqlEnv.from: vendor must not be null");
084        }
085        return new CatalogRuntimeToSQLEnvBridge(
086            CatalogLoaders.fromProvider(provider, config, vendor), /* options */ null);
087    }
088}