Skip to content

Instantly share code, notes, and snippets.

@colinbull
Last active August 29, 2015 14:06
Show Gist options
  • Save colinbull/4ec846e146f4ca15f3e1 to your computer and use it in GitHub Desktop.
Save colinbull/4ec846e146f4ca15f3e1 to your computer and use it in GitHub Desktop.
SqlProvider with ComposableQuery
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
Copy link

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

@colinbull
Copy link
Author

@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