[ebean] v2.7 API Change for Conjunction / Disjunction (AND/OR) in expression API

461 views
Skip to first unread message

Rob Bygrave

unread,
May 20, 2010, 7:05:19 AM5/20/10
to ebean@googlegroups
I have made a change to the expression API for Conjunction (List of expressions added with AND) and Disjunction (List of expression added with OR).

Example:

List<Customer> list = server.find(Customer.class)
        .where().disjunction()
                    .startsWith("name", "r")
                    .eq("anniversary", onAfter)
                    .eq("status", Customer.Status.ACTIVE)
                    .endJunction()

         .where().gt("id", 10)

        .orderBy().asc("name")
        .findList();


So the above could be read as:

where ( name like r% OR
anniversary = :onAfter OR status = Customer.Status.ACTIVE) and ( id > 10)


So any expression after
disjunction() is added to that disjunction expression list until:

-
endJunction() .... to take you to the 'parent' expression list.

or

- where() ... to always take you to the 'root level' expression list (where clause).




Previously you had to get the ExpressionFactory and use that explicitly (and you can still do that). I think this makes using 
disjunction() and conjunction() much much easier to use (as long as people can understand what endJunction() means).



NOTE: This is logged as http://www.avaje.org/bugdetail-299.html


Cheers, Rob.


Daryl Stultz

unread,
May 20, 2010, 8:40:16 AM5/20/10
to Ebean ORM


On May 20, 7:05 am, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> I have made a change to the expression API for Conjunction (List of
> expressions added with AND) and Disjunction (List of expression added with
> OR).
>

I haven't gotten to using "OR" yet, but I was wondering how the
grouping would be managed. How would one do this:

where
(
(name like r% AND anniversary = :onAfter)
OR
(status = Customer.Status.ACTIVE AND id > 10)
)


/Daryl

Rob Bygrave

unread,
May 20, 2010, 5:06:15 PM5/20/10
to eb...@googlegroups.com

So this is an example with 'inner' AND/conjunctions .... wrapped in a OR/disjunction.

So again for people reading this:

disjunction = a list of expressions added together with OR
conjunction = a list of expressions added together with AND



    public void test() {
       
        ResetBasicData.reset();
       
        java.sql.Date onAfter = java.sql.Date.valueOf("2009-08-31");

        Query<Customer> q = Ebean.find(Customer.class)
            .setUseIndex(UseIndex.NO)
                .where()
                  .disjunction()
                    .conjunction()

                      .startsWith("name", "r")
                      .eq("anniversary", onAfter)
                      .endJunction()
                    .conjunction()
                      .eq("status", Customer.Status.ACTIVE)
                      .gt("id", 0)
                      .endJunction()
                .orderBy().asc("name");
       
        List<Customer> customers = q.findList();
        String sql = q.getGeneratedSql();

        ...
}

Results in:


<sql summary='Customer' >
select c.id c0, c.status c1, c.name c2, c.smallnote c3, c.anniversary c4, c.cretime c5, c.updtime c6, c.billing_address_id c7, c.shipping_address_id c8
from o_customer c
where ((c.name like ?  and c.anniversary = ? )  or (c.status = ?  and c.id > ? ) ) 
order by c.name
</sql>



Cheers, Rob.

Rob Bygrave

unread,
May 20, 2010, 5:09:18 PM5/20/10
to eb...@googlegroups.com

Please ignore the .setUseIndex(UseIndex.NO) ... I'll explain that later but in short Ebean 2.7 will be able to transparently hit the lucene index or the database and that gives you explicit control over whether you want to use the lucene index.

Daryl Stultz

unread,
May 20, 2010, 8:38:56 PM5/20/10
to Ebean ORM

On May 20, 5:06 pm, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> So this is an example with 'inner' AND/conjunctions .... wrapped in a
> OR/disjunction.

Good example, Rob, thanks.

/Daryl
Reply all
Reply to author
Forward
0 new messages