Dzoukr / Dapper.FSharp

Lightweight F# extension for StackOverflow Dapper with support for MSSQL, MySQL, PostgreSQL, and SQLite
MIT License
365 stars 35 forks source link

Match operator not possible inside PostgreSQL update computation expression #104

Open zeejers opened 3 weeks ago

zeejers commented 3 weeks ago
 update {
    for p in peopleService.Table do
        match person.FirstName 
        | Some firstName -> setColumn p.FirstName firstname
        | None -> ()

        where (p.Id = id)
  }

Gives me an error:

A custom operation may not be used in conjunction with 'use', 'try/with', 'try/finally', 'if/then/else' or 'match' operators within this computation expressionF# Compiler[3086](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-messages/fs3086)

Similarly, trying to use an if operator gives me this error: This control construct may only be used if the computation expression builder defines a 'Zero' methodF# Compiler[708](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-messages/fs0708)

It would be great to be able to match in the update CE for the purpose of accepting a Partial DTO and only setting columns for those fields which have Some value.

JordanMarr commented 3 weeks ago

The query expressions have a fairly narrow band of acceptable syntax. Match statements are not handled.

This is because the code in the query isn’t executed directly. Rather, it is just reading the code structures and converting them into a SQL string on your behalf.

Think of it as a strongly typed way to create a SQL command.

If you want to be able to conditionally set a field, you could fairly easily create a “setIf” extension method / custom operation that also takes a bool condition parameter.