SLICK 3.0 - 多个查询相互依赖 - db.run(action)

use*_*981 11 scala scalaquery slick slick-3.0

我是Slick 3的新手,到目前为止我已经理解db.run是异步调用.返回Future后运行.map或.flatMap.

我的代码中的问题是所有子查询都不起作用(嵌套db.run).

从概念上讲,我没有得到什么?这样的代码是否有效,如下所示?基本上在第一个查询的.map中我根据第一个查询执行一些操作.

我到处都看到有产量的循环,它是唯一的出路吗?我的代码中的问题是否与返回的Future值有关?

val enterprises = TableQuery[Enterprise]
val salaries = TableQuery[Salary]

//Check if entered enterprise exists 
val enterpriseQS = enterprises.filter(p => p.name.toUpperCase.trim === salaryItem.enterpriseName.toUpperCase.trim).result

val result=db.run(enterpriseQS.headOption).map(_ match 
{
    case Some(n) => {
      //if an enterprise exists use the ID from enterprise (n.id) when adding a record to salary table
      val addSalary1 = salaries += new SalaryRow(0, n.id, salaryItem.worker)
      db.run(addSalary1)
    }
    case None =>  {
        //if an enterprise with salaryItem.enterpriseName doesn't exist, a new enterprise is inserted in DB
        val enterpriseId = (enterprises returning enterprises.map(_.id)) += EnterpriseRow(0, salaryItem.enterpriseName)
        db.run(enterpriseId).map{
            e => {
                val salaryAdd2 = salaries += new SalaryRow(0, e, salaryItem.worker)
                db.run(salaryAdd2)
            }
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

Ric*_*way 14

我的代码中的问题是所有子查询都不起作用(嵌套db.run)

我怀疑你最终会得到嵌套的Future[R]结果.我没有调查过这个.因为...

从概念上讲,我没有得到什么?

我解决这个问题的方法是考虑结合DBIO[R].这可能是有帮助的概念.

你正在做的是尝试单独运行每个动作(查询,插入...).相反,将各个操作组合成一个操作并运行它.

我将重写主要逻辑:

  val action: DBIO[Int] = for {
    existingEnterprise <- enterpriseQS.headOption
    rowsAffected       <- existingEnterprise match {
      case Some(n) => salaries += new SalaryRow(0, n.id, salaryItem.worker)
      case None    => createNewEnterprise(salaryItem)
    }
  } yield rowsAffected
Run Code Online (Sandbox Code Playgroud)

对于这种None情况,我创建了一个帮助方法:

  def createNewEnterprise(salaryItem: SalaryItem): DBIO[Int] = for {
    eId          <- (enterprises returning enterprises.map(_.id)) += EnterpriseRow(0, salaryItem.enterpriseName)
    rowsAffected <- salaries += new SalaryRow(0, eId, salaryItem.worker)
  } yield rowsAffected
Run Code Online (Sandbox Code Playgroud)

最后,我们可以运行:

  val future: Future[Int] = db.run(action)
  // or db.run(action.transactionally)    

  val result = Await.result(future, 2 seconds)

  println(s"Result of action is: $result")
Run Code Online (Sandbox Code Playgroud)

我写博客文章的后半部分更多地讨论了这一点.

我使用的代码是:https://github.com/d6y/so-31471590