Skip to content

Commit 5ac7592

Browse files
author
Paul H
committed
Add Criteria.ConditionalsProjection for "switch-case" expressions with multiple Criterias/Projections (when/thens)
1 parent 204b33b commit 5ac7592

File tree

5 files changed

+402
-8
lines changed

5 files changed

+402
-8
lines changed

src/NHibernate.Test/Criteria/ProjectionsTest.cs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
3+
using System.Linq;
44
using System.Text.RegularExpressions;
55
using NHibernate.Criterion;
66
using NHibernate.Dialect;
@@ -78,7 +78,7 @@ public void UsingSqlFunctions_Concat()
7878
[Test]
7979
public void UsingSqlFunctions_Concat_WithCast()
8080
{
81-
if(Dialect is Oracle8iDialect)
81+
if (Dialect is Oracle8iDialect)
8282
{
8383
Assert.Ignore("Not supported by the active dialect:{0}.", Dialect);
8484
}
@@ -198,6 +198,84 @@ public void UsingConditionals()
198198
}
199199
}
200200

201+
[Test]
202+
public void UsingMultiConditionals()
203+
{
204+
if (TestDialect.HasBrokenTypeInferenceOnSelectedParameters)
205+
Assert.Ignore("Current dialect does not support this test");
206+
207+
var students = new[]
208+
{
209+
new Student() { StudentNumber = 6L, Name = "testa", },
210+
new Student() { StudentNumber = 5L, Name = "testz", },
211+
new Student() { StudentNumber = 4L, Name = "test1", },
212+
new Student() { StudentNumber = 3L, Name = "test2", },
213+
new Student() { StudentNumber = 2L, Name = "test998", },
214+
new Student() { StudentNumber = 1L, Name = "test999", },
215+
};
216+
217+
var expecteds = new[]
218+
{
219+
students[0],
220+
students[1],
221+
students[2],
222+
students[3],
223+
};
224+
225+
// student, sortingindex
226+
var testData = new Tuple<Student, string>[]
227+
{
228+
System.Tuple.Create(expecteds[0], "1"),
229+
System.Tuple.Create(expecteds[1], "2"),
230+
System.Tuple.Create(expecteds[2], "3"),
231+
System.Tuple.Create(expecteds[3], "4"),
232+
};
233+
234+
using (ISession session = this.Sfi.OpenSession())
235+
{
236+
using (ITransaction transaction = session.BeginTransaction())
237+
{
238+
session.Save<Student>(students);
239+
transaction.Commit();
240+
}
241+
242+
using (ITransaction transaction = session.BeginTransaction())
243+
{
244+
// when Name = "testa" then 1 ...
245+
var criterionProjections = testData
246+
.Select(x => new ConditionalCriterionProjectionPair(Expression.Eq(nameof(Student.Name), x.Item1.Name), Projections.Constant(x.Item2)))
247+
.ToArray();
248+
249+
// ... else 99
250+
var elseProjection = Projections.Constant("99");
251+
252+
var conditionalsProjection = Projections.Conditionals(criterionProjections, elseProjection);
253+
254+
var order = Order.Asc(conditionalsProjection);
255+
256+
var criteria = session.CreateCriteria(typeof(Student))
257+
.AddOrder(order);
258+
259+
var actuals = criteria.List<Student>();
260+
261+
Assert.GreaterOrEqual(actuals.Count, expecteds.Length);
262+
for (int i = 0; i < expecteds.Length; i++)
263+
{
264+
var expected = expecteds[i];
265+
var actual = actuals[i];
266+
267+
Assert.AreEqual(expected.Name, actual.Name);
268+
}
269+
}
270+
271+
using (ITransaction transaction = session.BeginTransaction())
272+
{
273+
session.Delete<Student>(students);
274+
transaction.Commit();
275+
}
276+
}
277+
}
278+
201279
[Test]
202280
public void UseInWithProjection()
203281
{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace NHibernate.Test.Criteria
2+
{
3+
internal static class SessionExtender
4+
{
5+
public static void SaveOrUpdate<T>(this ISession session, params T[] models)
6+
{
7+
for (int i = 0; i < models.Length; i++)
8+
{
9+
var model = models[i];
10+
session.SaveOrUpdate(model);
11+
}
12+
}
13+
14+
public static void Save<T>(this ISession session, params T[] models)
15+
{
16+
for (int i = 0; i < models.Length; i++)
17+
{
18+
var model = models[i];
19+
session.Save(model);
20+
}
21+
}
22+
23+
public static void Delete<T>(this ISession session, params T[] models)
24+
{
25+
for (int i = 0; i < models.Length; i++)
26+
{
27+
var model = models[i];
28+
session.Delete(model);
29+
}
30+
}
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace NHibernate.Criterion
2+
{
3+
/// <summary>
4+
/// Defines a pair of <see cref="ConditionalCriterionProjectionPair.Criterion"/> and <see cref="ConditionalCriterionProjectionPair.Projection"/>.
5+
/// </summary>
6+
public class ConditionalCriterionProjectionPair
7+
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref="ConditionalCriterionProjectionPair"/> class.
10+
/// </summary>
11+
/// <param name="criterion">The <see cref="ConditionalCriterionProjectionPair.Criterion"/>.</param>
12+
/// <param name="projection">The <see cref="ConditionalCriterionProjectionPair.Projection"/>.</param>
13+
public ConditionalCriterionProjectionPair(ICriterion criterion, IProjection projection)
14+
{
15+
this.Criterion = criterion;
16+
this.Projection = projection;
17+
}
18+
19+
/// <summary>
20+
/// Gets the <see cref="ICriterion"/>.
21+
/// </summary>
22+
public ICriterion Criterion { get; }
23+
24+
/// <summary>
25+
/// Gets the <see cref="IProjection"/>.
26+
/// </summary>
27+
public IProjection Projection { get; }
28+
}
29+
}

0 commit comments

Comments
 (0)