简体   繁体   中英

Can Slick Delete/Update DB Query return Negative Integer Values as the result

Normally when we perfom a Scala Slick Delete/Update query it return 0 if the delete/update is unsuccessfull due to not found the required id. On the other hand if the delete/update is successfull it will return the number of rows that affected with the delete/update operation.

  override def deletePerson(id: Long): Future[Int] = {
    db.run(personQuery.filter(_.id === id).delete)
  }

For example the above slcik query will return 0 if the requested id is not found for the deletion. Moroever it will return possitive value which is equal to the deleted rows if any rows deleted.

What I need to know is that can there be negative values (ex: -1) return as the result for a delete/update result in scala slick. Because I need to perform a validation based on the return result.

It's the number of affected rows, so I doubt it. If there is anything wrong your query, it would return errors rather than a negative number.

Still, I wouldn't recommend to do things like != 0 to check if query succeeded. result > 0 would be better, but the best would be another query that actually validates something, that you run in the same transaction as insert/update.

You won't see -1 from that query.

Slick uses an API called JDBC at a lower-level. It defines the results from update and delete statements. You can look into the specification to see what results are returned from executeUpdate at that level. You can find the specification at: https://www.jcp.org/en/jsr/detail?id=221

The API is implemented by a driver for your database. That driver will be compliant with the specification.

However, there are a couple of comments on this:

  • I'm not sure what will happen if you have a query that returns more than Integer.MAX_INT results. Probably unlikely for the query you have shown.

  • The JDBC API does allow for -1 from a getUpdateCount call, but to get that you'd need to be doing something low-level level (lower than the example you have).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM