@Test public void dealSqlCommentTest() { String testSQLWithComment = "CREATE TABLE MyTable --this is a comment\n"; Assert.assertEquals("CREATE TABLE MyTable ", DtStringUtil.dealSqlComment(testSQLWithComment)); testSQLWithComment = "CREATE TABLE 'MyTable--' --this is a comment"; Assert.assertEquals("CREATE TABLE 'MyTable--' ", DtStringUtil.dealSqlComment(testSQLWithComment)); testSQLWithComment = "CREATE TABLE MyTable -- this is a '--comment'\n"; Assert.assertEquals("CREATE TABLE MyTable ", DtStringUtil.dealSqlComment(testSQLWithComment)); }
public static String dealSqlComment(String sql) { boolean inQuotes = false; boolean inSingleQuotes = false; int bracketLeftNum = 0; StringBuilder b = new StringBuilder(sql.length()); char[] chars = sql.toCharArray(); for (int index = 0; index < chars.length; index ++) { if (index == chars.length) { return b.toString(); } StringBuilder tempSb = new StringBuilder(2); if (index > 1) { tempSb.append(chars[index - 1]); tempSb.append(chars[index]); } if (tempSb.toString().equals("--")) { if (inQuotes) { b.append(chars[index]); } else if (inSingleQuotes) { b.append(chars[index]); } else if (bracketLeftNum > 0) { b.append(chars[index]); } else { b.deleteCharAt(b.length() - 1); while (chars[index] != '\n') { if (index == chars.length - 1) { break; } index++; } } } else if (chars[index] == '\"' && '\\' != chars[index] && !inSingleQuotes) { inQuotes = !inQuotes; b.append(chars[index]); } else if (chars[index] == '\'' && '\\' != chars[index] && !inQuotes) { inSingleQuotes = !inSingleQuotes; b.append(chars[index]); } else { b.append(chars[index]); } } return b.toString(); }
DtStringUtil { public static String dealSqlComment(String sql) { boolean inQuotes = false; boolean inSingleQuotes = false; int bracketLeftNum = 0; StringBuilder b = new StringBuilder(sql.length()); char[] chars = sql.toCharArray(); for (int index = 0; index < chars.length; index ++) { if (index == chars.length) { return b.toString(); } StringBuilder tempSb = new StringBuilder(2); if (index > 1) { tempSb.append(chars[index - 1]); tempSb.append(chars[index]); } if (tempSb.toString().equals("--")) { if (inQuotes) { b.append(chars[index]); } else if (inSingleQuotes) { b.append(chars[index]); } else if (bracketLeftNum > 0) { b.append(chars[index]); } else { b.deleteCharAt(b.length() - 1); while (chars[index] != '\n') { if (index == chars.length - 1) { break; } index++; } } } else if (chars[index] == '\"' && '\\' != chars[index] && !inSingleQuotes) { inQuotes = !inQuotes; b.append(chars[index]); } else if (chars[index] == '\'' && '\\' != chars[index] && !inQuotes) { inSingleQuotes = !inSingleQuotes; b.append(chars[index]); } else { b.append(chars[index]); } } return b.toString(); } }
DtStringUtil { public static String dealSqlComment(String sql) { boolean inQuotes = false; boolean inSingleQuotes = false; int bracketLeftNum = 0; StringBuilder b = new StringBuilder(sql.length()); char[] chars = sql.toCharArray(); for (int index = 0; index < chars.length; index ++) { if (index == chars.length) { return b.toString(); } StringBuilder tempSb = new StringBuilder(2); if (index > 1) { tempSb.append(chars[index - 1]); tempSb.append(chars[index]); } if (tempSb.toString().equals("--")) { if (inQuotes) { b.append(chars[index]); } else if (inSingleQuotes) { b.append(chars[index]); } else if (bracketLeftNum > 0) { b.append(chars[index]); } else { b.deleteCharAt(b.length() - 1); while (chars[index] != '\n') { if (index == chars.length - 1) { break; } index++; } } } else if (chars[index] == '\"' && '\\' != chars[index] && !inSingleQuotes) { inQuotes = !inQuotes; b.append(chars[index]); } else if (chars[index] == '\'' && '\\' != chars[index] && !inQuotes) { inSingleQuotes = !inSingleQuotes; b.append(chars[index]); } else { b.append(chars[index]); } } return b.toString(); }  }
DtStringUtil { public static String dealSqlComment(String sql) { boolean inQuotes = false; boolean inSingleQuotes = false; int bracketLeftNum = 0; StringBuilder b = new StringBuilder(sql.length()); char[] chars = sql.toCharArray(); for (int index = 0; index < chars.length; index ++) { if (index == chars.length) { return b.toString(); } StringBuilder tempSb = new StringBuilder(2); if (index > 1) { tempSb.append(chars[index - 1]); tempSb.append(chars[index]); } if (tempSb.toString().equals("--")) { if (inQuotes) { b.append(chars[index]); } else if (inSingleQuotes) { b.append(chars[index]); } else if (bracketLeftNum > 0) { b.append(chars[index]); } else { b.deleteCharAt(b.length() - 1); while (chars[index] != '\n') { if (index == chars.length - 1) { break; } index++; } } } else if (chars[index] == '\"' && '\\' != chars[index] && !inSingleQuotes) { inQuotes = !inQuotes; b.append(chars[index]); } else if (chars[index] == '\'' && '\\' != chars[index] && !inQuotes) { inSingleQuotes = !inSingleQuotes; b.append(chars[index]); } else { b.append(chars[index]); } } return b.toString(); }  static List<String> splitIgnoreQuota(String str, char delimiter); static List<String> splitField(String str); static String replaceIgnoreQuota(String str, String oriStr, String replaceStr); static String dealSqlComment(String sql); static String col2string(Object column, String type); static String getPluginTypeWithoutVersion(String engineType); static String addJdbcParam(String dbUrl, Map<String, String> addParams, boolean isForce); static boolean isJson(String str); static Object parse(String str,Class clazz); static String firstUpperCase(String str); static String getTableFullPath(String schema, String tableName); static String rmStrQuote(String str); static String addQuoteForStr(String column); static String getStartQuote(); static String getEndQuote(); }
DtStringUtil { public static String dealSqlComment(String sql) { boolean inQuotes = false; boolean inSingleQuotes = false; int bracketLeftNum = 0; StringBuilder b = new StringBuilder(sql.length()); char[] chars = sql.toCharArray(); for (int index = 0; index < chars.length; index ++) { if (index == chars.length) { return b.toString(); } StringBuilder tempSb = new StringBuilder(2); if (index > 1) { tempSb.append(chars[index - 1]); tempSb.append(chars[index]); } if (tempSb.toString().equals("--")) { if (inQuotes) { b.append(chars[index]); } else if (inSingleQuotes) { b.append(chars[index]); } else if (bracketLeftNum > 0) { b.append(chars[index]); } else { b.deleteCharAt(b.length() - 1); while (chars[index] != '\n') { if (index == chars.length - 1) { break; } index++; } } } else if (chars[index] == '\"' && '\\' != chars[index] && !inSingleQuotes) { inQuotes = !inQuotes; b.append(chars[index]); } else if (chars[index] == '\'' && '\\' != chars[index] && !inQuotes) { inSingleQuotes = !inSingleQuotes; b.append(chars[index]); } else { b.append(chars[index]); } } return b.toString(); }  static List<String> splitIgnoreQuota(String str, char delimiter); static List<String> splitField(String str); static String replaceIgnoreQuota(String str, String oriStr, String replaceStr); static String dealSqlComment(String sql); static String col2string(Object column, String type); static String getPluginTypeWithoutVersion(String engineType); static String addJdbcParam(String dbUrl, Map<String, String> addParams, boolean isForce); static boolean isJson(String str); static Object parse(String str,Class clazz); static String firstUpperCase(String str); static String getTableFullPath(String schema, String tableName); static String rmStrQuote(String str); static String addQuoteForStr(String column); static String getStartQuote(); static String getEndQuote();  }