forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyExpression.cs
143 lines (130 loc) · 4.33 KB
/
PropertyExpression.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
namespace NHibernate.Criterion
{
using System;
using System.Collections.Generic;
using Engine;
using SqlCommand;
using Util;
/// <summary>
/// Superclass for an <see cref="ICriterion"/> that represents a
/// constraint between two properties (with SQL binary operators).
/// </summary>
[Serializable]
public abstract class PropertyExpression : AbstractCriterion
{
private static readonly TypedValue[] NoTypedValues = Array.Empty<TypedValue>();
private readonly string _lhsPropertyName;
private readonly string _rhsPropertyName;
private readonly IProjection _lhsProjection;
private readonly IProjection _rhsProjection;
/// <summary>
/// Initializes a new instance of the <see cref="PropertyExpression"/> class.
/// </summary>
/// <param name="lhsProjection">The projection.</param>
/// <param name="rhsPropertyName">Name of the RHS property.</param>
protected PropertyExpression(IProjection lhsProjection, string rhsPropertyName)
{
this._lhsProjection = lhsProjection;
_rhsPropertyName = rhsPropertyName;
}
/// <summary>
/// Initializes a new instance of the <see cref="PropertyExpression"/> class.
/// </summary>
/// <param name="lhsProjection">The LHS projection.</param>
/// <param name="rhsProjection">The RHS projection.</param>
protected PropertyExpression(IProjection lhsProjection, IProjection rhsProjection)
{
this._lhsProjection = lhsProjection;
this._rhsProjection = rhsProjection;
}
/// <summary>
/// Initializes a new instance of the <see cref="PropertyExpression"/> class.
/// </summary>
/// <param name="lhsPropertyName">Name of the LHS property.</param>
/// <param name="rhsPropertyName">Name of the RHS property.</param>
protected PropertyExpression(string lhsPropertyName, string rhsPropertyName)
{
this._lhsPropertyName = lhsPropertyName;
this._rhsPropertyName = rhsPropertyName;
}
/// <summary>
/// Initializes a new instance of the <see cref="PropertyExpression"/> class.
/// </summary>
/// <param name="lhsPropertyName">Name of the LHS property.</param>
/// <param name="rhsProjection">The RHS projection.</param>
protected PropertyExpression(string lhsPropertyName, IProjection rhsProjection)
{
this._lhsPropertyName = lhsPropertyName;
this._rhsProjection = rhsProjection;
}
/// <summary>
/// Get the Sql operator to use for the property expression.
/// </summary>
protected abstract string Op { get; }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var columnNames =
CriterionUtil.GetColumnNamesAsSqlStringParts(_lhsPropertyName, _lhsProjection, criteriaQuery, criteria);
var otherColumnNames =
CriterionUtil.GetColumnNamesAsSqlStringParts(_rhsPropertyName, _rhsProjection, criteriaQuery, criteria);
switch (columnNames.Length)
{
case 1:
return new SqlString(columnNames[0], Op, otherColumnNames[0]);
case 0:
return SqlString.Empty;
}
var sb = new SqlStringBuilder();
sb.Add(StringHelper.OpenParen);
sb.AddObject(columnNames[0]).Add(Op).AddObject(otherColumnNames[0]);
for (var i = 1; i < columnNames.Length; i++)
{
sb.Add(" and ");
sb.AddObject(columnNames[i]).Add(Op).AddObject(otherColumnNames[i]);
}
sb.Add(StringHelper.ClosedParen);
return sb.ToSqlString();
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (_lhsProjection == null && _rhsProjection == null)
{
return NoTypedValues;
}
else
{
List<TypedValue> types = new List<TypedValue>();
if(_lhsProjection!=null)
{
types.AddRange(_lhsProjection.GetTypedValues(criteria, criteriaQuery));
}
if (_rhsProjection != null)
{
types.AddRange(_rhsProjection.GetTypedValues(criteria, criteriaQuery));
}
return types.ToArray();
}
}
/// <summary></summary>
public override string ToString()
{
return (_lhsProjection ?? (object)_lhsPropertyName) + Op + (_rhsProjection ?? (object)_rhsPropertyName);
}
public override IProjection[] GetProjections()
{
if(_lhsProjection != null && _rhsProjection != null)
{
return new IProjection[] {_lhsProjection, _rhsProjection};
}
if(_lhsProjection != null)
{
return new IProjection[] {_lhsProjection};
}
if(_rhsProjection != null)
{
return new IProjection[] {_rhsProjection};
}
return null;
}
}
}