fix: 代码生成器 未完成的解析器

This commit is contained in:
cuijiawang 2025-09-29 14:44:18 +08:00
parent a4d6306259
commit 5e47425ab5
9 changed files with 336 additions and 28 deletions

View File

@ -66,7 +66,9 @@ public class GeneratorController {
// 检查是否有错误
if (result.containsKey("error")) {
return R.fail(result.get("error"));
String errorMsg = result.get("error");
log.warn("代码生成失败: {}", errorMsg);
return R.fail(errorMsg);
}
// 记录模板使用日志

View File

@ -62,6 +62,7 @@ public class GeneratorServiceImpl implements IGeneratorService {
// 1. 解析SQL获取表结构信息
String tableSql = MapUtil.getString(options, "tableSql");
String ignorePrefix = MapUtil.getString(options, "ignorePrefix", "");
String dataType = MapUtil.getString(options, "dataType", "sql");
if (StringUtils.isBlank(tableSql)) {
log.error("SQL语句为空");
@ -69,13 +70,52 @@ public class GeneratorServiceImpl implements IGeneratorService {
return result;
}
ClassInfo classInfo = TableParseUtil.parseTableSql(tableSql, ignorePrefix);
if (classInfo == null) {
log.error("解析SQL失败");
result.put("error", "解析SQL失败");
// 根据解析引擎类型选择不同的解析方法
ClassInfo classInfo = null;
try {
switch (dataType) {
case "sql":
classInfo = TableParseUtil.parseTableSql(tableSql, ignorePrefix);
break;
case "select-sql":
log.warn("SELECT SQL解析引擎暂未实现使用DDL引擎降级处理");
result.put("error", "SELECT SQL解析引擎暂未实现请使用DDL SQL引擎或提供CREATE TABLE语句");
return result;
case "create-sql":
log.warn("CREATE SQL@JSqlParser引擎暂未实现使用DDL引擎降级处理");
result.put("error", "CREATE SQL@JSqlParser引擎暂未实现请使用DDL SQL@自研引擎");
return result;
case "json":
log.warn("JSON解析引擎暂未实现使用DDL引擎降级处理");
result.put("error", "JSON解析引擎暂未实现请使用DDL SQL引擎或提供CREATE TABLE语句");
return result;
case "insert-sql":
log.warn("INSERT SQL解析引擎暂未实现使用DDL引擎降级处理");
result.put("error", "INSERT SQL解析引擎暂未实现请使用DDL SQL引擎或提供CREATE TABLE语句");
return result;
default:
log.warn("未知的解析引擎类型: {}", dataType);
classInfo = TableParseUtil.parseTableSql(tableSql, ignorePrefix);
break;
}
} catch (Exception e) {
log.error("解析SQL失败引擎类型: {}", dataType, e);
result.put("error", "解析SQL失败: " + e.getMessage());
return result;
}
if (classInfo == null) {
log.error("解析SQL失败无法获取表结构信息");
result.put("error", "解析SQL失败请检查SQL语句格式是否正确");
return result;
}
// 确保classComment不为空提供默认值
if (StringUtils.isBlank(classInfo.getClassComment())) {
classInfo.setClassComment("数据表");
log.info("表注释为空,使用默认值: 数据表");
}
// 2. 设置生成参数
options.put("classInfo", classInfo);
options.put("tableName", classInfo.getTableName());
@ -199,16 +239,47 @@ public class GeneratorServiceImpl implements IGeneratorService {
// 1. 解析SQL获取表结构信息
String tableSql = MapUtil.getString(options, "tableSql");
String ignorePrefix = MapUtil.getString(options, "ignorePrefix", "");
String dataType = MapUtil.getString(options, "dataType", "sql");
if (StringUtils.isBlank(tableSql)) {
log.error("SQL语句为空");
return "// 错误: SQL语句不能为空";
}
ClassInfo classInfo = TableParseUtil.parseTableSql(tableSql, ignorePrefix);
// 根据解析引擎类型选择不同的解析方法
ClassInfo classInfo = null;
try {
switch (dataType) {
case "sql":
classInfo = TableParseUtil.parseTableSql(tableSql, ignorePrefix);
break;
case "select-sql":
return "// 错误: SELECT SQL解析引擎暂未实现请使用DDL SQL引擎或提供CREATE TABLE语句";
case "create-sql":
return "// 错误: CREATE SQL@JSqlParser引擎暂未实现请使用DDL SQL@自研引擎";
case "json":
return "// 错误: JSON解析引擎暂未实现请使用DDL SQL引擎或提供CREATE TABLE语句";
case "insert-sql":
return "// 错误: INSERT SQL解析引擎暂未实现请使用DDL SQL引擎或提供CREATE TABLE语句";
default:
log.warn("未知的解析引擎类型: {}, 使用默认DDL引擎", dataType);
classInfo = TableParseUtil.parseTableSql(tableSql, ignorePrefix);
break;
}
} catch (Exception e) {
log.error("解析SQL失败引擎类型: {}", dataType, e);
return "// 错误: 解析SQL失败: " + e.getMessage();
}
if (classInfo == null) {
log.error("解析SQL失败");
return "// 错误: 解析SQL失败";
log.error("解析SQL失败无法获取表结构信息");
return "// 错误: 解析SQL失败请检查SQL语句格式是否正确";
}
// 确保classComment不为空提供默认值
if (StringUtils.isBlank(classInfo.getClassComment())) {
classInfo.setClassComment("数据表");
log.info("表注释为空,使用默认值: 数据表");
}
// 2. 设置生成参数

View File

@ -26,17 +26,31 @@ public class TableParseUtil {
return null;
}
ClassInfo classInfo = new ClassInfo();
List<FieldInfo> fieldList = new ArrayList<>();
try {
ClassInfo classInfo = new ClassInfo();
List<FieldInfo> fieldList = new ArrayList<>();
// 解析表名和注释
parseTableInfo(sql, classInfo, ignorePrefix);
// 解析表名和注释
parseTableInfo(sql, classInfo, ignorePrefix);
// 解析字段信息
parseFieldInfo(sql, fieldList);
// 解析字段信息
parseFieldInfo(sql, fieldList);
classInfo.setFieldList(fieldList);
return classInfo;
classInfo.setFieldList(fieldList);
// 验证解析结果
if (StringUtils.isBlank(classInfo.getTableName())) {
throw new RuntimeException("无法解析表名请检查SQL语句格式");
}
if (fieldList.isEmpty()) {
throw new RuntimeException("无法解析字段信息请检查SQL语句格式");
}
return classInfo;
} catch (Exception e) {
throw new RuntimeException("解析DDL SQL失败: " + e.getMessage(), e);
}
}
/**

View File

@ -1,11 +1,31 @@
[
{
[{
"group": "ui",
"templates": [{
"id": "10",
"name": "swagger-ui",
"description": "swagger-ui"
}]
},
{
"id": "11",
"name": "element-ui",
"description": "element-ui"
},
{
"id": "12",
"name": "bootstrap-ui",
"description": "bootstrap-ui"
},
{
"id": "13",
"name": "layui-edit",
"description": "layui-edit"
},
{
"id": "14",
"name": "layui-list",
"description": "layui-list"
}
]
},
{
"group": "mybatis",
@ -46,6 +66,60 @@
}
]
},
{
"group": "jpa",
"templates": [{
"id": "30",
"name": "entity",
"description": "entity"
},
{
"id": "31",
"name": "repository",
"description": "repository"
},
{
"id": "32",
"name": "jpacontroller",
"description": "jpacontroller"
}
]
},
{
"group": "jdbc-template",
"templates": [{
"id": "40",
"name": "jtdao",
"description": "jtdao"
},
{
"id": "41",
"name": "jtdaoimpl",
"description": "jtdaoimpl"
}
]
},
{
"group": "beetlsql",
"templates": [{
"id": "50",
"name": "beetlmd",
"description": "beetlmd"
},
{
"id": "51",
"name": "beetlentity",
"description": "beetlentity"
},
{
"id": "52",
"name": "beetlcontroller",
"description": "beetlcontroller"
}
]
},
{
"group": "mybatis-plus",
@ -70,5 +144,152 @@
"description": "plusentity"
}
]
},
{
"group": "util",
"templates": [{
"id": "70",
"name": "beanutil",
"description": "beanutil"
},
{
"id": "71",
"name": "json",
"description": "json"
},
{
"id": "72",
"name": "xml",
"description": "xml"
},
{
"id": "73",
"name": "sql",
"description": "sql"
},
{
"id": "74",
"name": "swagger-yml",
"description": "swagger-yml"
}
]
},
{
"group": "common-mapper",
"templates": [{
"id": "81",
"name": "tkentity",
"description": "tkentity"
},
{
"id": "82",
"name": "tkmapper",
"description": "tkmapper"
}
]
},
{
"group": "renren-fast",
"templates": [{
"id": "91",
"name": "menu-sql",
"description": "menu-sql"
},
{
"id": "92",
"name": "vue-list",
"description": "vue-list"
},
{
"id": "93",
"name": "vue-edit",
"description": "vue-edit"
},
{
"id": "94",
"name": "rr-controller",
"description": "rr-controller"
},
{
"id": "95",
"name": "rr-dao",
"description": "rr-dao"
},
{
"id": "96",
"name": "rr-daoxml",
"description": "rr-daoxml"
},
{
"id": "97",
"name": "rr-service",
"description": "rr-service"
}
]
},
{
"group": "jpa-starp",
"templates": [{
"id": "101",
"name": "starp-entity",
"description": "entity"
},
{
"id": "102",
"name": "starp-repository",
"description": "repository"
},
{
"id": "103",
"name": "starp-jpa-controller",
"description": "jpacontroller"
}
]
},
{
"group": "bi",
"templates": [{
"id": "201",
"name": "qliksense",
"description": "qlik sense"
}]
},
{
"group": "cloud",
"templates": [
{
"id": "301",
"name": "bigquery",
"description": "GCP BigQuery"
},
{
"id": "302",
"name": "dataflowjjs",
"description": "GCP Dataflow JJS"
}
]
},
{
"group": "tk-mybatis",
"templates": [
{
"id": "401",
"name": "tk-entity",
"description": "tk-entity"
},
{
"id": "402",
"name": "tk-mapper",
"description": "tk-mapper"
},
{
"id": "403",
"name": "tk-controller",
"description": "tk-controller"
}
]
}
]

View File

@ -1,7 +1,7 @@
<el-form :inline="true" :model="submitData" class="demo-form-inline" :rules="rules" ref="ruleForm">
<el-card class="box-card">
<div slot="header" class="header clearfix">
<span>${classInfo.classComment}</span>
<span>${classInfo.classComment!'数据管理'}</span>
<el-button v-if="!ischeck && !isFind" class="fr" type="primary" @click="validate('ruleForm')">提交</el-button>
<el-button v-else class="fr" type="primary" @click="goBack">返回</el-button>
</div>

View File

@ -10,13 +10,13 @@
<form class="layui-form layui-form-pane" action="">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">${classInfo.classComment}Id</label>
<label class="layui-form-label">${classInfo.classComment!'数据'}Id</label>
<div class="layui-input-inline">
<input type="text" name="${classInfo.className?uncap_first}Id" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">${classInfo.classComment}名称</label>
<label class="layui-form-label">${classInfo.classComment!'数据'}名称</label>
<div class="layui-input-inline">
<input type="text" name="${classInfo.className?uncap_first}Name" autocomplete="off" class="layui-input">
</div>
@ -31,8 +31,8 @@
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-addition"></i>${classInfo.classComment} </button>
<#-- <button class="layui-btn layui-btn-normal layui-btn-sm layui-btn-danger data-delete-btn" lay-event="del"> 删除${classInfo.classComment} </button>-->
<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-addition"></i>${classInfo.classComment!'数据'} </button>
<#-- <button class="layui-btn layui-btn-normal layui-btn-sm layui-btn-danger data-delete-btn" lay-event="del"> 删除${classInfo.classComment!'数据'} </button>-->
</div>
</script>

View File

@ -1,8 +1,8 @@
@ApiOperation(value = "${classInfo.classComment}", notes = "${classInfo.classComment}")
@ApiOperation(value = "${classInfo.classComment!'API接口'}", notes = "${classInfo.classComment!'API接口说明'}")
@ApiImplicitParams({
<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
<#list classInfo.fieldList as fieldItem >
@ApiImplicitParam(name = "${fieldItem.fieldName}", value = "${fieldItem.fieldComment}", required = false, dataType = "${fieldItem.fieldClass}")<#if fieldItem_has_next>,</#if>
@ApiImplicitParam(name = "${fieldItem.fieldName}", value = "${fieldItem.fieldComment!''}", required = false, dataType = "${fieldItem.fieldClass}")<#if fieldItem_has_next>,</#if>
</#list>
</#if>
}

View File

@ -1,5 +1,5 @@
/**
* ${classInfo.classComment}对象Get Set
* ${classInfo.classComment!'数据'}对象Get Set
* @author ${authorName} ${.now?string('yyyy-MM-dd')}
*/

View File

@ -1,5 +1,5 @@
<!--
${classInfo.classComment}对象Get Set
${classInfo.classComment!'数据'}对象Get Set
@author ${authorName} ${.now?string('yyyy-MM-dd')}
-->
<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>