Zaid-Ajaj / DustyTables

Thin F# API for SqlClient for easy data access to ms sql server with functional seasoning on top
MIT License
74 stars 11 forks source link

Return unmatched case for faulty used SqlValue #10

Closed tforkmann closed 4 years ago

tforkmann commented 4 years ago

Is your feature request related to a problem? Please describe. I tried to read and Id and though the Id would be of the type tinyint. Somehow the query returned a empty array. Turned out the id as of the type int instead. I fiquered out it helps if you print out the unmatched case of the SqlValue.

Describe the solution you'd like

Before you just returned None as a result of the read value:

let readTinyInt name (row: SqlRow) =
   row
   |> List.tryFind (fun (colName, value) -> colName = name)
   |> Option.map snd
   |> function
   | Some(SqlValue.TinyInt value) -> Some value
   | _ -> None

I added a little helper to print out the unmatched case:

let returnInfo (x:SqlValue option) name =
    printfn "got %A for name %s return None for now" x.Value name
    None
let readTinyInt name (row: SqlRow) =
    row
    |> List.tryFind (fun (colName, value) -> colName = name)
    |> Option.map snd
    |> function
    | Some(SqlValue.TinyInt value) -> Some value
    | x -> returnInfo x name 

This at least shows that there were some values and you just didn't picked the correct SqlValue type.

Zaid-Ajaj commented 4 years ago

v2.0 has been released and includes reading functions that distinguish between existing values, nullables values and throws when the types are incorrect:

connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM dbo.[Users]"
|> Sql.execute (fun read -> read.int "non_nullable_int", read.stringOrNone "nullable_string")

Would you give it a try and see if the problem gets resolved?

Zaid-Ajaj commented 4 years ago

Also, now the library handles integers much better: if you try to read an integer of small size into a bigger size (say if column type is tinyint and you read it as int) then it is automatically converted.

tforkmann commented 4 years ago

Hi Zaid!

Thank you soo much! This works great now!

Testing the next issue now!