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