blackbeam / rust-mysql-simple

Mysql client library implemented in rust.
Apache License 2.0
666 stars 145 forks source link

Add a `query_one` method that ensures a single result #358

Open tgross35 opened 1 year ago

tgross35 commented 1 year ago

This kind of method is helpful for when you expect a single item, and any more would indicate an error. The signature would be approximately:

fn query_one<T, Q>(&mut self, query: Q) -> Result<T>
where
    Q: AsRef<str>,
    T: FromRow,

Any result count other than one returns an error.

blackbeam commented 1 year ago

Hi. Whether some query returns one or multiple rows depends mostly on the query itself that, in turn, depends on the application logic. I believe this shouldn't be a driver problem. From the driver perspective this adds a new error variant ManyOrNoRowsReturnedWhileExactlyOneRowIsExpected that seems odd for a database driver to have.