提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.db;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.lang.Assert;
5 import cn.hutool.core.util.StrUtil;
6 import com.iailab.framework.mybatis.core.util.JdbcUtils;
7 import com.iailab.module.infra.dal.dataobject.db.DataSourceConfigDO;
8 import com.baomidou.mybatisplus.annotation.DbType;
9 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
10 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
11 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
12 import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
13 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
14 import com.baomidou.mybatisplus.generator.config.rules.DateType;
15 import com.baomidou.mybatisplus.generator.query.SQLQuery;
16 import org.springframework.stereotype.Service;
17
18 import javax.annotation.Resource;
19 import java.util.Comparator;
20 import java.util.List;
21 import java.util.Objects;
22 import java.util.stream.Collectors;
23
24 /**
25  * 数据库表 Service 实现类
26  *
27  * @author iailab
28  */
29 @Service
30 public class DatabaseTableServiceImpl implements DatabaseTableService {
31
32     @Resource
33     private DataSourceConfigService dataSourceConfigService;
34
35     @Override
36     public List<TableInfo> getTableList(Long dataSourceConfigId, String nameLike, String commentLike) {
37         List<TableInfo> tables = getTableList0(dataSourceConfigId, null);
38         return tables.stream().filter(tableInfo -> (StrUtil.isEmpty(nameLike) || tableInfo.getName().contains(nameLike))
39                         && (StrUtil.isEmpty(commentLike) || tableInfo.getComment().contains(commentLike)))
40                 .collect(Collectors.toList());
41     }
42
43     @Override
44     public TableInfo getTable(Long dataSourceConfigId, String name) {
45         return CollUtil.getFirst(getTableList0(dataSourceConfigId, name));
46     }
47
48     private List<TableInfo> getTableList0(Long dataSourceConfigId, String name) {
49         // 获得数据源配置
50         DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
51         Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);
52         DbType dbType = JdbcUtils.getDbType(config.getUrl());
53
54         // 使用 MyBatis Plus Generator 解析表结构
55         DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(config.getUrl(), config.getUsername(),
56                 config.getPassword());
57         if (Objects.equals(dbType, DbType.SQL_SERVER)) { // 特殊:SQLServer jdbc 非标准,参见 https://github.com/baomidou/mybatis-plus/issues/5419
58             dataSourceConfigBuilder.databaseQueryClass(SQLQuery.class);
59         }
60         StrategyConfig.Builder strategyConfig = new StrategyConfig.Builder().enableSkipView(); // 忽略视图,业务上一般用不到
61         if (StrUtil.isNotEmpty(name)) {
62             strategyConfig.addInclude(name);
63         } else {
64             // 移除工作流和定时任务前缀的表名
65             strategyConfig.addExclude("ACT_[\\S\\s]+|QRTZ_[\\S\\s]+|FLW_[\\S\\s]+");
66             // 移除 ORACLE 相关的系统表
67             strategyConfig.addExclude("IMPDP_[\\S\\s]+|ALL_[\\S\\s]+|HS_[\\S\\\\s]+");
68             strategyConfig.addExclude("[\\S\\s]+\\$[\\S\\s]+|[\\S\\s]+\\$"); // 表里不能有 $,一般有都是系统的表
69         }
70
71         GlobalConfig globalConfig = new GlobalConfig.Builder().dateType(DateType.TIME_PACK).build(); // 只使用 LocalDateTime 类型,不使用 LocalDate
72         ConfigBuilder builder = new ConfigBuilder(null, dataSourceConfigBuilder.build(), strategyConfig.build(),
73                 null, globalConfig, null);
74         // 按照名字排序
75         List<TableInfo> tables = builder.getTableInfoList();
76         tables.sort(Comparator.comparing(TableInfo::getName));
77         return tables;
78     }
79
80 }