在Scala中的Doobie上获取或插入交易

时间:2018-12-22 09:27:23

标签: sql scala doobie

我正在阅读Doobie文档,并尝试在事务中进行简单的获取或创建。我在第一个查询中选择了一个选项,尝试执行getOrElse并在其他查询中运行插入操作,但是在value map is not a member of Any调用中仍然收到getOrElse。在instances中获取现有行或创建新行并将其返回交易的正确方法是什么?

import doobie._
import doobie.implicits._
import cats._
import cats.effect._
import cats.implicits._
import org.joda.time.DateTime

import scala.concurrent.ExecutionContext

case class Instance(id : Int, hostname : String)

case class User(id : Int, instanceId: Int, username : String, email : String, created : DateTime)

class Database(dbUrl : String, dbUser: String, dbPass: String) {

  implicit val cs = IO.contextShift(ExecutionContext.global)

  val xa = Transactor.fromDriverManager[IO](
    "org.postgresql.Driver", dbUrl, dbUser, dbPass
  )

  def getOrCreateInstance(hostname: String) = for {
    existingInstance <- sql"SELECT id, hostname FROM instances i WHERE i.hostname = $hostname".query[Instance].option
    ensuredInstance <- existingInstance.getOrElse(sql"INSERT INTO instances(hostname) VALUES(?)".update.withGeneratedKeys[Instance]("id", "hostname"))
  } yield ensuredInstance

}

2 个答案:

答案 0 :(得分:1)

由于#scala / freenode聊天室中的人员,我得到了以下答案。我将其发布在此处是出于完整性的考虑,如果人们有兴趣这样做而又没有其他答案的理解。

  def getOrCreateInstance(hostname: String): ConnectionIO[Instance] =
        OptionT(sql"SELECT id, hostname FROM instances i WHERE i.hostname = $hostname".query[Instance].option)
          .getOrElseF(sql"INSERT INTO instances(hostname) VALUES($hostname)".update.withGeneratedKeys[Instance]("id", "hostname").compile.lastOrError)

答案 1 :(得分:0)

我相信类似的东西应该对您有用,

def getOrCreateInstance(hostname: String): ConnectionIO[Instance] = for {
  existingInstance <- sql"SELECT id, hostname FROM instances i WHERE i.hostname = $hostname".query[Instance].option
  ensuredInstance <- existingInstance.fold(sql"INSERT INTO instances(hostname) VALUES($hostname)".update.withGeneratedKeys[Instance]("id", "hostname").take(1).compile.lastOrError)(_.pure[ConnectionIO])
} yield ensuredInstance

您将在其中编译fs2 Stream,并在现有实例已经存在的情况下将其提升到ConnectionIO中。