Scala slick query where in list

后端 未结 4 1415
忘掉有多难
忘掉有多难 2021-02-03 22:57

I am attempting to learn to use Slick to query MySQL. I have the following type of query working to get a single Visit object:

Q.query[(Int,Int), Visit](\"\"\"
          


        
4条回答
  •  长情又很酷
    2021-02-03 23:14

    If you have a complex query and the for comprehension mentioned above is not an option, you can do something like the following in Slick 3. But you need to make sure you validate the data in your list query parameter yourself to prevent SQL injection:

    val locationCodes = "'" + List("loc1","loc2","loc3").mkString("','") + "'"
    sql"""
      select * from visit where visitor = $visitor 
        and location_code in (#$locationCodes)
    """
    

    The # in front of the variable reference disables the type validation and allows you to solve this without supplying a function for the implicit conversion of the list query parameter.

提交回复
热议问题