JdbcUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2011-2024, baomidou (jobob@qq.com).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus.extension.toolkit;
  17. import com.baomidou.mybatisplus.annotation.DbType;
  18. import com.baomidou.mybatisplus.core.toolkit.Assert;
  19. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  20. import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
  21. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  22. import org.apache.ibatis.executor.Executor;
  23. import org.apache.ibatis.logging.Log;
  24. import org.apache.ibatis.logging.LogFactory;
  25. import java.sql.Connection;
  26. import java.sql.SQLException;
  27. import java.util.Map;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.regex.Pattern;
  30. /**
  31. * JDBC 工具类
  32. *
  33. * @author nieqiurong
  34. * @since 2016-12-05
  35. */
  36. public class JdbcUtils {
  37. private static final Log logger = LogFactory.getLog(JdbcUtils.class);
  38. private static final Map<String, DbType> JDBC_DB_TYPE_CACHE = new ConcurrentHashMap<>();
  39. /**
  40. * 不关闭 Connection,因为是从事务里获取的,sqlSession会负责关闭
  41. *
  42. * @param executor Executor
  43. * @return DbType
  44. */
  45. public static DbType getDbType(Executor executor) {
  46. try {
  47. Connection conn = executor.getTransaction().getConnection();
  48. return CollectionUtils.computeIfAbsent(JDBC_DB_TYPE_CACHE, conn.getMetaData().getURL(), JdbcUtils::getDbType);
  49. } catch (SQLException e) {
  50. throw ExceptionUtils.mpe(e);
  51. }
  52. }
  53. /**
  54. * 根据连接地址判断数据库类型
  55. *
  56. * @param jdbcUrl 连接地址
  57. * @return ignore
  58. */
  59. public static DbType getDbType(String jdbcUrl) {
  60. Assert.isFalse(StringUtils.isBlank(jdbcUrl), "Error: The jdbcUrl is Null, Cannot read database type");
  61. String url = jdbcUrl.toLowerCase();
  62. if (url.contains(":mysql:") || url.contains(":cobar:")) {
  63. return DbType.MYSQL;
  64. } else if (url.contains(":mariadb:")) {
  65. return DbType.MARIADB;
  66. } else if (url.contains(":oracle:")) {
  67. return DbType.ORACLE;
  68. } else if (url.contains(":sqlserver:") || url.contains(":microsoft:")) {
  69. return DbType.SQL_SERVER;
  70. } else if (url.contains(":postgresql:")) {
  71. return DbType.POSTGRE_SQL;
  72. } else if (url.contains(":hsqldb:")) {
  73. return DbType.HSQL;
  74. } else if (url.contains(":db2:")) {
  75. return DbType.DB2;
  76. } else if (url.contains(":sqlite:")) {
  77. return DbType.SQLITE;
  78. } else if (url.contains(":h2:")) {
  79. return DbType.H2;
  80. } else if (url.contains(":lealone:")) {
  81. return DbType.LEALONE;
  82. } else if (regexFind(":dm\\d*:", url)) {
  83. return DbType.DM;
  84. } else if (url.contains(":xugu:")) {
  85. return DbType.XU_GU;
  86. } else if (regexFind(":kingbase\\d*:", url)) {
  87. return DbType.KINGBASE_ES;
  88. } else if (url.contains(":phoenix:")) {
  89. return DbType.PHOENIX;
  90. } else if (url.contains(":zenith:")) {
  91. return DbType.GAUSS;
  92. } else if (url.contains(":gbase:")) {
  93. return DbType.GBASE;
  94. } else if (url.contains(":gbasedbt-sqli:") || url.contains(":informix-sqli:")) {
  95. return DbType.GBASE_8S;
  96. } else if (url.contains(":gbase8s-pg:")){
  97. return DbType.GBASE8S_PG;
  98. } else if (url.contains(":gbase8c:")) {
  99. return DbType.GBASE_8C;
  100. } else if (url.contains(":ch:") || url.contains(":clickhouse:")) {
  101. return DbType.CLICK_HOUSE;
  102. } else if (url.contains(":oscar:")) {
  103. return DbType.OSCAR;
  104. } else if (url.contains(":sybase:")) {
  105. return DbType.SYBASE;
  106. } else if (url.contains(":oceanbase:")) {
  107. return DbType.OCEAN_BASE;
  108. } else if (url.contains(":highgo:")) {
  109. return DbType.HIGH_GO;
  110. } else if (url.contains(":cubrid:")) {
  111. return DbType.CUBRID;
  112. } else if (url.contains(":sundb:")) {
  113. return DbType.SUNDB;
  114. } else if (url.contains(":sap:")) {
  115. return DbType.SAP_HANA;
  116. } else if (url.contains(":impala:")) {
  117. return DbType.IMPALA;
  118. } else if (url.contains(":vertica:")) {
  119. return DbType.VERTICA;
  120. } else if (url.contains(":xcloud:")) {
  121. return DbType.XCloud;
  122. } else if (url.contains(":firebirdsql:")) {
  123. return DbType.FIREBIRD;
  124. } else if (url.contains(":redshift:")) {
  125. return DbType.REDSHIFT;
  126. } else if (url.contains(":opengauss:")) {
  127. return DbType.OPENGAUSS;
  128. } else if (url.contains(":taos:") || url.contains(":taos-rs:")) {
  129. return DbType.TDENGINE;
  130. } else if (url.contains(":informix")) {
  131. return DbType.INFORMIX;
  132. } else if (url.contains(":sinodb")) {
  133. return DbType.SINODB;
  134. } else if (url.contains(":uxdb:")) {
  135. return DbType.UXDB;
  136. } else if (url.contains(":trino:")) {
  137. return DbType.TRINO;
  138. } else if (url.contains(":presto:")) {
  139. return DbType.PRESTO;
  140. } else if (url.contains(":derby:")) {
  141. return DbType.DERBY;
  142. } else if (url.contains(":vastbase:")) {
  143. return DbType.VASTBASE;
  144. } else if (url.contains(":goldendb:")) {
  145. return DbType.GOLDENDB;
  146. } else if (url.contains(":duckdb:")){
  147. return DbType.DUCKDB;
  148. } else {
  149. logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
  150. return DbType.OTHER;
  151. }
  152. }
  153. /**
  154. * 正则匹配
  155. *
  156. * @param regex 正则
  157. * @param input 字符串
  158. * @return 验证成功返回 true,验证失败返回 false
  159. */
  160. public static boolean regexFind(String regex, CharSequence input) {
  161. if (null == input) {
  162. return false;
  163. }
  164. return Pattern.compile(regex).matcher(input).find();
  165. }
  166. }