SQLParser之后得到AST(抽象语法树)之后,可以修改AST来添加条件。Druid在1.0.15版本后提供了一个易于使用的工具类方法:
com.alibaba.druid.sql.SQLUtils.addCondition(String, String, String)
使用如下:
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", null));
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", null));