I have been using JOOQ and it seems to be a really useful library to generate SQL queries. However, I'm stuck at generating them dynamically. For example, the following is what I want to do (Note that I just want use this to do SQL building without any code generation)
DSLContext context = DSL.using(SQLDialect.DEFAULT);
List<Field<?>> select = getSelect(String fields ...); // gets the selection fields
Select query = create.select(select)
.from(name("table"));
if(where)
query = query.addWhere(Conditions...);
if(groupBy)
query = query.groupBy(List<Field<?>> ...);
if(orderBy)
query = query.orderBy(List<Field<?>> ...);
Unfortunately, I can only find a way to do them all in single step like below:
query.select()
.from(...)
.where(...)
.orderBy(...)
.fetch();
The above one could be useful if the .where, .orderBy can accept null and then not add those clauses to the SQL if they are null, but seems like that's not possible.
Here are my questions:
Is there a way to create a SELECTION query dynamically and then VALIDATE it without the use of any code generation as my use case involves just constructing a plain String SQL? This answer: https://stackoverflow.com/a/14053108/1787599f seems no longer valid as I don't find a Factory method in 3.6.x, and what is the best approach for this kind of use case?
While I can set values and do a .getSQL() to get a SQL with ?, there is another step of getting the bind variables for me to pass this on to PreparedStatement, can it be done in a single step?
How to integrate this with JDBCTemplate?