Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use IN clause in plain sql Slick?

Tags:

For example, I want to create the following query:

SELECT c.* FROM Coffees c WHERE c.name IN ('robusta', 'arabica') 

My attempt failed:

val cnames = List("robusta", "arabica") sql""" SELECT c.* FROM Coffees c WHERE c.name IN ${cnames} """   could not find implicit value for parameter pconv:    scala.slick.jdbc.SetParameter[List[String]] 

Is it possible to somehow use in clause in Slick plain sql queries?

like image 499
Rogach Avatar asked Jul 01 '13 15:07

Rogach


2 Answers

The type safe "lifted embedding" API supports this as well:

val ids = List(1,2,3) val q = for {   f <- Foo if f.id inSet ids // ids is not bound } 

slick.typesafe.com/doc/1.0.1/api/index.html#scala.slick.lifted.ColumnExtensionMethods

like image 167
cvogt Avatar answered Sep 18 '22 08:09

cvogt


Although it's not safe for SQL injection, you can use #$ interpolator:

val ids = idList.mkString("'", "','", "'") val q = sql"""select name from mytable where id in (#$ids)""" 
like image 23
juanignaciosl Avatar answered Sep 18 '22 08:09

juanignaciosl