forked from fsprojects/Rezoom.SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsynchronous.fs
More file actions
132 lines (115 loc) · 6.62 KB
/
Asynchronous.fs
File metadata and controls
132 lines (115 loc) · 6.62 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
namespace Rezoom.SQL.Asynchronous
open System.Runtime.CompilerServices
open System.Threading
open System.Collections.Generic
open System.Data.Common
open FSharp.Control.Tasks.ContextInsensitive
open Rezoom.SQL
open Rezoom.SQL.Mapping
/// Extension methods for executing commands asynchronously against a database.
[<Extension>]
type Extensions =
/// Execute the command on a connection and return its result sets.
[<Extension>]
static member Execute(cmd : Command<'a>, conn : DbConnection, token : CancellationToken) =
task {
let batch = AsyncCommandBatch(conn, tran = null)
return! batch.Batch cmd token
}
/// Execute the command on a connection and return its result sets.
[<Extension>]
static member Execute(cmd : Command<'a>, conn : DbConnection) =
cmd.Execute(conn, CancellationToken.None)
/// Execute the command on a connection and return its result sets.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member Execute(cmd : Command<'a>, context : ConnectionContext, token : CancellationToken) =
cmd.Execute(context.GetConnection(cmd.ConnectionName), token)
/// Execute the command on a connection and return its result sets.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member Execute(cmd : Command<'a>, context : ConnectionContext) =
cmd.Execute(context, CancellationToken.None)
/// Execute the command on a connection and return its scalar result.
[<Extension>]
static member ExecuteScalar(cmd : Command<#IScalar<_>>, conn : DbConnection, token) =
task {
let! result = cmd.Execute(conn, token)
return result.ScalarValue
}
/// Execute the command on a connection and return its scalar result.
[<Extension>]
static member ExecuteScalar(cmd : Command<#IScalar<_>>, conn : DbConnection) =
cmd.ExecuteScalar(conn, CancellationToken.None)
/// Execute the command on a connection and return its scalar result.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member ExecuteScalar(cmd : Command<#IScalar<_>>, context : ConnectionContext, token) =
cmd.ExecuteScalar(context.GetConnection(cmd.ConnectionName), token)
/// Execute the command on a connection and return its scalar result.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member ExecuteScalar(cmd : Command<#IScalar<_>>, context : ConnectionContext) =
cmd.ExecuteScalar(context, CancellationToken.None)
/// Execute the command on a connection and return the optional first and only row of its single result set.
/// If the command returns more than 1 row, this throws an exception.
[<Extension>]
static member ExecuteTryExactlyOne(cmd : Command<#IReadOnlyList<_>>, conn : DbConnection, token) =
task {
let! result = cmd.Execute(conn, token)
return
if result.Count > 1 then
failwith "Expected no more than one result from SQL command"
elif result.Count = 0 then None
else Some <| result.[0]
}
/// Execute the command on a connection and return the optional first and only row of its single result set.
/// If the command returns more than 1 row, this throws an exception.
[<Extension>]
static member ExecuteTryExactlyOne(cmd : Command<#IReadOnlyList<_>>, conn : DbConnection) =
cmd.ExecuteTryExactlyOne(conn, CancellationToken.None)
/// Execute the command on a connection and return the optional first and only row of its single result set.
/// If the command returns more than 1 row, this throws an exception.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member ExecuteTryExactlyOne(cmd : Command<#IReadOnlyList<_>>, context : ConnectionContext, token) =
cmd.ExecuteTryExactlyOne(context.GetConnection(cmd.ConnectionName), token)
/// Execute the command on a connection and return the optional first and only row of its single result set.
/// If the command returns more than 1 row, this throws an exception.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member ExecuteTryExactlyOne(cmd : Command<#IReadOnlyList<_>>, context : ConnectionContext) =
cmd.ExecuteTryExactlyOne(context, CancellationToken.None)
/// Execute the command on a connection and return the first and only row of its single result set.
/// If the command returns no rows or more than 1 row, this throws an exception.
[<Extension>]
static member ExecuteExactlyOne(cmd : Command<#IReadOnlyList<_>>, conn : DbConnection, token) =
task {
let! result = cmd.Execute(conn, token)
return result |> Seq.exactlyOne
}
/// Execute the command on a connection and return the first and only row of its single result set.
/// If the command returns no rows or more than 1 row, this throws an exception.
[<Extension>]
static member ExecuteExactlyOne(cmd : Command<#IReadOnlyList<_>>, conn : DbConnection) =
cmd.ExecuteExactlyOne(conn, CancellationToken.None)
/// Execute the command on a connection and return the first and only row of its single result set.
/// If the command returns no rows or more than 1 row, this throws an exception.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member ExecuteExactlyOne(cmd : Command<#IReadOnlyList<_>>, context : ConnectionContext, token) =
cmd.ExecuteExactlyOne(context.GetConnection(cmd.ConnectionName), token)
/// Execute the command on a connection and return the first and only row of its single result set.
/// If the command returns no rows or more than 1 row, this throws an exception.
/// The connection is obtained from the given `ConnectionContext` according to the command's `ConnectionName`
/// property.
[<Extension>]
static member ExecuteExactlyOne(cmd : Command<#IReadOnlyList<_>>, context : ConnectionContext) =
cmd.ExecuteExactlyOne(context, CancellationToken.None)