001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (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 com.mybatisflex.core.dialect;
017
018
019import com.mybatisflex.core.FlexGlobalConfig;
020import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
021import com.mybatisflex.core.dialect.impl.DB2105Dialect;
022import com.mybatisflex.core.dialect.impl.DmDialect;
023import com.mybatisflex.core.dialect.impl.OracleDialect;
024import com.mybatisflex.core.util.MapUtil;
025import com.mybatisflex.core.util.ObjectUtil;
026
027import java.util.EnumMap;
028import java.util.Map;
029
030/**
031 * 方言工厂类,用于创建方言
032 */
033public class DialectFactory {
034
035    private DialectFactory() {
036    }
037
038    /**
039     * 数据库类型和方言的映射关系,可以通过其读取指定的方言,亦可能通过其扩展其他方言
040     * 比如,在 mybatis-flex 实现的方言中有 bug 或者 有自己的独立实现,可以添加自己的方言实现到
041     * 此 map 中,用于覆盖系统的方言实现
042     */
043    private static final Map<DbType, IDialect> dialectMap = new EnumMap<>(DbType.class);
044
045    /**
046     * 通过设置当前线程的数据库类型,以达到在代码执行时随时切换方言的功能
047     */
048    private static final ThreadLocal<DbType> dbTypeThreadLocal = new ThreadLocal<>();
049
050    /**
051     * 获取方言
052     *
053     * @return IDialect
054     */
055    public static IDialect getDialect() {
056        DbType dbType = ObjectUtil.requireNonNullElse(dbTypeThreadLocal.get(),
057            FlexGlobalConfig.getDefaultConfig().getDbType());
058        return MapUtil.computeIfAbsent(dialectMap, dbType, DialectFactory::createDialect);
059    }
060
061    /**
062     * 设置当前线程的 dbType
063     *
064     * @param dbType
065     */
066    public static void setHintDbType(DbType dbType) {
067        dbTypeThreadLocal.set(dbType);
068    }
069
070    /**
071     * 获取当前线程的 dbType
072     *
073     * @return dbType
074     */
075    public static DbType getHintDbType() {
076        return dbTypeThreadLocal.get();
077    }
078
079
080    /**
081     * 清除当前线程的 dbType
082     */
083    public static void clearHintDbType() {
084        dbTypeThreadLocal.remove();
085    }
086
087
088    /**
089     * 可以为某个 dbType 注册(新增或覆盖)自己的方言
090     *
091     * @param dbType  数据库类型
092     * @param dialect 方言的实现
093     */
094    public static void registerDialect(DbType dbType, IDialect dialect) {
095        dialectMap.put(dbType, dialect);
096    }
097
098
099    private static IDialect createDialect(DbType dbType) {
100        switch (dbType) {
101            case MYSQL:
102            case H2:
103            case MARIADB:
104            case GBASE:
105            case OSCAR:
106            case XUGU:
107            case OCEAN_BASE:
108            case CUBRID:
109            case GOLDILOCKS:
110            case CSIIDB:
111            case HIVE:
112            case DORIS:
113                return new CommonsDialectImpl(KeywordWrap.BACK_QUOTE, LimitOffsetProcessor.MYSQL);
114            case CLICK_HOUSE:
115            case GBASE_8S:
116                return new CommonsDialectImpl(KeywordWrap.NONE, LimitOffsetProcessor.MYSQL);
117            case DM:
118                return new DmDialect();
119            case ORACLE:
120                return new OracleDialect();
121            case GAUSS:
122                return new CommonsDialectImpl(KeywordWrap.DOUBLE_QUOTATION, LimitOffsetProcessor.ORACLE);
123            case POSTGRE_SQL:
124            case SQLITE:
125            case HSQL:
126            case KINGBASE_ES:
127            case PHOENIX:
128            case SAP_HANA:
129            case IMPALA:
130            case HIGH_GO:
131            case VERTICA:
132            case REDSHIFT:
133            case OPENGAUSS:
134            case UXDB:
135            case LEALONE:
136                return new CommonsDialectImpl(KeywordWrap.DOUBLE_QUOTATION, LimitOffsetProcessor.POSTGRESQL);
137            case TDENGINE:
138                return new CommonsDialectImpl(KeywordWrap.BACK_QUOTE, LimitOffsetProcessor.POSTGRESQL);
139            case ORACLE_12C:
140                return new OracleDialect(LimitOffsetProcessor.DERBY);
141            case FIREBIRD:
142            case DB2:
143                return new CommonsDialectImpl(KeywordWrap.NONE, LimitOffsetProcessor.DERBY);
144            case DB2_1005:
145                return new DB2105Dialect(KeywordWrap.NONE, DB2105Dialect.DB2105LimitOffsetProcessor.DB2105);
146            case SQLSERVER:
147                return new CommonsDialectImpl(KeywordWrap.NONE_CASE_SENSITIVE, LimitOffsetProcessor.SQLSERVER);
148            case SQLSERVER_2005:
149                return new CommonsDialectImpl(KeywordWrap.NONE_CASE_SENSITIVE, LimitOffsetProcessor.SQLSERVER_2005);
150            case INFORMIX:
151                return new CommonsDialectImpl(KeywordWrap.NONE, LimitOffsetProcessor.INFORMIX);
152            case SINODB:
153                return new CommonsDialectImpl(KeywordWrap.DOUBLE_QUOTATION, LimitOffsetProcessor.SINODB);
154            case SYBASE:
155                return new CommonsDialectImpl(KeywordWrap.DOUBLE_QUOTATION, LimitOffsetProcessor.SYBASE);
156            default:
157                return new CommonsDialectImpl();
158        }
159    }
160
161}