Last active
August 29, 2015 14:06
-
-
Save colinbull/4ec846e146f4ca15f3e1 to your computer and use it in GitHub Desktop.
SqlProvider with ComposableQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open FSharp.Data.Sql | |
open FSharpComposableQuery | |
FSharp.Data.Sql.Common.QueryEvents.SqlQueryEvent |> Event.add (printfn "Executing SQL: %s") | |
type HR = SqlDataProvider<ConnectionStringName = "***********", DatabaseVendor = Common.DatabaseProviderTypes.ORACLE> | |
let ctx = HR.GetDataContext() | |
type AuditRecord = { | |
Id : decimal | |
Message : string | |
User : string | |
} | |
type Predicate = | |
| After of DateTime | |
| Before of DateTime | |
| And of Predicate * Predicate | |
| Or of Predicate * Predicate | |
| Not of Predicate | |
let audits = | |
<@ fun p -> query { | |
for u in ctx.AuditTrail do | |
if p u.RecordedOn | |
then yield u | |
} @> | |
let rec eval(t) = | |
match t with | |
| After n -> <@ fun x -> x >= n @> | |
| Before n -> <@ fun x -> x < n @> | |
| And (t1,t2) -> <@ fun x -> (%eval t1) x && (%eval t2) x @> | |
| Or (t1,t2) -> <@ fun x -> (%eval t1) x || (%eval t2) x @> | |
| Not (t0) -> <@ fun x -> not((%eval t0) x) @> | |
[<EntryPoint>] | |
let main argv = | |
let search = And(After(DateTime(2014, 09, 23)), Before(DateTime(2014, 09, 26))) | |
let result = | |
query { for aud in ((%audits) (%eval search)) do | |
yield { Id = aud.AuditTrailId; Message = aud.AuditMessage; User = aud.RecordedBy } | |
} |> Seq.toList | |
result |> List.iter (printfn "%A") | |
printfn "Finished" | |
Console.ReadLine() |> ignore |
@christolo Sorry, missed this comment... I should have mentioned that you will need my for to get this stuff working..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get this exception:
Message: A first chance exception of type 'System.Exception' occurred in FSharp.Core.dll
Additional information: Unsupported expression. Ensure all server-side objects appear on the left hand side of predicates. The In and Not In operators only support the inline array syntax.
Tried both FSharp.Core 4.3.0.0 and 4.3.1.0. Ideas?
@chriskeenan