Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-Insensitive String Comparison in Slick?

Tags:

orm

scala

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.

like image 328
Brent Faust Avatar asked May 22 '13 19:05

Brent Faust


People also ask

How do you compare two string cases insensitive?

Comparing strings in a case insensitive manner means to compare them without taking care of the uppercase and lowercase letters. To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function. Example 1: This example uses toUpperCase() function to compare two strings.

Which will do case insensitive comparison of string contents?

strcasecmp() — Case-insensitive string comparison.

Are string comparisons case-sensitive?

CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.

Is JavaScript match case insensitive?

Case-insensitive: It means the text or typed input that is not sensitive to capitalization of letters, like “Geeks” and “GEEKS” must be treated as same in case-insensitive search. In Javascript, we use string. match() function to search a regexp in a string and match() function returns the matches, as an Array object.


1 Answers

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.

like image 178
Cristian Boariu Avatar answered Oct 03 '22 13:10

Cristian Boariu