+ * 忽略大小写 + **考虑增加这个类是, 如果在 StringUtils 中加工具方法, 使用起来代码非常冗长且不方便 + * @author Nisus + * @see String + */ +@AllArgsConstructor +public class NonCaseString implements CharSequence { + private String value; + + public static NonCaseString of(String str) { + assert str != null; + return new NonCaseString(str); + } + + + /** + * {@link String#indexOf(String)} 增强, 忽略大小写 + */ + public int indexOf(String m) { + String text = this.value; + if (text == null || m == null || text.length() < m.length()) { + return -1; + } + return text.toLowerCase().indexOf(m.toLowerCase()); + } + + /** + * {@link String#lastIndexOf(String)} 增强, 忽略大小写 + */ + public int lastIndexOf(String m) { + String text = this.value; + if (text == null || m == null || text.length() < m.length()) { + return -1; + } + return text.toLowerCase().lastIndexOf(m.toLowerCase()); + } + + /** + * 是否包含, 大小写不敏感 + *
true + * "abcxyz" 包含 "ABC" => true + * "abcxyz" 包含 "aBC" => true + *+ * + * @param m 被包含字符串 + */ + public boolean contains(String m) { + String text = this.value; + if (text.length() < m.length()) { + return false; + } + return text.toLowerCase().contains(m.toLowerCase()); + } + + /** + * 任意一个包含返回true + *
+ * containsAny("abcdef", "a", "b) + * 等价 + * "abcdef".contains("a") || "abcdef".contains("b") + *+ * + * @param matchers 多个要判断的被包含项 + */ + public boolean containsAny(String... matchers) { + for (String matcher : matchers) { + if (contains(matcher)) { + return true; + } + } + return false; + } + + /** + * 所有都包含才返回true + * + * @param matchers 多个要判断的被包含项 + */ + public boolean containsAllIgnoreCase(String... matchers) { + for (String matcher : matchers) { + if (contains(matcher) == false) { + return false; + } + } + return true; + } + + public NonCaseString trim() { + return NonCaseString.of(this.value.trim()); + } + + public NonCaseString replace(char oldChar, char newChar) { + return NonCaseString.of(this.value.replace(oldChar, newChar)); + } + + public NonCaseString replaceAll(String regex, String replacement) { + return NonCaseString.of(this.value.replaceAll(regex, replacement)); + } + + public NonCaseString substring(int beginIndex) { + return NonCaseString.of(this.value.substring(beginIndex)); + } + + public NonCaseString substring(int beginIndex, int endIndex) { + return NonCaseString.of(this.value.substring(beginIndex, endIndex)); + } + + public boolean isNotEmpty() { + return !this.value.isEmpty(); + } + + @Override + public int length() { + return this.value.length(); + } + + @Override + public char charAt(int index) { + return this.value.charAt(index); + } + + + @Override + public CharSequence subSequence(int start, int end) { + return this.value.subSequence(start, end); + } + + public String[] split(String regex) { + return this.value.split(regex); + } + + + /** + * @return 原始字符串 + */ + public String get() { + return this.value; + } + + @Override + public String toString() { + return this.value; + } +} diff --git a/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java b/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java index f123ed01..061d94ae 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java +++ b/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java @@ -40,7 +40,7 @@ public static String underlineToCamelCase(String underscoreName) { boolean flag = false; for (int i = 0; i < underscoreName.length(); i++) { char ch = underscoreName.charAt(i); - if ("_".charAt(0) == ch) { + if ('_' == ch) { flag = true; } else { if (flag) { @@ -54,6 +54,42 @@ public static String underlineToCamelCase(String underscoreName) { } return result.toString(); } + + /** + * 转 user_name 风格 + * + * 不管原始是什么风格 + */ + public static String toUnderline(String str, boolean upperCase) { + if (str == null || str.trim().isEmpty()) { + return str; + } + + StringBuilder result = new StringBuilder(); + boolean preIsUnderscore = false; + for (int i = 0; i < str.length(); i++) { + char ch = str.charAt(i); + if (ch == '_') { + preIsUnderscore = true; + } else if (ch == '-') { + ch = '_'; + preIsUnderscore = true; // -A -> _a + } else if (ch >= 'A' && ch <= 'Z') { + // A -> _a + if (!preIsUnderscore && i > 0) { // _A -> _a + result.append("_"); + } + preIsUnderscore = false; + } else { + preIsUnderscore = false; + } + result.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch)); + } + + return result.toString(); + } + + public static boolean isNotNull(String str){ return org.apache.commons.lang3.StringUtils.isNotEmpty(str); } diff --git a/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java b/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java index 979a8a04..7104ad83 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java +++ b/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java @@ -1,18 +1,16 @@ package com.softdev.system.generator.util; -import com.softdev.system.generator.util.mysqlJavaTypeUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.softdev.system.generator.entity.ClassInfo; import com.softdev.system.generator.entity.FieldInfo; +import com.softdev.system.generator.entity.NonCaseString; import com.softdev.system.generator.entity.ParamInfo; import java.io.IOException; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; @@ -34,7 +32,7 @@ public class TableParseUtil { public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo) throws IOException { //process the param - String tableSql = paramInfo.getTableSql(); + NonCaseString tableSql = NonCaseString.of(paramInfo.getTableSql()); String nameCaseType = MapUtil.getString(paramInfo.getOptions(),"nameCaseType"); Boolean isPackageType = MapUtil.getBoolean(paramInfo.getOptions(),"isPackageType"); @@ -42,15 +40,20 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo) throw new CodeGenerateException("Table structure can not be empty. 表结构不能为空。"); } //deal with special character - tableSql = tableSql.trim().replaceAll("'", "`").replaceAll("\"", "`").replaceAll(",", ",").toLowerCase(); + tableSql = tableSql.trim() + .replaceAll("'", "`") + .replaceAll("\"", "`") + .replaceAll(",", ",") + // 这里全部转小写, 会让驼峰风格的字段名丢失驼峰信息(真有驼峰sql字段名的呢(* ̄︶ ̄)); 下文使用工具方法处理包含等 + // .toLowerCase() + ; //deal with java string copy \n" tableSql = tableSql.trim().replaceAll("\\\\n`", "").replaceAll("\\+", "").replaceAll("``", "`").replaceAll("\\\\", ""); // table Name String tableName = null; - if (tableSql.contains("TABLE") && tableSql.contains("(")) { - tableName = tableSql.substring(tableSql.indexOf("TABLE") + 5, tableSql.indexOf("(")); - } else if (tableSql.contains("table") && tableSql.contains("(")) { - tableName = tableSql.substring(tableSql.indexOf("table") + 5, tableSql.indexOf("(")); + int tableKwIx = tableSql.indexOf("TABLE"); // 包含判断和位置一次搞定 + if (tableKwIx > -1 && tableSql.contains("(")) { + tableName = tableSql.substring(tableKwIx + 5, tableSql.indexOf("(")).get(); } else { throw new CodeGenerateException("Table structure incorrect.表结构不正确。"); } @@ -88,9 +91,11 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo) String classComment = null; //mysql是comment=,pgsql/oracle是comment on table, //2020-05-25 优化表备注的获取逻辑 - if (tableSql.contains("comment=") || tableSql.contains("comment on table")) { - String classCommentTmp = (tableSql.contains("comment=")) ? - tableSql.substring(tableSql.lastIndexOf("comment=") + 8).trim() : tableSql.substring(tableSql.lastIndexOf("comment on table") + 17).trim(); + if (tableSql.containsAny("comment=", "comment on table")) { + int ix = tableSql.lastIndexOf("comment="); + String classCommentTmp = (ix > -1) ? + tableSql.substring(ix + 8).trim().get() : + tableSql.substring(tableSql.lastIndexOf("comment on table") + 17).trim().get(); if (classCommentTmp.contains("`")) { classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("`") + 1); classCommentTmp = classCommentTmp.substring(0, classCommentTmp.indexOf("`")); @@ -109,7 +114,7 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo) List