Hi Max

Thank you for your request. I do see that this is not yet properly
documented in the manual, although it does deserve a full section. I've
registered a feature request for this:
https://github.com/jOOQ/jOOQ/issues/4931

In general, you should never really need to have any local variables of a
XXXStep type. For example, your dynamic JOIN clause:


2016-01-11 19:55 GMT+01:00 Max Kremer <[email protected]>:

>
>
> TableOnConditionStep fromClause = table("B")
>  .join(table("B")).on("A.id = B.id");
>
>
> if (joinTableC == true){
>     fromClause = fromClause.join(table("C")).on("B.id = C.d");
> }
>
>
> dsl.select(    ...  )
> .from( fromClause );
>
>
>
Instead, write:

Table<?> table = table("A")...


This isn't much of an immprovement in terms of lines of code. But by using
the Table<?> type, you indicate much more clearly what the type really
means, than if you were using the TableOnConditionStep type.


> Another example
>
> SelectOnConditionStep statement  = select(Temperatures.VALUE, Temperatures
> .TIMESTAMP)
>   .from(Temperatures)
>   .where(Temperatures.VALUE.greaterOrEqual(0));
>
>
> if (filter.hasMaxValue())
>   statement.and(Temperatures.VALUE.lessOrEqual(filter.getMaxValue());
>
>
> if (filter.hasLocation())
>   statement.and(Temperatures.LOCATION.equal(filter.getLocation());
>
>
> // And so on with the rest of filters to end with:
> statement.orderBy(Temperature.TIMESTAMP)
>   .fetch();
>
>
>
Here, the problem is more obvious. Why add predicates dynamically to the
weird SelectOnConditionStep, when you can construct a predicate rather
easily as follows:

Condition condition = Temperatures.VALUE.ge(0);

if (filter.hasMaxValue())
    condition = condition.and(Temperatures.VALUE.le(filter.getMaxValue());

if (filter.hasLocation())
    condition =
condition.and(Temperatures.LOCATION.eq(filter.getLocation());

// Only now construct the query:
select(Temperatures.VALUE, Temperatures.TIMESTAMP)
.from(Temperatures)
.where(condition)
.orderBy(Temperatures.TIMESTAMP)
.fetch();


Of course, you're completely free how and where to construct your query
parts. For instance, you could be doing this instead:


public Condition condition(Filter filter) {
    Condition condition = Temperatures.VALUE.ge(0);

    if (filter.hasMaxValue())
        condition =
condition.and(Temperatures.VALUE.le(filter.getMaxValue());

    if (filter.hasLocation())
        condition =
condition.and(Temperatures.LOCATION.eq(filter.getLocation());

    return condition;
}

// Only now construct the query:
select(Temperatures.VALUE, Temperatures.TIMESTAMP)
.from(Temperatures)
.where(condition(filter))
.orderBy(Temperatures.TIMESTAMP)
.fetch();


The once pretty fluent style becomes chopped up and hard to follow. The
> blog post here
> <http://www.programania.net/diseno-de-software/functional-trick-to-compose-conditions-in-jooq/>
> discusses this in more detail and presents an elegant solution. I'd like to
> hear how other people are handling this... Also wondering if future
> versions of JOOQ will have this in mind,
>

The blog post you linked shows a nice functional approach, which is roughly
equivalent to the procedural approach that I've shown. We do have a new
feature for jOOQ 3.8, where we support the "Query by Example" pattern:
https://github.com/jOOQ/jOOQ/issues/4735

This will allow to construct a Condition from a Record via
DSL.condition(record). It will work only to some extent for you, because
Query by Example can only build Field.equal() predicates.

Another approach would be to allow for jOOQ API to "leak" into your UI. In
the end, you might not be gaining that much from duplicating everything you
already have in your database schema into a Filter object that is
essentially just the same as a jOOQ Condition. That would certainly save
you some work, although I do appreciate that some people cargo cult the
clean three-tier layering approach, in case of which my suggestion would be
a heresy :-)

I hope this helps. If you see any possible improvements for jOOQ, just let
us know. There's always an improvement to be made!
Lukas

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to