fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
578 stars 146 forks source link

ValueOption support #766

Closed Thorium closed 2 years ago

Thorium commented 2 years ago

Proposed Changes

This changes the parameter UseOptionTypes to be, instead of current true/false, an DU:

Many of the database types are primitives and thus a struct would do fine. This is not changing the SqlEntity class itself, so you can still update the database values and save datacontext as usual.

Thorium commented 2 years ago

By my initial testing, the ValueOption<_> used 50% less CPU and 10% less memory than Option<_> which might be relevant if you have a lot of things going on.

The only restriciton is that ValueOption cannot be compared nicely (this is due to FSharp, not SQLProvider):

query {
       for emp in ctx.Dbo.Employees do
       where (emp.FirstName = ValueSome("Steven"))
       select emp.FirstName
} // System.InvalidOperationException: The binary operator Equal is not defined for the types

But you can do of course e.g.:

query {
       for emp in ctx.Dbo.Employees do
       where (emp.FirstName.IsSome && emp.FirstName.Value = "Steven")
       select emp.FirstName
} // works

...and if you absolutely want to compare 2 voptions, you can wrap them e.g. like this:

query {
       for emp in ctx.Dbo.Employees do
       where (Some(emp.FirstName) = Some(ValueSome("Steven")))
       select emp.FirstName
}