Slick/Scala:什么是 Rep[Bind] 以及如何将其转换为值? [英] Slick/Scala: What is a Rep[Bind] and how do I turn it into a value?

查看:73
本文介绍了Slick/Scala:什么是 Rep[Bind] 以及如何将其转换为值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出 Slick(Scala 函数关系模型).我已经开始在 Slick 3.0.0 中构建原型,但当然……大部分文档要么过时要么不完整.

I'm trying to figure out Slick (the Scala functional relational model). I've started to build a prototype in Slick 3.0.0 but of course... most of the documentation is either out of date or incomplete.

我已经达到了可以创建模式并从数据库返回对象的程度.

I've managed to get to a point where I can create a schema and return an object from the database.

问题是,我返回的是Rep[Bind]",而不是我希望返回的对象.我不知道该怎么做这个值.例如,如果我尝试诸如 rep.countDistinct.result 之类的东西,我就会崩溃.

The problem is, what I'm getting back is a "Rep[Bind]" and not the object I would expect to get back. I can't figure out what to do this this value. For instance, if I try something like rep.countDistinct.result, I get a crash.

以下是代码的简要概述……为简洁起见,删除了一些:

Here's a quick synopsis of the code... some removed for brevity:

class UserModel(tag: Tag) extends Table[User](tag, "app_dat_user_t") {
    def id = column[Long]("n_user_id", O.PrimaryKey)
    def created = column[Long]("d_timestamp_created")

    def * = (id.?, created) <> (User.tupled, User.unapply)
}

case class User(id: Option[Long], created: Long)

val users = TableQuery[UserModel]

(users.schema).create

db.run(users += User(Option(1), 2))

println("ID is ... " + users.map(_.id)) // prints "Rep[Bind]"... huh?

val users = for (user <- users) yield user

println(users.map(_.id).toString) // Also prints "Rep[Bind]"...

我找不到解开"Rep 对象的方法,也找不到关于它是什么或如何使用它的任何明确说明.

I can't find a way to "unwrap" the Rep object and I can't find any clear explanation of what it is or how to use it.

推荐答案

Rep[] 是 slick 中使用的 Column[] 数据类型的替代.

users.map(_.id) 返回所有行的 Column('n_user_id') 值

users.map(_.id) returns values of the Column('n_user_id') for all rows

val result : Rep[Long] = users.map(_.id)

users.map(_.id) // => select n_user_id from app_dat_user_t;

获得的值的类型为 Column[Long] [ 现在是 Rep[Long] ].您不能直接打印上述 resultSet 的值,因为它不是任何 Scala 集合类型

The obtained value is of type Column[Long] [ which is now Rep[Long] ]. You cannot directly print values of the above resultSet as it is not of any scala collection type

  • 您可以先将其转换为一些 Scala 集合,然后将其打印为下面:

  • You can first convert it to some scala collection and then print it as below :

var idList : List[Long] = List()
users.map(_.id).forEach(id =>
idList = idList :+ id
)

println(idList)**//如果你需要一次打印所有的id

println(idList)** // if you need to print all ids at once

否则你可以简单地使用:

else you can simply use :

users.map(_.id).forEach(id =>
println(id)
) // print for each id

还有,

val users = TableQuery[UserModel] // => returns Query[UserModel, UserModel#TableElementType, Seq])

val users = for (user <- users) yield user // => returns Query[UserModel, UserModel#TableElementType, Seq])

两者意思相同,所以可以直接使用前者,去掉后者

both mean the same , So you can directly use the former and remove the latter

这篇关于Slick/Scala:什么是 Rep[Bind] 以及如何将其转换为值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