Commit f1069aa3062fcc580c715f4df729c58e5cd92a1f

Authored by YunaiV
1 parent 3e869a07

增加 PostgreSQL 的代码生成支持

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/db/DatabaseTablePostgreSQLDAOImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.infra.dal.mysql.db;
  2 +
  3 +import cn.hutool.core.util.StrUtil;
  4 +import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
  5 +import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseColumnDO;
  6 +import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO;
  7 +import com.baomidou.mybatisplus.annotation.DbType;
  8 +import org.springframework.stereotype.Repository;
  9 +
  10 +import java.sql.Connection;
  11 +import java.util.List;
  12 +
  13 +/**
  14 + * {@link DatabaseTableDAO} 的 PostgreSQL 实现类
  15 + *
  16 + * @author 芋道源码
  17 + */
  18 +@Repository
  19 +public class DatabaseTablePostgreSQLDAOImpl implements DatabaseTableDAO {
  20 +
  21 + @Override
  22 + public List<DatabaseTableDO> selectTableList(Connection connection, String tableNameLike, String tableCommentLike) {
  23 + // 拼接 SQL
  24 + String sql = "SELECT tbl.tablename, obj_description(c.oid)" +
  25 + " FROM pg_tables tbl, pg_class c" +
  26 + " WHERE tbl.schemaname = CURRENT_SCHEMA()" +
  27 + " AND tbl.tablename = c.relname";
  28 + if (StrUtil.isNotEmpty(tableNameLike)) {
  29 + sql += StrUtil.format(" AND tbl.tablename LIKE '%{}%'", tableNameLike);
  30 + }
  31 + if (StrUtil.isNotEmpty(tableCommentLike)) {
  32 + sql += StrUtil.format(" AND obj_description(c.oid) LIKE '%{}%'", tableCommentLike);
  33 + }
  34 + // 执行并返回结果
  35 + return JdbcUtils.query(connection, sql, (rs, rowNum) -> DatabaseTableDO.builder()
  36 + .tableName(rs.getString("tablename"))
  37 + .tableComment(rs.getString("obj_description"))
  38 + .build());
  39 + }
  40 +
  41 + @Override
  42 + public List<DatabaseColumnDO> selectColumnList(Connection connection, String tableName) {
  43 + // 拼接 SQL
  44 + String sql = "SELECT table_name, column_name, data_type, column_comment, ordinal_position," +
  45 + " (CASE WHEN is_nullable = 'yes' THEN '1' ELSE '0' END) AS nullable," +
  46 + " (CASE WHEN column_key = 'PRI' THEN '1' ELSE '0' END) AS primary_key," +
  47 + " (CASE WHEN extra = 'auto_increment' THEN '1' ELSE '0' END) AS auto_increment" +
  48 + " FROM information_schema.COLUMNS" +
  49 + " WHERE table_schema = (SELECT DATABASE())" +
  50 + String.format(" AND table_name = '%s'", tableName);
  51 + // 执行并返回结果
  52 + return JdbcUtils.query(connection, sql, (rs, rowNum) -> DatabaseColumnDO.builder()
  53 + .tableName(rs.getString("table_name"))
  54 + .columnName(rs.getString("column_name"))
  55 + .dataType(rs.getString("data_type"))
  56 + .columnComment(rs.getString("column_comment"))
  57 + .nullable(rs.getBoolean("nullable"))
  58 + .primaryKey(rs.getBoolean("primary_key"))
  59 + .autoIncrement(rs.getBoolean("auto_increment"))
  60 + .ordinalPosition(rs.getInt("ordinal_position"))
  61 + .build());
  62 + }
  63 +
  64 + @Override
  65 + public DbType getType() {
  66 + return DbType.MYSQL;
  67 + }
  68 +
  69 +}
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableService.java
... ... @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.infra.service.db;
2 2  
3 3 import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseColumnDO;
4 4 import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO;
  5 +import com.baomidou.mybatisplus.generator.config.po.TableInfo;
5 6  
6 7 import java.util.List;
7 8  
... ... @@ -40,4 +41,24 @@ public interface DatabaseTableService {
40 41 */
41 42 List<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName);
42 43  
  44 +
  45 + /**
  46 + * 获得表列表,基于表名称 + 表描述进行模糊匹配
  47 + *
  48 + * @param dataSourceConfigId 数据源配置的编号
  49 + * @param tableNameLike 表名称,模糊匹配
  50 + * @param tableCommentLike 表描述,模糊匹配
  51 + * @return 表列表
  52 + */
  53 + List<TableInfo> getTableList2(Long dataSourceConfigId, String tableNameLike, String tableCommentLike);
  54 +
  55 + /**
  56 + * 获得指定表名
  57 + *
  58 + * @param dataSourceConfigId 数据源配置的编号
  59 + * @param tableName 表名称
  60 + * @return 表
  61 + */
  62 + TableInfo getTable2(Long dataSourceConfigId, String tableName);
  63 +
43 64 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java
... ... @@ -8,11 +8,13 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseColumnDO;
8 8 import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO;
9 9 import cn.iocoder.yudao.module.infra.dal.mysql.db.DatabaseTableDAO;
10 10 import com.baomidou.mybatisplus.annotation.DbType;
  11 +import com.baomidou.mybatisplus.generator.config.po.TableInfo;
11 12 import lombok.SneakyThrows;
12 13 import org.springframework.stereotype.Service;
13 14  
14 15 import javax.annotation.Resource;
15 16 import java.sql.Connection;
  17 +import java.util.Comparator;
16 18 import java.util.List;
17 19  
18 20 /**
... ... @@ -49,10 +51,22 @@ public class DatabaseTableServiceImpl implements DatabaseTableService {
49 51 @SneakyThrows
50 52 public List<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName) {
51 53 try (Connection connection = getConnection(dataSourceConfigId)) {
52   - return getDatabaseTableDAO(dataSourceConfigId).selectColumnList(connection, tableName);
  54 + List<DatabaseColumnDO> columns = getDatabaseTableDAO(dataSourceConfigId).selectColumnList(connection, tableName);
  55 + columns.sort(Comparator.comparing(DatabaseColumnDO::getOrdinalPosition));
  56 + return columns;
53 57 }
54 58 }
55 59  
  60 + @Override
  61 + public List<TableInfo> getTableList2(Long dataSourceConfigId, String tableNameLike, String tableCommentLike) {
  62 + return null;
  63 + }
  64 +
  65 + @Override
  66 + public TableInfo getTable2(Long dataSourceConfigId, String tableName) {
  67 + return null;
  68 + }
  69 +
56 70 private Connection getConnection(Long dataSourceConfigId) {
57 71 DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
58 72 Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);
... ...