personList = Person.withCriteria { if (params.firstName) eq('firstName', params.firstName).ignoreCase() if (params.lastName) eq('lastName', params.lastName).ignoreCase() if (params.login) eq('login', params.login).ignoreCase() if (params.personHashId) like('personHashId', params.personHashId) maxResults(Constants.SEARCH_MAX_RESULTS) }
Code Snippets, Ideas and Rants from a Tech Lead interested primarily in server side web development and leading teams who build web apps.
Tuesday, November 23, 2010
Case Insensitive withCriteria
I found this easy way to do a case insensitive query on the DB using with Criteria. Unfortunately Grails documentation does not mention this, but remember you are using Hibernate. So using .ignoreCase() on the eq works perfectly.
Wednesday, November 3, 2010
Elvis Operator
Is really neat and not used enough. Basically it is a modified ternary.
Here is and example for returning a sensible value if an expressions resolves to null (or false).
Here is and example for returning a sensible value if an expressions resolves to null (or false).
def a = b ?: "No Value"In that example a should equal b unless b is null or false, then it would be assigned "No Value"
Subscribe to:
Posts (Atom)