gpt4 book ai didi

scala - Doobie 更新和插入 case 类语法

转载 作者:行者123 更新时间:2023-12-04 04:39:44 26 4
gpt4 key购买 nike

杜比 jar 头select *使用 case 类来方便和正确地传递参数,但我不知道如何使用 update 以类似的方式工作和 insert .

例如,给定一个这样的案例类:

case class Course(
sku: String,
title: String,
id: Id,
price: Int,
instructorid: Id,
groupid: Id,
shortdescription: String = "",
transcript: String = "",
project_home: String = "",
repository: String = "",
category: String = "",
image: String = "",
privacy: String = "",
language: String = "",
keywords: String = "",
goals: String = "",
instructionallevel: String = "",
audience: String = "",
studenttasks: String = "",
sections: String = "",
active: Boolean = true,
video: String = "",
paypal_button_id: String = "",
prerequisite_ids: String = ""
)

我可以很好 select记录。这个不错的语法是可能的,因为 Doobie 遍历 Course case 类属性并通过将它们的名称与 courses 匹配来为它们赋值。数据库记录字段:

    def find(id: Id): Option[Course] =
sql"select * from courses where id = $id"
.query[Course]
.option
.transact(SQLSupport.xa)
.unsafeRunSync

然而 insert需要手动列出所有案例类属性,并与值匹配,这是可怕且容易出错的:

    /** @return saved Course with new Id */
def save(course: Course): Course = {
val insert: doobie.ConnectionIO[Course] = sql"""insert into courses (
sku,
title,
price,
instructorid,
groupid,
shortdescription,
transcript,
project_home,
repository,
category,
image,
privacy,
language,
keywords,
goals,
instructionallevel,
audience,
studenttasks,
sections,
active,
video,
paypal_button_id,
prerequisite_ids
) values (
${ course.sku },
${ course.title },
${ course.price },
${ course.instructorid },
${ course.groupid },
${ course.shortdescription },
${ course.transcript },
${ course.project_home },
${ course.repository },
${ course.category },
${ course.image },
${ course.privacy },
${ course.language },
${ course.keywords },
${ course.goals },
${ course.instructionallevel },
${ course.audience },
${ course.studenttasks },
${ course.sections },
${ course.active },
${ course.video },
${ course.paypal_button_id },
${ course.prerequisite_ids }
)"""
.update
.withUniqueGeneratedKeys("id")
val newCourse: Course = insert.transact(SQLSupport.xa).unsafeRunSync
newCourse
}

还有 update同样可怕:

    /** @return updated Course, which should be identical to the given course */
def update(course: Course): Course = {
val update: doobie.ConnectionIO[Course] = sql"""update courses set
sku = ${ course.sku },
title = ${ course.title },
id = ${ course.id },
price = ${ course.price },
instructorid = ${ course.instructorid },
groupid = ${ course.groupid },
shortdescription = ${ course.shortdescription },
transcript = ${ course.transcript },
project_home = ${ course.project_home },
repository = ${ course.repository },
category = ${ course.category },
image = ${ course.image },
privacy = ${ course.privacy },
language = ${ course.language },
keywords = ${ course.keywords },
goals = ${ course.goals },
instructionallevel = ${ course.instructionallevel },
audience = ${ course.audience },
studenttasks = ${ course.studenttasks },
sections = ${ course.sections },
active = ${ course.active },
video = ${ course.video },
paypal_button_id = ${ course.paypal_button_id },
prerequisite_ids = ${ course.prerequisite_ids }
where id = ${ course.id }"""
.update
.withUniqueGeneratedKeys("id")
val modifiedCourse: Course = update.transact(SQLSupport.xa).unsafeRunSync
modifiedCourse
}

有没有更好的办法?

最佳答案

如果你使用 Postgres,你可以看看 Rob Norris 的另一个库 - skunk .

它允许您编写自定义编解码器:

  case class City(id: Int, name: String, code: String, district: String, pop: Int)

val city: Codec[City] =
(int4 ~ varchar ~ bpchar(3) ~ varchar ~ int4).gimap[City]

val insertCity: Command[City] =
sql"""
INSERT INTO city
VALUES ($city)
""".command

Check examples .

关于scala - Doobie 更新和插入 case 类语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com