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

Update table with dynamic column name? #25

Closed dredgy closed 3 years ago

dredgy commented 3 years ago

Not exactly a big deal but wanting to do a simple update column function

eg let updateColumn key column value

Looking at the partial update expression, I’d want something similar to this:

let updateColumn key column value  =
    update {
        table "Persons"
        set {| column = value |}
        where (eq "person-id" key)
    } |> conn.UpdateAsync

Is this possible? Computation expressions are very confusing to me!

Dzoukr commented 3 years ago

Good day to you too! 😄

If you need this kind of flexibility, where you don't know column name or value upfront, fallback to plain Dapper then. Column names are generated via reflection from anonymous/named records and if you take away this, there's no need to use anything else than plain Dapper like:

let updateColumn key column value =
    let sql = "UPDATE Persons SET @col = @val WHERE person-id = @key"
    let pars = [("col", box column); ("val", box value); ("key", box key)]
    connection.ExecuteAsync(sql, pars)
dredgy commented 3 years ago

Thanks a lot! That’s what I’m currently using, just was converting all my functions from plain dapper to your library where I could and this was the only place I couldn’t! So was more curious about passing arguments into expressions as it seemed a good thing to be able to learn.

Loving the library so far, is so easy to use!


From: Roman Provazník @.> Sent: Monday, May 10, 2021 4:00:00 PM To: Dzoukr/Dapper.FSharp @.> Cc: dredgy @.>; Author @.> Subject: Re: [Dzoukr/Dapper.FSharp] Update table with dynamic column name? (#25)

Good day to you too! 😄

If you need this kind of flexibility, where you don't know column name or value upfront, fallback to plain Dapper then. Column names are generated via reflection from anonymous/named records and if you take away this, there's no need to use anything else than plain Dapper like:

let updateColumn key column value =

let sql = "UPDATE Persons SET @col = @val WHERE person-id = @key"

let pars = [("col", box column); ("val", box value); ("key", box key)]

connection.ExecuteAsync(sql, pars)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Dzoukr/Dapper.FSharp/issues/25#issuecomment-836219399, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ARHIIMMO3JP5HB3WX546BFLTM5Y6BANCNFSM44PBB2NQ.