New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stable release: add combinators, helpers and nullability patterns #10

Merged
merged 1 commit into from Nov 6, 2018

Conversation

Projects
None yet
3 participants
@Zaid-Ajaj
Owner

Zaid-Ajaj commented Nov 6, 2018

1- Upgrade the library with new combinators to easily with nullable rows:

Sql.readInt columnName row 
Sql.readString columnName row 
// etc.

Allows us to write functions such as

let getAllUsers() : User list =
    defaultConnection
    |> Sql.connect
    |> Sql.query "SELECT * FROM \"users\""
    |> Sql.executeTable 
    |> Sql.mapEachRow (fun row ->
        option {
            let! id = Sql.readInt "user_id" row 
            let! fname = Sql.readString "first_name" row 
            let! lname = Sql.readString "last_name" row
            return { Id = id; FirstName = fname; LastName = lname }
        }) 

2 - Fix Null value roundtrip because sending null is considored a null parameter instead of null value
3 - Add nullable active patterns

let value = SqlValue.Null 

match value with 
| NullDecimal (Some decimal) -> decimal
| NullDecimal None -> 0.0M
| otherwise -> (* you get the idea *)

Breaking changes 0.8.0 -> 1.0.0

The Sql type is now renamed to SqlValue and requires qualified access. But added simple overloaded function Sql.Value to create instances of SqlValue.

For example if you had this:

let userExists (name: string) : bool =
    defaultConnection
    |> Sql.connect
    |> Sql.func "user_exists"
    |> Sql.parameters ["username", String name]
    |> Sql.executeScalarSafe
    |> function 
           | Ok (Bool value) -> Ok value 
           | _ -> Error "Error while calling function 'user_exists'" 

then it becomes

let userExists (name: string) : bool =
    defaultConnection
    |> Sql.connect
    |> Sql.func "user_exists"
    |> Sql.parameters ["username", Sql.Value name] // or SqlValue.String name
    |> Sql.executeScalarSafe
    |> function 
           | Ok (SqlValue.Bool value) -> Ok value 
           | _ -> Error "Error while calling function 'user_exists'" 

@Zaid-Ajaj Zaid-Ajaj requested review from rfrerebe and removed request for rfrerebe Nov 6, 2018

@rfrerebe

Fine for me.
Nice to see this library evolving in a beautiful way.

@tonyx

This comment has been minimized.

tonyx commented Nov 6, 2018

👍🏻

@Zaid-Ajaj Zaid-Ajaj merged commit f34710f into master Nov 6, 2018

@Zaid-Ajaj Zaid-Ajaj deleted the next branch Nov 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment