Scala-Slick:仅执行一系列操作

时间:2017-02-03 04:12:53

标签: mysql scala playframework slick

拥有一段代码,用于添加/更新产品,并将一个或多个代码与其关联。标签实际上已添加到TagGroup,并与产品相关联。

我面临的问题是只有"部分" addOrUpdateProductWithTags()执行。产品已更新或创建,但未添加标签。如果我评论最后一个查询(请参阅注释),那么一切正常。打开了#34;"确认这一点。

lazy val pRetId = prods returning prods.map(_.id)

def addTags(keywords: Seq[String]) = {
  for {
    k <- keywords
  } yield {
    tags.filter(_.keyword === k).take(1).result.headOption.flatMap {
      case Some(tag) => { 
        Logger.debug("Using existing tag: " + k)  
        DBIO.successful(tag.id)
      }
      case None => {
        Logger.debug("Adding new tag: " + k)
        tags.returning(tags.map(_.id)) += Tag(k, Some("DUMMY")) 
      }
    } 
  }   
}

def addOrUpdateProductWithTags(prod: Product, tagSet: Seq[String]): Future[Option[Long]] = {

  // handle add or update product
  val prodObject = prod.id match {
    case 0L => pRetId += prod
    case _ => prods.withFilter(_.id === prod.id).update(prod)
  }

  val action = for {
    pid <- prodObject
    tids <- DBIO.sequence(addTags(tagSet))
  } yield (tids, pid)

  val finalAction = action.flatMap {
    case (tids, pid) => {
      val prodId = if (prod.id > 0L) prod.id else pid.asInstanceOf[Number].longValue
      val delAction = tagGroups.filter(_.prodId === prodId).delete
      val tgAction = for {
        tid <- tids
      } yield {
        tagGroups += TagGroup("Ignored-XX", prodId, tid)
      }
      delAction.flatMap { x => DBIO.sequence(tgAction) }

      // IF LINE BELOW IS COMMENTED THEN TagGroup is created else even delete above doesn't happen
      prods.filter(_.id === prodId).map(_.id).result.headOption
    }
  }

  db.run(finalAction.transactionally)
}

这是调用此方法的控制器中的代码段。我怀疑打电话的等待时间不长但不确定......

val prod = Prod(...)
val tagSet = generateTags(prod.tags)
val add = prodsService.addOrUpdateProductWithTags(prod, tagSet)
add.map { value =>
  Redirect(controllers.www.routes.Dashboard.dashboard)
}.recover {
  case th => 
    InternalServerError("bad things happen in life: " + th)
}         

有任何疑问查询的错误是什么? Stack:Scala 2.11.7,播放版本2.5.4,play-slick 2.0.0(slick 3.1)

1 个答案:

答案 0 :(得分:0)

终于找到了解决方案:

取代以下两行:

    delAction.flatMap { x => DBIO.sequence(tgAction) }
    prods.filter(_.id === prodId).map(_.id).result.headOption

我将这些操作与andThen运算符结合起来如下:

    delAction >> DBIO.sequence(tgAction) >> prods.filter(_.id === prodId).map(_.id).result.headOption

现在整个序列被执行了。我仍然不知道原始解决方案有什么问题,但这有效。