From f1069aa3062fcc580c715f4df729c58e5cd92a1f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 29 Apr 2022 19:39:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20PostgreSQL=20=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db/DatabaseTablePostgreSQLDAOImpl.java | 69 +++++++++++++++++++ .../service/db/DatabaseTableService.java | 21 ++++++ .../service/db/DatabaseTableServiceImpl.java | 16 ++++- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/db/DatabaseTablePostgreSQLDAOImpl.java diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/db/DatabaseTablePostgreSQLDAOImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/db/DatabaseTablePostgreSQLDAOImpl.java new file mode 100644 index 000000000..933680848 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/db/DatabaseTablePostgreSQLDAOImpl.java @@ -0,0 +1,69 @@ +package cn.iocoder.yudao.module.infra.dal.mysql.db; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils; +import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseColumnDO; +import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO; +import com.baomidou.mybatisplus.annotation.DbType; +import org.springframework.stereotype.Repository; + +import java.sql.Connection; +import java.util.List; + +/** + * {@link DatabaseTableDAO} 的 PostgreSQL 实现类 + * + * @author 芋道源码 + */ +@Repository +public class DatabaseTablePostgreSQLDAOImpl implements DatabaseTableDAO { + + @Override + public List selectTableList(Connection connection, String tableNameLike, String tableCommentLike) { + // 拼接 SQL + String sql = "SELECT tbl.tablename, obj_description(c.oid)" + + " FROM pg_tables tbl, pg_class c" + + " WHERE tbl.schemaname = CURRENT_SCHEMA()" + + " AND tbl.tablename = c.relname"; + if (StrUtil.isNotEmpty(tableNameLike)) { + sql += StrUtil.format(" AND tbl.tablename LIKE '%{}%'", tableNameLike); + } + if (StrUtil.isNotEmpty(tableCommentLike)) { + sql += StrUtil.format(" AND obj_description(c.oid) LIKE '%{}%'", tableCommentLike); + } + // 执行并返回结果 + return JdbcUtils.query(connection, sql, (rs, rowNum) -> DatabaseTableDO.builder() + .tableName(rs.getString("tablename")) + .tableComment(rs.getString("obj_description")) + .build()); + } + + @Override + public List selectColumnList(Connection connection, String tableName) { + // 拼接 SQL + String sql = "SELECT table_name, column_name, data_type, column_comment, ordinal_position," + + " (CASE WHEN is_nullable = 'yes' THEN '1' ELSE '0' END) AS nullable," + + " (CASE WHEN column_key = 'PRI' THEN '1' ELSE '0' END) AS primary_key," + + " (CASE WHEN extra = 'auto_increment' THEN '1' ELSE '0' END) AS auto_increment" + + " FROM information_schema.COLUMNS" + + " WHERE table_schema = (SELECT DATABASE())" + + String.format(" AND table_name = '%s'", tableName); + // 执行并返回结果 + return JdbcUtils.query(connection, sql, (rs, rowNum) -> DatabaseColumnDO.builder() + .tableName(rs.getString("table_name")) + .columnName(rs.getString("column_name")) + .dataType(rs.getString("data_type")) + .columnComment(rs.getString("column_comment")) + .nullable(rs.getBoolean("nullable")) + .primaryKey(rs.getBoolean("primary_key")) + .autoIncrement(rs.getBoolean("auto_increment")) + .ordinalPosition(rs.getInt("ordinal_position")) + .build()); + } + + @Override + public DbType getType() { + return DbType.MYSQL; + } + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableService.java index 426d88639..4dae6c539 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableService.java +++ b/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; import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseColumnDO; import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO; +import com.baomidou.mybatisplus.generator.config.po.TableInfo; import java.util.List; @@ -40,4 +41,24 @@ public interface DatabaseTableService { */ List getColumnList(Long dataSourceConfigId, String tableName); + + /** + * 获得表列表,基于表名称 + 表描述进行模糊匹配 + * + * @param dataSourceConfigId 数据源配置的编号 + * @param tableNameLike 表名称,模糊匹配 + * @param tableCommentLike 表描述,模糊匹配 + * @return 表列表 + */ + List getTableList2(Long dataSourceConfigId, String tableNameLike, String tableCommentLike); + + /** + * 获得指定表名 + * + * @param dataSourceConfigId 数据源配置的编号 + * @param tableName 表名称 + * @return 表 + */ + TableInfo getTable2(Long dataSourceConfigId, String tableName); + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java index 234e6d4a6..34589b3c6 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DatabaseTableServiceImpl.java +++ b/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; import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO; import cn.iocoder.yudao.module.infra.dal.mysql.db.DatabaseTableDAO; import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.generator.config.po.TableInfo; import lombok.SneakyThrows; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.sql.Connection; +import java.util.Comparator; import java.util.List; /** @@ -49,10 +51,22 @@ public class DatabaseTableServiceImpl implements DatabaseTableService { @SneakyThrows public List getColumnList(Long dataSourceConfigId, String tableName) { try (Connection connection = getConnection(dataSourceConfigId)) { - return getDatabaseTableDAO(dataSourceConfigId).selectColumnList(connection, tableName); + List columns = getDatabaseTableDAO(dataSourceConfigId).selectColumnList(connection, tableName); + columns.sort(Comparator.comparing(DatabaseColumnDO::getOrdinalPosition)); + return columns; } } + @Override + public List getTableList2(Long dataSourceConfigId, String tableNameLike, String tableCommentLike) { + return null; + } + + @Override + public TableInfo getTable2(Long dataSourceConfigId, String tableName) { + return null; + } + private Connection getConnection(Long dataSourceConfigId) { DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId); Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);