滴滴撕滴颩 2022-12-19 11:38 采纳率: 57.1%
浏览 46
已结题

Java格式化mysql语句问题

问题遇到的现象和发生背景

想利用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;

}
  • 写回答

2条回答 默认 最新

  • bingbingyihao 2022-12-19 11:48
    关注

    回答:实际判断 "= =",非常简单,且转换起来也很容易,只需要得到“=”的下标,并判断在"="后面接着出现的是否是" ="即可,如果是的话,就将这三个下标改为"=="即可,这里的话需要用到字符串拼接了,拼接出新的字符串,采用StringBuffer即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月28日
  • 已采纳回答 12月20日
  • 创建了问题 12月19日

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?