How to pass an array to a slick SQL plain query?

爷,独闯天下 提交于 2020-01-06 06:09:37

问题


How to pass an array to a slick SQL plain query? I tried as follows but it fails:

// "com.typesafe.slick" %% "slick" % "3.3.2",  // latest version
val ids = Array(1, 2, 3)
db.run(sql"""select name from person where id in ($ids)""".as[String])

Error: could not find implicit value for parameter e: slick.jdbc.SetParameter[Array[Int]]

However this ticket seems to say that it should work: https://github.com/tminglei/slick-pg/issues/131

Note: I am not interested in the following approach:

db.run(sql"""select name from person where id in #${ids.mkString("(", ",", ")")}""".as[Int])

回答1:


The issue you linked points to a commit which adds this:

def mkArraySetParameter[T: ClassTag](/* ... */): SetParameter[Seq[T]]
def mkArrayOptionSetParameter[T: ClassTag](/* ... */): SetParameter[Option[Seq[T]]]

Note that they are not implicit.

You'll need to do something like

implicit val setIntArray: SetParameter[Array[Int]] = mkArraySetParameter[Int](...)

and make sure that is in scope when you try to construct your sql"..." string.



来源:https://stackoverflow.com/questions/58306080/how-to-pass-an-array-to-a-slick-sql-plain-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!