问题遇到的现象和发生背景
想利用jexlEngine将sql当成代码执行,遇到格式化转换问题;
jexlEngine的等号(=)要两个等号(==)才能正常转换,当where条件是(==)则正常,如果是一个(=)或者(= =)中间有空格,我怎么判断将=好转两个==
遇到的现象和发生背景
代码如下:怎么修改才能判断到是一个=还是两个=,怎么去 = = 中间的空格?
String where = "pay_time >= '2019/10/21' and pay_time <= '2019/10/22' and pay_time = = '2019/10/22'";
public static String formatWhere(String where){
if (where.contains("=")) {
// where = where.replace(" ", "");
List<Integer> strAllIndex = getStrAllIndex(where, "=");
for (int i = 0; i < strAllIndex.size(); i++) {
Integer index = strAllIndex.get(i);
String targetBefore = Character.toString(where.charAt(index - 1));
String targetEnd = Character.toString(where.charAt(index + 1));
if (!">".equals(targetBefore) && !"<".equals(targetBefore) && !"=".equals(targetBefore) && !"!".equals(targetBefore)) {
if (!"=".equals(targetEnd)) {
where = where.substring(0, index) + "==" + where.substring(index + 1);
}
}
}
}
if (where.contains(" and ")) {
where = where.replace(" and ", " && ");
}
if (where.contains(" AND ")) {
where = where.replace(" AND ", " && ");
}
if (where.contains(" or ")) {
where = where.replace(" or ", " || ");
}
if (where.contains(" OR ")) {
where = where.replace(" OR ", " || ");
}
// System.out.println("where:" + where);
// return where;
}