forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetweenExpression.cs
86 lines (74 loc) · 2.55 KB
/
BetweenExpression.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Expression
{
/// <summary>
/// An <see cref="ICriterion"/> that represents a "between" constraint.
/// </summary>
public class BetweenExpression : AbstractCriterion
{
private readonly string _propertyName;
private readonly object _lo;
private readonly object _hi;
/// <summary>
/// Initialize a new instance of the <see cref="BetweenExpression" /> class for
/// the named Property.
/// </summary>
/// <param name="propertyName">The name of the Property of the Class.</param>
/// <param name="lo">The low value for the BetweenExpression.</param>
/// <param name="hi">The high value for the BetweenExpression.</param>
public BetweenExpression( string propertyName, object lo, object hi )
{
_propertyName = propertyName;
_lo = lo;
_hi = hi;
}
public override SqlString ToSqlString( ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses )
{
//TODO: add a default capacity
SqlStringBuilder sqlBuilder = new SqlStringBuilder();
IType propertyType = AbstractCriterion.GetType( factory, persistentClass,_propertyName, aliasClasses );
string[ ] columnNames = AbstractCriterion.GetColumns( factory, persistentClass, _propertyName, alias, aliasClasses );
Parameter[ ] loParameters = Parameter.GenerateParameters(
factory,
StringHelper.Suffix( columnNames, "_lo" ),
propertyType );
Parameter[ ] hiParameters = Parameter.GenerateParameters(
factory,
StringHelper.Suffix( columnNames, "_hi" ),
propertyType );
bool andNeeded = false;
for( int i = 0; i < columnNames.Length; i++ )
{
if( andNeeded )
{
sqlBuilder.Add( " AND " );
}
andNeeded = true;
sqlBuilder.Add( columnNames[ i ] )
.Add( " between " )
.Add( loParameters[ i ] )
.Add( " and " )
.Add( hiParameters[ i ] );
}
return sqlBuilder.ToSqlString();
}
public override TypedValue[ ] GetTypedValues( ISessionFactoryImplementor sessionFactory, System.Type persistentClass, IDictionary aliasClasses )
{
return new TypedValue[ ]
{
AbstractCriterion.GetTypedValue( sessionFactory, persistentClass, _propertyName, _lo, aliasClasses ),
AbstractCriterion.GetTypedValue( sessionFactory, persistentClass, _propertyName, _hi, aliasClasses )
};
}
/// <summary></summary>
public override string ToString()
{
return _propertyName + " between " + _lo + " and " + _hi;
}
}
}