1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
using System;
|
28
|
|
using System.Collections;
|
29
|
|
using System.Data;
|
30
|
|
using System.Data.Common;
|
31
|
|
using System.Reflection;
|
32
|
|
|
33
|
|
using IBatisNet.Common.Exceptions;
|
34
|
|
|
35
|
|
namespace IBatisNet.Common.Utilities
|
36
|
|
{
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
public class DBHelperParameterCache
|
43
|
|
{
|
44
|
|
|
45
|
|
|
46
|
0
|
private DBHelperParameterCache() {}
|
47
|
|
|
48
|
|
#region Private fields
|
49
|
|
private static Hashtable _paramCache = Hashtable.Synchronized(new Hashtable());
|
50
|
|
#endregion
|
51
|
|
|
52
|
|
#region private methods
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
4
|
private static IDataParameter[] DiscoverSpParameterSet(IDalSession session, string spName, bool includeReturnValueParameter)
|
62
|
|
{
|
63
|
4
|
using (IDbConnection connection = session.DataSource.Provider.GetConnection())
|
64
|
|
{
|
65
|
4
|
connection.ConnectionString = session.DataSource.ConnectionString;
|
66
|
4
|
connection.Open();
|
67
|
4
|
return InternalDiscoverSpParameterSet(session.DataSource.Provider, connection, spName, includeReturnValueParameter);
|
68
|
|
}
|
69
|
|
}
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
4
|
private static IDataParameter[] InternalDiscoverSpParameterSet(Provider provider,
|
80
|
|
IDbConnection connection, string spName, bool includeReturnValueParameter)
|
81
|
|
{
|
82
|
4
|
using (IDbCommand cmd = connection.CreateCommand())
|
83
|
|
{
|
84
|
4
|
cmd.CommandType = CommandType.StoredProcedure;
|
85
|
4
|
cmd.CommandText = spName;
|
86
|
|
|
87
|
4
|
DeriveParameters(provider, cmd);
|
88
|
|
|
89
|
4
|
if (cmd.Parameters.Count > 0) {
|
90
|
4
|
IDataParameter firstParameter = (IDataParameter)cmd.Parameters[0];
|
91
|
4
|
if (firstParameter.Direction == ParameterDirection.ReturnValue) {
|
92
|
4
|
if (!includeReturnValueParameter) {
|
93
|
4
|
cmd.Parameters.RemoveAt(0);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
}
|
97
|
|
|
98
|
|
|
99
|
4
|
IDataParameter[] discoveredParameters = new IDataParameter[cmd.Parameters.Count];
|
100
|
|
|
101
|
4
|
cmd.Parameters.CopyTo(discoveredParameters, 0);
|
102
|
|
|
103
|
4
|
return discoveredParameters;
|
104
|
|
}
|
105
|
|
}
|
106
|
|
|
107
|
4
|
private static void DeriveParameters(Provider provider, IDbCommand command)
|
108
|
|
{
|
109
|
4
|
Type commandBuilderType;
|
110
|
|
|
111
|
|
|
112
|
4
|
if (provider == null)
|
113
|
0
|
throw new ArgumentNullException("provider");
|
114
|
4
|
if ((provider.CommandBuilderClass == null) || (provider.CommandBuilderClass.Length < 1))
|
115
|
0
|
throw new Exception(String.Format(
|
116
|
|
"CommandBuilderClass not defined for provider \"{0}\".",
|
117
|
|
provider.Name));
|
118
|
4
|
commandBuilderType = provider.GetCommandBuilderType();
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
4
|
try
|
123
|
|
{
|
124
|
4
|
commandBuilderType.InvokeMember("DeriveParameters",
|
125
|
|
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null,
|
126
|
|
new object[] {command});
|
127
|
|
}
|
128
|
|
catch(Exception ex)
|
129
|
|
{
|
130
|
0
|
throw new IBatisNetException("Could not retrieve parameters for the store procedure named "+command.CommandText, ex);
|
131
|
|
}
|
132
|
|
}
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
|
138
|
|
|
139
|
6
|
private static IDataParameter[] CloneParameters(IDataParameter[] originalParameters)
|
140
|
|
{
|
141
|
6
|
IDataParameter[] clonedParameters = new IDataParameter[originalParameters.Length];
|
142
|
|
|
143
|
27
|
for (int i = 0, j = originalParameters.Length; i < j; i++)
|
144
|
|
{
|
145
|
21
|
clonedParameters[i] = (IDataParameter) ((ICloneable) originalParameters[i]).Clone();
|
146
|
|
}
|
147
|
|
|
148
|
6
|
return clonedParameters;
|
149
|
|
}
|
150
|
|
|
151
|
|
#endregion private methods, variables, and constructors
|
152
|
|
|
153
|
|
#region caching functions
|
154
|
|
|
155
|
|
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
0
|
public static void CacheParameterSet(string connectionString, string commandText, params IDataParameter[] commandParameters)
|
162
|
|
{
|
163
|
|
string hashKey = connectionString + ":" + commandText;
|
164
|
|
|
165
|
|
_paramCache[hashKey] = commandParameters;
|
166
|
|
}
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
|
|
172
|
|
|
173
|
0
|
public static void Clear()
|
174
|
|
{
|
175
|
|
_paramCache.Clear();
|
176
|
|
}
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
0
|
public static IDataParameter[] GetCachedParameterSet(string connectionString, string commandText)
|
185
|
|
{
|
186
|
|
string hashKey = connectionString + ":" + commandText;
|
187
|
|
|
188
|
|
IDataParameter[] cachedParameters = (IDataParameter[]) _paramCache[hashKey];
|
189
|
|
|
190
|
|
if (cachedParameters == null)
|
191
|
|
{
|
192
|
|
return null;
|
193
|
|
}
|
194
|
|
else
|
195
|
|
{
|
196
|
|
return CloneParameters(cachedParameters);
|
197
|
|
}
|
198
|
|
}
|
199
|
|
|
200
|
|
#endregion caching functions
|
201
|
|
|
202
|
|
#region Parameter Discovery Functions
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
|
|
208
|
|
|
209
|
|
|
210
|
|
|
211
|
|
|
212
|
|
|
213
|
6
|
public static IDataParameter[] GetSpParameterSet(IDalSession session, string spName)
|
214
|
|
{
|
215
|
6
|
return GetSpParameterSet(session, spName, false);
|
216
|
|
}
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
|
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
|
|
228
|
6
|
public static IDataParameter[] GetSpParameterSet(IDalSession session
|
229
|
|
, string spName, bool includeReturnValueParameter)
|
230
|
|
{
|
231
|
6
|
string hashKey = session.DataSource.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter":"");
|
232
|
|
|
233
|
6
|
IDataParameter[] cachedParameters;
|
234
|
|
|
235
|
6
|
cachedParameters = (IDataParameter[]) _paramCache[hashKey];
|
236
|
|
|
237
|
6
|
if (cachedParameters == null)
|
238
|
|
{
|
239
|
4
|
_paramCache[hashKey] = DiscoverSpParameterSet(session, spName, includeReturnValueParameter);
|
240
|
4
|
cachedParameters = (IDataParameter[]) _paramCache[hashKey];
|
241
|
|
}
|
242
|
|
|
243
|
6
|
return CloneParameters(cachedParameters);
|
244
|
|
}
|
245
|
|
|
246
|
|
#endregion Parameter Discovery Functions
|
247
|
|
}
|
248
|
|
}
|
249
|
|
|