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 Imports
|
28
|
|
using System;
|
29
|
|
using System.Collections;
|
30
|
|
using System.Text;
|
31
|
|
|
32
|
|
using IBatisNet.Common;
|
33
|
|
using IBatisNet.DataMapper.Configuration.Sql;
|
34
|
|
using IBatisNet.DataMapper.Configuration.Sql.SimpleDynamic;
|
35
|
|
using IBatisNet.DataMapper.Configuration.Statements;
|
36
|
|
using IBatisNet.DataMapper.Configuration.ParameterMapping;
|
37
|
|
using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Elements;
|
38
|
|
using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Handlers;
|
39
|
|
using IBatisNet.DataMapper.Scope;
|
40
|
|
using IBatisNet.DataMapper.TypeHandlers;
|
41
|
|
|
42
|
|
#endregion
|
43
|
|
|
44
|
|
namespace IBatisNet.DataMapper.Configuration.Sql.Dynamic
|
45
|
|
{
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
internal class DynamicSql : ISql, IDynamicParent
|
53
|
|
{
|
54
|
|
|
55
|
|
#region Fields
|
56
|
|
|
57
|
|
private IList _children = new ArrayList();
|
58
|
|
private IStatement _statement = null ;
|
59
|
|
private InlineParameterMapParser _paramParser = null;
|
60
|
|
private TypeHandlerFactory _typeHandlerFactory = null;
|
61
|
|
private bool _usePositionalParameters = false;
|
62
|
|
|
63
|
|
#endregion
|
64
|
|
|
65
|
|
#region Constructor (s) / Destructor
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
24820
|
internal DynamicSql(TypeHandlerFactory typeHandlerFactory, IStatement statement, bool usePositionalParameters)
|
73
|
|
{
|
74
|
24820
|
_statement = statement;
|
75
|
24820
|
_typeHandlerFactory = typeHandlerFactory;
|
76
|
24820
|
_usePositionalParameters = usePositionalParameters;
|
77
|
|
}
|
78
|
|
#endregion
|
79
|
|
|
80
|
|
#region Methods
|
81
|
|
|
82
|
|
#region ISql IDynamicParent
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
30430
|
public void AddChild(ISqlChild child)
|
89
|
|
{
|
90
|
30430
|
_children.Add(child);
|
91
|
|
}
|
92
|
|
|
93
|
|
#endregion
|
94
|
|
|
95
|
|
#region ISql Members
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
49
|
public RequestScope GetRequestScope(object parameterObject, IDalSession session)
|
104
|
|
{
|
105
|
49
|
RequestScope request = new RequestScope();
|
106
|
49
|
_paramParser = new InlineParameterMapParser(request.ErrorContext);
|
107
|
49
|
request.ResultMap = _statement.ResultMap;
|
108
|
|
|
109
|
49
|
string sqlStatement = Process(request, parameterObject);
|
110
|
49
|
request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement);
|
111
|
|
|
112
|
49
|
return request;
|
113
|
|
}
|
114
|
|
|
115
|
|
|
116
|
|
#endregion
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
49
|
private string Process(RequestScope request, object parameterObject)
|
125
|
|
{
|
126
|
49
|
SqlTagContext ctx = new SqlTagContext();
|
127
|
49
|
IList localChildren = _children;
|
128
|
|
|
129
|
49
|
ProcessBodyChildren(request, ctx, parameterObject, localChildren);
|
130
|
|
|
131
|
|
|
132
|
49
|
ParameterMap map = new ParameterMap(_usePositionalParameters);
|
133
|
49
|
map.Id = _statement.Id + "-InlineParameterMap";
|
134
|
|
|
135
|
|
|
136
|
49
|
IList parameters = ctx.GetParameterMappings();
|
137
|
108
|
for(int i=0;i<parameters.Count;i++)
|
138
|
|
{
|
139
|
59
|
map.AddParameterProperty( (ParameterProperty)parameters[i] );
|
140
|
|
}
|
141
|
49
|
request.ParameterMap = map;
|
142
|
|
|
143
|
49
|
string dynSql = ctx.BodyText;
|
144
|
|
|
145
|
|
|
146
|
49
|
if ( SimpleDynamicSql.IsSimpleDynamicSql(dynSql) )
|
147
|
|
{
|
148
|
4
|
dynSql = new SimpleDynamicSql(_typeHandlerFactory, dynSql, _statement).GetSql(parameterObject);
|
149
|
|
}
|
150
|
49
|
return dynSql;
|
151
|
|
}
|
152
|
|
|
153
|
|
|
154
|
|
|
155
|
|
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
49
|
private void ProcessBodyChildren(RequestScope request, SqlTagContext ctx,
|
162
|
|
object parameterObject, IList localChildren)
|
163
|
|
{
|
164
|
49
|
StringBuilder buffer = ctx.GetWriter();
|
165
|
49
|
ProcessBodyChildren(request, ctx, parameterObject, localChildren.GetEnumerator(), buffer);
|
166
|
|
}
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
|
|
172
|
|
|
173
|
|
|
174
|
|
|
175
|
|
|
176
|
|
|
177
|
154
|
private void ProcessBodyChildren(RequestScope request, SqlTagContext ctx,
|
178
|
|
object parameterObject, IEnumerator localChildren, StringBuilder buffer)
|
179
|
|
{
|
180
|
416
|
while (localChildren.MoveNext())
|
181
|
|
{
|
182
|
262
|
ISqlChild child = (ISqlChild) localChildren.Current;
|
183
|
|
|
184
|
262
|
if (child is SqlText)
|
185
|
|
{
|
186
|
125
|
SqlText sqlText = (SqlText) child;
|
187
|
125
|
string sqlStatement = sqlText.Text;
|
188
|
125
|
if (sqlText.IsWhiteSpace)
|
189
|
|
{
|
190
|
0
|
buffer.Append(sqlStatement);
|
191
|
|
}
|
192
|
|
else
|
193
|
|
{
|
194
|
|
|
195
|
|
|
196
|
|
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
|
|
208
|
|
|
209
|
125
|
buffer.Append(" ");
|
210
|
125
|
buffer.Append(sqlStatement);
|
211
|
|
|
212
|
125
|
ParameterProperty[] parameters = sqlText.Parameters;
|
213
|
125
|
if (parameters != null)
|
214
|
|
{
|
215
|
136
|
for (int i = 0; i< parameters.Length; i++)
|
216
|
|
{
|
217
|
35
|
ctx.AddParameterMapping(parameters[i]);
|
218
|
|
}
|
219
|
|
}
|
220
|
|
}
|
221
|
|
}
|
222
|
137
|
else if (child is SqlTag)
|
223
|
|
{
|
224
|
137
|
SqlTag tag = (SqlTag) child;
|
225
|
137
|
ISqlTagHandler handler = tag.Handler;
|
226
|
137
|
int response = BaseTagHandler.INCLUDE_BODY;
|
227
|
|
|
228
|
137
|
do
|
229
|
|
{
|
230
|
161
|
StringBuilder body = new StringBuilder();
|
231
|
|
|
232
|
161
|
response = handler.DoStartFragment(ctx, tag, parameterObject);
|
233
|
161
|
if (response != BaseTagHandler.SKIP_BODY)
|
234
|
|
{
|
235
|
105
|
if (ctx.IsOverridePrepend
|
236
|
|
&& ctx.FirstNonDynamicTagWithPrepend == null
|
237
|
|
&& tag.IsPrependAvailable
|
238
|
|
&& !(tag.Handler is DynamicTagHandler))
|
239
|
|
{
|
240
|
20
|
ctx.FirstNonDynamicTagWithPrepend = tag;
|
241
|
|
}
|
242
|
|
|
243
|
105
|
ProcessBodyChildren(request, ctx, parameterObject, tag.GetChildrenEnumerator(), body);
|
244
|
|
|
245
|
105
|
response = handler.DoEndFragment(ctx, tag, parameterObject, body);
|
246
|
105
|
handler.DoPrepend(ctx, tag, parameterObject, body);
|
247
|
105
|
if (response != BaseTagHandler.SKIP_BODY)
|
248
|
|
{
|
249
|
105
|
if (body.Length > 0)
|
250
|
|
{
|
251
|
|
|
252
|
|
|
253
|
101
|
if (handler.IsPostParseRequired)
|
254
|
|
{
|
255
|
24
|
SqlText sqlText = _paramParser.ParseInlineParameterMap(_typeHandlerFactory, null, body.ToString() );
|
256
|
24
|
buffer.Append(sqlText.Text);
|
257
|
24
|
ParameterProperty[] mappings = sqlText.Parameters;
|
258
|
24
|
if (mappings != null)
|
259
|
|
{
|
260
|
48
|
for (int i = 0; i< mappings.Length; i++)
|
261
|
|
{
|
262
|
24
|
ctx.AddParameterMapping(mappings[i]);
|
263
|
|
}
|
264
|
|
}
|
265
|
|
}
|
266
|
|
else
|
267
|
|
{
|
268
|
77
|
buffer.Append(" ");
|
269
|
77
|
buffer.Append(body.ToString());
|
270
|
|
}
|
271
|
101
|
if (tag.IsPrependAvailable && tag == ctx.FirstNonDynamicTagWithPrepend)
|
272
|
|
{
|
273
|
20
|
ctx.IsOverridePrepend = false;
|
274
|
|
}
|
275
|
|
}
|
276
|
|
}
|
277
|
|
}
|
278
|
|
}
|
279
|
161
|
while (response == BaseTagHandler.REPEAT_BODY);
|
280
|
|
}
|
281
|
|
}
|
282
|
|
}
|
283
|
|
|
284
|
|
|
285
|
|
|
286
|
|
|
287
|
|
|
288
|
|
|
289
|
|
|
290
|
|
|
291
|
|
|
292
|
49
|
private PreparedStatement BuildPreparedStatement(IDalSession session, RequestScope request, string sqlStatement)
|
293
|
|
{
|
294
|
49
|
PreparedStatementFactory factory = new PreparedStatementFactory( session, request, _statement, sqlStatement);
|
295
|
49
|
return factory.Prepare();
|
296
|
|
}
|
297
|
|
#endregion
|
298
|
|
|
299
|
|
}
|
300
|
|
}
|
301
|
|
|