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
|
|
#region Using
|
28
|
|
using System.Data;
|
29
|
|
using System.Collections;
|
30
|
|
using System.Text;
|
31
|
|
|
32
|
|
using log4net;
|
33
|
|
|
34
|
|
using IBatisNet.Common;
|
35
|
|
using IBatisNet.Common.Utilities.Objects;
|
36
|
|
|
37
|
|
using IBatisNet.DataMapper.Configuration.Statements;
|
38
|
|
using IBatisNet.DataMapper.Configuration.ParameterMapping;
|
39
|
|
using IBatisNet.DataMapper.Exceptions;
|
40
|
|
using IBatisNet.DataMapper.Scope;
|
41
|
|
#endregion
|
42
|
|
|
43
|
|
namespace IBatisNet.DataMapper.Commands
|
44
|
|
{
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
internal class DefaultPreparedCommand : IPreparedCommand
|
49
|
|
{
|
50
|
|
|
51
|
|
#region Fields
|
52
|
|
private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
|
53
|
|
#endregion
|
54
|
|
|
55
|
|
#region IPreparedCommand Members
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
301
|
public IDbCommand Create(RequestScope request, IDalSession session, IStatement statement, object parameterObject )
|
69
|
|
{
|
70
|
|
|
71
|
301
|
IDbCommand command = session.CreateCommand(statement.CommandType);
|
72
|
|
|
73
|
301
|
command.CommandText = request.PreparedStatement.PreparedSql;
|
74
|
|
|
75
|
301
|
if (_logger.IsDebugEnabled)
|
76
|
|
{
|
77
|
301
|
_logger.Debug("PreparedStatement : [" + command.CommandText + "]");
|
78
|
|
}
|
79
|
|
|
80
|
301
|
ApplyParameterMap( session, command, request, statement, parameterObject );
|
81
|
|
|
82
|
300
|
return command;
|
83
|
|
}
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
301
|
protected virtual void ApplyParameterMap
|
95
|
|
( IDalSession session, IDbCommand command,
|
96
|
|
RequestScope request, IStatement statement, object parameterObject )
|
97
|
|
{
|
98
|
301
|
ArrayList properties = request.PreparedStatement.DbParametersName;
|
99
|
301
|
ArrayList parameters = request.PreparedStatement.DbParameters;
|
100
|
301
|
StringBuilder paramLogList = new StringBuilder();
|
101
|
301
|
StringBuilder typeLogList = new StringBuilder();
|
102
|
|
|
103
|
800
|
for ( int i = 0; i < properties.Count; ++i )
|
104
|
|
{
|
105
|
500
|
IDataParameter sqlParameter = (IDataParameter)parameters[i];
|
106
|
500
|
string propertyName = (string)properties[i];
|
107
|
500
|
IDataParameter parameterCopy = command.CreateParameter();
|
108
|
500
|
ParameterProperty property = request.ParameterMap.GetProperty(i);
|
109
|
|
|
110
|
|
#region Logging
|
111
|
500
|
if (_logger.IsDebugEnabled)
|
112
|
|
{
|
113
|
500
|
paramLogList.Append(sqlParameter.ParameterName);
|
114
|
500
|
paramLogList.Append("=[");
|
115
|
500
|
typeLogList.Append(sqlParameter.ParameterName);
|
116
|
500
|
typeLogList.Append("=[");
|
117
|
|
}
|
118
|
|
#endregion
|
119
|
|
|
120
|
500
|
if (command.CommandType == CommandType.StoredProcedure)
|
121
|
|
{
|
122
|
|
#region store procedure command
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
24
|
if (request.ParameterMap == null)
|
127
|
|
{
|
128
|
0
|
throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '"+statement.Id+"'.");
|
129
|
|
}
|
130
|
|
else
|
131
|
|
{
|
132
|
24
|
if (property.DirectionAttribute.Length == 0)
|
133
|
|
{
|
134
|
21
|
property.Direction = sqlParameter.Direction;
|
135
|
|
}
|
136
|
|
|
137
|
|
|
138
|
|
|
139
|
|
|
140
|
|
|
141
|
|
|
142
|
24
|
sqlParameter.Direction = property.Direction;
|
143
|
|
}
|
144
|
|
#endregion
|
145
|
|
}
|
146
|
|
|
147
|
|
#region Logging
|
148
|
500
|
if (_logger.IsDebugEnabled)
|
149
|
|
{
|
150
|
500
|
paramLogList.Append( property.PropertyName );
|
151
|
500
|
paramLogList.Append( "," );
|
152
|
|
}
|
153
|
|
#endregion
|
154
|
|
|
155
|
500
|
request.ParameterMap.SetParameter(property, parameterCopy, parameterObject );
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
499
|
parameterCopy.Direction = sqlParameter.Direction;
|
161
|
|
|
162
|
|
|
163
|
499
|
if (statement.ParameterMap != null)
|
164
|
|
{
|
165
|
445
|
if (request.ParameterMap.GetProperty(i).DbType != null &&
|
166
|
|
request.ParameterMap.GetProperty(i).DbType.Length >0)
|
167
|
|
{
|
168
|
64
|
string dbTypePropertyName = session.DataSource.Provider.ParameterDbTypeProperty;
|
169
|
|
|
170
|
64
|
ObjectProbe.SetPropertyValue(parameterCopy, dbTypePropertyName, ObjectProbe.GetPropertyValue(sqlParameter, dbTypePropertyName));
|
171
|
|
}
|
172
|
|
else
|
173
|
|
{
|
174
|
|
|
175
|
|
}
|
176
|
|
}
|
177
|
|
else
|
178
|
|
{
|
179
|
|
|
180
|
|
}
|
181
|
|
|
182
|
|
|
183
|
|
#region Logging
|
184
|
499
|
if (_logger.IsDebugEnabled)
|
185
|
|
{
|
186
|
499
|
if (parameterCopy.Value == System.DBNull.Value)
|
187
|
|
{
|
188
|
29
|
paramLogList.Append("null");
|
189
|
29
|
paramLogList.Append( "], " );
|
190
|
29
|
typeLogList.Append("System.DBNull, null");
|
191
|
29
|
typeLogList.Append( "], " );
|
192
|
|
}
|
193
|
|
else
|
194
|
|
{
|
195
|
|
|
196
|
470
|
paramLogList.Append( parameterCopy.Value.ToString() );
|
197
|
470
|
paramLogList.Append( "], " );
|
198
|
|
|
199
|
|
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
470
|
typeLogList.Append( parameterCopy.DbType.ToString() );
|
206
|
470
|
typeLogList.Append( ", " );
|
207
|
470
|
typeLogList.Append( parameterCopy.Value.GetType().ToString() );
|
208
|
470
|
typeLogList.Append( "], " );
|
209
|
|
}
|
210
|
|
}
|
211
|
|
#endregion
|
212
|
|
|
213
|
|
|
214
|
499
|
if (session.DataSource.Provider.SetDbParameterSize)
|
215
|
|
{
|
216
|
499
|
if (((IDbDataParameter)sqlParameter).Size > 0)
|
217
|
|
{
|
218
|
13
|
((IDbDataParameter)parameterCopy).Size = ((IDbDataParameter)sqlParameter).Size;
|
219
|
|
}
|
220
|
|
}
|
221
|
|
|
222
|
499
|
if (session.DataSource.Provider.SetDbParameterPrecision)
|
223
|
|
{
|
224
|
499
|
((IDbDataParameter)parameterCopy).Precision = ((IDbDataParameter)sqlParameter).Precision;
|
225
|
|
}
|
226
|
|
|
227
|
499
|
if (session.DataSource.Provider.SetDbParameterScale)
|
228
|
|
{
|
229
|
499
|
((IDbDataParameter)parameterCopy).Scale = ((IDbDataParameter)sqlParameter).Scale;
|
230
|
|
}
|
231
|
|
|
232
|
499
|
parameterCopy.ParameterName = sqlParameter.ParameterName;
|
233
|
|
|
234
|
499
|
command.Parameters.Add( parameterCopy );
|
235
|
|
}
|
236
|
|
|
237
|
|
#region Logging
|
238
|
|
|
239
|
300
|
if (_logger.IsDebugEnabled && properties.Count>0)
|
240
|
|
{
|
241
|
219
|
_logger.Debug("Parameters: [" + paramLogList.ToString(0, paramLogList.Length - 2) + "]");
|
242
|
219
|
_logger.Debug("Types: [" + typeLogList.ToString(0, typeLogList.Length - 2) + "]");
|
243
|
|
}
|
244
|
|
#endregion
|
245
|
|
}
|
246
|
|
|
247
|
|
#endregion
|
248
|
|
}
|
249
|
|
}
|
250
|
|
|