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.Text;
|
30
|
|
|
31
|
|
using IBatisNet.DataMapper.Exceptions;
|
32
|
|
using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Elements;
|
33
|
|
using IBatisNet.Common.Utilities.Objects;
|
34
|
|
#endregion
|
35
|
|
|
36
|
|
namespace IBatisNet.DataMapper.Configuration.Sql.Dynamic.Handlers
|
37
|
|
{
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
public abstract class ConditionalTagHandler : BaseTagHandler
|
42
|
|
{
|
43
|
|
|
44
|
|
#region Const
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
public const long NOT_COMPARABLE = long.MinValue;
|
49
|
|
#endregion
|
50
|
|
|
51
|
|
#region Methods
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
public abstract bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject);
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
102
|
public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject)
|
69
|
|
{
|
70
|
102
|
if (IsCondition(ctx, tag, parameterObject))
|
71
|
|
{
|
72
|
54
|
return BaseTagHandler.INCLUDE_BODY;
|
73
|
|
}
|
74
|
|
else
|
75
|
|
{
|
76
|
48
|
return BaseTagHandler.SKIP_BODY;
|
77
|
|
}
|
78
|
|
}
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
54
|
public override int DoEndFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuilder bodyContent)
|
89
|
|
{
|
90
|
54
|
return BaseTagHandler.INCLUDE_BODY;
|
91
|
|
}
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
34
|
protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
|
101
|
|
{
|
102
|
34
|
Conditional tag = (Conditional)sqlTag;
|
103
|
34
|
string propertyName = tag.Property;
|
104
|
34
|
string comparePropertyName = tag.CompareProperty;
|
105
|
34
|
string compareValue = tag.CompareValue;
|
106
|
|
|
107
|
34
|
object value1 = null;
|
108
|
34
|
Type type = null;
|
109
|
34
|
if (propertyName != null && propertyName.Length > 0)
|
110
|
|
{
|
111
|
24
|
value1 = ObjectProbe.GetPropertyValue(parameterObject, propertyName);
|
112
|
24
|
type = value1.GetType();
|
113
|
|
}
|
114
|
|
else
|
115
|
|
{
|
116
|
10
|
value1 = parameterObject;
|
117
|
10
|
if (value1 != null)
|
118
|
|
{
|
119
|
10
|
type = parameterObject.GetType();
|
120
|
|
}
|
121
|
|
else
|
122
|
|
{
|
123
|
0
|
type = typeof(object);
|
124
|
|
}
|
125
|
|
}
|
126
|
34
|
if (comparePropertyName != null && comparePropertyName.Length > 0)
|
127
|
|
{
|
128
|
2
|
object value2 = ObjectProbe.GetPropertyValue(parameterObject, comparePropertyName);
|
129
|
2
|
return CompareValues(type, value1, value2);
|
130
|
|
}
|
131
|
32
|
else if (compareValue != null && compareValue != "")
|
132
|
|
{
|
133
|
32
|
return CompareValues(type, value1, compareValue);
|
134
|
|
}
|
135
|
|
else
|
136
|
|
{
|
137
|
0
|
throw new DataMapperException("Error comparing in conditional fragment. Uknown 'compare to' values.");
|
138
|
|
}
|
139
|
|
}
|
140
|
|
|
141
|
|
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
|
|
148
|
34
|
protected long CompareValues(Type type, object value1, object value2)
|
149
|
|
{
|
150
|
34
|
long result = NOT_COMPARABLE;
|
151
|
|
|
152
|
34
|
if (value1 == null || value2 == null)
|
153
|
|
{
|
154
|
0
|
result = value1 == value2 ? 0 : NOT_COMPARABLE;
|
155
|
|
}
|
156
|
|
else
|
157
|
|
{
|
158
|
34
|
if (value2.GetType() != type)
|
159
|
|
{
|
160
|
26
|
value2 = ConvertValue(type, value2.ToString());
|
161
|
|
}
|
162
|
34
|
if (value2 is string && type != typeof(string))
|
163
|
|
{
|
164
|
0
|
value1 = value1.ToString();
|
165
|
|
}
|
166
|
34
|
if (!(value1 is IComparable && value2 is IComparable))
|
167
|
|
{
|
168
|
0
|
value1 = value1.ToString();
|
169
|
0
|
value2 = value2.ToString();
|
170
|
|
}
|
171
|
34
|
result = ((IComparable) value1).CompareTo(value2);
|
172
|
|
}
|
173
|
|
|
174
|
34
|
return result;
|
175
|
|
}
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
26
|
protected object ConvertValue(Type type, string value)
|
184
|
|
{
|
185
|
|
|
186
|
26
|
if (type == typeof(String))
|
187
|
|
{
|
188
|
0
|
return value;
|
189
|
|
}
|
190
|
26
|
else if (type == typeof(bool))
|
191
|
|
{
|
192
|
1
|
return System.Convert.ToBoolean(value);
|
193
|
|
}
|
194
|
25
|
else if (type == typeof(Byte))
|
195
|
|
{
|
196
|
0
|
return System.Convert.ToByte(value);
|
197
|
|
}
|
198
|
25
|
else if (type == typeof(Char))
|
199
|
|
{
|
200
|
0
|
return System.Convert.ToChar(value.Substring(0,1));
|
201
|
|
}
|
202
|
25
|
else if (type == typeof(DateTime))
|
203
|
|
{
|
204
|
1
|
try
|
205
|
|
{
|
206
|
1
|
return System.Convert.ToDateTime(value);
|
207
|
|
}
|
208
|
|
catch (Exception e)
|
209
|
|
{
|
210
|
0
|
throw new DataMapperException("Error parsing date. Cause: " + e.Message, e);
|
211
|
|
}
|
212
|
|
}
|
213
|
24
|
else if (type == typeof(Decimal))
|
214
|
|
{
|
215
|
0
|
return System.Convert.ToDecimal(value);
|
216
|
|
}
|
217
|
24
|
else if (type == typeof(Double))
|
218
|
|
{
|
219
|
0
|
return System.Convert.ToDouble(value);
|
220
|
|
}
|
221
|
24
|
else if (type == typeof(Int16))
|
222
|
|
{
|
223
|
0
|
return System.Convert.ToInt16(value);
|
224
|
|
}
|
225
|
24
|
else if (type == typeof(Int32))
|
226
|
|
{
|
227
|
22
|
return System.Convert.ToInt32(value);
|
228
|
|
}
|
229
|
2
|
else if (type == typeof(Int64))
|
230
|
|
{
|
231
|
2
|
return System.Convert.ToInt64(value);
|
232
|
|
}
|
233
|
0
|
else if (type == typeof(Single))
|
234
|
|
{
|
235
|
|
return System.Convert.ToSingle(value);
|
236
|
|
}
|
237
|
|
else
|
238
|
|
{
|
239
|
|
return value;
|
240
|
|
}
|
241
|
|
|
242
|
|
}
|
243
|
|
#endregion
|
244
|
|
|
245
|
|
}
|
246
|
|
}
|
247
|
|
|