mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	增加 PostgreSQL 的代码生成支持
This commit is contained in:
		| @@ -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<DatabaseTableDO> 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<DatabaseColumnDO> 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; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -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.DatabaseColumnDO; | ||||||
| import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO; | import cn.iocoder.yudao.module.infra.dal.dataobject.db.DatabaseTableDO; | ||||||
|  | import com.baomidou.mybatisplus.generator.config.po.TableInfo; | ||||||
|  |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| @@ -40,4 +41,24 @@ public interface DatabaseTableService { | |||||||
|      */ |      */ | ||||||
|     List<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName); |     List<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 获得表列表,基于表名称 + 表描述进行模糊匹配 | ||||||
|  |      * | ||||||
|  |      * @param dataSourceConfigId 数据源配置的编号 | ||||||
|  |      * @param tableNameLike 表名称,模糊匹配 | ||||||
|  |      * @param tableCommentLike 表描述,模糊匹配 | ||||||
|  |      * @return 表列表 | ||||||
|  |      */ | ||||||
|  |     List<TableInfo> getTableList2(Long dataSourceConfigId, String tableNameLike, String tableCommentLike); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 获得指定表名 | ||||||
|  |      * | ||||||
|  |      * @param dataSourceConfigId 数据源配置的编号 | ||||||
|  |      * @param tableName 表名称 | ||||||
|  |      * @return 表 | ||||||
|  |      */ | ||||||
|  |     TableInfo getTable2(Long dataSourceConfigId, String tableName); | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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.dataobject.db.DatabaseTableDO; | ||||||
| import cn.iocoder.yudao.module.infra.dal.mysql.db.DatabaseTableDAO; | import cn.iocoder.yudao.module.infra.dal.mysql.db.DatabaseTableDAO; | ||||||
| import com.baomidou.mybatisplus.annotation.DbType; | import com.baomidou.mybatisplus.annotation.DbType; | ||||||
|  | import com.baomidou.mybatisplus.generator.config.po.TableInfo; | ||||||
| import lombok.SneakyThrows; | import lombok.SneakyThrows; | ||||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||||
|  |  | ||||||
| import javax.annotation.Resource; | import javax.annotation.Resource; | ||||||
| import java.sql.Connection; | import java.sql.Connection; | ||||||
|  | import java.util.Comparator; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -49,10 +51,22 @@ public class DatabaseTableServiceImpl implements DatabaseTableService { | |||||||
|     @SneakyThrows |     @SneakyThrows | ||||||
|     public List<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName) { |     public List<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName) { | ||||||
|         try (Connection connection = getConnection(dataSourceConfigId)) { |         try (Connection connection = getConnection(dataSourceConfigId)) { | ||||||
|             return getDatabaseTableDAO(dataSourceConfigId).selectColumnList(connection, tableName); |             List<DatabaseColumnDO> columns = getDatabaseTableDAO(dataSourceConfigId).selectColumnList(connection, tableName); | ||||||
|  |             columns.sort(Comparator.comparing(DatabaseColumnDO::getOrdinalPosition)); | ||||||
|  |             return columns; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public List<TableInfo> getTableList2(Long dataSourceConfigId, String tableNameLike, String tableCommentLike) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public TableInfo getTable2(Long dataSourceConfigId, String tableName) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private Connection getConnection(Long dataSourceConfigId) { |     private Connection getConnection(Long dataSourceConfigId) { | ||||||
|         DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId); |         DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId); | ||||||
|         Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId); |         Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV