1
|
|
#region Apache Notice
|
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
|
|
#endregion
|
27
|
|
|
28
|
|
#region Imports
|
29
|
|
|
30
|
|
using System;
|
31
|
|
using System.Data;
|
32
|
|
using System.Collections;
|
33
|
|
using IBatisNet.Common;
|
34
|
|
using IBatisNet.Common.Utilities.Objects;
|
35
|
|
using IBatisNet.DataMapper.Configuration.Statements;
|
36
|
|
using IBatisNet.DataMapper.Configuration.ParameterMapping;
|
37
|
|
using IBatisNet.DataMapper.Exceptions;
|
38
|
|
using IBatisNet.DataMapper.Scope;
|
39
|
|
|
40
|
|
#endregion
|
41
|
|
|
42
|
|
namespace IBatisNet.DataMapper.Commands
|
43
|
|
{
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
internal class EmbedParamsPreparedCommand : DefaultPreparedCommand
|
48
|
|
{
|
49
|
|
#region EmbedParamsPreparedCommand Members
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
0
|
protected override void ApplyParameterMap
|
60
|
|
(IDalSession session, IDbCommand command,
|
61
|
|
RequestScope request, IStatement statement, object parameterObject)
|
62
|
|
{
|
63
|
|
ArrayList properties = request.PreparedStatement.DbParametersName;
|
64
|
|
ArrayList parameters = request.PreparedStatement.DbParameters;
|
65
|
|
object parameterValue = null;
|
66
|
|
|
67
|
|
for (int i = 0; i < properties.Count; ++i)
|
68
|
|
{
|
69
|
|
IDataParameter sqlParameter = (IDataParameter) parameters[i];
|
70
|
|
string propertyName = (string) properties[i];
|
71
|
|
|
72
|
|
if (command.CommandType == CommandType.Text)
|
73
|
|
{
|
74
|
|
if (propertyName != "value")
|
75
|
|
{
|
76
|
|
ParameterProperty property = request.ParameterMap.GetProperty(i);
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
}
|
81
|
|
else
|
82
|
|
{
|
83
|
|
parameterValue = parameterObject;
|
84
|
|
}
|
85
|
|
}
|
86
|
|
else
|
87
|
|
{
|
88
|
|
|
89
|
|
|
90
|
|
if (request.ParameterMap == null)
|
91
|
|
{
|
92
|
|
throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '" + statement.Id + "'.");
|
93
|
|
}
|
94
|
|
else
|
95
|
|
{
|
96
|
|
ParameterProperty property = request.ParameterMap.GetProperty(i);
|
97
|
|
|
98
|
|
if (property.DirectionAttribute.Length == 0)
|
99
|
|
{
|
100
|
|
property.Direction = sqlParameter.Direction;
|
101
|
|
}
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
|
sqlParameter.Direction = property.Direction;
|
109
|
|
|
110
|
|
}
|
111
|
|
}
|
112
|
|
|
113
|
|
IDataParameter parameterCopy = command.CreateParameter();
|
114
|
|
|
115
|
|
sqlParameter.Value = parameterValue;
|
116
|
|
parameterCopy.Value = parameterValue;
|
117
|
|
|
118
|
|
parameterCopy.Direction = sqlParameter.Direction;
|
119
|
|
|
120
|
|
|
121
|
|
if (statement.ParameterMap != null)
|
122
|
|
{
|
123
|
|
if (request.ParameterMap.GetProperty(i).DbType != null &&
|
124
|
|
request.ParameterMap.GetProperty(i).DbType.Length >0)
|
125
|
|
{
|
126
|
|
string dbTypePropertyName = session.DataSource.Provider.ParameterDbTypeProperty;
|
127
|
|
|
128
|
|
ObjectProbe.SetPropertyValue(parameterCopy, dbTypePropertyName, ObjectProbe.GetPropertyValue(sqlParameter, dbTypePropertyName));
|
129
|
|
}
|
130
|
|
else
|
131
|
|
{
|
132
|
|
|
133
|
|
}
|
134
|
|
}
|
135
|
|
else
|
136
|
|
{
|
137
|
|
|
138
|
|
}
|
139
|
|
|
140
|
|
|
141
|
|
if (session.DataSource.Provider.SetDbParameterSize)
|
142
|
|
{
|
143
|
|
if (((IDbDataParameter)sqlParameter).Size > 0)
|
144
|
|
{
|
145
|
|
((IDbDataParameter)parameterCopy).Size = ((IDbDataParameter)sqlParameter).Size;
|
146
|
|
}
|
147
|
|
}
|
148
|
|
|
149
|
|
if (session.DataSource.Provider.SetDbParameterPrecision)
|
150
|
|
{
|
151
|
|
((IDbDataParameter)parameterCopy).Precision = ((IDbDataParameter)sqlParameter).Precision;
|
152
|
|
}
|
153
|
|
|
154
|
|
if (session.DataSource.Provider.SetDbParameterScale)
|
155
|
|
{
|
156
|
|
((IDbDataParameter)parameterCopy).Scale = ((IDbDataParameter)sqlParameter).Scale;
|
157
|
|
}
|
158
|
|
|
159
|
|
parameterCopy.ParameterName = sqlParameter.ParameterName;
|
160
|
|
|
161
|
|
command.Parameters.Add(parameterCopy);
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
|
|
172
|
|
IDataParameter p;
|
173
|
|
for (int iCnt = command.Parameters.Count - 1; iCnt >= 0; iCnt--)
|
174
|
|
{
|
175
|
|
p = (IDataParameter) command.Parameters[iCnt];
|
176
|
|
if (p.Direction == ParameterDirection.Input &&
|
177
|
|
command.CommandText.IndexOf(p.ParameterName) > 0)
|
178
|
|
{
|
179
|
|
switch (p.DbType)
|
180
|
|
{
|
181
|
|
case DbType.String:
|
182
|
|
case DbType.AnsiString:
|
183
|
|
case
|
184
|
|
DbType.AnsiStringFixedLength:
|
185
|
|
case DbType.StringFixedLength:
|
186
|
|
command.CommandText =
|
187
|
|
command.CommandText.Replace(p.ParameterName,
|
188
|
|
"\'" + p.Value.ToString().Replace("\'", "\'\'") + "\'");
|
189
|
|
break;
|
190
|
|
case DbType.Date:
|
191
|
|
case DbType.DateTime:
|
192
|
|
DateTime v =
|
193
|
|
Convert.ToDateTime(p.Value);
|
194
|
|
command.CommandText =
|
195
|
|
command.CommandText.Replace(p.ParameterName,
|
196
|
|
String.Format("\'{0}.{1}.{2} {3}:{4}:{5}.{6}\'", v.Year, v.Month,
|
197
|
|
v.Day, v.Hour, v.Minute, v.Second, v.Millisecond));
|
198
|
|
|
199
|
|
command.CommandText.Replace(p.ParameterName,
|
200
|
|
"\'" + p.Value.ToString() + "\'");
|
201
|
|
break;
|
202
|
|
case DbType.Double:
|
203
|
|
case DbType.Decimal:
|
204
|
|
case DbType.Currency:
|
205
|
|
case DbType.Single:
|
206
|
|
command.CommandText =
|
207
|
|
command.CommandText.Replace(p.ParameterName,
|
208
|
|
p.Value.ToString().Replace(',', '.'));
|
209
|
|
break;
|
210
|
|
default:
|
211
|
|
command.CommandText =
|
212
|
|
command.CommandText.Replace(p.ParameterName, p.Value.ToString());
|
213
|
|
break;
|
214
|
|
}
|
215
|
|
command.Parameters.RemoveAt(iCnt);
|
216
|
|
}
|
217
|
|
}
|
218
|
|
|
219
|
|
|
220
|
|
}
|
221
|
|
}
|
222
|
|
|
223
|
|
#endregion
|
224
|
|
}
|
225
|
|
}
|
226
|
|
|
227
|
|
|