在使用NHibernate中,经常使用查询,又不愿意去写,就写了这个东东,依对象的属性,通过反射来生成查询。
先来看一下使用方式:
一个Filter类:
Code
public class UserInfoFilter
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string logName = null;
private string name = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HqlCondition( EnumHqlCondition.Like)]
public string LogName
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (string.IsNullOrEmpty(logName))
return null;
else
return logName;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
logName = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HqlCondition( EnumHqlCondition.Like)]
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (string.IsNullOrEmpty(name))
return null;
else
return name;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
name = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HqlCondition]
public DicDept DicDept = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HqlCondition("UserRole.RoleInfo", EnumHqlCondition.EQ)]
public RoleInfo RoleInfo = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HqlCondition("DicUseFlag", EnumHqlCondition.NE)]
public DicUseFlag Deleted = DicUseFlag.CreateInstance((int)Enums.EnumUseFlag.Deleted);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HqlOrder]
public string Order = "Id asc";
}
使用查询:
Code
public static IList<UserInfo> List(UserInfoFilter filter)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Debug.Assert(filter != null);
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IQuery query = HqlQueryBuilder.BuildFilter<UserInfo>(filter);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return query.List<UserInfo>();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下面是实现代码:
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Reflection;
6
using System.Diagnostics;
7
using NHibernate;
8
using System.Collections;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace Grain.Components.NHibernateHelper
11![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public class HqlQueryBuildParameter
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
private string name;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public string Name
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
20![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
private MemberInfo memberInfo;
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
public MemberInfo MemberInfo
26![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return memberInfo; }
28![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ memberInfo = value; }
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
private HqlValidateAttribute hqlAttribute;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
public HqlValidateAttribute HqlAttribute
34![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return hqlAttribute; }
36![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ hqlAttribute = value; }
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
private object value;
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
public object Value
44![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.value; }
46![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.value = value; }
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
53
/// 依一个Filter对象来生成IQuery
54
///
55
/// </summary>
56
public class HqlQueryBuilder
57![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
private static object GetValue(MemberInfo mi,object obj)
60![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
if (mi is FieldInfo)
62
return ((FieldInfo)mi).GetValue(obj);
63
else if (mi is PropertyInfo)
64
return ((PropertyInfo)mi).GetGetMethod().Invoke(obj, null);
65
else
66
return null;
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
public static IQuery BuildFilter<T>(object filter)
70![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
return BuildFilter<T>(Database.Session, filter);
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
public static IQuery BuildFilter<T>(object filter, string hqlWhereEx)
75![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
return BuildFilter<T>(Database.Session, filter, hqlWhereEx);
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
public static IQuery BuildFilter<T>(ISession session, object filter)
80![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
return BuildFilter<T>(session, filter, string.Empty);
82
}
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
public static IQuery BuildFilter<T>(ISession session, object filter, string hqlWhereEx)
85![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
return BuildFilter(session, typeof(T), filter, hqlWhereEx);
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
public static IQuery BuildFilter(ISession session, Type type, object filter, string hqlWhereEx)
91![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
Type typeFilter = filter.GetType();
93
string typeClassName = type.Name;
94
string typeClassPrefix = new string((from c in typeClassName where char.IsUpper(c) == true select c).ToArray<char>()).ToLower();
95
string orderString = string.Empty;
96
List<HqlQueryBuildParameter> prms = new List<HqlQueryBuildParameter>();
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
StringBuilder sb = new StringBuilder(string.Format("from {0} {1}", typeClassName, typeClassPrefix));
99
MemberInfo[] fis = typeFilter.GetMembers();
100
bool bAddedWhere = false;
101
if (!string.IsNullOrEmpty(hqlWhereEx))
102![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
sb.AppendFormat(" where {0}", hqlWhereEx.Replace(typeClassName + ".", typeClassPrefix + "."));
104
bAddedWhere = true;
105
}
106
foreach (MemberInfo fi in fis)
107![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
object value = GetValue(fi,filter);
110
if (value == null)
111![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
continue;
113
}
114![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
HqlAttribute hqlAttr = (HqlAttribute)Attribute.GetCustomAttribute(fi, typeof(HqlAttribute));
116
if (hqlAttr != null)
117![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
if (hqlAttr is HqlIgoreAttribute)
119![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
continue;
121
}
122
else if (hqlAttr is HqlValidateAttribute)
123![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
HqlValidateAttribute attrValidate = (HqlValidateAttribute)hqlAttr;
125![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
if (value is ValueType && IsNullValue(value))
127![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
continue;
129
}
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
if (!bAddedWhere)
132![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
sb.Append(" where 1=1");
134
bAddedWhere = true;
135
}
136
sb.AppendFormat(" and {0}.{1}", typeClassPrefix, attrValidate.BuildHql(fi, fi.Name));
137![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
prms.Add(new HqlQueryBuildParameter()
{ Name = fi.Name, Value = value, HqlAttribute = attrValidate, MemberInfo = fi });
139![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
}
142
else if (hqlAttr is HqlWhereAttribute)
143![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
if (!bAddedWhere)
145![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
sb.Append(" where 1=1");
147
bAddedWhere = true;
148
}
149
sb.AppendFormat(" and {0}", value.ToString().Replace(typeClassName + ".", typeClassPrefix + "."));
150
}
151
else if (hqlAttr is HqlOrderAttribute)
152![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
orderString = (string)value;
154
}
155
else
156![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
157
continue;
158
}
159![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
160
}
161
}
162![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
163
if (!string.IsNullOrEmpty(orderString))
164![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
string hqlOrder = orderString.Replace(typeClassName + ".", typeClassPrefix + ".");
166
if(!hqlOrder.StartsWith(typeClassPrefix + "."))
167
hqlOrder = typeClassPrefix + "." + hqlOrder;
168![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
169
sb.AppendFormat(" order by {0}", hqlOrder);
170
}
171![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
172
IQuery query = session.CreateQuery(sb.ToString());
173![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175
//设置参数
176
foreach(HqlQueryBuildParameter prm in prms)
177![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
prm.HqlAttribute.SetParameterValue(query, prm);
179
}
180![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
181![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
182
return query;
183![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
184
}
185![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
186
private static bool IsNullValue(object value)
187![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
188
if(
189
(value is byte) ||
190
(value is Int32) ||
191
(value is Int16) ||
192
(value is Int64))
193![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
194
if(value.Equals(0))
195
return true;
196![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
197
return false;
198
}
199
if(value is DateTime)
200![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
201
if(value.Equals(DateTime.MinValue))
202
return true;
203![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
204
return false;
205
}
206![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
207
if(value is Guid)
208![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
209
if(value.Equals(Guid.Empty))
210
return true;
211![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
212
return false;
213
}
214![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
215![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
216
throw new NotImplementedException("未对此类型的默认值检查");
217
}
218![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
219
}
220![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
221![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
222
/// HQL生成基类
223
/// </summary>
224
public abstract class HqlAttribute : Attribute
225![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
226![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
227
}
228![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
229![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
230
/// 生成查询时,乎略这个字段
231
/// </summary>
232
public class HqlIgoreAttribute : HqlAttribute
233![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
234
}
235![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
236![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
237
/// 表示这个字段要生成无参数的条件
238
/// </summary>
239
public class HqlWhereAttribute : HqlAttribute
240![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
241![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
242![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
243
}
244![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
245
/// 表示这个字段用来执行排序
246
/// </summary>
247
public class HqlOrderAttribute : HqlAttribute
248![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
249![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
250
}
251![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
252
public enum EnumHqlCondition
253![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
254![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
255
/// 等于
256
/// </summary>
257
EQ = 0,
258![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
259
/// 大于等于
260
/// </summary>
261
GEQ = 1,
262![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
263
/// 小于
264
/// </summary>
265
LT = 2,
266![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
267![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
268
/// 不等于
269
/// </summary>
270
NE = 3,
271![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
272![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
273
/// 小于等于
274
/// </summary>
275
LEQ = 4,
276![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
277![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
278
/// 相当于SQL的IN运算符
279
/// </summary>
280
IN = 5,
281![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
282![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
283
/// 使用Like查询,应用的字段要求是字符串,或者正确实现了ToString()方法
284
/// </summary>
285
Like = 6
286![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
287
}
288![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
289![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
290![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
291![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
292
/// 表示一个字段的查询
293
/// </summary>
294
public abstract class HqlValidateAttribute : HqlAttribute
295![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
296![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
297
/// 这个值作用的字段,可以为 PropertyA.ProperyB
PropertyN
298
/// </summary>
299
private string targetName = string.Empty;
300
private object nullValue = null;
301![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
302
public object NullValue
303![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
304![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return nullValue; }
305![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ nullValue = value; }
306
}
307![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public abstract string Operator
{ get; }
308![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
309![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
310
public HqlValidateAttribute()
311![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
312![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
313
}
314
public HqlValidateAttribute(object nullValue)
315![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
316
this.nullValue = nullValue;
317
}
318![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
319![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
320
public HqlValidateAttribute(string targetName)
321![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
322
this.targetName = targetName;
323
}
324
public HqlValidateAttribute(string targetName, object nullValue)
325![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
326
this.targetName = targetName;
327
this.nullValue = nullValue;
328
}
329![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
330![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
331![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
332
public string BuildHql(MemberInfo fieldInfo, string paramName)
333![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
334
if (string.IsNullOrEmpty(this.targetName))
335![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
336
this.targetName = fieldInfo.Name;
337
}
338![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
339
return BuildHql(this.targetName, paramName);
340
}
341![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
342
public virtual void SetParameterValue(IQuery query, HqlQueryBuildParameter prm)
343![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
344
Type fieldType = null;
345
if (prm.MemberInfo is FieldInfo)
346![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
347
FieldInfo fi = (FieldInfo)prm.MemberInfo;
348
fieldType = fi.FieldType;
349
}
350
else if (prm.MemberInfo is PropertyInfo)
351![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
352
PropertyInfo pi = (PropertyInfo)prm.MemberInfo;
353
fieldType = pi.PropertyType;
354
}
355![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
356
object value = prm.Value;
357
string paramName = prm.Name;
358![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
359
if (fieldType == typeof(byte[]))
360![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
361
query.SetBinary(paramName, (byte[])value);
362
}
363
else if (fieldType == typeof(bool))
364![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
365
query.SetBoolean(paramName, (bool)value);
366
}
367
else if (fieldType == typeof(byte))
368![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
369
query.SetByte(paramName, (byte)value);
370
}
371
else if (fieldType == typeof(char))
372![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
373
query.SetCharacter(paramName, (char)value);
374
}
375
else if (fieldType == typeof(DateTime))
376![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
377
query.SetDateTime(paramName, (DateTime)value);
378
}
379
else if (fieldType == typeof(decimal))
380![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
381
query.SetDecimal(paramName, (decimal)value);
382
}
383
else if (fieldType == typeof(double))
384![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
385
query.SetDouble(paramName, (double)value);
386
}
387
else if (fieldType == typeof(Int16))
388![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
389
query.SetInt16(paramName, (Int16)value);
390
}
391
else if (fieldType == typeof(Int32))
392![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
393
query.SetInt32(paramName, (Int32)value);
394
}
395![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
396
else if (fieldType == typeof(Int64))
397![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
398
query.SetInt64(paramName, (Int64)value);
399
}
400![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
401
else if (fieldType == typeof(float))
402![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
403
query.SetSingle(paramName, (float)value);
404
}
405![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
406
else if (fieldType == typeof(string))
407![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
408
query.SetString(paramName, (string)value);
409
}
410
else if (Database.Configuration.GetClassMapping(fieldType) != null)
411![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
412
query.SetEntity(paramName, value);
413
}
414
else
415![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
416
query.SetParameter(paramName, value);
417
}
418
}
419![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
420
public virtual string BuildHql(string targetName, string paramName)
421![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
422
return string.Format("{0} {1} :{2}", targetName, this.Operator, paramName);
423
}
424
}
425![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
426![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
427![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
428
/// 表示一个字段的查询时,要求Target的属性等于该值
429
/// </summary>
430
public class HqlConditionAttribute : HqlValidateAttribute
431![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
432
private EnumHqlCondition condition = EnumHqlCondition.EQ;
433
public override string Operator
434![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
435
get
436![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
437
string oper = "=";
438
switch (condition)
439![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
440
case EnumHqlCondition.EQ:
441
oper = "=";
442
break;
443
case EnumHqlCondition.GEQ:
444
oper = ">=";
445
break;
446
case EnumHqlCondition.LEQ:
447
oper = "<=";
448
break;
449
case EnumHqlCondition.LT:
450
oper = "<";
451
break;
452
case EnumHqlCondition.NE:
453
oper = "!=";
454
break;
455
case EnumHqlCondition.IN:
456
oper = "in";
457
break;
458
case EnumHqlCondition.Like:
459
oper = "like";
460
break;
461
default:
462
break;
463![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
464
}
465
return oper;
466
}
467
}
468![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
469![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
470
/// 依该字段的名称作为TargetName来进行相等条件的筛选
471
/// </summary>
472
public HqlConditionAttribute()
473
: base()
474![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
475![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
476
}
477![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
478![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
479
/// 依该字段的名称作为TargetName来筛选
480
/// </summary>
481
/// <param name="condition">条件</param>
482
public HqlConditionAttribute(EnumHqlCondition condition)
483
: base()
484![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
485
this.condition = condition;
486
}
487![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
488![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
489![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
490
/// 通过指定筛选属性和条件来筛选
491
/// </summary>
492
/// <param name="targetName">筛选属性,可以为 PropertyA.ProperyB
PropertyN的形式</param>
493
/// <param name="condition">条件</param>
494
public HqlConditionAttribute(string targetName, EnumHqlCondition condition)
495
: base(targetName)
496![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
497
this.condition = condition;
498
}
499![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
500![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
501![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
502
/// 依该字段的名称作为TargetName来筛选
503
/// </summary>
504
/// <param name="condition">条件</param>
505
public HqlConditionAttribute(EnumHqlCondition condition, object nullValue)
506
: base(nullValue)
507![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
508
this.condition = condition;
509
}
510![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
511![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
512![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
513
/// 通过指定筛选属性和条件来筛选
514
/// </summary>
515
/// <param name="targetName">筛选属性,可以为 PropertyA.ProperyB
PropertyN的形式</param>
516
/// <param name="condition">条件</param>
517
public HqlConditionAttribute(string targetName, EnumHqlCondition condition, object nullValue)
518
: base(targetName,nullValue)
519![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
520
this.condition = condition;
521
}
522![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
523![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
524![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
525![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
526
public override void SetParameterValue(IQuery query, HqlQueryBuildParameter prm)
527![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
528![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
529
switch (condition)
530![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
531
case EnumHqlCondition.IN:
532
query.SetParameterList(prm.Name, (IEnumerable)prm.Value);
533
break;
534
case EnumHqlCondition.Like:
535
query.SetString(prm.Name, string.Format("%{0}%", (string)prm.Value));
536
break;
537
default:
538
base.SetParameterValue(query, prm);
539
break;
540
}
541
}
542![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
543
public override string BuildHql(string targetName, string paramName)
544![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
545
string hql = string.Empty;
546
switch (condition)
547![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
548
case EnumHqlCondition.IN:
549
hql = string.Format("{0} {1} (:{2})", targetName, this.Operator, paramName);
550
break;
551
default:
552
hql = base.BuildHql(targetName, paramName);
553
break;
554
}
555![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
556
return hql;
557
}
558
}
559![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
560![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
561![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
562![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
563
}
564