All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cool.scx.jdbc.dialect.DialectSelector Maven / Gradle / Ivy

There is a newer version: 2.7.4
Show newest version
package cool.scx.jdbc.dialect;

import javax.sql.DataSource;
import java.sql.Driver;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public final class DialectSelector {

    private static final List DIALECT_LIST;

    static {
        DIALECT_LIST = new ArrayList<>();
        var loader = ServiceLoader.load(Dialect.class);
        for (var dialect : loader) {
            DIALECT_LIST.add(dialect);
        }
    }

    /**
     * 根据驱动查找
     *
     * @param realDriver realDriver
     * @return a
     */
    public static Dialect findDialect(Driver realDriver) {
        for (var dialect : DIALECT_LIST) {
            if (dialect.canHandle(realDriver)) {
                return dialect;
            }
        }
        throw new IllegalArgumentException("未找到对应的方言 !!! " + realDriver.getClass().getName());
    }

    /**
     * 根据数据源去查找
     *
     * @param dataSource da
     * @return a
     */
    public static Dialect findDialect(DataSource dataSource) {
        for (var dialect : DIALECT_LIST) {
            if (dialect.canHandle(dataSource)) {
                return dialect;
            }
        }
        throw new IllegalArgumentException("未找到对应的方言 !!! " + dataSource.getClass().getName());
    }

    /**
     * 根据数据库连接 URL 去找
     *
     * @param url a
     * @return a
     */
    public static Dialect findDialect(String url) {
        for (var dialect : DIALECT_LIST) {
            if (dialect.canHandle(url)) {
                return dialect;
            }
        }
        throw new IllegalArgumentException("未找到对应的方言 !!! " + url);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy