forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionalProjection.cs
222 lines (185 loc) · 6.18 KB
/
ConditionalProjection.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Criterion
{
/// <summary>
/// Defines a "switch" projection which supports multiple "cases" ("when/then's").
/// </summary>
/// <seealso cref="SimpleProjection" />
/// <seealso cref="ConditionalProjectionCase" />
[Serializable]
public class ConditionalProjection : SimpleProjection
{
private readonly ConditionalProjectionCase[] _cases;
private readonly IProjection _elseProjection;
/// <summary>
/// Initializes a new instance of the <see cref="ConditionalProjection"/> class.
/// </summary>
/// <param name="criterion">The <see cref="ICriterion"/></param>
/// <param name="whenTrue">The true <see cref="IProjection"/></param>
/// <param name="whenFalse">The else <see cref="IProjection"/>.</param>
public ConditionalProjection(ICriterion criterion, IProjection whenTrue, IProjection whenFalse)
{
_elseProjection = whenFalse;
_cases = new[] {new ConditionalProjectionCase(criterion, whenTrue)};
}
/// <summary>
/// Initializes a new instance of the <see cref="ConditionalProjection"/> class.
/// </summary>
/// <param name="cases">The <see cref="ConditionalProjectionCase"/>s containing <see cref="ICriterion"/> and <see cref="IProjection"/> pairs.</param>
/// <param name="elseProjection">The else <see cref="IProjection"/>.</param>
public ConditionalProjection(ConditionalProjectionCase[] cases, IProjection elseProjection)
{
if (cases == null)
throw new ArgumentNullException(nameof(cases));
if (cases.Length == 0)
throw new ArgumentException("Array should not be empty.", nameof(cases));
_cases = cases;
_elseProjection = elseProjection;
}
public override bool IsAggregate
{
get
{
if (_elseProjection.IsAggregate)
return true;
foreach (var projectionCase in _cases)
{
if (projectionCase.Projection.IsAggregate)
return true;
var projections = projectionCase.Criterion.GetProjections();
if (projections != null)
{
foreach (var projection in projections)
{
if (projection.IsAggregate)
{
return true;
}
}
}
}
return false;
}
}
public override bool IsGrouped
{
get
{
if (_elseProjection.IsGrouped)
return true;
foreach (var projectionCase in _cases)
{
if (projectionCase.Projection.IsGrouped)
return true;
var projections = projectionCase.Criterion.GetProjections();
if (projections != null)
{
foreach (var projection in projections)
{
if (projection.IsGrouped)
return true;
}
}
}
return false;
}
}
public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery)
{
var sqlBuilder = new SqlStringBuilder(5 + (_cases.Length * 4));
sqlBuilder.Add("(case");
foreach (var projectionCase in _cases)
{
sqlBuilder.Add(" when ");
sqlBuilder.Add(projectionCase.Criterion.ToSqlString(criteria, criteriaQuery));
sqlBuilder.Add(" then ");
sqlBuilder.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(projectionCase.Projection, criteriaQuery, criteria));
}
sqlBuilder.Add(" else ");
sqlBuilder.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(_elseProjection, criteriaQuery, criteria));
sqlBuilder.Add(" end) as ");
sqlBuilder.Add(GetColumnAlias(position));
return sqlBuilder.ToSqlString();
}
public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var elseTypes = _elseProjection.GetTypes(criteria, criteriaQuery);
for (var i = 0; i < _cases.Length; i++)
{
var subsequentTypes = _cases[i].Projection.GetTypes(criteria, criteriaQuery);
if (!AreTypesEqual(elseTypes, subsequentTypes))
{
string msg = "All projections must return the same types." + Environment.NewLine +
"But Else projection returns: [" + string.Join<IType>(", ", elseTypes) + "] " + Environment.NewLine +
"And When projection " + i + " returns: [" + string.Join<IType>(", ", subsequentTypes) + "]";
throw new HibernateException(msg);
}
}
return elseTypes;
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var typedValues = new List<TypedValue>();
foreach (var projectionCase in _cases)
{
typedValues.AddRange(projectionCase.Criterion.GetTypedValues(criteria, criteriaQuery));
typedValues.AddRange(projectionCase.Projection.GetTypedValues(criteria, criteriaQuery));
}
typedValues.AddRange(_elseProjection.GetTypedValues(criteria, criteriaQuery));
return typedValues.ToArray();
}
public override SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var sqlBuilder = new SqlStringBuilder();
foreach (var projection in _cases)
{
AddToGroupedSql(sqlBuilder, projection.Criterion.GetProjections(), criteria, criteriaQuery);
AddToGroupedSql(sqlBuilder, projection.Projection, criteria, criteriaQuery);
}
AddToGroupedSql(sqlBuilder, _elseProjection, criteria, criteriaQuery);
// Remove last comma
if (sqlBuilder.Count >= 2)
{
sqlBuilder.RemoveAt(sqlBuilder.Count - 1);
}
return sqlBuilder.ToSqlString();
}
private static bool AreTypesEqual(IType[] types1, IType[] types2)
{
bool areEqual = types1.Length == types2.Length;
if (!areEqual)
{
return false;
}
for (int i = 0; i < types1.Length; i++)
{
if (types1[i].ReturnedClass != types2[i].ReturnedClass)
{
return false;
}
}
return true;
}
private void AddToGroupedSql(SqlStringBuilder sqlBuilder, IProjection[] projections, ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (projections == null)
return;
foreach (var projection in projections)
{
AddToGroupedSql(sqlBuilder, projection, criteria, criteriaQuery);
}
}
private void AddToGroupedSql(SqlStringBuilder sqlBuilder, IProjection projection, ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (projection.IsGrouped)
{
sqlBuilder.Add(projection.ToGroupSqlString(criteria, criteriaQuery));
sqlBuilder.Add(", ");
}
}
}
}