-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathSqlClientSqlCommandSet.cs
137 lines (120 loc) · 4.33 KB
/
SqlClientSqlCommandSet.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
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
using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using NHibernate.Util;
namespace NHibernate.AdoNet
{
using SqlCommand = System.Data.SqlClient.SqlCommand;
/// <summary>
/// Expose the batch functionality in ADO.Net 4.0
/// Microsoft in its wisdom decided to make my life hard and mark it internal.
/// Through the use of Reflection and some delegates magic, I opened up the functionality.
///
/// Observable performance benefits are 50%+ when used, so it is really worth it.
/// </summary>
public class SqlClientSqlCommandSet : IDisposable
{
private static readonly System.Type sqlCmdSetType;
private static readonly Action<object, SqlConnection> connectionSetter;
private static readonly Action<object, SqlTransaction> transactionSetter;
private static readonly Action<object, int> commandTimeoutSetter;
private static readonly Func<object, SqlConnection> connectionGetter;
private static readonly Func<object, SqlCommand> batchCommandGetter;
private static readonly Action<object, SqlCommand> doAppend;
private static readonly Func<object, int> doExecuteNonQuery;
private static readonly Action<object> doDispose;
private readonly object instance;
private int countOfCommands;
static SqlClientSqlCommandSet()
{
var sysData = Assembly.Load("System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
sqlCmdSetType = sysData.GetType("System.Data.SqlClient.SqlCommandSet");
Debug.Assert(sqlCmdSetType != null, "Could not find SqlCommandSet!");
connectionSetter = DelegateHelper.BuildPropertySetter<SqlConnection>(sqlCmdSetType, "Connection");
connectionGetter = DelegateHelper.BuildPropertyGetter<SqlConnection>(sqlCmdSetType, "Connection");
transactionSetter = DelegateHelper.BuildPropertySetter<SqlTransaction>(sqlCmdSetType, "Transaction");
commandTimeoutSetter = DelegateHelper.BuildPropertySetter<int>(sqlCmdSetType, "CommandTimeout");
batchCommandGetter = DelegateHelper.BuildPropertyGetter<SqlCommand>(sqlCmdSetType, "BatchCommand");
doAppend = DelegateHelper.BuildAction<SqlCommand>(sqlCmdSetType, "Append");
doExecuteNonQuery = DelegateHelper.BuildFunc<int>(sqlCmdSetType, "ExecuteNonQuery");
doDispose = DelegateHelper.BuildAction(sqlCmdSetType, "Dispose");
}
public SqlClientSqlCommandSet()
{
instance = Activator.CreateInstance(sqlCmdSetType, true);
}
/// <summary>
/// Append a command to the batch
/// </summary>
/// <param name="command"></param>
public void Append(SqlCommand command)
{
AssertHasParameters(command);
doAppend(instance, command);
countOfCommands++;
}
/// <summary>
/// This is required because SqlClient.SqlCommandSet will throw if
/// the command has no parameters.
/// </summary>
/// <param name="command"></param>
private static void AssertHasParameters(SqlCommand command)
{
if (command.Parameters.Count == 0)
{
throw new ArgumentException("A command in SqlCommandSet must have parameters. You can't pass hardcoded sql strings.");
}
}
/// <summary>
/// Return the batch command to be executed
/// </summary>
public SqlCommand BatchCommand
{
get { return batchCommandGetter(instance); }
}
/// <summary>
/// The number of commands batched in this instance
/// </summary>
public int CountOfCommands
{
get { return countOfCommands; }
}
/// <summary>
/// Executes the batch
/// </summary>
/// <returns>
/// This seems to be returning the total number of affected rows in all queries
/// </returns>
public int ExecuteNonQuery()
{
if (Connection == null)
throw new ArgumentNullException(
"Connection was not set! You must set the connection property before calling ExecuteNonQuery()");
if (CountOfCommands == 0)
return 0;
return doExecuteNonQuery(instance);
}
public SqlConnection Connection
{
get { return connectionGetter(instance); }
set { connectionSetter(instance, value); }
}
public SqlTransaction Transaction
{
set { transactionSetter(instance, value); }
}
public int CommandTimeout
{
set { commandTimeoutSetter(instance, value); }
}
///<summary>
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///</summary>
///<filterpriority>2</filterpriority>
public void Dispose()
{
doDispose(instance);
}
}
}