11package com .softdev .system .generator .util ;
22
3- import com .softdev .system .generator .util .mysqlJavaTypeUtil ;
43import com .alibaba .fastjson .JSON ;
54import com .alibaba .fastjson .JSONArray ;
65import com .alibaba .fastjson .JSONObject ;
76import com .softdev .system .generator .entity .ClassInfo ;
87import com .softdev .system .generator .entity .FieldInfo ;
8+ import com .softdev .system .generator .entity .NonCaseString ;
99import com .softdev .system .generator .entity .ParamInfo ;
1010
1111import java .io .IOException ;
12- import java .math .BigDecimal ;
1312import java .util .ArrayList ;
1413import java .util .Arrays ;
15- import java .util .Date ;
1614import java .util .List ;
1715import java .util .concurrent .atomic .AtomicInteger ;
1816import java .util .regex .Matcher ;
@@ -34,23 +32,28 @@ public class TableParseUtil {
3432 public static ClassInfo processTableIntoClassInfo (ParamInfo paramInfo )
3533 throws IOException {
3634 //process the param
37- String tableSql = paramInfo .getTableSql ();
35+ NonCaseString tableSql = NonCaseString . of ( paramInfo .getTableSql () );
3836 String nameCaseType = MapUtil .getString (paramInfo .getOptions (),"nameCaseType" );
3937 Boolean isPackageType = MapUtil .getBoolean (paramInfo .getOptions (),"isPackageType" );
4038
4139 if (tableSql == null || tableSql .trim ().length () == 0 ) {
4240 throw new CodeGenerateException ("Table structure can not be empty. 表结构不能为空。" );
4341 }
4442 //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+ ;
4650 //deal with java string copy \n"
4751 tableSql = tableSql .trim ().replaceAll ("\\ \\ n`" , "" ).replaceAll ("\\ +" , "" ).replaceAll ("``" , "`" ).replaceAll ("\\ \\ " , "" );
4852 // table Name
4953 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 ();
5457 } else {
5558 throw new CodeGenerateException ("Table structure incorrect.表结构不正确。" );
5659 }
@@ -88,9 +91,11 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
8891 String classComment = null ;
8992 //mysql是comment=,pgsql/oracle是comment on table,
9093 //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 ();
9499 if (classCommentTmp .contains ("`" )) {
95100 classCommentTmp = classCommentTmp .substring (classCommentTmp .indexOf ("`" ) + 1 );
96101 classCommentTmp = classCommentTmp .substring (0 , classCommentTmp .indexOf ("`" ));
@@ -109,7 +114,7 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
109114 List <FieldInfo > fieldList = new ArrayList <FieldInfo >();
110115
111116 // 正常( ) 内的一定是字段相关的定义。
112- String fieldListTmp = tableSql .substring (tableSql .indexOf ("(" ) + 1 , tableSql .lastIndexOf (")" ));
117+ String fieldListTmp = tableSql .substring (tableSql .indexOf ("(" ) + 1 , tableSql .lastIndexOf (")" )). get () ;
113118
114119 // 匹配 comment,替换备注里的小逗号, 防止不小心被当成切割符号切割
115120 String commentPattenStr1 = "comment `(.*?)\\ `" ;
@@ -149,7 +154,8 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
149154 if (fieldLineList .length > 0 ) {
150155 int i = 0 ;
151156 //i为了解决primary key关键字出现的地方,出现在前3行,一般和id有关
152- for (String columnLine : fieldLineList ) {
157+ for (String columnLine0 : fieldLineList ) {
158+ NonCaseString columnLine = NonCaseString .of (columnLine0 );
153159 i ++;
154160 columnLine = columnLine .replaceAll ("\n " , "" ).replaceAll ("\t " , "" ).trim ();
155161 // `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
@@ -158,13 +164,10 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
158164 // 2019-2-22 zhengkai 要在条件中使用复杂的表达式
159165 // 2019-4-29 zhengkai 优化对普通和特殊storage关键字的判断(感谢@AhHeadFloating的反馈 )
160166 // 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 ) {
168171 //如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
169172 if (columnLine .length () < 5 ) {
170173 continue ;
@@ -174,24 +177,25 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
174177 columnLine = columnLine .replaceAll ("`" , " " ).replaceAll ("\" " , " " ).replaceAll ("'" , "" ).replaceAll (" " , " " ).trim ();
175178 //如果遇到username varchar(65) default '' not null,这种情况,判断第一个空格是否比第一个引号前
176179 try {
177- columnName = columnLine .substring (0 , columnLine .indexOf (" " ));
180+ columnName = columnLine .substring (0 , columnLine .indexOf (" " )). get () ;
178181 } catch (StringIndexOutOfBoundsException e ) {
179182 System .out .println ("err happened: " + columnLine );
180183 throw e ;
181184 }
182185
183186 // field Name
184187 // 2019-09-08 yj 添加是否下划线转换为驼峰的判断
188+ // 2023-8-27 zhangfei 支持原始列名任意命名风格, 不依赖用户是否输入下划线
185189 String fieldName = null ;
186190 if (ParamInfo .NAME_CASE_TYPE .CAMEL_CASE .equals (nameCaseType )) {
187191 fieldName = StringUtils .lowerCaseFirst (StringUtils .underlineToCamelCase (columnName ));
188192 if (fieldName .contains ("_" )) {
189193 fieldName = fieldName .replaceAll ("_" , "" );
190194 }
191195 } else if (ParamInfo .NAME_CASE_TYPE .UNDER_SCORE_CASE .equals (nameCaseType )) {
192- fieldName = StringUtils .lowerCaseFirst (columnName );
196+ fieldName = StringUtils .toUnderline (columnName , false );
193197 } 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 );
195199 } else {
196200 fieldName = columnName ;
197201 }
@@ -228,12 +232,12 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
228232 while (columnCommentMatcher .find ()) {
229233 String columnCommentTmp = columnCommentMatcher .group ();
230234 //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 () ;
232236 fieldComment = fieldComment .substring (0 , fieldComment .indexOf ("`" )).trim ();
233237 }
234238 } else if (columnLine .contains (" comment" )) {
235239 //20200518 zhengkai 修复包含comment关键字的问题
236- String commentTmp = columnLine .substring (columnLine .lastIndexOf ("comment" ) + 7 ).trim ();
240+ String commentTmp = columnLine .substring (columnLine .lastIndexOf ("comment" ) + 7 ).trim (). get () ;
237241 // '用户ID',
238242 if (commentTmp .contains ("`" ) || commentTmp .indexOf ("`" ) != commentTmp .lastIndexOf ("`" )) {
239243 commentTmp = commentTmp .substring (commentTmp .indexOf ("`" ) + 1 , commentTmp .lastIndexOf ("`" ));
@@ -275,6 +279,24 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
275279 return codeJavaInfo ;
276280 }
277281
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+
278300 /**
279301 * 解析JSON生成类信息
280302 *
0 commit comments