在带有 sql 插值的子句中

In clauses with sql interpolation

我可以在 ScalikeJDBC 的 SQL 插值中使用子句吗?例如

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

这失败了,因为 ids 没有被插值。

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

这也失败了,因为表达式被解释为字符串,而不是数字列表。例如... where id in ('1,2,3,5')

我还没弄清楚你的问题,但插值设置值应该可以。

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

像这样:

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)