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.Statements;
|
35
|
|
using IBatisNet.DataMapper.Scope;
|
36
|
|
using IBatisNet.DataMapper.Exceptions;
|
37
|
|
using IBatisNet.DataMapper.TypeHandlers;
|
38
|
|
using IBatisNet.Common.Utilities;
|
39
|
|
using IBatisNet.Common.Utilities.Objects;
|
40
|
|
#endregion
|
41
|
|
|
42
|
|
|
43
|
|
namespace IBatisNet.DataMapper.Configuration.Sql.SimpleDynamic
|
44
|
|
{
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
internal class SimpleDynamicSql : ISql
|
49
|
|
{
|
50
|
|
|
51
|
|
#region private
|
52
|
|
|
53
|
|
private const string ELEMENT_TOKEN = "$";
|
54
|
|
|
55
|
|
private string _simpleSqlStatement = string.Empty;
|
56
|
|
private IStatement _statement = null ;
|
57
|
|
private TypeHandlerFactory _typeHandlerFactory = null;
|
58
|
|
|
59
|
|
#endregion
|
60
|
|
|
61
|
|
#region Constructor (s) / Destructor
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
854
|
internal SimpleDynamicSql(TypeHandlerFactory typeHandlerFactory,string sqlStatement, IStatement statement)
|
69
|
|
{
|
70
|
854
|
_simpleSqlStatement = sqlStatement;
|
71
|
854
|
_statement = statement;
|
72
|
854
|
_typeHandlerFactory = typeHandlerFactory;
|
73
|
|
}
|
74
|
|
#endregion
|
75
|
|
|
76
|
|
|
77
|
|
#region Methods
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
4
|
public string GetSql(object parameterObject)
|
84
|
|
{
|
85
|
4
|
return ProcessDynamicElements(parameterObject);
|
86
|
|
}
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
20279
|
public static bool IsSimpleDynamicSql(string sqlStatement)
|
94
|
|
{
|
95
|
20279
|
return ( (sqlStatement != null) && (sqlStatement.IndexOf(ELEMENT_TOKEN) > -1) );
|
96
|
|
}
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
9
|
private string ProcessDynamicElements(object parameterObject)
|
104
|
|
{
|
105
|
|
|
106
|
9
|
char[] splitter = ELEMENT_TOKEN.ToCharArray();
|
107
|
|
|
108
|
9
|
StringTokenizer parser = new StringTokenizer(_simpleSqlStatement, ELEMENT_TOKEN, true);
|
109
|
|
|
110
|
9
|
StringBuilder newSql = new StringBuilder();
|
111
|
|
|
112
|
9
|
string token = null;
|
113
|
9
|
string lastToken = null;
|
114
|
|
|
115
|
9
|
IEnumerator enumerator = parser.GetEnumerator();
|
116
|
|
|
117
|
55
|
while (enumerator.MoveNext())
|
118
|
|
{
|
119
|
46
|
token = ((string)enumerator.Current);
|
120
|
|
|
121
|
46
|
if (ELEMENT_TOKEN.Equals(lastToken))
|
122
|
|
{
|
123
|
13
|
if (ELEMENT_TOKEN.Equals(token))
|
124
|
|
{
|
125
|
0
|
newSql.Append(ELEMENT_TOKEN);
|
126
|
0
|
token = null;
|
127
|
|
}
|
128
|
|
else
|
129
|
|
{
|
130
|
13
|
object value = null;
|
131
|
13
|
if (parameterObject != null)
|
132
|
|
{
|
133
|
13
|
if ( _typeHandlerFactory.IsSimpleType( parameterObject.GetType() ) == true)
|
134
|
|
{
|
135
|
4
|
value = parameterObject;
|
136
|
|
}
|
137
|
|
else
|
138
|
|
{
|
139
|
9
|
value = ObjectProbe.GetPropertyValue(parameterObject, token);
|
140
|
|
}
|
141
|
|
}
|
142
|
13
|
if (value != null)
|
143
|
|
{
|
144
|
13
|
newSql.Append(value.ToString());
|
145
|
|
}
|
146
|
|
|
147
|
13
|
enumerator.MoveNext();
|
148
|
13
|
token = ((string)enumerator.Current);
|
149
|
|
|
150
|
13
|
if (!ELEMENT_TOKEN.Equals(token))
|
151
|
|
{
|
152
|
0
|
throw new DataMapperException("Unterminated dynamic element in sql (" + _simpleSqlStatement + ").");
|
153
|
|
}
|
154
|
13
|
token = null;
|
155
|
|
}
|
156
|
|
}
|
157
|
|
else
|
158
|
|
{
|
159
|
33
|
if (!ELEMENT_TOKEN.Equals(token))
|
160
|
|
{
|
161
|
20
|
newSql.Append(token);
|
162
|
|
}
|
163
|
|
}
|
164
|
|
|
165
|
46
|
lastToken = token;
|
166
|
|
}
|
167
|
|
|
168
|
9
|
return newSql.ToString();
|
169
|
|
}
|
170
|
|
|
171
|
|
|
172
|
|
#region ISql Members
|
173
|
|
|
174
|
|
|
175
|
|
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
5
|
public RequestScope GetRequestScope(object parameterObject, IDalSession session)
|
181
|
|
{
|
182
|
5
|
string sqlStatement = ProcessDynamicElements(parameterObject);
|
183
|
|
|
184
|
5
|
RequestScope request = new RequestScope();
|
185
|
|
|
186
|
5
|
request.ParameterMap = _statement.ParameterMap;
|
187
|
5
|
request.ResultMap = _statement.ResultMap;
|
188
|
5
|
request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement);
|
189
|
|
|
190
|
5
|
return request;
|
191
|
|
}
|
192
|
|
|
193
|
|
|
194
|
|
|
195
|
|
|
196
|
|
|
197
|
|
|
198
|
|
|
199
|
5
|
private PreparedStatement BuildPreparedStatement(IDalSession session, RequestScope request, string sqlStatement)
|
200
|
|
{
|
201
|
5
|
PreparedStatementFactory factory = new PreparedStatementFactory( session, request, _statement, sqlStatement);
|
202
|
5
|
return factory.Prepare();
|
203
|
|
}
|
204
|
|
#endregion
|
205
|
|
|
206
|
|
#endregion
|
207
|
|
|
208
|
|
}
|
209
|
|
}
|
210
|
|
|