-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathDonaldExample.fs
57 lines (47 loc) · 1.54 KB
/
DonaldExample.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module SampleApp.DonaldExample
open System.Data
open Microsoft.Data.SqlClient
open SampleApp.AdventureWorks // Generated Types
open FSharp.Control.Tasks.V2
open Donald
let connect() =
let cs = "Server=localhost,12019;Database=AdventureWorks;User=sa;Password=Password#123;Connect Timeout=3;TrustServerCertificate=True"
let conn = new SqlConnection(cs)
conn.Open()
conn
let getTop10Products(conn: SqlConnection) = task {
use! reader =
conn
|> Db.newCommand "SELECT TOP 10 * FROM Production.Product p"
|> Db.Async.read
let hydra = HydraReader(reader :?> SqlDataReader)
return [
while reader.Read() do
hydra.``Production.Product``.Read()
]
}
let getOrderHeadersAndDetails(conn: SqlConnection) = task {
let sql =
"""
SELECT TOP 10 *
FROM Purchasing.PurchaseOrderHeader h
LEFT JOIN Purchasing.PurchaseOrderDetail d ON h.PurchaseOrderId = d.PurchaseOrderId
"""
use! reader =
conn
|> Db.newCommand sql
|> Db.Async.read
let hydra = HydraReader(reader :?> SqlDataReader)
return [
while reader.Read() do
hydra.``Purchasing.PurchaseOrderHeader``.Read(),
hydra.``Purchasing.PurchaseOrderDetail``.ReadIfNotNull()
]
}
let runQueries() = task {
use conn = connect()
let! products = getTop10Products conn
printfn "Products with Thumbnails Count: %i" (products |> Seq.length)
let! orders = getOrderHeadersAndDetails(conn)
printfn "Orders: %A" orders
}