1
1
package com .softdev .system .generator .util ;
2
2
3
- import com .softdev .system .generator .util .mysqlJavaTypeUtil ;
4
3
import com .alibaba .fastjson .JSON ;
5
4
import com .alibaba .fastjson .JSONArray ;
6
5
import com .alibaba .fastjson .JSONObject ;
7
6
import com .softdev .system .generator .entity .ClassInfo ;
8
7
import com .softdev .system .generator .entity .FieldInfo ;
8
+ import com .softdev .system .generator .entity .NonCaseString ;
9
9
import com .softdev .system .generator .entity .ParamInfo ;
10
10
11
11
import java .io .IOException ;
12
- import java .math .BigDecimal ;
13
12
import java .util .ArrayList ;
14
13
import java .util .Arrays ;
15
- import java .util .Date ;
16
14
import java .util .List ;
17
15
import java .util .concurrent .atomic .AtomicInteger ;
18
16
import java .util .regex .Matcher ;
@@ -34,23 +32,28 @@ public class TableParseUtil {
34
32
public static ClassInfo processTableIntoClassInfo (ParamInfo paramInfo )
35
33
throws IOException {
36
34
//process the param
37
- String tableSql = paramInfo .getTableSql ();
35
+ NonCaseString tableSql = NonCaseString . of ( paramInfo .getTableSql () );
38
36
String nameCaseType = MapUtil .getString (paramInfo .getOptions (),"nameCaseType" );
39
37
Boolean isPackageType = MapUtil .getBoolean (paramInfo .getOptions (),"isPackageType" );
40
38
41
39
if (tableSql == null || tableSql .trim ().length () == 0 ) {
42
40
throw new CodeGenerateException ("Table structure can not be empty. 表结构不能为空。" );
43
41
}
44
42
//deal with special character
45
- tableSql = tableSql .trim ().replaceAll ("'" , "`" ).replaceAll ("\" " , "`" ).replaceAll ("," , "," ).toLowerCase ();
43
+ tableSql = tableSql .trim ()
44
+ .replaceAll ("'" , "`" )
45
+ .replaceAll ("\" " , "`" )
46
+ .replaceAll ("," , "," )
47
+ // 这里全部转小写, 会让驼峰风格的字段名丢失驼峰信息(真有驼峰sql字段名的呢(* ̄) ̄)); 下文使用工具方法处理包含等
48
+ // .toLowerCase()
49
+ ;
46
50
//deal with java string copy \n"
47
51
tableSql = tableSql .trim ().replaceAll ("\\ \\ n`" , "" ).replaceAll ("\\ +" , "" ).replaceAll ("``" , "`" ).replaceAll ("\\ \\ " , "" );
48
52
// table Name
49
53
String tableName = null ;
50
- if (tableSql .contains ("TABLE" ) && tableSql .contains ("(" )) {
51
- tableName = tableSql .substring (tableSql .indexOf ("TABLE" ) + 5 , tableSql .indexOf ("(" ));
52
- } else if (tableSql .contains ("table" ) && tableSql .contains ("(" )) {
53
- tableName = tableSql .substring (tableSql .indexOf ("table" ) + 5 , tableSql .indexOf ("(" ));
54
+ int tableKwIx = tableSql .indexOf ("TABLE" ); // 包含判断和位置一次搞定
55
+ if (tableKwIx > -1 && tableSql .contains ("(" )) {
56
+ tableName = tableSql .substring (tableKwIx + 5 , tableSql .indexOf ("(" )).get ();
54
57
} else {
55
58
throw new CodeGenerateException ("Table structure incorrect.表结构不正确。" );
56
59
}
@@ -88,9 +91,11 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
88
91
String classComment = null ;
89
92
//mysql是comment=,pgsql/oracle是comment on table,
90
93
//2020-05-25 优化表备注的获取逻辑
91
- if (tableSql .contains ("comment=" ) || tableSql .contains ("comment on table" )) {
92
- String classCommentTmp = (tableSql .contains ("comment=" )) ?
93
- tableSql .substring (tableSql .lastIndexOf ("comment=" ) + 8 ).trim () : tableSql .substring (tableSql .lastIndexOf ("comment on table" ) + 17 ).trim ();
94
+ if (tableSql .containsAny ("comment=" , "comment on table" )) {
95
+ int ix = tableSql .lastIndexOf ("comment=" );
96
+ String classCommentTmp = (ix > -1 ) ?
97
+ tableSql .substring (ix + 8 ).trim ().get () :
98
+ tableSql .substring (tableSql .lastIndexOf ("comment on table" ) + 17 ).trim ().get ();
94
99
if (classCommentTmp .contains ("`" )) {
95
100
classCommentTmp = classCommentTmp .substring (classCommentTmp .indexOf ("`" ) + 1 );
96
101
classCommentTmp = classCommentTmp .substring (0 , classCommentTmp .indexOf ("`" ));
@@ -109,7 +114,7 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
109
114
List <FieldInfo > fieldList = new ArrayList <FieldInfo >();
110
115
111
116
// 正常( ) 内的一定是字段相关的定义。
112
- String fieldListTmp = tableSql .substring (tableSql .indexOf ("(" ) + 1 , tableSql .lastIndexOf (")" ));
117
+ String fieldListTmp = tableSql .substring (tableSql .indexOf ("(" ) + 1 , tableSql .lastIndexOf (")" )). get () ;
113
118
114
119
// 匹配 comment,替换备注里的小逗号, 防止不小心被当成切割符号切割
115
120
String commentPattenStr1 = "comment `(.*?)\\ `" ;
@@ -149,7 +154,8 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
149
154
if (fieldLineList .length > 0 ) {
150
155
int i = 0 ;
151
156
//i为了解决primary key关键字出现的地方,出现在前3行,一般和id有关
152
- for (String columnLine : fieldLineList ) {
157
+ for (String columnLine0 : fieldLineList ) {
158
+ NonCaseString columnLine = NonCaseString .of (columnLine0 );
153
159
i ++;
154
160
columnLine = columnLine .replaceAll ("\n " , "" ).replaceAll ("\t " , "" ).trim ();
155
161
// `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
@@ -158,13 +164,10 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
158
164
// 2019年2月22日 zhengkai 要在条件中使用复杂的表达式
159
165
// 2019年4月29日 zhengkai 优化对普通和特殊storage关键字的判断(感谢@AhHeadFloating的反馈 )
160
166
// 2020年10月20日 zhengkai 优化对fulltext/index关键字的处理(感谢@WEGFan的反馈)
161
- boolean specialFlag = (!columnLine .contains ("key " ) && !columnLine .contains ("constraint" ) && !columnLine .contains ("using" ) && !columnLine .contains ("unique " )
162
- && !(columnLine .contains ("primary " ) && columnLine .indexOf ("storage" ) + 3 > columnLine .indexOf ("(" ))
163
- && !columnLine .contains ("fulltext " ) && !columnLine .contains ("index " )
164
- && !columnLine .contains ("pctincrease" )
165
- && !columnLine .contains ("buffer_pool" ) && !columnLine .contains ("tablespace" )
166
- && !(columnLine .contains ("primary " ) && i > 3 ));
167
- if (specialFlag ) {
167
+ // 2023年8月27日 zhangfei 改用工具方法判断, 且修改变量名(非特殊标识), 方法抽取
168
+ boolean notSpecialFlag = isNotSpecialColumnLine (columnLine , i );
169
+
170
+ if (notSpecialFlag ) {
168
171
//如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
169
172
if (columnLine .length () < 5 ) {
170
173
continue ;
@@ -174,24 +177,25 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
174
177
columnLine = columnLine .replaceAll ("`" , " " ).replaceAll ("\" " , " " ).replaceAll ("'" , "" ).replaceAll (" " , " " ).trim ();
175
178
//如果遇到username varchar(65) default '' not null,这种情况,判断第一个空格是否比第一个引号前
176
179
try {
177
- columnName = columnLine .substring (0 , columnLine .indexOf (" " ));
180
+ columnName = columnLine .substring (0 , columnLine .indexOf (" " )). get () ;
178
181
} catch (StringIndexOutOfBoundsException e ) {
179
182
System .out .println ("err happened: " + columnLine );
180
183
throw e ;
181
184
}
182
185
183
186
// field Name
184
187
// 2019年09月08日 yj 添加是否下划线转换为驼峰的判断
188
+ // 2023年8月27日 zhangfei 支持原始列名任意命名风格, 不依赖用户是否输入下划线
185
189
String fieldName = null ;
186
190
if (ParamInfo .NAME_CASE_TYPE .CAMEL_CASE .equals (nameCaseType )) {
187
191
fieldName = StringUtils .lowerCaseFirst (StringUtils .underlineToCamelCase (columnName ));
188
192
if (fieldName .contains ("_" )) {
189
193
fieldName = fieldName .replaceAll ("_" , "" );
190
194
}
191
195
} else if (ParamInfo .NAME_CASE_TYPE .UNDER_SCORE_CASE .equals (nameCaseType )) {
192
- fieldName = StringUtils .lowerCaseFirst (columnName );
196
+ fieldName = StringUtils .toUnderline (columnName , false );
193
197
} else if (ParamInfo .NAME_CASE_TYPE .UPPER_UNDER_SCORE_CASE .equals (nameCaseType )) {
194
- fieldName = StringUtils .lowerCaseFirst (columnName .toUpperCase ());
198
+ fieldName = StringUtils .toUnderline (columnName .toUpperCase (), true );
195
199
} else {
196
200
fieldName = columnName ;
197
201
}
@@ -228,12 +232,12 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
228
232
while (columnCommentMatcher .find ()) {
229
233
String columnCommentTmp = columnCommentMatcher .group ();
230
234
//System.out.println(columnCommentTmp);
231
- fieldComment = tableSql .substring (tableSql .indexOf (columnCommentTmp ) + columnCommentTmp .length ()).trim ();
235
+ fieldComment = tableSql .substring (tableSql .indexOf (columnCommentTmp ) + columnCommentTmp .length ()).trim (). get () ;
232
236
fieldComment = fieldComment .substring (0 , fieldComment .indexOf ("`" )).trim ();
233
237
}
234
238
} else if (columnLine .contains (" comment" )) {
235
239
//20200518 zhengkai 修复包含comment关键字的问题
236
- String commentTmp = columnLine .substring (columnLine .lastIndexOf ("comment" ) + 7 ).trim ();
240
+ String commentTmp = columnLine .substring (columnLine .lastIndexOf ("comment" ) + 7 ).trim (). get () ;
237
241
// '用户ID',
238
242
if (commentTmp .contains ("`" ) || commentTmp .indexOf ("`" ) != commentTmp .lastIndexOf ("`" )) {
239
243
commentTmp = commentTmp .substring (commentTmp .indexOf ("`" ) + 1 , commentTmp .lastIndexOf ("`" ));
@@ -275,6 +279,24 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
275
279
return codeJavaInfo ;
276
280
}
277
281
282
+ private static boolean isNotSpecialColumnLine (NonCaseString columnLine , int lineSeq ) {
283
+ return (
284
+ !columnLine .containsAny (
285
+ "key " ,
286
+ "constraint" ,
287
+ "using" ,
288
+ "unique " ,
289
+ "fulltext " ,
290
+ "index " ,
291
+ "pctincrease" ,
292
+ "buffer_pool" ,
293
+ "tablespace"
294
+ )
295
+ && !(columnLine .contains ("primary " ) && columnLine .indexOf ("storage" ) + 3 > columnLine .indexOf ("(" ))
296
+ && !(columnLine .contains ("primary " ) && lineSeq > 3 )
297
+ );
298
+ }
299
+
278
300
/**
279
301
* 解析JSON生成类信息
280
302
*
0 commit comments