JordanMarr / SqlHydra

SqlHydra is a suite of NuGet packages for working with databases in F# including code generation tools and query expressions.
MIT License
223 stars 22 forks source link

Use of DataTimeOffset means you must use SqlDataReader #14

Closed isaacabraham closed 2 years ago

isaacabraham commented 2 years ago

Dapper returns an IDataReader on it's extension method on top of the SqlConnection. Unfortunately, this does not contain the GetDateTimeOffset member that SqlDataReader does, so the generated SqlHydra code does not work.

Is there an alternative for the use of this member that would work with an IDataReader?

JordanMarr commented 2 years ago

Are you talking about the HydraReader constructor? If so you could just cast the IDataReader: https://github.com/JordanMarr/SqlHydra/blob/1dabec93fe9e0d8b8a06e3e900497e74e966edb5/src/SampleApp/DonaldExample.fs#L21

isaacabraham commented 2 years ago

The problem is that the IDataReader I'm using - Dapper's ExecuteReader extension method on top of a SqlConnection doesn't give back a SqlDataReader underneath the IDataReader.

JordanMarr commented 2 years ago

Interesting... Apparently the Dapper crew wrapped it in an extra layer, so I had to do this:

let getProductsWithThumbnailDapper(conn: SqlConnection) = task {
    use wrappedReader = conn.ExecuteReader("SELECT TOP 2 * FROM SalesLT.Product p WHERE ThumbNailPhoto IS NOT NULL")
    use reader = (wrappedReader :?> IWrappedDataReader).Reader :?> SqlDataReader
    let hydraReader = SalesLT.HydraReader(reader)
    return [ 
        while reader.Read() do
            hydraReader.Product.Read() 
    ]
}
JordanMarr commented 2 years ago

Async version:

let getProductsWithThumbnailDapperAsync(conn: SqlConnection) = task {
    use! wrappedReader = conn.ExecuteReaderAsync("SELECT TOP 2 * FROM SalesLT.Product p WHERE ThumbNailPhoto IS NOT NULL")
    use reader = (unbox<IWrappedDataReader> wrappedReader).Reader :?> SqlDataReader
    let hydra = SalesLT.HydraReader(reader)
    return [ 
        while wrappedReader.Read() do
            hydra.Product.Read() 
    ]
}

Added Dapper to samples.

isaacabraham commented 2 years ago

Perfect, thanks.