Cutting off DateTime precision?
See original GitHub issueHi, I haven’t been able to verify that this is a SqlHydra issue and not a SqlKata issue, but I’m having some DateTimes getting truncated to the second in my queries.
I’ve created a query expression to log a select query when executing it:
let private stringifyQuery (query:SqlKata.Query) =
let compiler = SqlKata.Compilers.SqlServerCompiler()
let sql = compiler.Compile(query)
let bindings =
String.concat "; "
[
for KeyValue (k, v) in sql.NamedBindings ->
match v with
| :? DateTime as d -> sprintf "(%A: %s)" k (d.ToString("yyyy-MM-dd hh:mm:ss.fffffff"))
| _ -> sprintf "(%A: %A)" k v
]
sprintf "'%s' with parameters '%s'" sql.Sql bindings
type LoggingSelectBuilder<'a, 'b, 'c when 'c :> Common.DbDataReader> (reader : ('c -> unit -> 'a), ct:ContextType) =
inherit SelectAsyncBuilder<'a, 'b, 'c>(reader, ct)
member this.Run(state: QuerySource<'a, SqlKata.Query>) =
printfn "%s" <| stringifyQuery state.Query
base.Run(state)
let loggingSelect reader connStr =
LoggingSelectBuilder(reader, (ContextType.Create (openContext connStr)))
I have a query like:
let! myCount =
loggingSelect MyDB.HydraReader.Read connStr {
for record in table<MyDB.dbo.MyTable> do
where (record.entry_timestamp > ts)
select (minBy record.entry_timestamp)
}
|>> Array.ofSeq
|>> Array.head
printfn "results in: %s" (myCount.ToString("yyyy-MM-dd hh:mm:ss.fffffff"))
which outputs:
'SELECT MIN([MyTable].[entry_timestamp]) FROM [MyTable] WHERE ([MyTable].[entry_timestamp] > @p0)' with parameters '("@p0": 2022-07-15 09:40:42.6078010)'
results in: 2022-07-15 09:40:42.6077917
it seems like I have to add a full second to the timestamp in the query to get the results I want.
Do you think this is a SqlKata issue or a SqlHydra issue? Do you have any recommendations for how to debug this? A SqlKata developer claims that SqlKata sends raw DateTime parameters to the DB server unaltered in this issue, and is correct as far as what I can tell from the SqlKata source. But I also can’t see in SqlHydra source anywhere that the truncation would be happening.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:11 (11 by maintainers)
No worries, that stuff is hard. I personally never use the devcontainer setup, mostly because I prefer using Visual Studio over vscode. So I just do
docker-compose up
instead and then use VS.As for modifying the table, maybe you can append the new table create script to the install.sql file and then re-run the script?
At least that’s what we did for the test tables created in the postgres install.sql file.
Nice work on the testing! It looks as though this issue can be resolved in SqlHydra. A similar mechanism is already in place for SqlHydra.Npgsql
jsonb
types that we can use here. I think it should be pretty straight forward change.