Commit 2bd23134347b9e311f600316063413c10925b000

Authored by YunaiV
1 parent 6f18adb5

适配 Oracle 数据库

1. 适配表名是大写的情况
2. 优化代码生成器的校验逻辑
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ErrorCodeConstants.java
... ... @@ -40,6 +40,8 @@ public interface ErrorCodeConstants {
40 40 ErrorCode CODEGEN_COLUMN_NOT_EXISTS = new ErrorCode(1003001005, "字段义不存在");
41 41 ErrorCode CODEGEN_SYNC_COLUMNS_NULL = new ErrorCode(1003001006, "同步的字段不存在");
42 42 ErrorCode CODEGEN_SYNC_NONE_CHANGE = new ErrorCode(1003001007, "同步失败,不存在改变");
  43 + ErrorCode CODEGEN_TABLE_INFO_TABLE_COMMENT_IS_NULL = new ErrorCode(1003001008, "数据库的表注释未填写");
  44 + ErrorCode CODEGEN_TABLE_INFO_COLUMN_COMMENT_IS_NULL = new ErrorCode(1003001009, "数据库的表字段({})注释未填写");
43 45  
44 46 // ========== 字典类型(测试)1001005000 ==========
45 47 ErrorCode TEST_DEMO_NOT_EXISTS = new ErrorCode(1001005000, "测试示例不存在");
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java
1 1 package cn.iocoder.yudao.module.infra.service.codegen;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.util.StrUtil;
4 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 6 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
6 7 import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.CodegenCreateListReqVO;
... ... @@ -74,12 +75,7 @@ public class CodegenServiceImpl implements CodegenService {
74 75  
75 76 private Long createCodegen0(Long userId, Long dataSourceConfigId, TableInfo tableInfo) {
76 77 // 校验导入的表和字段非空
77   - if (tableInfo == null) {
78   - throw exception(CODEGEN_IMPORT_TABLE_NULL);
79   - }
80   - if (CollUtil.isEmpty(tableInfo.getFields())) {
81   - throw exception(CODEGEN_IMPORT_COLUMNS_NULL);
82   - }
  78 + checkTableInfo(tableInfo);
83 79 // 校验是否已经存在
84 80 if (codegenTableMapper.selectByTableNameAndDataSourceConfigId(tableInfo.getName(),
85 81 dataSourceConfigId) != null) {
... ... @@ -92,12 +88,34 @@ public class CodegenServiceImpl implements CodegenService {
92 88 table.setScene(CodegenSceneEnum.ADMIN.getScene()); // 默认配置下,使用管理后台的模板
93 89 table.setAuthor(userApi.getUser(userId).getNickname());
94 90 codegenTableMapper.insert(table);
  91 +
95 92 // 构建 CodegenColumnDO 数组,插入到 DB 中
96 93 List<CodegenColumnDO> columns = codegenBuilder.buildColumns(table.getId(), tableInfo.getFields());
  94 + // 如果没有主键,则使用第一个字段作为主键
  95 + if (!tableInfo.isHavePrimaryKey()) {
  96 + columns.get(0).setPrimaryKey(true);
  97 + }
97 98 codegenColumnMapper.insertBatch(columns);
98 99 return table.getId();
99 100 }
100 101  
  102 + private void checkTableInfo(TableInfo tableInfo) {
  103 + if (tableInfo == null) {
  104 + throw exception(CODEGEN_IMPORT_TABLE_NULL);
  105 + }
  106 + if (StrUtil.isEmpty(tableInfo.getComment())) {
  107 + throw exception(CODEGEN_TABLE_INFO_TABLE_COMMENT_IS_NULL);
  108 + }
  109 + if (CollUtil.isEmpty(tableInfo.getFields())) {
  110 + throw exception(CODEGEN_IMPORT_COLUMNS_NULL);
  111 + }
  112 + tableInfo.getFields().forEach(field -> {
  113 + if (StrUtil.isEmpty(field.getComment())) {
  114 + throw exception(CODEGEN_TABLE_INFO_COLUMN_COMMENT_IS_NULL, field.getName());
  115 + }
  116 + });
  117 + }
  118 +
101 119 @Override
102 120 @Transactional(rollbackFor = Exception.class)
103 121 public void updateCodegen(CodegenUpdateReqVO updateReqVO) {
... ... @@ -129,12 +147,9 @@ public class CodegenServiceImpl implements CodegenService {
129 147 }
130 148  
131 149 private void syncCodegen0(Long tableId, TableInfo tableInfo) {
132   - // 校验导入的字段不为空
  150 + // 校验导入的表和字段非空
  151 + checkTableInfo(tableInfo);
133 152 List<TableField> tableFields = tableInfo.getFields();
134   - if (CollUtil.isEmpty(tableFields)) {
135   - throw exception(CODEGEN_SYNC_COLUMNS_NULL);
136   - }
137   - Set<String> tableFieldNames = CollectionUtils.convertSet(tableFields, TableField::getName);
138 153  
139 154 // 构建 CodegenColumnDO 数组,只同步新增的字段
140 155 List<CodegenColumnDO> codegenColumns = codegenColumnMapper.selectListByTableId(tableId);
... ... @@ -142,6 +157,7 @@ public class CodegenServiceImpl implements CodegenService {
142 157 // 移除已经存在的字段
143 158 tableFields.removeIf(column -> codegenColumnNames.contains(column.getColumnName()));
144 159 // 计算需要删除的字段
  160 + Set<String> tableFieldNames = CollectionUtils.convertSet(tableFields, TableField::getName);
145 161 Set<Long> deleteColumnIds = codegenColumns.stream().filter(column -> !tableFieldNames.contains(column.getColumnName()))
146 162 .map(CodegenColumnDO::getId).collect(Collectors.toSet());
147 163 if (CollUtil.isEmpty(tableFields) && CollUtil.isEmpty(deleteColumnIds)) {
... ... @@ -206,9 +222,9 @@ public class CodegenServiceImpl implements CodegenService {
206 222 public List<DatabaseTableRespVO> getDatabaseTableList(Long dataSourceConfigId, String name, String comment) {
207 223 List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId, name, comment);
208 224 // 移除置顶前缀的表名 // TODO 未来做成可配置
209   - tables.removeIf(table -> table.getName().startsWith("QRTZ_"));
210   - tables.removeIf(table -> table.getName().startsWith("ACT_"));
211   - tables.removeIf(table -> table.getName().startsWith("FLW_"));
  225 + tables.removeIf(table -> table.getName().toUpperCase().startsWith("QRTZ_"));
  226 + tables.removeIf(table -> table.getName().toUpperCase().startsWith("ACT_"));
  227 + tables.removeIf(table -> table.getName().toUpperCase().startsWith("FLW_"));
212 228 // 移除已经生成的表
213 229 // 移除在 Codegen 中,已经存在的
214 230 Set<String> existsTables = CollectionUtils.convertSet(
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenBuilder.java
... ... @@ -105,15 +105,17 @@ public class CodegenBuilder {
105 105 * @param table 表定义
106 106 */
107 107 private void initTableDefault(CodegenTableDO table) {
108   - // 以 system_dept 举例子。moduleName 为 system、businessName 为 dept、className 为 SystemDept
109   - // 如果不希望 System 前缀,则可以手动在【代码生成 - 修改生成配置 - 基本信息】,将实体类名称改为 Dept 即可
110   - table.setModuleName(subBefore(table.getTableName(), '_', false)); // 第一个 _ 前缀的前面,作为 module 名字
111   - table.setBusinessName(toCamelCase(subAfter(table.getTableName(),
112   - '_', false))); // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰
113   - table.setClassName(upperFirst(toCamelCase( // 驼峰 + 首字母大写
114   - subAfter(table.getTableName(), '_', false)))); // 第一个 _ 前缀的前面,作为 class 名字
115   - table.setClassComment(subBefore(table.getTableComment(), // 去除结尾的表,作为类描述
116   - '表', true));
  108 + // 以 system_dept 举例子。moduleName 为 system、businessName 为 dept、className 为 Dept
  109 + // 如果希望以 System 前缀,则可以手动在【代码生成 - 修改生成配置 - 基本信息】,将实体类名称改为 SystemDept 即可
  110 + String tableName = table.getTableName().toLowerCase();
  111 + // 第一步,_ 前缀的前面,作为 module 名字;第二步,moduleName 必须小写;
  112 + table.setModuleName(subBefore(tableName, '_', false).toLowerCase());
  113 + // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰; 第三步,businessName 必须小写;
  114 + table.setBusinessName(toCamelCase(subAfter(tableName, '_', false)).toLowerCase());
  115 + // 驼峰 + 首字母大写;第一步,第一个 _ 前缀的后面,作为 class 名字;第二步,驼峰命名
  116 + table.setClassName(upperFirst(toCamelCase(subAfter(tableName, '_', false))));
  117 + // 去除结尾的表,作为类描述
  118 + table.setClassComment(StrUtil.removeSuffixIgnoreCase(table.getTableComment(), "表"));
117 119 table.setTemplateType(CodegenTemplateTypeEnum.CRUD.getType());
118 120 }
119 121  
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/dal/do.vm
... ... @@ -10,7 +10,8 @@ import ${BaseDOClassName};
10 10 *
11 11 * @author ${table.author}
12 12 */
13   -@TableName("${table.tableName}")
  13 +@TableName("${table.tableName.toLowerCase()}")
  14 +@KeySequence("${table.tableName.toLowerCase()}_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
14 15 @Data
15 16 @EqualsAndHashCode(callSuper = true)
16 17 @ToString(callSuper = true)
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/sql/h2.vm
1 1 -- 将该建表 SQL 语句,添加到 yudao-module-${table.moduleName}-biz 模块的 test/resources/sql/create_tables.sql 文件里
2   -CREATE TABLE IF NOT EXISTS "${table.tableName}" (
  2 +CREATE TABLE IF NOT EXISTS "${table.tableName.toLowerCase()}" (
3 3 #foreach ($column in $columns)
4 4 #if (${column.javaType} == 'Long')
5 5 #set ($dataType='bigint')
... ... @@ -24,11 +24,11 @@ CREATE TABLE IF NOT EXISTS &quot;${table.tableName}&quot; (
24 24 #elseif (${column.columnName} == 'deleted')
25 25 "deleted" bit NOT NULL DEFAULT FALSE,
26 26 #else
27   - "${column.columnName}" ${dataType}#if (${column.nullable} == false) NOT NULL#end,
  27 + "${column.columnName.toLowerCase()}" ${dataType}#if (${column.nullable} == false) NOT NULL#end,
28 28 #end
29 29 #end
30 30 #end
31   - PRIMARY KEY ("${primaryColumn.columnName}")
  31 + PRIMARY KEY ("${primaryColumn.columnName.toLowerCase()}")
32 32 ) COMMENT '${table.tableComment}';
33 33  
34 34 -- 将该删表 SQL 语句,添加到 yudao-module-${table.moduleName}-biz 模块的 test/resources/sql/clean.sql 文件里
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/sql/sql.vm
1 1 -- 菜单 SQL
2   -INSERT INTO `system_menu`(
3   - `name`, `permission`, `menu_type`, `sort`, `parent_id`,
4   - `path`, `icon`, `component`, `status`
  2 +INSERT INTO system_menu(
  3 + name, permission, type, sort, parent_id,
  4 + path, icon, component, status
5 5 )
6 6 VALUES (
7 7 '${table.classComment}管理', '', 2, 0, ${table.parentMenuId},
... ... @@ -16,9 +16,9 @@ SELECT @parentId := LAST_INSERT_ID();
16 16 #set ($functionOps = ['query', 'create', 'update', 'delete', 'export'])
17 17 #foreach ($functionName in $functionNames)
18 18 #set ($index = $foreach.count - 1)
19   -INSERT INTO `system_menu`(
20   - `name`, `permission`, `menu_type`, `sort`, `parent_id`,
21   - `path`, `icon`, `component`, `status`
  19 +INSERT INTO system_menu(
  20 + name, permission, type, sort, parent_id,
  21 + path, icon, component, status
22 22 )
23 23 VALUES (
24 24 '${table.classComment}${functionName}', '${permissionPrefix}:${functionOps.get($index)}', 3, $foreach.count, @parentId,
... ...