-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTransaction.CreateCommand.cs
97 lines (88 loc) · 5.3 KB
/
Transaction.CreateCommand.cs
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
namespace Open.Database.Extensions;
/// <summary>
/// Core non-DB-specific extensions for database transactions.
/// </summary>
public static partial class TransactionExtensions
{
const string EmptyOrWhiteSpace = "Command is empty or whitespace.";
/// <summary>
/// Shortcut for creating an IDbCommand from any IDbTransaction.
/// </summary>
/// <param name="transaction">The transaction to create a command from.</param>
/// <param name="type">The command type. Text, StoredProcedure, or TableDirect.</param>
/// <param name="commandText">The command text or stored procedure name to use.</param>
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
/// <returns>The created SqlCommand.</returns>
public static IDbCommand CreateCommand(this IDbTransaction transaction,
CommandType type, string commandText, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
{
if (transaction is null) throw new ArgumentNullException(nameof(transaction));
if (commandText is null) throw new ArgumentNullException(nameof(commandText));
if (string.IsNullOrWhiteSpace(commandText)) throw new ArgumentException(EmptyOrWhiteSpace, nameof(commandText));
if (transaction.Connection is null) throw new InvalidOperationException("Transaction has no connection.");
Contract.EndContractBlock();
IDbCommand command = transaction.Connection.CreateCommand(type, commandText, secondsTimeout);
command.Transaction = transaction;
return command;
}
/// <summary>
/// Shortcut for creating a text IDbCommand from any IDbTransaction.
/// </summary>
/// <param name="transaction">The transaction to create a command from.</param>
/// <param name="commandText">The command text or stored procedure name to use.</param>
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
/// <returns>The created SqlCommand.</returns>
public static IDbCommand CreateTextCommand(this IDbTransaction transaction,
string commandText, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
=> transaction.CreateCommand(CommandType.Text, commandText, secondsTimeout);
/// <summary>
/// Shortcut for creating a stored procedure IDbCommand from any IDbTransaction.
/// </summary>
/// <param name="transaction">The transaction to create a command from.</param>
/// <param name="procedureName">The command text or stored procedure name to use.</param>
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
/// <returns>The created SqlCommand.</returns>
public static IDbCommand CreateStoredProcedureCommand(this IDbTransaction transaction,
string procedureName, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
=> transaction.CreateCommand(CommandType.StoredProcedure, procedureName, secondsTimeout);
/// <summary>
/// Shortcut for creating an DbCommand from any DbTransaction.
/// </summary>
/// <param name="transaction">The transaction to create a command from.</param>
/// <param name="type">The command type. Text, StoredProcedure, or TableDirect.</param>
/// <param name="commandText">The command text or stored procedure name to use.</param>
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
/// <returns>The created SqlCommand.</returns>
public static DbCommand CreateCommand(this DbTransaction transaction,
CommandType type, string commandText, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
{
if (transaction is null) throw new ArgumentNullException(nameof(transaction));
if (commandText is null) throw new ArgumentNullException(nameof(commandText));
if (string.IsNullOrWhiteSpace(commandText)) throw new ArgumentException(EmptyOrWhiteSpace, nameof(commandText));
if (transaction.Connection is null) throw new InvalidOperationException("Transaction has no connection.");
Contract.EndContractBlock();
DbCommand command = transaction.Connection.CreateCommand(type, commandText, secondsTimeout);
command.Transaction = transaction;
return command;
}
/// <summary>
/// Shortcut for creating a text DbCommand from any DbTransaction.
/// </summary>
/// <param name="transaction">The transaction to create a command from.</param>
/// <param name="commandText">The command text or stored procedure name to use.</param>
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
/// <returns>The created SqlCommand.</returns>
public static DbCommand CreateTextCommand(this DbTransaction transaction,
string commandText, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
=> transaction.CreateCommand(CommandType.Text, commandText, secondsTimeout);
/// <summary>
/// Shortcut for creating a stored procedure DbCommand from any DbTransaction.
/// </summary>
/// <param name="transaction">The transaction to create a command from.</param>
/// <param name="procedureName">The command text or stored procedure name to use.</param>
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
/// <returns>The created SqlCommand.</returns>
public static DbCommand CreateStoredProcedureCommand(this DbTransaction transaction,
string procedureName, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
=> transaction.CreateCommand(CommandType.StoredProcedure, procedureName, secondsTimeout);
}