forked from fsprojects/Rezoom.SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronous.fs
More file actions
67 lines (59 loc) · 3.33 KB
/
Synchronous.fs
File metadata and controls
67 lines (59 loc) · 3.33 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
namespace Rezoom.SQL.Synchronous
open System.Runtime.CompilerServices
open System.Threading
open System.Collections.Generic
open System.Data.Common
open Rezoom.SQL
open Rezoom.SQL.Mapping
/// Extension methods for executing commands synchronously 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) =
let batch = SyncCommandBatch(conn, tran = null)
batch.Batch cmd ()
/// 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.GetConnection(cmd.ConnectionName))
/// Execute the command on a connection and return its scalar result.
[<Extension>]
static member ExecuteScalar(cmd : Command<#IScalar<_>>, conn : DbConnection) =
cmd.Execute(conn).ScalarValue
/// 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.GetConnection(cmd.ConnectionName))
/// 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) =
let result = cmd.Execute(conn)
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.
/// 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.GetConnection(cmd.ConnectionName))
/// 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.Execute(conn) |> 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.
/// 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.GetConnection(cmd.ConnectionName))