forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetweenExpression.cs
118 lines (104 loc) · 3.15 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Linq;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Criterion
{
/// <summary>
/// An <see cref="ICriterion"/> that represents a "between" constraint.
/// </summary>
[Serializable]
public class BetweenExpression : AbstractCriterion
{
private readonly object _hi;
private readonly object _lo;
private readonly IProjection _projection;
private readonly string _propertyName;
/// <summary>
/// Initializes a new instance of the <see cref="BetweenExpression"/> class.
/// </summary>
/// <param name="projection">The _projection.</param>
/// <param name="lo">The _lo.</param>
/// <param name="hi">The _hi.</param>
public BetweenExpression(IProjection projection, object lo, object hi)
{
this._projection = projection;
this._lo = lo;
this._hi = 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(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
//TODO: add a default capacity
SqlStringBuilder sqlBuilder = new SqlStringBuilder();
var parametersTypes = GetTypedValues(criteria, criteriaQuery).ToArray();
var lowType = parametersTypes[0];
var highType = parametersTypes[1];
var columnNames =
CriterionUtil.GetColumnNamesAsSqlStringParts(_propertyName, _projection, criteriaQuery, criteria);
if (columnNames.Length == 1)
{
sqlBuilder
.AddObject(columnNames[0])
.Add(" between ")
.Add(criteriaQuery.NewQueryParameter(lowType).Single())
.Add(" and ")
.Add(criteriaQuery.NewQueryParameter(highType).Single());
}
else
{
bool andNeeded = false;
var lowParameters = criteriaQuery.NewQueryParameter(lowType).ToArray();
for (int i = 0; i < columnNames.Length; i++)
{
if (andNeeded)
{
sqlBuilder.Add(" AND ");
}
andNeeded = true;
sqlBuilder.AddObject(columnNames[i])
.Add(" >= ")
.Add(lowParameters[i]);
}
var highParameters = criteriaQuery.NewQueryParameter(highType).ToArray();
for (int i = 0; i < columnNames.Length; i++)
{
sqlBuilder.Add(" AND ")
.AddObject(columnNames[i])
.Add(" <= ")
.Add(highParameters[i]);
}
}
return sqlBuilder.ToSqlString();
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return CriterionUtil.GetTypedValues(criteriaQuery, criteria, _projection, _propertyName, _lo, _hi);
}
public override IProjection[] GetProjections()
{
if(_projection != null)
{
return new IProjection[] { _projection };
}
return null;
}
public override string ToString()
{
return _propertyName + " between " + _lo + " and " + _hi;
}
}
}