In articles with sql interpolation

Can I use in-clauses with Interlolation SQL ScalikeJDBC? eg.

val ids = Set(1,2,3,5)
sql"""update foo set bar=${bar} where id in ${ids}""".update().apply()

      

It doesn't work because it ids

doesn't interpolate.

sql"""update foo set bar=${bar} where id in (${ids.mkString(",")})""".update().apply()

      

This also fails because the expression is intepreted as a String, not a list of numbers. eg... where id in ('1,2,3,5')

+3


source to share


1 answer


I didn't understand your problem, but interpolating the Set value should work.

libraryDependencies ++= Seq(
  "org.scalikejdbc" %% "scalikejdbc"       % "2.2.6",
  "com.h2database"  %  "h2"                % "1.4.187",
  "ch.qos.logback"  %  "logback-classic"   % "1.1.3"
)

      



like this:

scala> import scalikejdbc._
import scalikejdbc._

scala> val ids = Set(1,2,3,5)
ids: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 5)

scala> val s = sql"""update foo set bar=1 where id in (${ids})"""
s: scalikejdbc.SQL[Nothing,scalikejdbc.NoExtractor] = scalikejdbc.SQLToTraversableImpl@633229c7

scala> s.statement
res1: String = update foo set bar=1 where id in (?, ?, ?, ?)

scala> s.parameters
res2: Seq[Any] = List(1, 2, 3, 5)

      

+5


source







All Articles