如何使用带有光滑TSQL插补器的案例类?

时间:2016-05-11 21:17:08

标签: scala slick

sql interporlator允许案例类作为给定GetResult实例的结果

val action:DBIO[Seq[VisibilityRow]] = sql"SELECT * FROM visibility".as[VisibilityRow] //compiles fine

Checking the docs,我没有看到将tsql与案例类一起使用的示例。

我决定尝试一下

val action2:DBIO[Seq[VisibilityRow]] = tsql"SELECT * FROM visibility"

但是我收到错误

[error] ... : type mismatch;
[error]  found   : slick.profile.SqlStreamingAction[Vector[(Long, String, java.sql.Timestamp, java.sql.Timestamp, java.sql.Timestamp)],(Long, String, java.sql.Timestamp, java.sql.Timestamp, java.sql.Timestamp),slick.dbio.Effect]
[error]  required: slick.driver.PostgresDriver.api.DBIO[Seq[com.fevo.slick.Schema.VisibilityRow]]
[error]     (which expands to)  slick.dbio.DBIOAction[Seq[com.fevo.slick.Schema.VisibilityRow],slick.dbio.NoStream,slick.dbio.Effect.All]
[error]     val action2:DBIO[Seq[VisibilityRow]] = tsql"SELECT * FROM visibility"

我是否有一种优雅的方法可以使用tsql提取这些类型,我可以映射应用但是我仍然需要指定(和更新)DBIO参数的元组类型相应

1 个答案:

答案 0 :(得分:2)

如果案例类构造函数与您的字段匹配,您可以简单地映射结果以获得所需的类型:

val action2:DBIO[Seq[VisibilityRow]] = 
  tsql"SELECT * FROM visibility".map(_.map(VisibilityRow.apply.tupled))