我应该如何在Scala和Anorm中使用MayErr [IntegrityConstraintViolation,Int]?

| 我使用Anorm进行数据库查询。当我做“ 0”时,应该如何正确地处理错误?它的返回类型为
MayErr[IntegrityConstraintViolation,Int]
,这是Set还是Map? 有一个示例,但是我不明白如何处理返回值:
val result = SQL(\"delete from City where id = 99\").executeUpdate().fold( 
    e => \"Oops, there was an error\" , 
    c => c + \" rows were updated!\"
)
如何检查查询是否失败? (使用
result
),如果查询成功,如何获取受影响的行数? 目前,我使用以下代码:
SQL(
\"\"\"
INSERT INTO users (firstname, lastname) VALUES ({firstname}, {lastname})
\"\"\"
).on(\"firstname\" -> user.firstName, \"lastname\" -> user.lastName)
    .executeUpdate().fold(
            e => \"Oops, therw was an error\",
            c => c + \" rows were updated!\"
)
但是我不知道我的错误处理代码应该是什么样子。关于如何使用类型为“ 1”的返回值有任何示例吗?     
已邀请:
        你显然可以做一个
val updateResult = ....executeUpdate()
val success = updateResult.fold(e => false, c => true)
看来你也可以打电话
val success = updateResult.isRight
通常,您可以使用
updateResult.e match {
    case Left(error) => ... do something with error ...
    case Right(updateCount) => ...do something with updateCount...
}
也许更熟悉Play的人会解释scala的原因。     
        看来
MayErr
正在包裹
Either
。因此,它既不是“ 11”也不是“ 12”,而是可以包含两个不同类型的对象之一的对象。 看一下这个问题,您将看到一些处理Either对象的方法,在这种情况下,该对象包含IntegrityConstraintViolation或Int。参考http://scala.playframework.org/.../Scala$MayErr.html,看起来您可以通过引用值成员
e
来获取Either对象。似乎也有隐式转换可用,因此您可以将
MayErr[IntegrityConstraintViolation,Int]
视为
Either[IntegrityConstraintViolation,Int]
,而无需进行进一步的仪式。     

要回复问题请先登录注册