How to use MayErr [IntegrityConstraintViolation, Int] in Scala and Anorm?

I use Anorm to execute database queries. When I do executeUpdate(), how should I do the correct error handling? Does it have a return type MayErr[IntegrityConstraintViolation,Int], is it a set or a card?

There is an example , but I do not understand how I should handle the return value:

val result = SQL("delete from City where id = 99").executeUpdate().fold( 
    e => "Oops, there was an error" , 
    c => c + " rows were updated!"
)

How to check if the request failed? (using result), and how can I get the number of rows affected if the request was successful?

I am currently using this code:

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!"
)

But I do not know what the error handling code should look like. Is there any example of how to use the return value of a type MayErr[IntegrityConstraintViolation,Int]?

+3
2

, ,

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?

+2

, MayErr - Either. , a Map Set, , .

, , IntegrityConstraintViolation, Int. http:// scala.playframework.org/.../Scala $MayErr.html, , , e. , MayErr[IntegrityConstraintViolation,Int] Either[IntegrityConstraintViolation,Int] .

+3

All Articles