JdbcUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2011-2021, 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.ExceptionUtils;
  20. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  21. import org.apache.ibatis.executor.Executor;
  22. import org.apache.ibatis.logging.Log;
  23. import org.apache.ibatis.logging.LogFactory;
  24. import java.sql.Connection;
  25. import java.sql.SQLException;
  26. import java.util.regex.Pattern;
  27. /**
  28. * JDBC 工具类
  29. *
  30. * @author nieqiurong
  31. * @since 2016-12-05
  32. */
  33. public class JdbcUtils {
  34. private static final Log logger = LogFactory.getLog(JdbcUtils.class);
  35. /**
  36. * 不关闭 Connection,因为是从事务里获取的,sqlSession会负责关闭
  37. *
  38. * @param executor Executor
  39. * @return DbType
  40. */
  41. public static DbType getDbType(Executor executor) {
  42. try {
  43. Connection conn = executor.getTransaction().getConnection();
  44. return getDbType(conn.getMetaData().getURL());
  45. } catch (SQLException e) {
  46. throw ExceptionUtils.mpe(e);
  47. }
  48. }
  49. /**
  50. * 根据连接地址判断数据库类型
  51. *
  52. * @param jdbcUrl 连接地址
  53. * @return ignore
  54. */
  55. public static DbType getDbType(String jdbcUrl) {
  56. Assert.isFalse(StringUtils.isBlank(jdbcUrl), "Error: The jdbcUrl is Null, Cannot read database type");
  57. String url = jdbcUrl.toLowerCase();
  58. if (url.contains(":mysql:") || url.contains(":cobar:")) {
  59. return DbType.MYSQL;
  60. } else if (url.contains(":mariadb:")) {
  61. return DbType.MARIADB;
  62. } else if (url.contains(":oracle:")) {
  63. return DbType.ORACLE;
  64. } else if (url.contains(":sqlserver:") || url.contains(":microsoft:")) {
  65. return DbType.SQL_SERVER2005;
  66. } else if (url.contains(":sqlserver2012:")) {
  67. return DbType.SQL_SERVER;
  68. } else if (url.contains(":postgresql:")) {
  69. return DbType.POSTGRE_SQL;
  70. } else if (url.contains(":hsqldb:")) {
  71. return DbType.HSQL;
  72. } else if (url.contains(":db2:")) {
  73. return DbType.DB2;
  74. } else if (url.contains(":sqlite:")) {
  75. return DbType.SQLITE;
  76. } else if (url.contains(":h2:")) {
  77. return DbType.H2;
  78. } else if (regexFind(":dm\\d*:", url)) {
  79. return DbType.DM;
  80. } else if (url.contains(":xugu:")) {
  81. return DbType.XU_GU;
  82. } else if (regexFind(":kingbase\\d*:", url)) {
  83. return DbType.KINGBASE_ES;
  84. } else if (url.contains(":phoenix:")) {
  85. return DbType.PHOENIX;
  86. } else if (jdbcUrl.contains(":zenith:")) {
  87. return DbType.GAUSS;
  88. } else if (jdbcUrl.contains(":gbase:")) {
  89. return DbType.GBASE;
  90. } else if (jdbcUrl.contains(":gbasedbt-sqli:")) {
  91. return DbType.GBASEDBT;
  92. } else if (jdbcUrl.contains(":clickhouse:")) {
  93. return DbType.CLICK_HOUSE;
  94. } else if (jdbcUrl.contains(":oscar:")) {
  95. return DbType.OSCAR;
  96. } else if (jdbcUrl.contains(":sybase:")) {
  97. return DbType.SYBASE;
  98. } else if (jdbcUrl.contains(":oceanbase:")) {
  99. return DbType.OCEAN_BASE;
  100. } else if (url.contains(":highgo:")) {
  101. return DbType.HIGH_GO;
  102. } else if (url.contains(":cubrid:")) {
  103. return DbType.CUBRID;
  104. } else if (url.contains(":goldilocks:")) {
  105. return DbType.GOLDILOCKS;
  106. } else if (url.contains(":csiidb:")) {
  107. return DbType.CSIIDB;
  108. } else if (url.contains(":sap:")) {
  109. return DbType.SAP_HANA;
  110. }else {
  111. logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
  112. return DbType.OTHER;
  113. }
  114. }
  115. /**
  116. * 正则匹配
  117. *
  118. * @param regex 正则
  119. * @param input 字符串
  120. * @return 验证成功返回 true,验证失败返回 false
  121. */
  122. public static boolean regexFind(String regex, CharSequence input) {
  123. if (null == input) {
  124. return false;
  125. }
  126. return Pattern.compile(regex).matcher(input).find();
  127. }
  128. }