简体   繁体   English

Slick / Scala:什么是Rep [Bind],如何将其变成值?

[英]Slick/Scala: What is a Rep[Bind] and how do I turn it into a value?

I'm trying to figure out Slick (the Scala functional relational model). 我试图弄清楚Slick(Scala功能关系模型)。 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. 我已经开始在Slick 3.0.0中构建原型,但当然......大多数文档都已过时或不完整。

I've managed to get to a point where I can create a schema and return an object from the database. 我已经设法达到了可以创建模式并从数据库返回对象的程度。

The problem is, what I'm getting back is a "Rep[Bind]" and not the object I would expect to get back. 问题是,我要回的是“Rep [Bind]”而不是我希望得到的对象。 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. 例如,如果我尝试像rep.countDistinct.result这样的东西,我就会崩溃。

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]"...

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对象的方法,但我找不到任何明确的解释它是什么或如何使用它。

Rep[] is a replacement to the Column[] datatype used in slick . Rep []是光滑中使用的Column []数据类型的替代品。

users.map(_.id) returns values of the Column( 'n_user_id' ) for all rows users.map(_。id)返回所有行的Column( 'n_user_id' )的值

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

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

The obtained value is of type Column[Long] [ which is now Rep[Long] ]. 获得的值的类型为Column [Long] [现为Rep [Long] ]。 You cannot directly print values of the above resultSet as it is not of any scala collection type 您不能直接打印上述resultSet的值,因为它不是任何scala集合类型

  • You can first convert it to some scala collection and then print it as below : 您可以先将其转换为某些scala集合,然后按如下方式打印:

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

    println(idList)** // if you need to print all ids at once println(idList)** //如果您需要一次打印所有ID

  • else you can simply use : 否则你可以简单地使用:

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

And , 而且,

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 两者的意思相同,所以你可以直接使用前者并删除后者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM