我正在尝试获取查询的结果,该查询作为Slick 3.0的DBIO.sequence()方法的参数传递的一系列操作执行。以下是相同的代码段。
val query = for {
(tt, th) <- tmpTrades join TableQuery[TrdHeader] on (_.tradeNum === _.tradeNum)
} yield (tt.tradeNum, th.internalInd, th.tradeStatusInd, th.tradeDt, th.tradeInputDt, th.lastModifyDt)
val queryAction = query.result
val actions = Seq(tmpTrades.schema.create, tmpTrades ++= trades.toSeq: _*, queryAction, tmpTrades.schema.drop)
val resultFuture = db.run(DBIO.sequence(actions))
我希望DBIO.sequence(actions)的结果作为对应于查询的yield子句的六个属性的元组,并将其分配给resultFuture变量。我怎样才能实现同样的目标?
感谢。
答案 0 :(得分:0)
您应该使用flatMap
代替DBIO.sequence
:
val myAction = tmpTrades.schema.create.flatMap { _ =>
tmpTrades ++= trades.toSeq
}.flatMap { _ =>
queryAction
}.flatMap { queryResult =>
// Return the result at the end.
tmpTrades.schema.drop.map { _ => queryResult }
}
db.run(myAction.transactionally)
或与for-yield
:
val myAction = for {
_ <- tmpTrades.schema.create
_ <- (tmpTrades ++= trades.toSeq)
queryResult <- queryAction
_ <- tmpTrades.schema.drop
} yield queryResult
db.run(myAction.transactionally)