Sql 在Scala中Doobie上的事务中获取或插入
我正在阅读Doobie文档,并尝试在事务中执行简单的get或create。我从第一个查询中获得一个选项,并尝试执行Sql 在Scala中Doobie上的事务中获取或插入,sql,scala,doobie,Sql,Scala,Doobie,我正在阅读Doobie文档,并尝试在事务中执行简单的get或create。我从第一个查询中获得一个选项,并尝试执行getOrElse并在else中运行insert,但是我一直得到值映射不是getOrElse调用中任何的成员。在实例中获取现有行或创建新行并返回事务结果的正确方法是什么 import doobie._ import doobie.implicits._ import cats._ import cats.effect._ import cats.implicits._ import
getOrElse
并在else中运行insert,但是我一直得到值映射不是getOrElse
调用中任何的成员。在实例
中获取现有行或创建新行并返回事务结果的正确方法是什么
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
}
导入doobie_
导入doobie.its_
进口猫_
进口猫。效果_
进口猫_
导入org.joda.time.DateTime
导入scala.concurrent.ExecutionContext
案例类实例(id:Int,主机名:String)
案例类用户(id:Int,instanceId:Int,用户名:String,电子邮件:String,created:DateTime)
类数据库(dbUrl:String、dbUser:String、dbPass:String){
隐式val cs=IO.contextShift(ExecutionContext.global)
val xa=事务处理程序.fromDriverManager[IO](
“org.postgresql.Driver”,dbUrl,dbUser,dbPass
)
def getOrCreateInstance(主机名:字符串)=用于{
我相信这样的事情应该适合你
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
def getOrCreateInstance(主机名:字符串):ConnectionIO[Instance]=for{
我相信这样的事情应该适合你
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
def getOrCreateInstance(主机名:字符串):ConnectionIO[Instance]=for{
existingInstance多亏了#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)
多亏了#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)