how to write sql condition in jooq

185 views
Skip to first unread message

nan...@rcapl.com

unread,
Aug 28, 2014, 9:42:43 PM8/28/14
to jooq...@googlegroups.com
Hi,

i use jooq for data base manipulation.I want to write a select statement with custom where cause. Meaning conditions comes as map and i creating a where cause by iterating a map.data in the map change time to time. That's why i iterating the map and create where cause

Map =>{fromDate=>2014-05-10,toDate=>2014-06-10,userId=25,type=>STAFF}



then creating a where cause like below.
where (fromDate="2014-05-10" and toDate="2014-06-10" and userId=25 and type="STAFF")


So can i write a where cause inline.


Thank you
Amila

Lukas Eder

unread,
Aug 29, 2014, 1:26:33 AM8/29/14
to jooq...@googlegroups.com
Hello,

I'm not 100% sure what you mean by creating a where clause "inline". But probably, the best solution is to write a utility function like this:

public static Condition condition(Map<Field<?>, Object> map) {
    Condition result = DSL.trueCondition();

    for (Entry<Field<?>, Object> entry : map.entrySet()) {
        result = result.and(((Field) entry.getKey()).eq(entry.getValue()));
    }

    return result;
}

You can then, "inline" that condition into your jOOQ statement:

DSL.using(configuration)
   .select(...)
   .from(...)
   .where(condition(map))
   .fetch();

As a matter fo fact, I think we might want to add such a method to the jOOQ API. This could be useful for a lot of people.

Let me know if this is what you had in mind.

Cheers
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 jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages