@@ -29,7 +29,7 @@ public static ClassInfo processTableIntoClassInfo(String tableSql) throws IOExce
2929 if (tableSql ==null || tableSql .trim ().length ()==0 ) {
3030 throw new CodeGenerateException ("Table structure can not be empty." );
3131 }
32- tableSql = tableSql .trim ().replaceAll ("'" ,"`" ).replaceAll ("\" " ,"`" ).toLowerCase ();
32+ tableSql = tableSql .trim ().replaceAll ("'" ,"`" ).replaceAll ("\" " ,"`" ).replaceAll ( "," , "," ). toLowerCase ();
3333
3434 // table Name
3535 String tableName = null ;
@@ -65,33 +65,47 @@ public static ClassInfo processTableIntoClassInfo(String tableSql) throws IOExce
6565
6666 // class Comment
6767 String classComment = null ;
68- if (tableSql .contains ("COMMENT=" )) {
69- String classCommentTmp = tableSql .substring (tableSql .lastIndexOf ("COMMENT=" )+8 ).trim ();
70- if (classCommentTmp .contains ("'" ) || classCommentTmp .indexOf ("'" )!=classCommentTmp .lastIndexOf ("'" )) {
71- classCommentTmp = classCommentTmp .substring (classCommentTmp .indexOf ("'" )+1 , classCommentTmp .lastIndexOf ("'" ));
68+ //mysql是comment=,pgsql/oracle是comment on table,
69+ if (tableSql .contains ("comment=" )) {
70+ String classCommentTmp = tableSql .substring (tableSql .lastIndexOf ("comment=" )+8 ).replaceAll ("`" ,"" ).trim ();
71+ if (classCommentTmp .indexOf (" " )!=classCommentTmp .lastIndexOf (" " )) {
72+ classCommentTmp = classCommentTmp .substring (classCommentTmp .indexOf (" " )+1 , classCommentTmp .lastIndexOf (" " ));
7273 }
7374 if (classCommentTmp !=null && classCommentTmp .trim ().length ()>0 ) {
7475 classComment = classCommentTmp ;
7576 }else {
7677 //修复表备注为空问题
7778 classComment = className ;
7879 }
80+ }else if (tableSql .contains ("comment on table" )) {
81+ //COMMENT ON TABLE CT_BAS_FEETYPE IS 'CT_BAS_FEETYPE';
82+ String classCommentTmp = tableSql .substring (tableSql .lastIndexOf ("comment on table" )+17 ).trim ();
83+ //证明这是一个常规的COMMENT ON TABLE xxx IS 'xxxx'
84+ if (classCommentTmp .contains ("`" )) {
85+ classCommentTmp = classCommentTmp .substring (classCommentTmp .indexOf ("`" )+1 );
86+ classCommentTmp = classCommentTmp .substring (0 ,classCommentTmp .indexOf ("`" ));
87+ classComment = classCommentTmp ;
88+ }else {
89+ //非常规的没法分析
90+ classComment = tableName ;
91+ }
7992 }else {
8093 //修复表备注为空问题
81- classComment = className ;
94+ classComment = tableName ;
8295 }
8396
8497 // field List
8598 List <FieldInfo > fieldList = new ArrayList <FieldInfo >();
8699
100+ // 正常( ) 内的一定是字段相关的定义。
87101 String fieldListTmp = tableSql .substring (tableSql .indexOf ("(" )+1 , tableSql .lastIndexOf (")" ));
88102
89- // replave "," by "," in comment
90- Matcher matcher = Pattern .compile ("\\ COMMENT '(.*?)\\ '" ).matcher (fieldListTmp ); // "\\{(.*?)\\}"
103+ // 匹配 comment,替换备注里的小逗号, 防止不小心被当成切割符号切割
104+ Matcher matcher = Pattern .compile ("\\ comment '(.*?)\\ '" ).matcher (fieldListTmp ); // "\\{(.*?)\\}"
91105 while (matcher .find ()){
92106
93107 String commentTmp = matcher .group ();
94- commentTmp = commentTmp .replaceAll ("\\ COMMENT '|\\ '" , "" ); // "\\{|\\}"
108+ commentTmp = commentTmp .replaceAll ("\\ comment '|\\ '" , "" ); // "\\{|\\}"
95109
96110 if (commentTmp .contains ("," )) {
97111 String commentTmpFinal = commentTmp .replaceAll ("," , "," );
@@ -150,14 +164,24 @@ public static ClassInfo processTableIntoClassInfo(String tableSql) throws IOExce
150164 fieldClass = String .class .getSimpleName ();
151165 }
152166
153- // field comment
167+ // field comment,MySQL的一般位于field行,而pgsql和oralce多位于后面。
154168 String fieldComment = null ;
155- if (columnLine .contains ("COMMENT " )) {
156- String commentTmp = fieldComment = columnLine .substring (columnLine .indexOf ("COMMENT " )+7 ).trim (); // '用户ID',
157- if (commentTmp .contains ("' " ) || commentTmp .indexOf ("' " )!=commentTmp .lastIndexOf ("' " )) {
158- commentTmp = commentTmp .substring (commentTmp .indexOf ("' " )+1 , commentTmp .lastIndexOf ("' " ));
169+ if (columnLine .contains ("comment " )) {
170+ String commentTmp = columnLine .substring (columnLine .indexOf ("comment " )+7 ).trim (); // '用户ID',
171+ if (commentTmp .contains ("` " ) || commentTmp .indexOf ("` " )!=commentTmp .lastIndexOf ("` " )) {
172+ commentTmp = commentTmp .substring (commentTmp .indexOf ("` " )+1 , commentTmp .lastIndexOf ("` " ));
159173 }
160174 fieldComment = commentTmp ;
175+ }else if (tableSql .contains ("comment on column" )&&tableSql .contains ("." +columnName +" is `" )){
176+ //新增对pgsql/oracle的字段备注支持
177+ //COMMENT ON COLUMN public.check_info.check_name IS '检查者名称';
178+ Matcher columnCommentMatcher = Pattern .compile ("." +columnName +" is `" ).matcher (tableSql ); // "\\{(.*?)\\}"
179+ while (columnCommentMatcher .find ()){
180+ String columnCommentTmp = columnCommentMatcher .group ();
181+ System .out .println (columnCommentTmp );
182+ fieldComment = tableSql .substring (tableSql .indexOf (columnCommentTmp )+columnCommentTmp .length ()).trim ();
183+ fieldComment = fieldComment .substring (0 ,fieldComment .indexOf ("`" )).trim ();
184+ }
161185 }else {
162186 //修复comment不存在导致报错的问题
163187 fieldComment = columnName ;
@@ -175,7 +199,7 @@ public static ClassInfo processTableIntoClassInfo(String tableSql) throws IOExce
175199 }
176200
177201 if (fieldList .size () < 1 ) {
178- throw new CodeGenerateException ("Table structure anomaly. " );
202+ throw new CodeGenerateException ("表结构分析失败,请检查语句或者提交issue给我 " );
179203 }
180204
181205 ClassInfo codeJavaInfo = new ClassInfo ();
0 commit comments