增加 PostgreSQL 的代码生成支持

pull/2/head
YunaiV 2022-04-29 19:39:56 +08:00
parent 3e869a07fd
commit f1069aa306
3 changed files with 105 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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<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);
}

View File

@ -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<DatabaseColumnDO> getColumnList(Long dataSourceConfigId, String tableName) {
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) {
DataSourceConfigDO config = dataSourceConfigService.getDataSourceConfig(dataSourceConfigId);
Assert.notNull(config, "数据源({}) 不存在!", dataSourceConfigId);