Query - How to change where ExpressionList (not only add new Expressions)

197 views
Skip to first unread message

Ridcully

unread,
Nov 1, 2010, 4:39:06 PM11/1/10
to Ebean ORM
I did not find a possibility to actually change a once defined where()
expression of Query, other than adding additional expressions.
What I'd need is to define a Query with some base expressions, and
later on I'd like to add some expressions to the base, do something
with the query, and switch back to the base expression later on,
without the need to create the query from scratch.

I found the copy() method of the DefaultOrmQuery class, which I can
use for my needs, but I have to cast to the specific class, which is
not very clean coding. So is there an official way for this? I guess
something like Query.getWhere() and Query.setWhere() would be
sufficient.
If could cache the base where-expression using getWhere(), add
additional expressions, and later on reset everything using setWhere()
with the earlier cached expressionlist (well there would be some
copying of the expressionlist be involved somewhere too...)

Thanks, Robert

Rob Bygrave

unread,
Nov 2, 2010, 3:51:25 AM11/2/10
to eb...@googlegroups.com
>>
What I'd need is to define a Query with some base expressions, and
later on I'd like to add some expressions to the base, do something
with the query, and switch back to the base expression later on,
without the need to create the query from scratch.
<<

I'd say it a little differently ... in that you need a "prototype" query which has some existing expressions. You want to clone/copy the query (creating a new query instance) that you can then add more expressions to and then execute.

Note that I say "prototype" in that a Query is not threadsafe. You can't have multiple threads executing the same query instance.  So you create a Query that's sole purpose is to act as a "prototype" or starting point... which you can then copy/clone ... and then add extra expressions to the copied/cloned instance.

You kind of get this with "Named Queries".


 >> So is there an official way for this?

No, not yet - but making the query.copy() method public would be the approach.

>> Query.setWhere()

Possibly, although I think you would want to use expressionList.copy() in that case (and that isn't public either).


Ultimately I see this as a request to make query.copy() public (so that a query can act as a "prototype" to create other queries). I'll have another look at the code and confirm that that will happen.


Cheers, Rob.

Rob Bygrave

unread,
Nov 2, 2010, 4:13:59 AM11/2/10
to eb...@googlegroups.com
>> Ultimately I see this as a request to make query.copy() public

Ok, I had another review and I'm happy with this approach.  I have logged and Enhancement and will make query.copy() public.


Cheers, Rob.

Ridcully

unread,
Nov 2, 2010, 6:22:26 AM11/2/10
to Ebean ORM
Hi Rob, thanks a lot. Yes a prototype query is exactly what I need (my
description is a bit clumsy...).
The setWhere() idea was just that - an idea - but with the copy() the
prototype can also contain a having() clause etc.
So thanks again for the quick and (at least for me) perfect solution.
Cheers, Robert
Reply all
Reply to author
Forward
0 new messages