DbBase.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package jnpf.database.source;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
  4. import jnpf.database.constant.DbConst;
  5. import jnpf.database.datatype.db.interfaces.DtInterface;
  6. import jnpf.database.datatype.model.DtModelDTO;
  7. import jnpf.database.enums.DbAliasEnum;
  8. import jnpf.database.model.dbfield.DbFieldModel;
  9. import jnpf.database.model.entity.DbLinkEntity;
  10. import jnpf.database.model.interfaces.DbSourceOrDbLink;
  11. import jnpf.database.source.impl.*;
  12. import jnpf.database.sql.model.DbStruct;
  13. import jnpf.database.util.DbTypeUtil;
  14. import jnpf.exception.DataException;
  15. import jnpf.util.StringUtil;
  16. import jnpf.util.TenantHolder;
  17. import lombok.Data;
  18. import java.sql.ResultSet;
  19. import java.util.Collections;
  20. import java.util.List;
  21. /**
  22. * 数据库基础模型表
  23. *
  24. * @author JNPF开发平台组 YanYu
  25. * @version V3.2.0
  26. * @copyright 引迈信息技术有限公司
  27. * @date 2021/6/21
  28. */
  29. @Data
  30. public abstract class DbBase {
  31. /**
  32. * JNPF数据库编码标准
  33. */
  34. public static final String MYSQL = "MySQL";
  35. public static final String DM = "DM";
  36. public static final String KINGBASE_ES = "KingbaseES";
  37. public static final String ORACLE = "Oracle";
  38. public static final String POSTGRE_SQL = "PostgreSQL";
  39. public static final String SQL_SERVER = "SQLServer";
  40. public static final String DORIS = "Doris";
  41. public static final DbBase[] DB_BASES = {new DbMySQL(), new DbSQLServer(), new DbDM(),
  42. new DbOracle(), new DbKingbase(), new DbPostgre(), new DbDoris()};
  43. public static final String[] DB_ENCODES = {MYSQL, ORACLE, SQL_SERVER, DM, KINGBASE_ES, POSTGRE_SQL, DORIS};
  44. /**
  45. * JNPF数据库编码
  46. */
  47. protected String jnpfDbEncode;
  48. /**
  49. * MybatisPlus数据库编码
  50. */
  51. protected DbType mpDbType;
  52. /**
  53. * DruidDbType
  54. */
  55. protected com.alibaba.druid.DbType druidDbType;
  56. /**
  57. * url里数据库标识
  58. */
  59. protected String connUrlEncode;
  60. /**
  61. * 数据库驱动
  62. */
  63. protected String driver;
  64. /**
  65. * 默认端口
  66. */
  67. protected String defaultPort;
  68. /**
  69. * 管理员用户名
  70. */
  71. protected String dbaUsername;
  72. /**
  73. * 默认预备url
  74. */
  75. protected String defaultPrepareUrl;
  76. /**
  77. * oracle连接扩展参数
  78. */
  79. public String oracleParam;
  80. /**
  81. * 无参构造
  82. */
  83. protected DbBase() {
  84. init();
  85. }
  86. /**
  87. * 初始赋值
  88. */
  89. protected abstract void init();
  90. /**
  91. * 数据库对象初始化
  92. * 指定子类被创建时,需要提供的参数
  93. */
  94. protected void setInstance(String jnpfDbEncode, DbType mpDbType, com.alibaba.druid.DbType druidDbType, String defaultPort, String dbaUsername, String connUrlEncode,
  95. String driver, String defaultPrepareUrl) {
  96. // 绑定:JNPF数据库编码
  97. this.jnpfDbEncode = jnpfDbEncode;
  98. // 绑定:MybatisPlus数据库编码
  99. this.mpDbType = mpDbType;
  100. this.druidDbType = druidDbType;
  101. // 绑定:Url数据库标识
  102. this.connUrlEncode = connUrlEncode;
  103. this.driver = driver;
  104. this.defaultPrepareUrl = defaultPrepareUrl;
  105. // 默认端口
  106. this.defaultPort = defaultPort;
  107. this.dbaUsername = dbaUsername;
  108. }
  109. public static List<String> dynamicAllTableName = Collections.emptyList();
  110. public DbBase[] DB_BASES(){
  111. return null;
  112. }
  113. /**
  114. * 获取最终动态表名, 处理是否动态表名
  115. * @return
  116. */
  117. public TableNameHandler getDynamicTableNameHandler(){
  118. return (sql, tableName) -> {
  119. //是否指定数据源, 且在初始库中包含的表
  120. if (StringUtil.isNotEmpty(TenantHolder.getDatasourceName())) {
  121. return getDynamicTableName(tableName);
  122. }
  123. return tableName;
  124. };
  125. }
  126. /**
  127. * 获取动态组合表名
  128. * @param tableName
  129. * @return
  130. */
  131. protected String getDynamicTableName(String tableName){
  132. return TenantHolder.getDatasourceName() + "." + tableName;
  133. }
  134. /**
  135. * 不同库间设置字段信息
  136. *
  137. * @param model 字段模型
  138. * @param result 结果集
  139. * @return 表字段模型
  140. * @throws DataException ignore
  141. */
  142. public void setPartFieldModel(DbFieldModel model, ResultSet result) throws Exception{
  143. model.setDtModelDTO(new DtModelDTO(
  144. DtInterface.newInstanceByDt(model.getDataType(), this.getJnpfDbEncode()),
  145. result.getLong(DbAliasEnum.CHAR_LENGTH.getAlias(jnpfDbEncode)),
  146. result.getInt(DbAliasEnum.NUM_PRECISION.getAlias(jnpfDbEncode)),
  147. result.getInt(DbAliasEnum.NUM_SCALE.getAlias(jnpfDbEncode))
  148. ));
  149. }
  150. /**
  151. * 获取数据库连接Url 关键参数:
  152. * 1、地址
  153. * 2、端口
  154. * 3、数据库名
  155. * 4、模式 (参数:?currentSchema = schema)
  156. * 5、jdbc-url自定义参数
  157. *
  158. * 此方法对DbTypeUtil与内部开放,对外关闭。外部调用url,用DbTypeUtil.getUrl()方法
  159. * @return String 连接
  160. */
  161. protected String getConnUrl(String prepareUrl, String host, Integer port, DbStruct struct){
  162. // 配置文件是否存在自定义数据连接url
  163. prepareUrl = StringUtil.isNotEmpty(prepareUrl) ? prepareUrl : defaultPrepareUrl;
  164. // 当地址为空,用本地回环地址
  165. prepareUrl = prepareUrl.replace(DbConst.HOST, StringUtil.isNotEmpty(host) ? host : "127.0.0.1");
  166. // 当端口为空,用数据库一般默认端口
  167. prepareUrl = prepareUrl.replace(DbConst.PORT, port != null ? port.toString() : defaultPort);
  168. return prepareUrl;
  169. }
  170. /*
  171. * 提供内部封装方法所独有的方法调用
  172. * 保持全局只有一处显性getUrl,getConn的方法(即在ConnUtil里)======================================
  173. */
  174. public static class BaseCommon{
  175. public static String getDbBaseConnUrl(DbSourceOrDbLink dbSourceOrDbLink, String dbName){
  176. DbLinkEntity dsd = dbSourceOrDbLink.init(dbName);
  177. try {
  178. return DbTypeUtil.getDb(dbSourceOrDbLink).getConnUrl(dsd.getPrepareUrl(), dsd.getHost(), dsd.getPort(), dsd.getDbStruct());
  179. } catch (DataException e) {
  180. e.printStackTrace();
  181. }
  182. return "";
  183. }
  184. }
  185. }