Skip to content

Commit 75b615d

Browse files
committed
Simplify type check in ConditionalProjection
1 parent 0fb8160 commit 75b615d

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace NHibernate.Test.NHSpecificTest.GH1180
1717
{
1818
using System.Threading.Tasks;
19-
[KnownBug("NH-3847 (GH-1180)")]
19+
//NH-3847 (GH-1180)
2020
[TestFixture]
2121
public class ByCodeFixtureAsync : TestCaseMappingByCode
2222
{
@@ -27,7 +27,7 @@ protected override HbmMapping GetMappings()
2727
mapper.Class<Entity>(rc =>
2828
{
2929
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
30-
rc.Property(x => x.Name, m => { m.Length(10); });
30+
rc.Property(x => x.Name, m => {m.Type(NHibernateUtil.AnsiString); m.Length(5); });
3131
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
3232
});
3333

@@ -58,39 +58,40 @@ public async Task StringTypesAsync()
5858
await (transaction.CommitAsync());
5959
}
6060

61-
// whenTrue is constant, whenFalse is property -> works even before the fix
61+
// whenTrue is constant, whenFalse is property
6262
using (var session = OpenSession())
6363
{
6464
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
6565

6666
var conditionalProjection = Projections.Conditional(
6767
Restrictions.Not(
6868
Restrictions.Like(nameof(Entity.Name), "B%")),
69-
Projections.Constant("other"),
69+
//Property - ansi string length 5; contstant - string, length 10
70+
Projections.Constant("otherstring"),
7071
Projections.Property(nameof(Entity.Name)));
7172
tagCriteria.SetProjection(conditionalProjection);
7273

7374
// run query
7475
var results = await (tagCriteria.ListAsync());
7576

76-
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
77+
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
7778
}
7879

79-
// whenTrue is property, whenFalse is constant -> fails before the fix
80+
// whenTrue is property, whenFalse is constant
8081
using (var session = OpenSession())
8182
{
8283
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
8384

8485
var conditionalProjection = Projections.Conditional(
8586
Restrictions.Like(nameof(Entity.Name), "B%"),
8687
Projections.Property(nameof(Entity.Name)),
87-
Projections.Constant("other"));
88+
Projections.Constant("otherstring"));
8889
tagCriteria.SetProjection(conditionalProjection);
8990

9091
// run query
9192
var results = await (tagCriteria.ListAsync());
9293

93-
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
94+
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
9495
}
9596
}
9697

@@ -100,46 +101,47 @@ public async Task DecimalTypesAsync()
100101
using (var session = OpenSession())
101102
using (var transaction = session.BeginTransaction())
102103
{
103-
await (session.SaveAsync(new Entity {Amount = 3.14m}));
104-
await (session.SaveAsync(new Entity {Amount = 42.13m}));
105-
await (session.SaveAsync(new Entity {Amount = 17.99m}));
104+
await (session.SaveAsync(new Entity {Amount = 3.141m}));
105+
await (session.SaveAsync(new Entity {Amount = 42.131m}));
106+
await (session.SaveAsync(new Entity {Amount = 17.991m}));
106107

107108
await (transaction.CommitAsync());
108109
}
109110

110-
// whenTrue is constant, whenFalse is property -> works even before the fix
111+
// whenTrue is constant, whenFalse is property
111112
using (var session = OpenSession())
112113
{
113114
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
114115

115116
var conditionalProjection = Projections.Conditional(
116117
Restrictions.Not(
117118
Restrictions.Ge(nameof(Entity.Amount), 20m)),
118-
Projections.Constant(20m),
119+
//Property scale is 2, make sure constant scale 3 is not lost
120+
Projections.Constant(20.123m),
119121
Projections.Property(nameof(Entity.Amount)));
120122
tagCriteria.SetProjection(conditionalProjection);
121123

122124
// run query
123125
var results = await (tagCriteria.ListAsync());
124126

125-
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
127+
Assert.That(results, Is.EquivalentTo(new[] {20.123m, 42.13m, 20.123m}));
126128
}
127129

128-
// whenTrue is property, whenFalse is constant -> fails before the fix
130+
// whenTrue is property, whenFalse is constant
129131
using (var session = OpenSession())
130132
{
131133
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
132134

133135
var conditionalProjection = Projections.Conditional(
134136
Restrictions.Ge(nameof(Entity.Amount), 20m),
135137
Projections.Property(nameof(Entity.Amount)),
136-
Projections.Constant(20m));
138+
Projections.Constant(20.123m));
137139
tagCriteria.SetProjection(conditionalProjection);
138140

139141
// run query
140142
var results = await (tagCriteria.ListAsync());
141143

142-
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
144+
Assert.That(results, Is.EquivalentTo(new[] {20.123m, 42.13m, 20.123m}));
143145
}
144146
}
145147
}

