mfp / ocaml-sqlexpr

Minimalistic syntax extension for type-safe, convenient execution of SQL statements.
Other
86 stars 17 forks source link

Provide select_one_maybe_f in addition to select_one_f_maybe #36

Closed vog closed 5 years ago

vog commented 5 years ago

The interface of select_one_f_maybe is a bit unflexible:

val select_one_f_maybe : db -> ('a -> 'b result) -> ('c, 'a, 'b option result) expression -> 'c

It forces f to transform the query result, rather than the result option. It then forces the option onto the result of f.

I propose to add a more flexible version, with the following signature:

val select_one_maybe_f : db -> ('a option -> 'b result) -> ('c, 'a, 'b result) expression -> 'c

This one passes the whole option to f, and f has full control over the result type.

Among others, this would enable the implementation of an "exists" function that returns true if there is a result row and false if the result was empty:

let select_exists db sql =
  S.select_one_maybe_f db (fun r -> r <> None) sql

Currently, it is impossible to define such a function in a generic way (i.e. for arbitrary SQL expressions with an arbitrary number of arguments), but the addition of select_one_maybe_f would enable that possibility.

mfp commented 5 years ago

I was unsure about the name at first, but after pondering for a minute I realized you had made the right choice and kept it as you indicated :+1:

vog commented 5 years ago

Well, I just tried to stick to your existing naming convention. ;-)