Skip to content

Commit 995e1e6

Browse files
committed
JSqlParser Engine全新升级,目前Select SQL模式相对稳定!
1 parent 2e74d50 commit 995e1e6

File tree

8 files changed

+100
-75
lines changed

8 files changed

+100
-75
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@
7070

7171
# 更新预告
7272
1.计划加入AI来帮忙生成更多样式的模板
73-
2.计划使用AI来改善现有模板
74-
3.深度支持Select SQL模式,以及探索JSON模式更多可能
73+
2.改进JSqlParser Engine (Select SQL and Create SQL)
7574

7675
# Update Logs
7776
| 更新日期 | 更新内容 |
7877
|:-----------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
79-
| 2025.09.13 | Create SQL by JSqlParser Engine升级<br>更新SpringBoot等类库版本,修复漏洞<br>修复CDN问题,切换为staticfile.org |
78+
| 2025.09.13 | JSqlParser Engine全新升级,目前Select SQL模式相对稳定! <br>更新SpringBoot等类库版本,修复漏洞<br>修复CDN问题,切换为staticfile.org |
8079
| 2025.03.31 | 优化说明 |
8180
| 2025.03.16 | NewUI V2前端优化:<br>移除不必要内容,优化Local和CDN静态文件引入。<br><br>修复由于SQL类型大写导致无法转换的问题。(感谢@zzy-design的反馈)<br><br>JPA模板优化(感谢@PenroseYang的反馈):<br>修复不开启Lombok情况下Set/Get方法生成问题;<br>修复importDdate判断为true后没有引入日期类的问题<br> |
8281
| 2024.12.29 | 优化前端加载速度,优化输出代码着色,CDN改字节跳动静态资源公共库。<br> |

generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public ReturnT generateCode(@RequestBody ParamInfo paramInfo) throws Exception {
8888
classInfo = generatorService.generateSelectSqlBySQLPraser(paramInfo);
8989
break;
9090
case "create-sql":
91-
//SelectSqlBySQLPraser模式:parse select sql by JSqlParser
91+
//CreateSqlBySQLPraser模式:parse create sql by JSqlParser
9292
classInfo = generatorService.generateCreateSqlBySQLPraser(paramInfo);
9393
break;
9494
default:

generator-web/src/main/java/com/softdev/system/generator/service/GeneratorServiceImpl.java

Lines changed: 56 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Except
9696
if (!CollectionUtils.isEmpty(tableNameList)) {
9797
String tableName = tableNameList.get(0).trim();
9898
classInfo.setTableName(tableName);
99+
classInfo.setOriginTableName(tableName);
99100
String className = StringUtilsPlus.upperCaseFirst(StringUtilsPlus.underlineToCamelCase(tableName));
100101
if (className.contains("_")) {
101102
className = className.replaceAll("_", "");
@@ -121,21 +122,15 @@ public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Except
121122
fieldName=fieldName.contains(".")?fieldName.substring(fieldName.indexOf(".")+1):fieldName;
122123
//转换前
123124
fieldInfo.setColumnName(fieldName);
124-
switch ((String) paramInfo.getOptions().get("nameCaseType")) {
125-
case ParamInfo.NAME_CASE_TYPE.CAMEL_CASE:
125+
fieldName = switch ((String) paramInfo.getOptions().get("nameCaseType")) {
126+
case ParamInfo.NAME_CASE_TYPE.CAMEL_CASE ->
126127
// 2024-1-27 L&J 适配任意(maybe)原始风格转小写驼峰
127-
fieldName = StringUtilsPlus.toLowerCamel(aliasName);
128-
break;
129-
case ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE:
130-
fieldName = StringUtilsPlus.toUnderline(aliasName, false);
131-
break;
132-
case ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE:
133-
fieldName = StringUtilsPlus.toUnderline(aliasName.toUpperCase(), true);
134-
break;
135-
default:
136-
fieldName = aliasName;
137-
break;
138-
}
128+
StringUtilsPlus.toLowerCamel(aliasName);
129+
case ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE -> StringUtilsPlus.toUnderline(aliasName, false);
130+
case ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE ->
131+
StringUtilsPlus.toUnderline(aliasName.toUpperCase(), true);
132+
default -> aliasName;
133+
};
139134
//转换后
140135
fieldInfo.setFieldName(fieldName);
141136

@@ -157,62 +152,57 @@ public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Except
157152
@Override
158153
public ClassInfo generateCreateSqlBySQLPraser(ParamInfo paramInfo) throws Exception {
159154
ClassInfo classInfo = new ClassInfo();
160-
Statement statement = CCJSqlParserUtil.parse(paramInfo.getTableSql());
161-
CCJSqlParserManager parserManager = new CCJSqlParserManager();
162-
statement = parserManager.parse(new StringReader(paramInfo.getTableSql()));
163-
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); // 创建表名发现者对象
164-
List<String> tableNameList = tablesNamesFinder.getTableList(statement); // 获取到表名列表
165-
//一般这里应该只解析到一个表名,除非多个表名,取第一个
166-
if (!CollectionUtils.isEmpty(tableNameList)) {
167-
String tableName = tableNameList.get(0).trim();
168-
classInfo.setTableName(tableName);
169-
String className = StringUtilsPlus.upperCaseFirst(StringUtilsPlus.underlineToCamelCase(tableName));
170-
if (className.contains("_")) {
171-
className = className.replaceAll("_", "");
172-
}
173-
classInfo.setClassName(className);
174-
classInfo.setClassComment(paramInfo.getTableSql());
155+
Statement statement = null;
156+
try {
157+
statement = CCJSqlParserUtil.parse(paramInfo.getTableSql().trim());
158+
}catch (Exception e) {
159+
e.printStackTrace();
160+
throw new SqlException("SQL语法错误:"+e.getMessage());
175161
}
176-
//解析查询元素
177-
Select select = null;
178-
select = (Select) CCJSqlParserUtil.parse(paramInfo.getTableSql());
179-
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
180-
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
181162

182-
// field List
163+
// 确保是CREATE TABLE语句
164+
if (!(statement instanceof CreateTable createTable)) {
165+
throw new SqlException("检测到SQL语句不是DLL CREATE TABLE语句");
166+
}
167+
168+
// 提取表名
169+
String tableName = createTable.getTable().getName();
170+
classInfo.setTableName(tableName);
171+
String className = StringUtilsPlus.upperCaseFirst(StringUtilsPlus.underlineToCamelCase(tableName));
172+
if (className.contains("_")) {
173+
className = className.replaceAll("_", "");
174+
}
175+
classInfo.setClassName(className);
176+
classInfo.setOriginTableName(tableName);
177+
classInfo.setClassComment(paramInfo.getTableSql());
178+
179+
// 提取字段信息
183180
List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
184-
selectItems.forEach(t->{
185-
FieldInfo fieldInfo = new FieldInfo();
186-
String fieldName = ((Column)t.getExpression()).getColumnName();
187-
String aliasName = t.getAlias() != null ? t.getAlias().getName() : ((Column)t.getExpression()).getColumnName();
188-
//存储原始字段名
189-
fieldInfo.setFieldComment(aliasName);fieldInfo.setColumnName(aliasName);
190-
//处理字段名是t.xxx的情况
191-
fieldName=fieldName.contains(".")?fieldName.substring(fieldName.indexOf(".")+1):fieldName;
192-
//转换前
193-
fieldInfo.setColumnName(fieldName);
194-
switch ((String) paramInfo.getOptions().get("nameCaseType")) {
195-
case ParamInfo.NAME_CASE_TYPE.CAMEL_CASE:
196-
// 2024-1-27 L&J 适配任意(maybe)原始风格转小写驼峰
197-
fieldName = StringUtilsPlus.toLowerCamel(aliasName);
198-
break;
199-
case ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE:
200-
fieldName = StringUtilsPlus.toUnderline(aliasName, false);
201-
break;
202-
case ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE:
203-
fieldName = StringUtilsPlus.toUnderline(aliasName.toUpperCase(), true);
204-
break;
205-
default:
206-
fieldName = aliasName;
207-
break;
181+
List<ColumnDefinition> columnDefinitions = createTable.getColumnDefinitions();
182+
183+
if (columnDefinitions != null) {
184+
for (ColumnDefinition columnDefinition : columnDefinitions) {
185+
FieldInfo fieldInfo = new FieldInfo();
186+
String columnName = columnDefinition.getColumnName();
187+
fieldInfo.setColumnName(columnName);
188+
fieldInfo.setFieldComment(columnDefinition.toString());
189+
190+
// 根据命名规则转换字段名
191+
String fieldName = switch ((String) paramInfo.getOptions().get("nameCaseType")) {
192+
case ParamInfo.NAME_CASE_TYPE.CAMEL_CASE -> StringUtilsPlus.toLowerCamel(columnName);
193+
case ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE -> StringUtilsPlus.toUnderline(columnName, false);
194+
case ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE ->
195+
StringUtilsPlus.toUnderline(columnName.toUpperCase(), true);
196+
default -> columnName;
197+
};
198+
fieldInfo.setFieldName(fieldName);
199+
200+
// 设置字段类型为String(因为无法准确推测类型)
201+
fieldInfo.setFieldClass("String");
202+
fieldList.add(fieldInfo);
208203
}
209-
//转换后
210-
fieldInfo.setFieldName(fieldName);
211-
212-
//无法推测类型,所有都set为String
213-
fieldInfo.setFieldClass("String");
214-
fieldList.add(fieldInfo);
215-
});
204+
}
205+
216206
classInfo.setFieldList(fieldList);
217207
log.info("classInfo:{}", JSON.toJSONString(classInfo));
218208
return classInfo;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.softdev.system.generator.util;
2+
3+
import java.io.Serial;
4+
5+
/**
6+
* @author xuxueli 2018-05-02 21:10:28
7+
*/
8+
public class SqlException extends RuntimeException {
9+
10+
@Serial
11+
private static final long serialVersionUID = 42L;
12+
13+
public SqlException() {
14+
super();
15+
}
16+
17+
public SqlException(String msg) {
18+
super(msg);
19+
}
20+
21+
public SqlException(String msg, Throwable cause) {
22+
super(msg, cause);
23+
}
24+
25+
public SqlException(Throwable cause) {
26+
super(cause);
27+
}
28+
29+
public SqlException(String message, Throwable cause,
30+
boolean enableSuppression,
31+
boolean writableStackTrace) {
32+
super(message, cause, enableSuppression, writableStackTrace);
33+
}
34+
35+
}

generator-web/src/main/resources/application-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ OEM:
5959
returnUtilSuccess: ResponseUtil.success
6060
returnUtilFailure: ResponseUtil.error
6161
outputStr: http://zhengkai.blog.csdn.net
62-
mode: CDN
62+
mode: local
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
spring:
22
profiles:
3-
active: bejson
3+
active: dev

generator-web/src/main/resources/statics/js/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ const vm = new Vue({
123123
//get value from codemirror
124124
vm.formData.tableSql=$.inputArea.getValue();
125125
axios.post(basePath+"/code/generate",vm.formData).then(function(res){
126-
if(res.code===500){
127-
error("生成失败,请检查SQL语句!!!");
126+
if(res.status===500||res.data.code===500){
127+
console.log(res);
128+
error("生成失败,请检查SQL语句!!!"+res.data.msg);
128129
return;
129130
}
130131
setAllCookie();

generator-web/src/main/resources/templates/newui2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ <h5 class="card-title m-0">生成设置</h5>
100100
</div>
101101

102102
<div class="card-body">
103-
<el-form-item label="生成引擎">
103+
<el-form-item label="🌟🌟🌟引擎">
104104
<el-select v-model="formData.options.dataType">
105105
<el-option label="DDL SQL@自研SQL解析引擎" value="sql"></el-option>
106106
<el-option label="SELECT SQL@JSqlParser引擎" value="select-sql"></el-option>

0 commit comments

Comments
 (0)