Sometimes is very important surround a sql statement between braces an example in DB2 could be
select union select
if you use
select fetch first union select fitch first DB2 complains about wrong syntax.
if you do
(select fetch) union (select fetch) it's OK
you can also do
(select fetch) union (select fetch) fetch
Because each DB could have a different behaviou, having an embrace instruction
public Query embrace (Query) can give the correct flexibility to statements generations
In this specific case it could be achived via Subquery operator
Another idea is the possibility that a a query has a flag embraced, if flagged the result query is outputted between braces.
I think the Subquery object provides the functionality you are looking for (as you found).
OK for examplpe how to use it correctly Subquery generate a SubQuery object, if I need to use it inside a union.
I have
SelectQuery q1=method_that_return_a_query_obj("p1")
SelectQuery q2=method_that_return_a_query_obj("p2")
UnionQuery unionQuery = UnionQuery.unionAll(q1, q2);
If I transform q1,q2 in Subquery obj I can't use them in UnionQuery.
Any Idea??
Ok this is a good option
It has a side effect
query.addCustomization(new DB2FetchFirstClause(limit));The first task could be accomplished via
Subquery sub = new Subquery(unionQuery);seems a good idea......BUTI need a specific OBJ for addCustomization and the method also return a Query object if I encapsulete query inside a Subquery() type can't be casted.
I'm sorry, i'm not following. could you provide a little more detail about what you are trying to accomplish and what isn't working?
I need to write
I can write the internal FETCH FIRST via query.addCustomization(new DB2FetchFirstClause(limit));
SELECT x,y FROM tableA-->SELECT x,y FROM tableA FETCH FIRST 200 ROWS ONLYSELECT u,v FROM tableB-->SELECT u,v FROM tableB FETCH FIRST 200 ROWS ONLYI can ADD parenthesis via new Subquery(q1) new Subquery(q2) new Subquery(unionQuery)
How to add the final fetch first to new Subquery(unionQuery)
Hmm, yeah, that's a gnarly problem. A Subquery isn't a customizable query object because it is an Expression, not a Query. i guess you would need a query "wrapper" of some sort which is a Query and customizable... hmmm.