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
|
|
#endregion
|
25
|
|
|
26
|
|
#region Using
|
27
|
|
|
28
|
|
using System;
|
29
|
|
using System.Collections;
|
30
|
|
using System.Text;
|
31
|
|
|
32
|
|
using IBatisNet.DataMapper.Configuration.Sql.Dynamic;
|
33
|
|
using IBatisNet.DataMapper.Scope;
|
34
|
|
using IBatisNet.DataMapper.TypeHandlers;
|
35
|
|
using IBatisNet.DataMapper.Exceptions;
|
36
|
|
|
37
|
|
using IBatisNet.Common.Utilities;
|
38
|
|
using IBatisNet.Common.Exceptions;
|
39
|
|
using IBatisNet.Common.Utilities.Objects;
|
40
|
|
using IBatisNet.DataMapper.Configuration.Statements;
|
41
|
|
|
42
|
|
#endregion
|
43
|
|
|
44
|
|
namespace IBatisNet.DataMapper.Configuration.ParameterMapping
|
45
|
|
{
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
internal class InlineParameterMapParser
|
50
|
|
{
|
51
|
|
|
52
|
|
#region Fields
|
53
|
|
|
54
|
|
private const string PARAMETER_TOKEN = "#";
|
55
|
|
private const string PARAM_DELIM = ":";
|
56
|
|
|
57
|
|
private ErrorContext _errorContext= null;
|
58
|
|
|
59
|
|
#endregion
|
60
|
|
|
61
|
|
#region Constructors
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
219
|
public InlineParameterMapParser(ErrorContext errorContext)
|
68
|
|
{
|
69
|
219
|
_errorContext = errorContext;
|
70
|
|
}
|
71
|
|
#endregion
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
49834
|
public SqlText ParseInlineParameterMap(TypeHandlerFactory typeHandlerFactory, IStatement statement, string sqlStatement)
|
81
|
|
{
|
82
|
49834
|
string newSql = sqlStatement;
|
83
|
49834
|
ArrayList mappingList = new ArrayList();
|
84
|
49834
|
Type parameterClass = null;
|
85
|
|
|
86
|
49834
|
if (statement != null)
|
87
|
|
{
|
88
|
17510
|
parameterClass = statement.ParameterClass;
|
89
|
|
}
|
90
|
|
|
91
|
49834
|
StringTokenizer parser = new StringTokenizer(sqlStatement, PARAMETER_TOKEN, true);
|
92
|
49834
|
StringBuilder newSqlBuffer = new StringBuilder();
|
93
|
|
|
94
|
49834
|
string token = null;
|
95
|
49834
|
string lastToken = null;
|
96
|
|
|
97
|
49834
|
IEnumerator enumerator = parser.GetEnumerator();
|
98
|
|
|
99
|
215000
|
while (enumerator.MoveNext())
|
100
|
|
{
|
101
|
165166
|
token = (string)enumerator.Current;
|
102
|
|
|
103
|
165166
|
if (PARAMETER_TOKEN.Equals(lastToken))
|
104
|
|
{
|
105
|
47114
|
if (PARAMETER_TOKEN.Equals(token))
|
106
|
|
{
|
107
|
1020
|
newSqlBuffer.Append(PARAMETER_TOKEN);
|
108
|
1020
|
token = null;
|
109
|
|
}
|
110
|
|
else
|
111
|
|
{
|
112
|
|
|
113
|
46094
|
ParameterProperty mapping = ParseMapping(token, parameterClass, typeHandlerFactory);
|
114
|
|
|
115
|
46094
|
mappingList.Add(mapping);
|
116
|
46094
|
newSqlBuffer.Append("? ");
|
117
|
|
|
118
|
46094
|
enumerator.MoveNext();
|
119
|
46094
|
token = (string)enumerator.Current;
|
120
|
46094
|
if (!PARAMETER_TOKEN.Equals(token))
|
121
|
|
{
|
122
|
0
|
throw new DataMapperException("Unterminated inline parameter in mapped statement (" + statement.Id + ").");
|
123
|
|
}
|
124
|
46094
|
token = null;
|
125
|
|
}
|
126
|
|
}
|
127
|
|
else
|
128
|
|
{
|
129
|
118052
|
if (!PARAMETER_TOKEN.Equals(token))
|
130
|
|
{
|
131
|
70938
|
newSqlBuffer.Append(token);
|
132
|
|
}
|
133
|
|
}
|
134
|
|
|
135
|
165166
|
lastToken = token;
|
136
|
|
}
|
137
|
|
|
138
|
49834
|
newSql = newSqlBuffer.ToString();
|
139
|
|
|
140
|
49834
|
ParameterProperty[] mappingArray = (ParameterProperty[]) mappingList.ToArray(typeof(ParameterProperty));
|
141
|
|
|
142
|
49834
|
SqlText sqlText = new SqlText();
|
143
|
49834
|
sqlText.Text = newSql;
|
144
|
49834
|
sqlText.Parameters = mappingArray;
|
145
|
|
|
146
|
49834
|
return sqlText;
|
147
|
|
}
|
148
|
|
|
149
|
|
|
150
|
46094
|
private ParameterProperty ParseMapping(string token, Type parameterClass, TypeHandlerFactory typeHandlerFactory)
|
151
|
|
{
|
152
|
46094
|
ParameterProperty mapping = new ParameterProperty();
|
153
|
|
|
154
|
46094
|
if (token.IndexOf(PARAM_DELIM) > -1)
|
155
|
|
{
|
156
|
3910
|
StringTokenizer paramParser = new StringTokenizer(token, PARAM_DELIM, true);
|
157
|
3910
|
IEnumerator enumeratorParam = paramParser.GetEnumerator();
|
158
|
|
|
159
|
3910
|
int n1 = paramParser.TokenNumber;
|
160
|
3910
|
if (n1 == 3)
|
161
|
|
{
|
162
|
3230
|
enumeratorParam.MoveNext();
|
163
|
3230
|
string propertyName = ((string)enumeratorParam.Current).Trim();
|
164
|
3230
|
mapping.PropertyName = propertyName;
|
165
|
|
|
166
|
3230
|
enumeratorParam.MoveNext();
|
167
|
3230
|
enumeratorParam.MoveNext();
|
168
|
3230
|
string dBType = ((string)enumeratorParam.Current).Trim();
|
169
|
3230
|
mapping.DbType = dBType;
|
170
|
|
|
171
|
3230
|
ITypeHandler handler = null;
|
172
|
3230
|
if (parameterClass == null)
|
173
|
|
{
|
174
|
1870
|
handler = typeHandlerFactory.GetUnkownTypeHandler();
|
175
|
|
}
|
176
|
|
else
|
177
|
|
{
|
178
|
1360
|
handler = ResolveTypeHandler(typeHandlerFactory, parameterClass, propertyName, null, dBType);
|
179
|
|
}
|
180
|
3230
|
mapping.TypeHandler = handler;
|
181
|
3230
|
mapping.Initialize(typeHandlerFactory, _errorContext);
|
182
|
|
}
|
183
|
680
|
else if (n1 >= 5)
|
184
|
|
{
|
185
|
680
|
enumeratorParam.MoveNext();
|
186
|
680
|
string propertyName = ((string)enumeratorParam.Current).Trim();
|
187
|
680
|
enumeratorParam.MoveNext();
|
188
|
680
|
enumeratorParam.MoveNext();
|
189
|
680
|
string dBType = ((string)enumeratorParam.Current).Trim();
|
190
|
680
|
enumeratorParam.MoveNext();
|
191
|
680
|
enumeratorParam.MoveNext();
|
192
|
680
|
string nullValue = ((string)enumeratorParam.Current).Trim();
|
193
|
680
|
while (enumeratorParam.MoveNext())
|
194
|
|
{
|
195
|
0
|
nullValue = nullValue + ((string)enumeratorParam.Current).Trim();
|
196
|
|
}
|
197
|
|
|
198
|
680
|
mapping.PropertyName = propertyName;
|
199
|
680
|
mapping.DbType = dBType;
|
200
|
680
|
mapping.NullValue = nullValue;
|
201
|
680
|
ITypeHandler handler;
|
202
|
680
|
if (parameterClass == null)
|
203
|
|
{
|
204
|
340
|
handler = typeHandlerFactory.GetUnkownTypeHandler();
|
205
|
|
}
|
206
|
|
else
|
207
|
|
{
|
208
|
340
|
handler = ResolveTypeHandler(typeHandlerFactory, parameterClass, propertyName, null, dBType);
|
209
|
|
}
|
210
|
680
|
mapping.TypeHandler = handler;
|
211
|
680
|
mapping.Initialize(typeHandlerFactory, _errorContext);
|
212
|
|
}
|
213
|
|
else
|
214
|
|
{
|
215
|
0
|
throw new ConfigurationException("Incorrect inline parameter map format: " + token);
|
216
|
|
}
|
217
|
|
}
|
218
|
|
else
|
219
|
|
{
|
220
|
42184
|
mapping.PropertyName = token;
|
221
|
42184
|
ITypeHandler handler;
|
222
|
42184
|
if (parameterClass == null)
|
223
|
|
{
|
224
|
25694
|
handler = typeHandlerFactory.GetUnkownTypeHandler();
|
225
|
|
}
|
226
|
|
else
|
227
|
|
{
|
228
|
16490
|
handler = ResolveTypeHandler(typeHandlerFactory, parameterClass, token, null, null);
|
229
|
|
}
|
230
|
42184
|
mapping.TypeHandler = handler;
|
231
|
42184
|
mapping.Initialize(typeHandlerFactory, _errorContext);
|
232
|
|
}
|
233
|
46094
|
return mapping;
|
234
|
|
}
|
235
|
|
|
236
|
|
|
237
|
|
|
238
|
|
|
239
|
|
|
240
|
|
|
241
|
|
|
242
|
|
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
18190
|
private ITypeHandler ResolveTypeHandler(TypeHandlerFactory typeHandlerFactory,
|
247
|
|
Type type, string propertyName,
|
248
|
|
string propertyType, string dbType)
|
249
|
|
{
|
250
|
18190
|
ITypeHandler handler = null;
|
251
|
|
|
252
|
18190
|
if (type == null)
|
253
|
|
{
|
254
|
0
|
handler = typeHandlerFactory.GetUnkownTypeHandler();
|
255
|
|
}
|
256
|
18190
|
else if (typeof(IDictionary).IsAssignableFrom(type))
|
257
|
|
{
|
258
|
2550
|
if (propertyType == null || propertyType.Length==0)
|
259
|
|
{
|
260
|
2550
|
handler = typeHandlerFactory.GetUnkownTypeHandler();
|
261
|
|
}
|
262
|
|
else
|
263
|
|
{
|
264
|
0
|
try
|
265
|
|
{
|
266
|
|
Type typeClass = Resources.TypeForName( propertyType );
|
267
|
|
handler = typeHandlerFactory.GetTypeHandler(typeClass, dbType);
|
268
|
|
}
|
269
|
|
catch (Exception e)
|
270
|
|
{
|
271
|
|
throw new ConfigurationException("Error. Could not set TypeHandler. Cause: " + e.Message, e);
|
272
|
|
}
|
273
|
|
}
|
274
|
|
}
|
275
|
15640
|
else if (typeHandlerFactory.GetTypeHandler(type, dbType) != null)
|
276
|
|
{
|
277
|
8330
|
handler = typeHandlerFactory.GetTypeHandler(type, dbType);
|
278
|
|
}
|
279
|
|
else
|
280
|
|
{
|
281
|
7310
|
Type typeClass = ObjectProbe.GetPropertyTypeForGetter(type, propertyName);
|
282
|
7310
|
handler = typeHandlerFactory.GetTypeHandler(typeClass, dbType);
|
283
|
|
}
|
284
|
|
|
285
|
18190
|
return handler;
|
286
|
|
}
|
287
|
|
|
288
|
|
}
|
289
|
|
}
|
290
|
|
|