|
3 | 3 |
|
4 | 4 | import cn.hutool.core.util.XmlUtil;
|
5 | 5 | import com.alibaba.fastjson.JSON;
|
| 6 | +import com.alibaba.fastjson.JSONArray; |
6 | 7 | import com.alibaba.fastjson.JSONObject;
|
7 | 8 | import com.softdev.system.generator.entity.ClassInfo;
|
8 | 9 | import com.softdev.system.generator.entity.FieldInfo;
|
@@ -32,14 +33,18 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
|
32 | 33 | throws IOException {
|
33 | 34 | //process the param
|
34 | 35 | String tableSql=paramInfo.getTableSql();
|
35 |
| - boolean isUnderLineToCamelCase=paramInfo.isUnderLineToCamelCase(); |
| 36 | + String nameCaseType=paramInfo.getNameCaseType(); |
36 | 37 | String tinyintTransType=paramInfo.getTinyintTransType();
|
37 | 38 |
|
38 | 39 | if (tableSql==null || tableSql.trim().length()==0) {
|
39 | 40 | throw new CodeGenerateException("Table structure can not be empty.");
|
40 | 41 | }
|
| 42 | + //deal with special character |
41 | 43 | tableSql = tableSql.trim().replaceAll("'","`").replaceAll("\"","`").replaceAll(",",",").toLowerCase();
|
42 |
| - |
| 44 | + //deal with java string copy \n" |
| 45 | + System.out.println(tableSql); |
| 46 | + tableSql = tableSql.trim().replaceAll("n`","").replaceAll("\\+","").replaceAll("``","`").replaceAll("\\\\",""); |
| 47 | + System.out.println(tableSql); |
43 | 48 | // table Name
|
44 | 49 | String tableName = null;
|
45 | 50 | if (tableSql.contains("TABLE") && tableSql.contains("(")) {
|
@@ -173,14 +178,18 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
|
173 | 178 | columnName = columnLine.substring(0, columnLine.indexOf(" "));
|
174 | 179 | // field Name
|
175 | 180 | // 2019-09-08 yj 添加是否下划线转换为驼峰的判断
|
176 |
| - String fieldName; |
177 |
| - if(isUnderLineToCamelCase){ |
| 181 | + String fieldName=null; |
| 182 | + if(ParamInfo.NAME_CASE_TYPE.CAMEL_CASE.equals(nameCaseType)){ |
178 | 183 | fieldName = StringUtils.lowerCaseFirst(StringUtils.underlineToCamelCase(columnName));
|
179 | 184 | if (fieldName.contains("_")) {
|
180 | 185 | fieldName = fieldName.replaceAll("_", "");
|
181 | 186 | }
|
182 |
| - }else { |
| 187 | + }else if(ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE.equals(nameCaseType)){ |
183 | 188 | fieldName = StringUtils.lowerCaseFirst(columnName);
|
| 189 | + }else if(ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE.equals(nameCaseType)){ |
| 190 | + fieldName = StringUtils.lowerCaseFirst(columnName.toUpperCase()); |
| 191 | + }else{ |
| 192 | + fieldName=columnName; |
184 | 193 | }
|
185 | 194 |
|
186 | 195 | // field class
|
@@ -305,25 +314,26 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
|
305 | 314 | * @return
|
306 | 315 | */
|
307 | 316 | public static ClassInfo processJsonToClassInfo(ParamInfo paramInfo){
|
308 |
| - // field List |
309 |
| - List<FieldInfo> fieldList = new ArrayList<FieldInfo>(); |
310 |
| - |
311 |
| - if(JSON.isValid(paramInfo.getTableSql())){ |
312 |
| - JSONObject jsonObject = JSONObject.parseObject(paramInfo.getTableSql().trim()); |
313 |
| - jsonObject.keySet().stream().forEach(jsonField->{ |
314 |
| - FieldInfo fieldInfo = new FieldInfo(); |
315 |
| - fieldInfo.setFieldName(jsonField); |
316 |
| - fieldInfo.setColumnName(jsonField); |
317 |
| - fieldInfo.setFieldClass(String.class.getSimpleName()); |
318 |
| - fieldInfo.setFieldComment(jsonField); |
319 |
| - fieldList.add(fieldInfo); |
320 |
| - }); |
321 |
| - } |
322 | 317 | ClassInfo codeJavaInfo = new ClassInfo();
|
323 | 318 | codeJavaInfo.setTableName("JsonDto");
|
324 | 319 | codeJavaInfo.setClassName("JsonDto");
|
325 | 320 | codeJavaInfo.setClassComment("JsonDto");
|
326 |
| - codeJavaInfo.setFieldList(fieldList); |
| 321 | + |
| 322 | + //support children json if forget to add '{' in front of json |
| 323 | + if(paramInfo.getTableSql().trim().startsWith("\"")){ |
| 324 | + paramInfo.setTableSql("{"+paramInfo.getTableSql()); |
| 325 | + } |
| 326 | + if(JSON.isValid(paramInfo.getTableSql())){ |
| 327 | + if(paramInfo.getTableSql().trim().startsWith("{")){ |
| 328 | + JSONObject jsonObject = JSONObject.parseObject(paramInfo.getTableSql().trim()); |
| 329 | + //parse FieldList by JSONObject |
| 330 | + codeJavaInfo.setFieldList(processJsonObjectToFieldList(jsonObject)); |
| 331 | + }else if(paramInfo.getTableSql().trim().startsWith("[")){ |
| 332 | + JSONArray jsonArray=JSONArray.parseArray(paramInfo.getTableSql().trim()); |
| 333 | + //parse FieldList by JSONObject |
| 334 | + codeJavaInfo.setFieldList(processJsonObjectToFieldList(jsonArray.getJSONObject(0))); |
| 335 | + } |
| 336 | + } |
327 | 337 |
|
328 | 338 | return codeJavaInfo;
|
329 | 339 | }
|
@@ -377,4 +387,39 @@ public static ClassInfo processTableToClassInfoByRegex(ParamInfo paramInfo){
|
377 | 387 | }
|
378 | 388 | return codeJavaInfo;
|
379 | 389 | }
|
| 390 | + public static List<FieldInfo> processJsonObjectToFieldList(JSONObject jsonObject){ |
| 391 | + // field List |
| 392 | + List<FieldInfo> fieldList = new ArrayList<FieldInfo>(); |
| 393 | + jsonObject.keySet().stream().forEach(jsonField->{ |
| 394 | + FieldInfo fieldInfo = new FieldInfo(); |
| 395 | + fieldInfo.setFieldName(jsonField); |
| 396 | + fieldInfo.setColumnName(jsonField); |
| 397 | + fieldInfo.setFieldClass(String.class.getSimpleName()); |
| 398 | + fieldInfo.setFieldComment("father:"+jsonField); |
| 399 | + fieldList.add(fieldInfo); |
| 400 | + if(jsonObject.get(jsonField) instanceof JSONArray){ |
| 401 | + jsonObject.getJSONArray(jsonField).stream().forEach(arrayObject->{ |
| 402 | + FieldInfo fieldInfo2 = new FieldInfo(); |
| 403 | + fieldInfo2.setFieldName(arrayObject.toString()); |
| 404 | + fieldInfo2.setColumnName(arrayObject.toString()); |
| 405 | + fieldInfo2.setFieldClass(String.class.getSimpleName()); |
| 406 | + fieldInfo2.setFieldComment("children:"+arrayObject.toString()); |
| 407 | + fieldList.add(fieldInfo2); |
| 408 | + }); |
| 409 | + }else if(jsonObject.get(jsonField) instanceof JSONObject){ |
| 410 | + jsonObject.getJSONObject(jsonField).keySet().stream().forEach(arrayObject->{ |
| 411 | + FieldInfo fieldInfo2 = new FieldInfo(); |
| 412 | + fieldInfo2.setFieldName(arrayObject.toString()); |
| 413 | + fieldInfo2.setColumnName(arrayObject.toString()); |
| 414 | + fieldInfo2.setFieldClass(String.class.getSimpleName()); |
| 415 | + fieldInfo2.setFieldComment("children:"+arrayObject.toString()); |
| 416 | + fieldList.add(fieldInfo2); |
| 417 | + }); |
| 418 | + } |
| 419 | + }); |
| 420 | + if(fieldList.size()<1){ |
| 421 | + throw new CodeGenerateException("JSON解析失败"); |
| 422 | + } |
| 423 | + return fieldList; |
| 424 | + } |
380 | 425 | }
|
0 commit comments