SQLParser之后得到AST(抽象语法树)之后,可以修改AST来添加条件。Druid在1.0.15版本后提供了一个易于使用的工具类方法:

    1. com.alibaba.druid.sql.SQLUtils.addCondition(String, String, String)

    使用如下:

    1. Assert.assertEquals("SELECT *" //
    2. + "\nFROM t" //
    3. + "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", null));
    4. Assert.assertEquals("SELECT *" //
    5. + "\nFROM t" //
    6. + "\nWHERE id = 0" //
    7. + "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", null));