Brent Foust Brent Foust - 3 months ago 18
Scala Question

Case-Insensitive String Comparison in Slick?

What is the proper way of finding the one User matching username?

With user-defined type User:

case class User (userId: String, username: String)

object User extends Table[User]("user") {
def userId = column[String]("userId", O.PrimaryKey)
def username = column[String]("username")
def * = userId ~ authId ~ username <>(User.apply _, User.unapply _)

Database.forDataSource(DB.getDataSource()) withSession {
implicit session: Session =>

val q = for { u <- User if u.username.equalsIgnoreCase(someUsername) }
yield u
q.headOption


user.username is of type Column[String] which has no conversion to String.

What is desired is to have the Database do the string-insensitive comparison as part of the query.

Answer

I had a similar situation and solved it by using toLowerCase extension method:

p <- u.party if p.loginName.toLowerCase === partyName.toLowerCase

You can find here more extension methods, especially String ones.