【问题标题】:How to access a database entry in a row with Scala Slick如何使用 Scala Slick 连续访问数据库条目
【发布时间】:2014-03-14 11:56:07
【问题描述】:

我正在使用以下教程中的代码: sysgears.com/articles/building-rest-service-with-scala/#16

我的数据库架构:

    /**
     * Customer entity.
     *
     * @param id        unique id
     * @param firstName first name
     * @param lastName  last name
     * @param accountBalance account balance
     * @param birthday  date of birth
     */
     case class Customer(id: Option[Long], firstName: String, lastName: String, accountBalance: Int, birthday: Option[java.util.Date])

     /**
      * Mapped customers table object.
      */
     object Customers extends Table[Customer]("customers") {

       def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

       def firstName = column[String]("first_name")

       def lastName = column[String]("last_name")

       def accountBalance = column[Int]("account_balance")

       def birthday = column[java.util.Date]("birthday", O.Nullable)

       def * = id.? ~ firstName ~ lastName ~ accountBalance ~ birthday.? <>(Customer, Customer.unapply _)

       implicit val dateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Date](
       {
        ud => new java.sql.Date(ud.getTime)
       }, {
         sd => new java.util.Date(sd.getTime)
       })

       val findById = for {
         id <- Parameters[Long]
         c <- this if c.id is id
       } yield c
     }

我正在编写一个函数来检索具有指定 ID 号的客户。然后我想访问该客户的帐户余额并对其进行计算。

这是我目前的代码:

     def withdraw(id: Long, amountToWithdraw: Long): Either[Failure, Customer] = {
         try {
           db.withSession {
            val customerRow = Customers.filter{ a => a.id === id}
            var amount = customerRow.accountBalance
             if (amountToWithdraw < accountBalance){
                accountBalance -= amountToWithdraw
                    update(customerId, customer)
                Right(customer)
             }
             else{
                 Left(insufficientFundsError(id))
                }
          }  
         } catch {
           case e: SQLException =>
             Left(databaseError(e))
         }
       }

当我尝试运行它时,我得到一个错误:

    value accountBalance is not a member of scala.slick.lifted.Query[com.sysgears.example.domain.Customers.type,scala.slick.lifted.NothingContainer#TableNothing]
    var amount = customerRow.accountBalance
                             ^

所以我想我的问题是我不知道如何访问指定客户的数据库条目 accountBalance?

【问题讨论】:

    标签: sql scala slick


    【解决方案1】:

    我实际上不需要提取任何客户。我只需要提取具有指定 id 的客户的 accountBalance 的值。我就是这样做的:

        val accBal = Customers.filter{ a => a.id === id}.map(_.accountBalance).firstOption
    

    因为 accBal 是 Option[Long] 类型,所以我不得不:

        val accBalAsLong: Long = accBal.getOrElse(0)
    

    【讨论】:

      【解决方案2】:

      Slick 的工作方式类似于 Scala 集合。 Customers 就像一个集合。我没有成员accountBalance,只有它的元素有。所以你必须使用.map。或者在您的情况下,立即获取客户记录可能更容易。

      val customerRow = Customers.filter{ a =&gt; a.id === id}.run.head

      【讨论】:

      • 谢谢。好的,这是我现在的代码: var customerRow = Customers.filter{ a => a.id === id}.map(_.accountBalance) var amount: Long = customerRow - amountToWithdraw 我收到一个错误:value -不是 scala.slick.lifted.Query[scala.slick.lifted.Column[Long],Long] 的成员,我不能对提取的值 accountBalance 执行操作吗?
      • Customers 是一个查询,类似于一个集合。这不是一行。您的变量名称具有误导性。作为比较,List[Customer] 没有方法 accountBalance。只有其中包含的客户对象才有。我建议的行执行查询并为您提供结果中的第一个客户,这可能是您想要的。另请阅读 Slick 文档,它应该使事情更清楚。一般提示:使用 Slick 时请考虑 Scala 集合。不要想 SQL。它具有与 Scala 集合不同的 API。
      猜你喜欢
      • 2017-08-15
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 2018-06-14
      • 1970-01-01
      相关资源
      最近更新 更多