How to dynamically generate SQL query based on user’s selections?

后端 未结 5 1425
误落风尘
误落风尘 2021-01-22 18:56

This is the same question as:

How to dynamically generate SQL query based on user's selections?

The only difference is, that I\'m interested in seeing soluti

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-22 19:34

    In my code, I'm using AND and OR objects for this. They take lists as parameters (looks nice with Java 5's variadic arguments) and concatenate them in Strings with the necessary spaces and parentheses. Pseudo code:

    AND(WhereCond ... conds) { this.conds = conds; }
    toString() { return conds.length == 0 ? "" : "(" + join(conds, " AND ") + ")" };
    

    where join() converts the object array to string array and then joins the elements with the parameter.

提交回复
热议问题