001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.db.driver;
017
018import io.jboot.db.datasource.DataSourceConfig;
019import io.jboot.utils.ClassUtil;
020import io.jboot.utils.StrUtil;
021
022import java.util.HashMap;
023import java.util.Map;
024
025public class DriverClassNames {
026
027    private static final Map<String, String[]> driverClassNames = new HashMap<>();
028
029    static {
030        driverClassNames.put(DataSourceConfig.TYPE_MYSQL, new String[]{"com.mysql.cj.jdbc.Driver", "com.mysql.jdbc.Driver"});
031        driverClassNames.put(DataSourceConfig.TYPE_ORACLE, new String[]{"oracle.jdbc.driver.OracleDriver", "oracle.jdbc.OracleDriver"});
032        driverClassNames.put(DataSourceConfig.TYPE_SQLSERVER, new String[]{"com.microsoft.sqlserver.jdbc.SQLServerDriver"});
033        driverClassNames.put(DataSourceConfig.TYPE_SQLITE, new String[]{"org.sqlite.JDBC"});
034        driverClassNames.put(DataSourceConfig.TYPE_POSTGRESQL, new String[]{"org.postgresql.Driver"});
035        driverClassNames.put(DataSourceConfig.TYPE_DM, new String[]{"dm.jdbc.driver.DmDriver"});
036        driverClassNames.put(DataSourceConfig.TYPE_CLICKHOUSE, new String[]{"com.github.housepower.jdbc.ClickHouseDriver", "ru.yandex.clickhouse.ClickHouseDriver"});
037        driverClassNames.put(DataSourceConfig.TYPE_INFORMIX, new String[]{"com.informix.jdbc.IfxDriver"});
038    }
039
040
041    /**
042     * Jboot 自己实现的驱动,比如 ClickHouse 为了适配 JFinal 做了一些驱动改动
043     */
044    private static final Map<String, String> jbootDriverMapping = new HashMap<>();
045
046    static {
047        jbootDriverMapping.put("com.github.housepower.jdbc.ClickHouseDriver", "io.jboot.db.driver.NativeClickHouseDriver");
048        jbootDriverMapping.put("ru.yandex.clickhouse.ClickHouseDriver", "io.jboot.db.driver.OfficialClickHouseDriver");
049    }
050
051
052    /**
053     * 获取 默认的 jdbc 驱动类
054     *
055     * @param type
056     * @return
057     */
058    public static String getDefaultDriverClass(String type) {
059        String[] drivers = driverClassNames.get(type.toLowerCase());
060        if (drivers == null || drivers.length == 0) {
061            return null;
062        }
063
064        for (String driver : drivers) {
065            if (ClassUtil.hasClass(driver)) {
066                String jbootDriver = jbootDriverMapping.get(driver);
067                return StrUtil.isNotBlank(jbootDriver) ? jbootDriver : driver;
068            }
069        }
070        return null;
071    }
072
073
074}