Skip to content

Simplify type check in ConditionalProjection #2535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

using NHibernate.Cfg.MappingSchema;
using NHibernate.Criterion;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1180
{
using System.Threading.Tasks;
[KnownBug("NH-3847 (GH-1180)")]
//NH-3847
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
Expand All @@ -27,7 +28,7 @@ protected override HbmMapping GetMappings()
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name, m => { m.Length(10); });
rc.Property(x => x.Name, m => {m.Type(NHibernateUtil.AnsiString); m.Length(5); });
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
});

Expand All @@ -47,6 +48,12 @@ protected override void OnTearDown()
[Test]
public async Task StringTypesAsync()
{
var whenFalse =
Dialect is Oracle8iDialect
//Most dialects allow to return DbType.String and DbType.AnsiString in case statement
//But Oracle throws 'ORA-12704: character set mismatch'
? Projections.Constant("otherstring", NHibernateUtil.AnsiString)
: Projections.Constant("otherstring");
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
Expand All @@ -58,88 +65,92 @@ public async Task StringTypesAsync()
await (transaction.CommitAsync());
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Like(nameof(Entity.Name), "B%")),
Projections.Constant("other"),
//Property - ansi string length 5; contstant - string, length 10
whenFalse,
Projections.Property(nameof(Entity.Name)));
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Like(nameof(Entity.Name), "B%"),
Projections.Property(nameof(Entity.Name)),
Projections.Constant("other"));
whenFalse);
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
}
}

[Test]
public async Task DecimalTypesAsync()
{
//On some dialects (SQLite) Scale mapping is ignored
var propertyResult = TestDialect.HasBrokenDecimalType ? 42.131m : 42.13m;
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
await (session.SaveAsync(new Entity {Amount = 3.14m}));
await (session.SaveAsync(new Entity {Amount = 42.13m}));
await (session.SaveAsync(new Entity {Amount = 17.99m}));
await (session.SaveAsync(new Entity {Amount = 3.141m}));
await (session.SaveAsync(new Entity {Amount = 42.131m}));
await (session.SaveAsync(new Entity {Amount = 17.991m}));

await (transaction.CommitAsync());
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Ge(nameof(Entity.Amount), 20m)),
Projections.Constant(20m),
//Property scale is 2, make sure constant scale 3 is not lost
Projections.Constant(20.123m),
Projections.Property(nameof(Entity.Amount)));
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
Assert.That(results, Is.EquivalentTo(new[] {20.123m, propertyResult, 20.123m}));
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Ge(nameof(Entity.Amount), 20m),
Projections.Property(nameof(Entity.Amount)),
Projections.Constant(20m));
Projections.Constant(20.123m));
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
Assert.That(results, Is.EquivalentTo(new[] {20.123m, propertyResult, 20.123m}));
}
}
}
Expand Down
45 changes: 28 additions & 17 deletions src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using NHibernate.Cfg.MappingSchema;
using NHibernate.Criterion;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

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

Expand All @@ -36,6 +37,12 @@ protected override void OnTearDown()
[Test]
public void StringTypes()
{
var whenFalse =
Dialect is Oracle8iDialect
//Most dialects allow to return DbType.String and DbType.AnsiString in case statement
//But Oracle throws 'ORA-12704: character set mismatch'
? Projections.Constant("otherstring", NHibernateUtil.AnsiString)
: Projections.Constant("otherstring");
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
Expand All @@ -47,88 +54,92 @@ public void StringTypes()
transaction.Commit();
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Like(nameof(Entity.Name), "B%")),
Projections.Constant("other"),
//Property - ansi string length 5; contstant - string, length 10
whenFalse,
Projections.Property(nameof(Entity.Name)));
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Like(nameof(Entity.Name), "B%"),
Projections.Property(nameof(Entity.Name)),
Projections.Constant("other"));
whenFalse);
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
Assert.That(results, Is.EquivalentTo(new[] {"otherstring", "Beta", "otherstring"}));
}
}

[Test]
public void DecimalTypes()
{
//On some dialects (SQLite) Scale mapping is ignored
var propertyResult = TestDialect.HasBrokenDecimalType ? 42.131m : 42.13m;
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Save(new Entity {Amount = 3.14m});
session.Save(new Entity {Amount = 42.13m});
session.Save(new Entity {Amount = 17.99m});
session.Save(new Entity {Amount = 3.141m});
session.Save(new Entity {Amount = 42.131m});
session.Save(new Entity {Amount = 17.991m});

transaction.Commit();
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Ge(nameof(Entity.Amount), 20m)),
Projections.Constant(20m),
//Property scale is 2, make sure constant scale 3 is not lost
Projections.Constant(20.123m),
Projections.Property(nameof(Entity.Amount)));
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
Assert.That(results, Is.EquivalentTo(new[] {20.123m, propertyResult, 20.123m}));
}

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

var conditionalProjection = Projections.Conditional(
Restrictions.Ge(nameof(Entity.Amount), 20m),
Projections.Property(nameof(Entity.Amount)),
Projections.Constant(20m));
Projections.Constant(20.123m));
tagCriteria.SetProjection(conditionalProjection);

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

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
Assert.That(results, Is.EquivalentTo(new[] {20.123m, propertyResult, 20.123m}));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/NHibernate/Criterion/ConditionalProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace NHibernate.Criterion
using Engine;
using SqlCommand;
using Type;
using Util;

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

bool areEqual = trueTypes.Length == falseTypes.Length;
if (trueTypes.Length == falseTypes.Length)
if (areEqual)
{
for (int i = 0; i < trueTypes.Length; i++)
{
if(trueTypes[i].Equals(falseTypes[i]) == false)
if(trueTypes[i].ReturnedClass != falseTypes[i].ReturnedClass)
{
areEqual = false;
break;
Expand Down