@@ -24,7 +24,8 @@ public class TableParseUtil {
24
24
* @param tableSql
25
25
* @return
26
26
*/
27
- public static ClassInfo processTableIntoClassInfo (String tableSql , boolean isUnderLineToCamelCase ) throws IOException {
27
+ public static ClassInfo processTableIntoClassInfo (String tableSql , boolean isUnderLineToCamelCase ,String tinyintTransType )
28
+ throws IOException {
28
29
if (tableSql ==null || tableSql .trim ().length ()==0 ) {
29
30
throw new CodeGenerateException ("Table structure can not be empty." );
30
31
}
@@ -41,7 +42,9 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
41
42
}
42
43
43
44
//新增处理create table if not exists members情况
44
- if (tableName .contains ("if not exists" )) tableName =tableName .replaceAll ("if not exists" ,"" );
45
+ if (tableName .contains ("if not exists" )) {
46
+ tableName =tableName .replaceAll ("if not exists" ,"" );
47
+ }
45
48
46
49
if (tableName .contains ("`" )) {
47
50
tableName = tableName .substring (tableName .indexOf ("`" )+1 , tableName .lastIndexOf ("`" ));
@@ -101,20 +104,22 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
101
104
String fieldListTmp = tableSql .substring (tableSql .indexOf ("(" )+1 , tableSql .lastIndexOf (")" ));
102
105
103
106
// 匹配 comment,替换备注里的小逗号, 防止不小心被当成切割符号切割
104
- Matcher matcher = Pattern .compile ("comment `(.*?)\\ `" ).matcher (fieldListTmp ); // "\\{(.*?)\\}"
105
- while (matcher .find ()){
107
+ String commentPattenStr1 ="comment `(.*?)\\ `" ;
108
+ Matcher matcher1 = Pattern .compile (commentPattenStr1 ).matcher (fieldListTmp );
109
+ while (matcher1 .find ()){
106
110
107
- String commentTmp = matcher .group ();
111
+ String commentTmp = matcher1 .group ();
108
112
//2018-9-27 zhengk 不替换,只处理,支持COMMENT评论里面多种注释
109
113
//commentTmp = commentTmp.replaceAll("\\ comment `|\\`", " "); // "\\{|\\}"
110
114
111
115
if (commentTmp .contains ("," )) {
112
116
String commentTmpFinal = commentTmp .replaceAll ("," , "," );
113
- fieldListTmp = fieldListTmp .replace (matcher .group (), commentTmpFinal );
117
+ fieldListTmp = fieldListTmp .replace (matcher1 .group (), commentTmpFinal );
114
118
}
115
119
}
116
120
//2018-10-18 zhengkai 新增支持double(10, 2)等类型中有英文逗号的特殊情况
117
- Matcher matcher2 = Pattern .compile ("\\ `(.*?)\\ `" ).matcher (fieldListTmp ); // "\\{(.*?)\\}"
121
+ String commentPattenStr2 ="\\ `(.*?)\\ `" ;
122
+ Matcher matcher2 = Pattern .compile (commentPattenStr2 ).matcher (fieldListTmp );
118
123
while (matcher2 .find ()){
119
124
String commentTmp2 = matcher2 .group ();
120
125
if (commentTmp2 .contains ("," )) {
@@ -123,7 +128,8 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
123
128
}
124
129
}
125
130
//2018-10-18 zhengkai 新增支持double(10, 2)等类型中有英文逗号的特殊情况
126
- Matcher matcher3 = Pattern .compile ("\\ ((.*?)\\ )" ).matcher (fieldListTmp ); // "\\{(.*?)\\}"
131
+ String commentPattenStr3 ="\\ ((.*?)\\ )" ;
132
+ Matcher matcher3 = Pattern .compile (commentPattenStr3 ).matcher (fieldListTmp );
127
133
while (matcher3 .find ()){
128
134
String commentTmp3 = matcher3 .group ();
129
135
if (commentTmp3 .contains ("," )) {
@@ -148,7 +154,6 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
148
154
&&!columnLine .contains ("pctincrease" )
149
155
&&!columnLine .contains ("buffer_pool" )&&!columnLine .contains ("tablespace" )
150
156
&&!(columnLine .contains ("primary" )&&i >3 ));
151
-
152
157
if (specialFlag ){
153
158
//如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
154
159
if (columnLine .length ()<5 ) {continue ;}
@@ -157,9 +162,8 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
157
162
columnLine =columnLine .replaceAll ("`" ," " ).replaceAll ("\" " ," " ).replaceAll ("'" ,"" ).replaceAll (" " ," " ).trim ();
158
163
//如果遇到username varchar(65) default '' not null,这种情况,判断第一个空格是否比第一个引号前
159
164
columnName = columnLine .substring (0 , columnLine .indexOf (" " ));
160
-
161
165
// field Name
162
- // 2019-09-08 yj 添加是否下划线转换为驼峰的判断
166
+ // 2019-09-08 yj 添加是否下划线转换为驼峰的判断
163
167
String fieldName ;
164
168
if (isUnderLineToCamelCase ){
165
169
fieldName = StringUtils .lowerCaseFirst (StringUtils .underlineToCamelCase (columnName ));
@@ -184,7 +188,7 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
184
188
fieldClass = Float .class .getSimpleName ();
185
189
} else if (columnLine .contains ("double" )) {
186
190
fieldClass = Double .class .getSimpleName ();
187
- } else if (columnLine .contains ("datetime" ) || columnLine .contains ("timestamp" )) {
191
+ } else if (columnLine .contains ("time" ) || columnLine . contains ( "date" ) || columnLine . contains ( " datetime" ) || columnLine .contains ("timestamp" )) {
188
192
fieldClass = Date .class .getSimpleName ();
189
193
} else if (columnLine .contains ("varchar" ) || columnLine .contains (" text" )|| columnLine .contains ("char" )
190
194
|| columnLine .contains ("clob" )||columnLine .contains ("blob" )||columnLine .contains ("json" )) {
@@ -221,9 +225,12 @@ public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnd
221
225
}else {
222
226
fieldClass = BigDecimal .class .getSimpleName ();
223
227
}
224
- } else if (columnLine .contains ("boolean" )|| columnLine . contains ( "tinyint" ) ) {
228
+ } else if (columnLine .contains ("boolean" )) {
225
229
//20190910 MOSHOW.K.ZHENG 新增对boolean的处理(感谢@violinxsc的反馈)以及修复tinyint类型字段无法生成boolean类型问题(感谢@hahaYhui的反馈)
226
230
fieldClass = Boolean .class .getSimpleName ();
231
+ } else if (columnLine .contains ("tinyint" ) ) {
232
+ //20191115 MOSHOW.K.ZHENG 支持对tinyint的特殊处理
233
+ fieldClass =tinyintTransType ;
227
234
} else {
228
235
fieldClass = String .class .getSimpleName ();
229
236
}
0 commit comments