在同一个表格中使用浮动多重投影

时间:2014-11-29 19:45:26

标签: scala slick slick-2.0

我有一个用户表,我希望有多个投影。例如,我可以拥有类似的东西


class Users(tag: Tag) extends Table [User] (tag, "user") {
def * = (id.?, emailId, firstName, lastName, gender) <> (User.tupled, User.unapply)
def allDetails = (id.?, emailId, firstName, lastName, gender, createdTime, modifiedTime)
...
}
我在谷歌搜索但找不到任何东西。有人可以告诉我如何使用allDetails?

我愿意这样做


object Users  {
  val users = TableQuery[Users]
  def getAllDetails = ??? // How can I use allDetails here
  }

2 个答案:

答案 0 :(得分:6)

最近有同样的需要,并开始使用类似的东西:

abstract class AnyUserTable[T](tag: Tag) extends Table[T](tag, "user") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def emailId = column[String]("email_id")
    def firstName = column[String]("firstname")
    def lastName = column[String]("lastname")
    def gender = column[String]("gender")
}

class Users(tag: Tag) extends AnyUserTable[User](tag) {
    def * = (emailId, firstName, lastName, gender, id.?) <> (User.tupled, User.unapply)
}

class UserDetails(tag: Tag) extends AnyUserTable[UserDetail](tag) {
    def createdTime = column[Option[Timestamp]]("created_time")
    def modifiedTime = column[Option[Timestamp]]("modified_time")
    def * = (emailId, firstName, lastName, gender, createdTime, modifiedTime, id.?) <> (UserDetail.tupled, UserDetail.unapply)
}

object Users  {
    val users = TableQuery[Users]
    val getAllDetails = TableQuery[UserDetails] // That is how I propose to get all the details
}

从Sky的回答中借用案例类。

case class User(
    emailId: String, firstName: String, lastName: String,
    gender: String, id: Option[Int] = None)

case class UserDetail(
    emailId: String,
    firstName: String, lastName: String, gender: String,
    createdTime: Option[Timestamp],
    modifiedTime: Option[Timestamp], id: Option[Int] = None)

我想这与我在sql中使用可更新视图的诱惑非常接近。

答案 1 :(得分:0)

这样做:

   class UserTable(tag: Tag) extends Table[UserInfo](tag, "user") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def emailId = column[String]("email_id", O.NotNull)
    def firstName = column[String]("firstname")
    def lastName = column[String]("lastname")
    def gender = column[String]("gender")
    def createdTime = column[Option[Timestamp]]("created_time")
    def modifiedTime = column[Option[Timestamp]]("modified_time")
    def userInfo = (emailId, firstName, lastName, gender, createdTime, modifiedTime, id.?) <> (User.tupled, User.unapply)

     def * = (emailId, firstName, lastName, gender, id.?) <> (UserInfo.tupled, UserInfo.unapply)

}

 case class User(emailId: String,
   firstName: String, lastName: String, gender: String, 
   createdTime: Option[Timestamp],    
   modifiedTime: Option[Timestamp], id: Option[Int] = None)

 case class UserInfo(emailId: String, firstName: String, lastName: String, 
      gender: String, id: Option[Int] = None)

进行额外投影:

  val userTable = TableQuery[UserTable]

 def insert(userInfo: UserInfo) (implicit session: Session): Int = {
   //return auto incremeted id
  userTable returning (userTable.map(_.id)) += userInfo

  }

 def update(userInfo: UserInfo)(implicit session: Session) = {
   userTable.filter(_.id === userInfo.id).update(userInfo)
 }
 def getUserInfo()(implicit session: Session): List[UserInfo] = {
  userTable.list

  }