001package gudusoft.gsqlparser.sqlenv.compat;
002
003import gudusoft.gsqlparser.catalog.diagnostic.CatalogException;
004import gudusoft.gsqlparser.catalog.input.CatalogInputException;
005import gudusoft.gsqlparser.catalog.input.CatalogInputReader;
006import gudusoft.gsqlparser.catalog.input.CatalogInputReaders;
007import gudusoft.gsqlparser.catalog.input.CatalogInputSource;
008import gudusoft.gsqlparser.catalog.input.CatalogLoadOptions;
009import gudusoft.gsqlparser.catalog.input.model.UnifiedCatalogModel;
010import gudusoft.gsqlparser.sqlenv.TSQLEnv;
011
012/**
013 * One-line convenience facade for callers who want an eagerly-populated {@link TSQLEnv}
014 * from a manifest. Delegates to {@link SQLEnvCatalogLoader}.
015 *
016 * <p>Plan §13.4. The two factories serve the two common entry points: a programmatic
017 * {@link UnifiedCatalogModel} and a file-based {@link CatalogInputSource} that the
018 * registered {@link CatalogInputReader}s can read.</p>
019 */
020public final class SqlEnvCatalogBridge {
021
022    private SqlEnvCatalogBridge() {
023        // Static utility — no instances.
024    }
025
026    /**
027     * Apply {@code model} to a freshly-constructed {@link TSQLEnv} via
028     * {@link SQLEnvCatalogLoader#loadToSQLEnv}. Throws {@link CatalogException} if the
029     * model fails validation under {@code options}.
030     */
031    public static TSQLEnv from(UnifiedCatalogModel model, CatalogLoadOptions options) {
032        return new SQLEnvCatalogLoader().loadToSQLEnv(model, options);
033    }
034
035    /**
036     * Read {@code source} via the registered {@link CatalogInputReader} for its kind, then
037     * apply the resulting {@link UnifiedCatalogModel} to a freshly-constructed
038     * {@link TSQLEnv}. Wraps reader I/O failures as {@link CatalogException} so callers
039     * have one exception type to catch.
040     */
041    public static TSQLEnv from(CatalogInputSource source, CatalogLoadOptions options) {
042        if (source == null) {
043            throw new IllegalArgumentException("SqlEnvCatalogBridge.from: source is required");
044        }
045        if (options == null) {
046            throw new IllegalArgumentException("SqlEnvCatalogBridge.from: options is required");
047        }
048        UnifiedCatalogModel model;
049        try {
050            CatalogInputReader reader = CatalogInputReaders.forSource(source, options);
051            model = reader.read(source, options);
052        } catch (CatalogInputException ex) {
053            throw new CatalogException(
054                "SqlEnvCatalogBridge.from failed to read source " + source.name() + ": "
055                    + ex.getMessage(), ex);
056        }
057        return from(model, options);
058    }
059}