Dapper.FSharp icon indicating copy to clipboard operation
Dapper.FSharp copied to clipboard

Match operator not possible inside PostgreSQL update computation expression

Open zeejers opened this issue 1 year ago • 1 comments

 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.

zeejers avatar Jun 05 '24 21:06 zeejers

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.

JordanMarr avatar Jun 05 '24 22:06 JordanMarr