I have a code that queries database using Grails criteria from groovy code. Below is only part of test code that adds where clause/condition to the criteria.
for (Map.Entry<String, String> entry : searchMap.entrySet()) {
String name = entry.getKey();
String city = entry.getValue();
eq("user.name", name)
eq("user.city", city)
}
I want the resulting WHERE
clause to look like,
WHERE ( user.name = ? and user.city = ?)
OR ( user.name = ? and user.city = ?)
OR ( user.name = ? and user.city = ?)
.......
But my code always generates WHERE
clause in the format,
WHERE ( user.name = ? and user.city = ?)
AND ( user.name = ? and user.city = ?)
AND ( user.name = ? and user.city = ?)
.......
Basically I want OR
instead of AND
between the name, city column groups.
I tried solutions like,
or {
eq("user.name", name)
eq("user.city", city)
}
or {
and {
eq("user.name", name)
eq("user.city", city)
}
}
But nothing replaces AND
with OR
.
How can I achieve that.
EDIT
With,
or {
eq("user.name", name)
eq("user.city", city)
}
the WHERE
clause is generated as,
WHERE ( user.name = ? and user.city = ?)
AND ( user.name = ? OR user.city = ?)
AND ( user.name = ? OR user.city = ?)
.......
which is exacly opposite of what I need.
Hope this will be a solution for you. You have to mention
and
block explicitly.output
As your code