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.


 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)
 }

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).
    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"