com-lihaoyi / scalasql

Scala ORM to query SQL databases from Scala via concise, type-safe, and familiar case classes and collection operations. Connects to Postgres, MySql, H2, and Sqlite out of the box
195 stars 22 forks source link

Add support for .single returing an Option[R] #33

Open samikrc opened 2 months ago

samikrc commented 2 months ago

Currently the .single method return a single row only, and errors out if there is 0 or 2+ rows. Request to change the response to an Option[R] signature, where for 0 rows the return will be None, 1 row, return the row, and for 2+ rows, return an assertion error.

The proposal is to introduce a runOption method in DbApi, which will provide a .singleOption method. This method witll return an Option[R].

samikrc commented 2 months ago

Raised an initial PR: https://github.com/com-lihaoyi/scalasql/pull/34 @lihaoyi Please comment if the approach is correct. Will add test case once the approach is finalized.