druid可以修改sql的查询字段和条件吗

zour9fqk  于 2022-10-22  发布在  Druid
关注(0)|答案(4)|浏览(680)

比如原sql是 "select a.user_id, a.user_name from table_a a where 1=1 and a.filter_Id='111' and a.filter_type='0'" 修改为 "select a.user_name from table_a a where 1=1 and a.filter_Id='111'" 。
可以去掉某个查询字段和条件

wnavrhmk

wnavrhmk1#

I don't know if druid can do this, but at lease you can do it without druid. If you are using Mybatis, you can add a interceptor to get the sql statement to run. Modify this sql with some sql parser framework (like jsqlparser). Then you will get the new sql.

For example, PageHelper use this technique to page

6yt4nkrj

6yt4nkrj2#

可以的,你要自己不过你要自己解析出 SQLSelectQueryBlock ,然后像下面的代码这样替换,应该条件的替换或去除也有类似这样的方法。你可以解析出原来的查询项,然后去掉你不要的重新设置查询项

SQLSelectQuery tempSelectQuery = (((SQLSelectStatement) parseStatements("select " + express + " as " + alias + " from temp1", JdbcConstants.MYSQL).iterator().next()).getSelect()).getQuery();
            SQLSelectQueryBlock tempDistinctColumnQuery = (SQLSelectQueryBlock) tempSelectQuery;
            SQLSelectItem distinctSelectItem = tempDistinctColumnQuery.getSelectList().iterator().next();
            sqlSelectQueryBlock.getSelectList().clear();
            sqlSelectQueryBlock.getSelectList().add(distinctSelectItem);
zu0ti5jz

zu0ti5jz3#

有没有visitor的例子可以参考。上述直接解析的话会有很多嵌套sql的场景考虑不到

相关问题