7
7
namespace NHibernate . Criterion
8
8
{
9
9
/// <summary>
10
- /// Defines a "switch" projection which supports multiple "cases" ("when/then's") using <see cref="ConditionalProjectionCase"/>.
11
- /// Can be used in Orderby for example.
10
+ /// Defines a "switch" projection which supports multiple "cases" ("when/then's").
12
11
/// </summary>
13
12
/// <seealso cref="SimpleProjection" />
14
13
/// <seealso cref="ConditionalProjectionCase" />
15
14
[ Serializable ]
16
15
public class ConditionalProjection : SimpleProjection
17
16
{
18
- private readonly ConditionalProjectionCase [ ] cases ;
19
- private readonly IProjection elseProjection ;
17
+ private readonly ConditionalProjectionCase [ ] _cases ;
18
+ private readonly IProjection _elseProjection ;
20
19
20
+ /// <summary>
21
+ /// Initializes a new instance of the <see cref="ConditionalProjection"/> class.
22
+ /// </summary>
23
+ /// <param name="criterion">The <see cref="ICriterion"/></param>
24
+ /// <param name="whenTrue">The true <see cref="IProjection"/></param>
25
+ /// <param name="whenFalse">The else <see cref="IProjection"/>.</param>
21
26
public ConditionalProjection ( ICriterion criterion , IProjection whenTrue , IProjection whenFalse )
22
27
{
23
- elseProjection = whenFalse ;
24
- cases = new [ ] { new ConditionalProjectionCase ( criterion , whenTrue ) } ;
28
+ _elseProjection = whenFalse ;
29
+ _cases = new [ ] { new ConditionalProjectionCase ( criterion , whenTrue ) } ;
25
30
}
26
31
27
32
/// <summary>
28
33
/// Initializes a new instance of the <see cref="ConditionalProjection"/> class.
29
34
/// </summary>
30
- /// <param name="cases">The <see cref="ConditionalProjectionCase"/>s containg your <see cref="ICriterion"/> and <see cref="IProjection"/> pairs.</param>
35
+ /// <param name="cases">The <see cref="ConditionalProjectionCase"/>s containing <see cref="ICriterion"/> and <see cref="IProjection"/> pairs.</param>
31
36
/// <param name="elseProjection">The else <see cref="IProjection"/>.</param>
32
- /// <exception cref="ArgumentNullException"><paramref name="cases"/> is null.</exception>
33
- /// <exception cref="ArgumentNullException"><paramref name="elseProjection"/> is null.</exception>
34
- /// <exception cref="ArgumentOutOfRangeException"><paramref name="cases"/> should not be empty.</exception>
35
37
public ConditionalProjection ( ConditionalProjectionCase [ ] cases , IProjection elseProjection )
36
38
{
37
- if ( elseProjection is null )
38
- {
39
- throw new ArgumentNullException ( nameof ( elseProjection ) ) ;
40
- }
41
-
42
- if ( cases is null )
43
- {
39
+ if ( cases == null )
44
40
throw new ArgumentNullException ( nameof ( cases ) ) ;
45
- }
46
-
47
41
if ( cases . Length == 0 )
48
- {
49
- throw new ArgumentOutOfRangeException ( nameof ( this . cases ) , "Array should not be empty." ) ;
50
- }
42
+ throw new ArgumentException ( "Array should not be empty." , nameof ( cases ) ) ;
51
43
52
- this . cases = cases ;
53
- this . elseProjection = elseProjection ;
44
+ _cases = cases ;
45
+ _elseProjection = elseProjection ;
54
46
}
55
47
56
48
public override bool IsAggregate
57
49
{
58
50
get
59
51
{
60
- if ( elseProjection . IsAggregate )
52
+ if ( _elseProjection . IsAggregate )
61
53
return true ;
62
54
63
- foreach ( var projectionCase in cases )
55
+ foreach ( var projectionCase in _cases )
64
56
{
65
57
if ( projectionCase . Projection . IsAggregate )
66
58
return true ;
@@ -86,10 +78,10 @@ public override bool IsGrouped
86
78
{
87
79
get
88
80
{
89
- if ( elseProjection . IsGrouped )
81
+ if ( _elseProjection . IsGrouped )
90
82
return true ;
91
83
92
- foreach ( var projectionCase in cases )
84
+ foreach ( var projectionCase in _cases )
93
85
{
94
86
if ( projectionCase . Projection . IsGrouped )
95
87
return true ;
@@ -111,11 +103,11 @@ public override bool IsGrouped
111
103
112
104
public override SqlString ToSqlString ( ICriteria criteria , int position , ICriteriaQuery criteriaQuery )
113
105
{
114
- var sqlBuilder = new SqlStringBuilder ( 5 + cases . Length * 4 ) ;
106
+ var sqlBuilder = new SqlStringBuilder ( 5 + _cases . Length * 4 ) ;
115
107
116
108
sqlBuilder . Add ( "(case" ) ;
117
109
118
- foreach ( var projectionCase in cases )
110
+ foreach ( var projectionCase in _cases )
119
111
{
120
112
sqlBuilder . Add ( " when " ) ;
121
113
sqlBuilder . Add ( projectionCase . Criterion . ToSqlString ( criteria , criteriaQuery ) ) ;
@@ -124,7 +116,7 @@ public override SqlString ToSqlString(ICriteria criteria, int position, ICriteri
124
116
}
125
117
126
118
sqlBuilder . Add ( " else " ) ;
127
- sqlBuilder . AddObject ( CriterionUtil . GetColumnNameAsSqlStringPart ( elseProjection , criteriaQuery , criteria ) ) ;
119
+ sqlBuilder . AddObject ( CriterionUtil . GetColumnNameAsSqlStringPart ( _elseProjection , criteriaQuery , criteria ) ) ;
128
120
129
121
sqlBuilder . Add ( " end) as " ) ;
130
122
sqlBuilder . Add ( GetColumnAlias ( position ) ) ;
@@ -134,11 +126,11 @@ public override SqlString ToSqlString(ICriteria criteria, int position, ICriteri
134
126
135
127
public override IType [ ] GetTypes ( ICriteria criteria , ICriteriaQuery criteriaQuery )
136
128
{
137
- var elseTypes = elseProjection . GetTypes ( criteria , criteriaQuery ) ;
129
+ var elseTypes = _elseProjection . GetTypes ( criteria , criteriaQuery ) ;
138
130
139
- for ( var i = 0 ; i < cases . Length ; i ++ )
131
+ for ( var i = 0 ; i < _cases . Length ; i ++ )
140
132
{
141
- var subsequentTypes = cases [ i ] . Projection . GetTypes ( criteria , criteriaQuery ) ;
133
+ var subsequentTypes = _cases [ i ] . Projection . GetTypes ( criteria , criteriaQuery ) ;
142
134
if ( ! AreTypesEqual ( elseTypes , subsequentTypes ) )
143
135
{
144
136
string msg = "All projections must return the same types." + Environment . NewLine +
@@ -156,13 +148,13 @@ public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery c
156
148
{
157
149
var typedValues = new List < TypedValue > ( ) ;
158
150
159
- foreach ( var projectionCase in cases )
151
+ foreach ( var projectionCase in _cases )
160
152
{
161
153
typedValues . AddRange ( projectionCase . Criterion . GetTypedValues ( criteria , criteriaQuery ) ) ;
162
154
typedValues . AddRange ( projectionCase . Projection . GetTypedValues ( criteria , criteriaQuery ) ) ;
163
155
}
164
156
165
- typedValues . AddRange ( elseProjection . GetTypedValues ( criteria , criteriaQuery ) ) ;
157
+ typedValues . AddRange ( _elseProjection . GetTypedValues ( criteria , criteriaQuery ) ) ;
166
158
167
159
return typedValues . ToArray ( ) ;
168
160
}
@@ -171,13 +163,13 @@ public override SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery cr
171
163
{
172
164
var sqlBuilder = new SqlStringBuilder ( ) ;
173
165
174
- foreach ( var projection in cases )
166
+ foreach ( var projection in _cases )
175
167
{
176
168
AddToGroupedSql ( sqlBuilder , projection . Criterion . GetProjections ( ) , criteria , criteriaQuery ) ;
177
169
AddToGroupedSql ( sqlBuilder , projection . Projection , criteria , criteriaQuery ) ;
178
170
}
179
171
180
- AddToGroupedSql ( sqlBuilder , elseProjection , criteria , criteriaQuery ) ;
172
+ AddToGroupedSql ( sqlBuilder , _elseProjection , criteria , criteriaQuery ) ;
181
173
182
174
// Remove last comma
183
175
if ( sqlBuilder . Count >= 2 )
0 commit comments