Selaa lähdekoodia

数据库类型归类优化

hubin 1 vuosi sitten
vanhempi
commit
95a196f7af

+ 40 - 2
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/DbType.java

@@ -129,7 +129,7 @@ public enum DbType {
     /**
      * Sinodb
      */
-    SINODB("sinodb","星瑞格数据库"),
+    SINODB("sinodb", "星瑞格数据库"),
     /**
      * Oscar
      */
@@ -154,7 +154,7 @@ public enum DbType {
      * CUBRID
      */
     CUBRID("cubrid", "CUBRID数据库"),
-   /**
+    /**
      * SUNDB
      */
     SUNDB("sundb", "SUNDB数据库"),
@@ -233,4 +233,42 @@ public enum DbType {
         }
         return OTHER;
     }
+
+    public boolean mysqlSameType() {
+        return this == DbType.MYSQL
+            || this == DbType.MARIADB
+            || this == DbType.GBASE
+            || this == DbType.OSCAR
+            || this == DbType.XU_GU
+            || this == DbType.CLICK_HOUSE
+            || this == DbType.OCEAN_BASE
+            || this == DbType.CUBRID
+            || this == DbType.SUNDB;
+    }
+
+    public boolean oracleSameType() {
+        return this == DbType.ORACLE
+            || this == DbType.DM
+            || this == DbType.GAUSS;
+    }
+
+    public boolean postgresqlSameType() {
+        return this == DbType.POSTGRE_SQL
+            || this == DbType.H2
+            || this == DbType.LEALONE
+            || this == DbType.SQLITE
+            || this == DbType.HSQL
+            || this == DbType.KINGBASE_ES
+            || this == DbType.PHOENIX
+            || this == DbType.SAP_HANA
+            || this == DbType.IMPALA
+            || this == DbType.HIGH_GO
+            || this == DbType.VERTICA
+            || this == DbType.REDSHIFT
+            || this == DbType.OPENGAUSS
+            || this == DbType.TDENGINE
+            || this == DbType.UXDB
+            || this == DbType.GBASE8S_PG
+            || this == DbType.GBASE_8C;
+    }
 }

+ 15 - 40
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/ddl/DdlHelper.java

@@ -147,47 +147,22 @@ public class DdlHelper {
 
     protected static IDdlGenerator getDdlGenerator(String jdbcUrl) throws RuntimeException {
         DbType dbType = JdbcUtils.getDbType(jdbcUrl);
-        switch (dbType) {
-	        case MYSQL:
-            case MARIADB:
-            case GBASE:
-            case OSCAR:
-            case XU_GU:
-            case CLICK_HOUSE:
-            case OCEAN_BASE:
-            case CUBRID:
-            case SUNDB:
-                return MysqlDdlGenerator.newInstance();
-            case ORACLE:
-            case DM:
-            case GAUSS:
-            case ORACLE_12C:
-            case FIREBIRD:
-            case SQL_SERVER:
-                return OracleDdlGenerator.newInstance();
-            case SQLITE:
-                return SQLiteDdlGenerator.newInstance();
-            case POSTGRE_SQL:
-            case H2:
-            case LEALONE:
-            case HSQL:
-            case KINGBASE_ES:
-            case PHOENIX:
-            case SAP_HANA:
-            case IMPALA:
-            case HIGH_GO:
-            case VERTICA:
-            case REDSHIFT:
-            case OPENGAUSS:
-            case TDENGINE:
-            case UXDB:
-            case GBASE8S_PG:
-            case GBASE_8C:
-                return PostgreDdlGenerator.newInstance();
-	        default:
-                throw ExceptionUtils.mpe("%s database not supported.", dbType.getDb());
+        // mysql same type
+        if (dbType.mysqlSameType()) {
+            return MysqlDdlGenerator.newInstance();
         }
-
+        // oracle same type
+        else if (dbType.oracleSameType()) {
+            return OracleDdlGenerator.newInstance();
+        }
+        else if (DbType.SQLITE == dbType){
+            return SQLiteDdlGenerator.newInstance();
+        }
+        // postgresql same type
+        else if (dbType.postgresqlSameType()) {
+            return PostgreDdlGenerator.newInstance();
+        }
+        throw new RuntimeException("Unsupported database type: " + jdbcUrl);
     }
 
     public static String getDatabase(String jdbcUrl) {

+ 21 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/ddl/history/SQLiteDdlGenerator.java

@@ -1,10 +1,30 @@
+/*
+ * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package com.baomidou.mybatisplus.extension.ddl.history;
 
 import org.springframework.stereotype.Component;
 
 import java.util.function.Function;
 
-
+/**
+ * SQLite DDL 生成器
+ *
+ * @author 呆猫
+ * @since 2024-04-01
+ */
 @Component
 public class SQLiteDdlGenerator implements IDdlGenerator {
 

+ 3 - 29
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/DialectFactory.java

@@ -38,41 +38,15 @@ public class DialectFactory {
                 throw ExceptionUtils.mpe("%s database not supported.", dbType.getDb());
             }
             // mysql same type
-            else if (dbType == DbType.MYSQL
-                || dbType == DbType.MARIADB
-                || dbType == DbType.GBASE
-                || dbType == DbType.OSCAR
-                || dbType == DbType.XU_GU
-                || dbType == DbType.CLICK_HOUSE
-                || dbType == DbType.OCEAN_BASE
-                || dbType == DbType.CUBRID
-                || dbType == DbType.SUNDB) {
+            else if (dbType.mysqlSameType()) {
                 dialect = new MySqlDialect();
             }
             // oracle same type
-            else if (dbType == DbType.ORACLE
-                || dbType == DbType.DM
-                || dbType == DbType.GAUSS) {
+            else if (dbType.oracleSameType()) {
                 dialect = new OracleDialect();
             }
             // postgresql same type
-            else if (dbType == DbType.POSTGRE_SQL
-                || dbType == DbType.H2
-                || dbType == DbType.LEALONE
-                || dbType == DbType.SQLITE
-                || dbType == DbType.HSQL
-                || dbType == DbType.KINGBASE_ES
-                || dbType == DbType.PHOENIX
-                || dbType == DbType.SAP_HANA
-                || dbType == DbType.IMPALA
-                || dbType == DbType.HIGH_GO
-                || dbType == DbType.VERTICA
-                || dbType == DbType.REDSHIFT
-                || dbType == DbType.OPENGAUSS
-                || dbType == DbType.TDENGINE
-                || dbType == DbType.UXDB
-                || dbType == DbType.GBASE8S_PG
-                || dbType == DbType.GBASE_8C) {
+            else if (dbType.postgresqlSameType()) {
                 dialect = new PostgreDialect();
             }
             // other types