【问题标题】:Is it possible to return a custom column value from the sql database table using slick?是否可以使用 slick 从 sql 数据库表中返回自定义列值?
【发布时间】:2017-01-07 20:14:03
【问题描述】:

有一种场景,requestType="HR"(来自 HTTP PUT 请求),它应该返回所有学生信息,但返回标题为“EMPLOYEE”

例如,考虑一个包含列 name、id 和 title 的“student”表

 +-------+----+--------------------+
 + name  | id | title              +            
 +-------+----+--------------------+
 | KING  | 10 | SOFTWARE ENGINEER  |
 | BLAKE | 30 | SYSTEMS  ENGINEER  |
 +-------+----+--------------------+

GOAL:返回所有学生,并覆盖 title="EMPLOYEE"

这是我目前所拥有的

 case class Student(name: String, id: Long, title: String)

 class StudentTable(tag: Tag) extends Table[Student](tag, "student") {
      def name = column[String]("name")
      def id = column[Long]("id")
      def title = column[String]("title")
      override def * = (name, id, title) <> ((Student.tupled, Student.unapply)
 }

 lazy val studentsQuery = TableQuery[StudentTable]

当我尝试映射和更改查询中的 title 值时,它抱怨“重新分配给 val”

 val f = studentsQuery.map(p => p.title = "EMPLOYEE).result

编译器错误:重新分配给 val

方法二: 我尝试将requestType作为函数参数传入StudentTable,这样我就可以根据requestType修改title值。但是后来无法定义studentQuery,因为它抱怨“必需的标签”。

 class StudentTable(tag: Tag)(reqType: String) extends Table[Student](tag, "student") {
      def name = column[String]("name")
      def id = column[Long]("id")
      def title = req.type match {
           case "HR" => "EMPLOYEE"
           case _ => column[String]("title")
      }
      override def * = (name, id, title) <> ((Student.tupled, Student.unapply)
 }

 // Didn't understand how to provide tag 
 lazy val studentsQuery = TableQuery[StudentTable]()("HR")

编译错误:未指定值参数:缺点:(Tag) => StudentTable

【问题讨论】:

    标签: scala slick-3.0 slick-2.0


    【解决方案1】:

    而不是

    val f = studentsQuery.map(p => p.title = "EMPLOYEE).result
    

    你应该有:

    val f = studentsQuery.result.map(_.map(p => p.copy(title = "EMPLOYEE")))
    

    【讨论】:

    • Hello Pawel 无法让您的代码执行编译器抱怨两件事:1. 无法解析具有此类签名的引用副本 2. 无法解析符号标题
    • 您使用的是哪个版本的 Slick?您提供了相互矛盾的标签(slick-2.0slick-3.0),所以不清楚您有哪一个。
    • 我正在使用 slick2.0
    【解决方案2】:

    我无法从 Slick 中做到这一点,但找到了一种使用 json 序列化的方法。

     import scala.concurrent.Await
     import scala.concurrent.duration._
     import slick.driver.MySQLDriver.api._
    
     case class Student(name: String, id: Long, title: String)
    
     class StudentTable(tag: Tag) extends Table[Student](tag, "student") {
          def name = column[String]("name")
          def id = column[Long]("id")
          def title = column[String]("title")
          override def * = (name, id, title) <> ((Student.tupled,Student.unapply)}
    
     lazy val studentsQuery = TableQuery[StudentTable]
    
    
     implicit def StudentDataWrites = new Writes[Student] {
         def writes(student: Student) =
              Json.obj(
                  "name"  -> student.name,
                  "id"    -> student.id,
                  "title" -> "EMPLOYEE"
                )
     }
    
     def getStudentsInfo() = Action {
        val students= Await.Result(db.run(studentsQuery.size.result), 10.seconds) 
        Ok(Json.obj("students" -> Json.toJson(students)))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-06
      • 2011-09-14
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多