Hi John

.and(yearRange != null ? APPOINTMENT_DATE.gt(yearRange) : expTrue())

Yeah, I remember trying this for Expressions as well initially.
It does get the job done but I was fussy and didn't like it for two reasons:

1. The first is that it results in "AND 1=1" being added to the SQL statement. I know it's silly but it bothers me, which means in six months or whatever when I'm trying to figure out some other bug and then I see this in some SQL statement in the log files that I'll waste time trying to figure out why it's there. I suppose I could just learn to ignore it.

2. The second is what does ".and( expTrue() )" actually mean in the relevant code section. It's simple in the abstract sense but if you're intently analyzing code one has to spend mental effort to now translate that into the contextual meaning/result. I can of course solve that by doing:

    var ANY_YEAR = expTrue();
exp = exp.and( yearRange != null ? APPOINTMENT_DATE.gt(yearRange) : ANY_YEAR );

Any way, so instead of hassling with all of the above I modified Expression so I could do:

.apply( yearRange != null, e -> e.and( APPOINTMENT_DATE.gt(yearRange) ) );

Which in my mind is clear, concise, and doesn't have side effects like "AND 1=1".

Thanks for this suggestion though.

Regards
Jurgen


------- Forwarded message -------
From: "John Huss" <johnth...@gmail.com>
To: dev@cayenne.apache.org
Cc:
Subject: Re: Fluent API addition request
Date: Mon, 08 Jul 2024 22:07:55 +0200

FWIW, when I need something like this for the WHERE part of a query I do it
with the ternary operator and a static import of ExpressionFactory (which
your IDE can add for you), like:

.and(yearRange != null ? APPOINTMENT_DATE.gt(yearRange) : expTrue())

I don't think there is a good alternative to handle prefetches, but this
handles the WHERE clause very well in my opinion.

Reply via email to