Browse Source

兼容 达梦数据库、kingbase数据库 不同不版本驱动可用

hubin 4 years ago
parent
commit
a759d309ee

+ 17 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/JdbcUtils.java

@@ -25,6 +25,7 @@ import org.apache.ibatis.logging.LogFactory;
 
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.regex.Pattern;
 
 /**
  * JDBC 工具类
@@ -80,11 +81,11 @@ public class JdbcUtils {
             return DbType.SQLITE;
         } else if (url.contains(":h2:")) {
             return DbType.H2;
-        } else if (url.contains(":dm:")) {
+        } else if (regexFind(":dm\\d*:", url)) {
             return DbType.DM;
         } else if (url.contains(":xugu:")) {
             return DbType.XU_GU;
-        } else if (url.contains(":kingbase:") || url.contains(":kingbase8:")) {
+        } else if (regexFind(":kingbase\\d*:", url)) {
             return DbType.KINGBASE_ES;
         } else if (url.contains(":phoenix:")) {
             return DbType.PHOENIX;
@@ -105,4 +106,18 @@ public class JdbcUtils {
             return DbType.OTHER;
         }
     }
+
+    /**
+     * 正则匹配
+     *
+     * @param regex 正则
+     * @param input 字符串
+     * @return 验证成功返回 true,验证失败返回 false
+     */
+    public static boolean regexFind(String regex, CharSequence input) {
+        if (null == input) {
+            return false;
+        }
+        return Pattern.compile(regex).matcher(input).find();
+    }
 }

+ 25 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/toolkit/JdbcUtilsTest.java

@@ -0,0 +1,25 @@
+package com.baomidou.mybatisplus.test.toolkit;
+
+import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Jdbc 工具类测试
+ *
+ * @author hubin
+ * @since 2020-09-15
+ */
+public class JdbcUtilsTest {
+
+    @Test
+    public void testPattern(){
+        String regex = ":dm\\d*:";
+        Assertions.assertTrue(JdbcUtils.regexFind(regex, ":dm:"));
+        Assertions.assertTrue(JdbcUtils.regexFind(regex, ":dm8:"));
+        Assertions.assertTrue(JdbcUtils.regexFind(regex, "123:dm6:abc"));
+        Assertions.assertTrue(JdbcUtils.regexFind(regex, ":dm7:abc"));
+        Assertions.assertTrue(JdbcUtils.regexFind(regex, "a12ds:dm71:"));
+        Assertions.assertFalse(JdbcUtils.regexFind(regex, "a12ds:dmc1:abc"));
+    }
+}