src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace NHibernate.Test.NHSpecificTest.GH1180
77
{
8-
[KnownBug("NH-3847 (GH-1180)")]
8+
//NH-3847 (GH-1180)
99
[TestFixture]
1010
public class ByCodeFixture : TestCaseMappingByCode
1111
{
@@ -16,7 +16,7 @@ protected override HbmMapping GetMappings()
1616
mapper.Class<Entity>(rc =>
1717
{
1818
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
19-
rc.Property(x => x.Name, m => { m.Length(10); });
19+
rc.Property(x => x.Name, m => {m.Type(NHibernateUtil.AnsiString); m.Length(5); });
2020
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
2121
});
2222

@@ -47,39 +47,40 @@ public void StringTypes()
4747
transaction.Commit();
4848
}
4949

50-
// whenTrue is constant, whenFalse is property -> works even before the fix
50+
// whenTrue is constant, whenFalse is property
5151
using (var session = OpenSession())
5252
{
5353
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
5454

5555
var conditionalProjection = Projections.Conditional(
5656
Restrictions.Not(
5757
Restrictions.Like(nameof(Entity.Name), "B%")),
58-
Projections.Constant("other"),
58+
//Property - ansi string length 5; contstant - string, length 10
59+
Projections.Constant("otherstring"),
5960
Projections.Property(nameof(Entity.Name)));
6061
tagCriteria.SetProjection(conditionalProjection);
6162

6263
// run query
6364
var results = tagCriteria.List();
6465

65-
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
66+
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
6667
}
6768

68-
// whenTrue is property, whenFalse is constant -> fails before the fix
69+
// whenTrue is property, whenFalse is constant
6970
using (var session = OpenSession())
7071
{
7172
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
7273

7374
var conditionalProjection = Projections.Conditional(
7475
Restrictions.Like(nameof(Entity.Name), "B%"),
7576
Projections.Property(nameof(Entity.Name)),
76-
Projections.Constant("other"));
77+
Projections.Constant("otherstring"));
7778
tagCriteria.SetProjection(conditionalProjection);
7879

7980
// run query
8081
var results = tagCriteria.List();
8182

82-
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
83+
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
8384
}
8485
}
8586

@@ -89,46 +90,47 @@ public void DecimalTypes()
8990
using (var session = OpenSession())
9091
using (var transaction = session.BeginTransaction())
9192
{
92-
session.Save(new Entity {Amount = 3.14m});
93-
session.Save(new Entity {Amount = 42.13m});
94-
session.Save(new Entity {Amount = 17.99m});
93+
session.Save(new Entity {Amount = 3.141m});
94+
session.Save(new Entity {Amount = 42.131m});
95+
session.Save(new Entity {Amount = 17.991m});
9596

9697
transaction.Commit();
9798
}
9899

99-
// whenTrue is constant, whenFalse is property -> works even before the fix
100+
// whenTrue is constant, whenFalse is property
100101
using (var session = OpenSession())
101102
{
102103
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
103104

104105
var conditionalProjection = Projections.Conditional(
105106
Restrictions.Not(
106107
Restrictions.Ge(nameof(Entity.Amount), 20m)),
107-
Projections.Constant(20m),
108+
//Property scale is 2, make sure constant scale 3 is not lost
109+
Projections.Constant(20.123m),
108110
Projections.Property(nameof(Entity.Amount)));
109111
tagCriteria.SetProjection(conditionalProjection);
110112

111113
// run query
112114
var results = tagCriteria.List();
113115

114-
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
116+
Assert.That(results, Is.EquivalentTo(new[] {20.123m, 42.13m, 20.123m}));
115117
}
116118

117-
// whenTrue is property, whenFalse is constant -> fails before the fix
119+
// whenTrue is property, whenFalse is constant
118120
using (var session = OpenSession())
119121
{
120122
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
121123

122124
var conditionalProjection = Projections.Conditional(
123125
Restrictions.Ge(nameof(Entity.Amount), 20m),
124126
Projections.Property(nameof(Entity.Amount)),
125-
Projections.Constant(20m));
127+
Projections.Constant(20.123m));
126128
tagCriteria.SetProjection(conditionalProjection);
127129

128130
// run query
129131
var results = tagCriteria.List();
130132

131-
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
133+
Assert.That(results, Is.EquivalentTo(new[] {20.123m, 42.13m, 20.123m}));
132134
}
133135
}
134136
}

src/NHibernate/Criterion/ConditionalProjection.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace NHibernate.Criterion
55
using Engine;
66
using SqlCommand;
77
using Type;
8-
using Util;
98

109
[Serializable]
1110
public class ConditionalProjection : SimpleProjection
@@ -57,11 +56,11 @@ public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuer
5756
IType[] falseTypes = whenFalse.GetTypes(criteria, criteriaQuery);
5857

5958
bool areEqual = trueTypes.Length == falseTypes.Length;
60-
if (trueTypes.Length == falseTypes.Length)
59+
if (areEqual)
6160
{
6261
for (int i = 0; i < trueTypes.Length; i++)
6362
{
64-
if(trueTypes[i].Equals(falseTypes[i]) == false)
63+
if(trueTypes[i].ReturnedClass != falseTypes[i].ReturnedClass)
6564
{
6665
areEqual = false;
6766
break;

0 commit comments

Comments
 (0)