forked from fsprojects/FSharp.Data.SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.fsx
More file actions
268 lines (206 loc) · 9.11 KB
/
output.fsx
File metadata and controls
268 lines (206 loc) · 9.11 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(*** hide ***)
#r @"..\..\bin\net462\FSharp.Data.SqlClient.dll"
#r "Microsoft.SqlServer.Types.dll"
open FSharp.Data
[<Literal>]
let connectionString = @"Data Source=.;Initial Catalog=AdventureWorks2012;Integrated Security=True;TrustServerCertificate=true"
(**
Controlling output
===============================================
*)
//Connection and query definition are shared for most of the examples below
[<Literal>]
let productsSql = "
SELECT TOP (@top) Name AS ProductName, SellStartDate, Size
FROM Production.Product
WHERE SellStartDate > @SellStartDate
"
(**
* Sequence of custom records is default result set type.
*)
type QueryProductAsRecords = SqlCommandProvider<productsSql, connectionString>
let queryProductAsRecords = new QueryProductAsRecords(connectionString)
let records =
queryProductAsRecords.AsyncExecute(top = 7L, SellStartDate = System.DateTime.Parse "2002-06-01")
|> Async.RunSynchronously
|> List.ofSeq
records |> Seq.iter (printfn "%A")
(**
These records implement `DynamicObject` for easy binding and JSON.NET serialization and `Equals` for structural equality.
* Sync execution
* Seq of tuples as result set type
* Consider ResultType.Tuples to work around unique column name limitation for ResultType.Records.
*)
type QueryProductSync = SqlCommandProvider<productsSql, connectionString, ResultType = ResultType.Tuples>
do
use cmd = new QueryProductSync(connectionString)
let tuples = cmd.Execute(top = 7L, SellStartDate = System.DateTime.Parse "2002-06-01")
for productName, sellStartDate, size in tuples do
printfn "Product name: %s. Sells start date %A, size: %A" productName sellStartDate size
(**
* Typed data table as result set
* DataTable result type is an enabler for data binding and update scenarios. Look at [data modification](data modification.html) for details.
*)
do
use cmd = new SqlCommandProvider<productsSql, connectionString, ResultType.DataTable>(connectionString)
let table = cmd.Execute(top = 7L, SellStartDate = System.DateTime.Parse "2002-06-01")
for row in table.Rows do
printfn "Product name: %s. Sells start date %O, size: %A" row.ProductName row.SellStartDate row.Size
(**
* Single row hint. Must be provided explicitly. Cannot be inferred
* Nullable columns mapped to `Option<_>` type
* Calling SQL Table-Valued Function
*)
type QueryPersonInfoSingletoneAsRecords =
SqlCommandProvider<"SELECT * FROM dbo.ufnGetContactInformation(@PersonId)"
, connectionString
, SingleRow = true>
let singleton = new QueryPersonInfoSingletoneAsRecords(connectionString)
let person = singleton.AsyncExecute(PersonId = 2) |> Async.RunSynchronously |> Option.get
match person.FirstName, person.LastName with
| Some first, Some last -> printfn "Person id: %i, name: %s %s" person.PersonID first last
| _ -> printfn "What's your name %i?" person.PersonID
(**
* Same as previous but using tuples as result type
*)
[<Literal>]
let queryPersonInfoSingletoneQuery =
"SELECT PersonID, FirstName, LastName FROM dbo.ufnGetContactInformation(@PersonId)"
type QueryPersonInfoSingletoneTuples =
SqlCommandProvider<queryPersonInfoSingletoneQuery, connectionString, ResultType.Tuples, SingleRow=true>
QueryPersonInfoSingletoneTuples
.Create(connectionString)
.Execute(PersonId = 2).Value
|> (function
| id, Some first, Some last -> printfn "Person id: %i, name: %s %s" person.PersonID first last
| id, _, _ -> printfn "What's your name %i?" person.PersonID
)
(**
* Same as previous but using typed DataTable as result type
*)
type QueryPersonInfoSingletoneDataTable =
SqlCommandProvider<
"SELECT * FROM dbo.ufnGetContactInformation(@PersonId)",
connectionString,
ResultType = ResultType.DataTable>
do
use cmd = new QueryPersonInfoSingletoneDataTable(connectionString)
let table = cmd .AsyncExecute(PersonId = 2) |> Async.RunSynchronously
for row in table.Rows do
printfn "Person info:Id - %i,FirstName - %O,LastName - %O" row.PersonID row.FirstName row.LastName
// you can refer to the table type
let table2 : QueryPersonInfoSingletoneDataTable.Table =
let cmd = new QueryPersonInfoSingletoneDataTable(connectionString)
cmd.Execute(PersonId = 2)
// you can refer to the row type
for row : QueryPersonInfoSingletoneDataTable.Table.Row in table2.Rows do
printfn "Person info:Id - %i,FirstName - %O,LastName - %O" row.PersonID row.FirstName row.LastName
(**
* Same as previous but using `SqlProgrammabilityProvider<...>`
* Worth noting that Stored Procedure/Function generated command instances have explicit ExecuteSingle/ AsyncExecuteSingle methods because there is no single place to specify SingleRow=true as for SqlCommandProvider.
*)
type AdventureWorks2012 = SqlProgrammabilityProvider<connectionString>
do
use cmd = new AdventureWorks2012.dbo.ufnGetContactInformation(connectionString)
cmd.ExecuteSingle(1) //opt-in for explicit call to
|> Option.iter(fun x ->
printfn "Person info:Id - %i,FirstName - %O,LastName - %O" x.PersonID x.FirstName x.LastName
)
(**
* One column only result set is inferred. Combined with `SingleRow` hint it gives single value as result
* `AsyncExecute/Execute` are just regular F# methods, so args can be passed by name or by position
*)
type QueryPersonInfoSingleValue =
SqlCommandProvider<
"SELECT FirstName + ' ' + LastName FROM dbo.ufnGetContactInformation(@PersonId)",
connectionString,
SingleRow=true>
do
let personId = 2
use cmd = new QueryPersonInfoSingleValue(connectionString)
cmd.Execute( personId)
|> Option.iter (fun name -> printf "Person with id %i has name %s" personId name.Value)
(**
* Single value
* Running the same command more than once with diff params
*)
type GetServerTime =
SqlCommandProvider<
"IF @IsUtc = CAST(1 AS BIT) SELECT GETUTCDATE() ELSE SELECT GETDATE()",
connectionString,
SingleRow=true>
let getSrvTime = new GetServerTime(connectionString)
getSrvTime.AsyncExecute(IsUtc = true) |> Async.RunSynchronously |> printfn "%A"
getSrvTime.Execute(IsUtc = false) |> printfn "%A"
(**
* Non-query
*)
[<Literal>]
let invokeSp = "
EXEC HumanResources.uspUpdateEmployeePersonalInfo
@BusinessEntityID,
@NationalIDNumber,
@BirthDate,
@MaritalStatus,
@Gender
"
type UpdateEmplInfoCommand = SqlCommandProvider<invokeSp, connectionString>
let nonQuery = new UpdateEmplInfoCommand(connectionString)
let rowsAffected =
nonQuery.Execute(
BusinessEntityID = 2, NationalIDNumber = "245797967",
BirthDate = System.DateTime(1965, 09, 01), MaritalStatus = "S", Gender = "F")
(**
* Non-query with MS SQL HierarchyId using `SqlProgrammabilityProvider<...>`
*)
open System
open System.Data
open Microsoft.SqlServer.Types
do
use cmd = new AdventureWorks2012.HumanResources.uspUpdateEmployeeLogin(connectionString)
let hierarchyId = SqlHierarchyId.Parse(SqlTypes.SqlString("/1/4/2/"))
cmd.Execute(
BusinessEntityID = 291,
CurrentFlag = true,
HireDate = DateTime(2013,1,1),
JobTitle = "gatekeeper",
LoginID = "adventure-works\gat0",
OrganizationNode = hierarchyId
)
|> printfn "Records afftected: %i"
(**
### Result sequence is un-buffered by default
Although it implements standard `seq<_>` (`IEnumerable<_>`) interface it can be evaluated only once.
It is done mostly for memory efficiency. It behaves as forward-only cursor similar to underlying SqlDataReader.
If multiple passes over the sequence required use standard `Seq.cache` combinator.
*)
type Get42 = SqlCommandProvider<"SELECT * FROM (VALUES (42), (43)) AS T(N)", connectionString>
let xs = (new Get42(connectionString)).Execute() |> Seq.cache
printfn "#1: %i " <| Seq.nth 0 xs
printfn "#2: %i " <| Seq.nth 1 xs //see it fails here if result is not piped into Seq.cache
(**
### Output result types summary:
* Records (default) .NET-style class with read-only properties. WebAPI/ASP.NET MVC/Json.NET/WPF, Data Binding
* Tuples - convenient option for F# combined with pattern matching
* DataTable with inferred data rows similar to Records. Update scenarios. WPF data binding
* DataReader - for rare cases when structure of output cannot be inferred
In later case, resulting `SqlDataReader` can be wrapped into something like that:
*)
module SqlDataReader =
open Microsoft.Data.SqlClient
let toMaps (reader: SqlDataReader) =
seq {
use __ = reader
while reader.Read() do
yield [
for i = 0 to reader.FieldCount - 1 do
if not( reader.IsDBNull(i))
then yield reader.GetName(i), reader.GetValue(i)
] |> Map.ofList
}
(**
Note that combined with `|> Map.tryFind(key)` this approach can be used to achieve `Option` semantics
for each row, in other words, such function will return `None` for `NULL` values. Keep in mind though that
the incorrect column name will also return `None`.
The same approach can be used to produce `ExpandoObject` for dynamic scenarios.
*)