Skip to content

Commit 085a170

Browse files
committed
Avoid additional casting and add more test cases
1 parent 05dd4d6 commit 085a170

23 files changed

+1114
-142
lines changed

src/NHibernate.DomainModel/Northwind/Entities/Northwind.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public IQueryable<User> Users
6969
get { return _session.Query<User>(); }
7070
}
7171

72+
public IQueryable<NumericEntity> NumericEntities
73+
{
74+
get { return _session.Query<NumericEntity>(); }
75+
}
76+
7277
public IQueryable<DynamicUser> DynamicUsers
7378
{
7479
get { return _session.Query<DynamicUser>(); }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace NHibernate.DomainModel.Northwind.Entities
2+
{
3+
public class NumericEntity
4+
{
5+
public virtual short Short { get; set; }
6+
7+
public virtual short? NullableShort { get; set; }
8+
9+
public virtual int Integer { get; set; }
10+
11+
public virtual int? NullableInteger { get; set; }
12+
13+
public virtual long Long { get; set; }
14+
15+
public virtual long? NullableLong { get; set; }
16+
17+
public virtual decimal Decimal { get; set; }
18+
19+
public virtual decimal? NullableDecimal { get; set; }
20+
21+
public virtual float Single { get; set; }
22+
23+
public virtual float? NullableSingle { get; set; }
24+
25+
public virtual double Double { get; set; }
26+
27+
public virtual double? NullableDouble { get; set; }
28+
}
29+
}

src/NHibernate.DomainModel/Northwind/Entities/User.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public class User : IUser, IEntity
5050

5151
public virtual User NotMappedUser => this;
5252

53-
public virtual short Short { get; set; }
54-
55-
public virtual short? NullableShort { get; set; }
56-
5753
public virtual EnumStoredAsString Enum1 { get; set; }
5854

5955
public virtual EnumStoredAsString? NullableEnum1 { get; set; }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate.DomainModel.Northwind.Entities" assembly="NHibernate.DomainModel">
3+
<class name="NumericEntity">
4+
<id name="Short">
5+
<generator class="assigned" />
6+
</id>
7+
8+
<property name="NullableShort" />
9+
<property name="Integer" not-null="true" column="`Integer`" />
10+
<property name="NullableInteger" />
11+
<property name="Long" not-null="true" column="`Long`" />
12+
<property name="NullableLong" />
13+
<property name="Decimal" not-null="true" column="`Decimal`" />
14+
<property name="NullableDecimal" />
15+
<property name="Single" not-null="true" column="`Single`" />
16+
<property name="NullableSingle" />
17+
<property name="Double" not-null="true" column="`Double`" />
18+
<property name="NullableDouble" />
19+
</class>
20+
</hibernate-mapping>

src/NHibernate.DomainModel/Northwind/Mappings/User.hbm.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
<column name="ModifiedById" />
2121
</many-to-one>
2222

23-
<property name="Short" column="Enum2" insert="false" update="false" not-null="true" />
24-
<property name="NullableShort" formula="(case when Enum2 = 0 then null else Enum2 end)" insert="false" update="false" />
25-
2623
<property name="Enum1" type="NHibernate.DomainModel.Northwind.Entities.EnumStoredAsStringType, NHibernate.DomainModel">
2724
<column name="Enum1" length="12" />
2825
</property>

src/NHibernate.Test/Async/Linq/NullComparisonTests.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -474,26 +474,27 @@ public async Task NullEqualityAsync()
474474
await (ExpectAsync(session.Query<User>().Where(o => o.CreatedBy.ModifiedBy.Id == 5), Does.Not.Contain("is null").IgnoreCase));
475475
await (ExpectAsync(session.Query<User>().Where(o => 5 == o.CreatedBy.ModifiedBy.Id), Does.Not.Contain("is null").IgnoreCase));
476476

477+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == o.NullableShort), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
478+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
479+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
480+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == o.NullableShort), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
481+
477482
short value = 3;
478-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort == value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
479-
await (ExpectAsync(session.Query<User>().Where(o => value == o.NullableShort), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
480-
481-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort.Value == value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
482-
await (ExpectAsync(session.Query<User>().Where(o => value == o.NullableShort.Value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
483-
await (ExpectAsync(session.Query<User>().Where(o => o.Short == value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
484-
await (ExpectAsync(session.Query<User>().Where(o => value == o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
485-
486-
var shouldCast = Sfi.Dialect is SQLiteDialect || // transparent cast is translated to sql
487-
Sfi.Dialect.TryGetCastTypeName(NHibernateUtil.Int16.SqlType, out var shortCast) &&
488-
Sfi.Dialect.TryGetCastTypeName(NHibernateUtil.Int32.SqlType, out var intCast) &&
489-
shortCast != intCast;
490-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort == 3), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
491-
await (ExpectAsync(session.Query<User>().Where(o => 3 == o.NullableShort), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
492-
493-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort.Value == 3), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
494-
await (ExpectAsync(session.Query<User>().Where(o => 3 == o.NullableShort.Value), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
495-
await (ExpectAsync(session.Query<User>().Where(o => o.Short == 3), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
496-
await (ExpectAsync(session.Query<User>().Where(o => 3 == o.Short), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
483+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
484+
await (ExpectAsync(db.NumericEntities.Where(o => value == o.NullableShort), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
485+
486+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value == value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
487+
await (ExpectAsync(db.NumericEntities.Where(o => value == o.NullableShort.Value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
488+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
489+
await (ExpectAsync(db.NumericEntities.Where(o => value == o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
490+
491+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == 3L), WithoutIsNullAndWithoutCast()));
492+
await (ExpectAsync(db.NumericEntities.Where(o => 3L == o.NullableShort), WithoutIsNullAndWithoutCast()));
493+
494+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value == 3L), WithoutIsNullAndWithoutCast()));
495+
await (ExpectAsync(db.NumericEntities.Where(o => 3L == o.NullableShort.Value), WithoutIsNullAndWithoutCast()));
496+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == 3L), WithoutIsNullAndWithoutCast()));
497+
await (ExpectAsync(db.NumericEntities.Where(o => 3L == o.Short), WithoutIsNullAndWithoutCast()));
497498
}
498499

499500
[Test]
@@ -583,26 +584,27 @@ public async Task NullInequalityAsync()
583584
await (ExpectAsync(session.Query<User>().Where(o => o.CreatedBy.ModifiedBy.Id != 5), Does.Contain("is null").IgnoreCase));
584585
await (ExpectAsync(session.Query<User>().Where(o => 5 != o.CreatedBy.ModifiedBy.Id), Does.Contain("is null").IgnoreCase));
585586

587+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != o.NullableShort), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
588+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
589+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != o.Short), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
590+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != o.NullableShort), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
591+
586592
short value = 3;
587-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort != value), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
588-
await (ExpectAsync(session.Query<User>().Where(o => value != o.NullableShort), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
589-
590-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort.Value != value), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
591-
await (ExpectAsync(session.Query<User>().Where(o => value != o.NullableShort.Value), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
592-
await (ExpectAsync(session.Query<User>().Where(o => o.Short != value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
593-
await (ExpectAsync(session.Query<User>().Where(o => value != o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
594-
595-
var shouldCast = Sfi.Dialect is SQLiteDialect || // transparent cast is translated to sql
596-
Sfi.Dialect.TryGetCastTypeName(NHibernateUtil.Int16.SqlType, out var shortCast) &&
597-
Sfi.Dialect.TryGetCastTypeName(NHibernateUtil.Int32.SqlType, out var intCast) &&
598-
shortCast != intCast;
599-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort != 3), shouldCast ? WithIsNullAndWithCast() : WithIsNullAndWithoutCast()));
600-
await (ExpectAsync(session.Query<User>().Where(o => 3 != o.NullableShort), shouldCast ? WithIsNullAndWithCast() : WithIsNullAndWithoutCast()));
601-
602-
await (ExpectAsync(session.Query<User>().Where(o => o.NullableShort.Value != 3), shouldCast ? WithIsNullAndWithCast() : WithIsNullAndWithoutCast()));
603-
await (ExpectAsync(session.Query<User>().Where(o => 3 != o.NullableShort.Value), shouldCast ? WithIsNullAndWithCast() : WithIsNullAndWithoutCast()));
604-
await (ExpectAsync(session.Query<User>().Where(o => o.Short != 3), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
605-
await (ExpectAsync(session.Query<User>().Where(o => 3 != o.Short), shouldCast ? WithoutIsNullAndWithCast() : WithoutIsNullAndWithoutCast()));
593+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != value), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
594+
await (ExpectAsync(db.NumericEntities.Where(o => value != o.NullableShort), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
595+
596+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value != value), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
597+
await (ExpectAsync(db.NumericEntities.Where(o => value != o.NullableShort.Value), Does.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
598+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != value), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
599+
await (ExpectAsync(db.NumericEntities.Where(o => value != o.Short), Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast")));
600+
601+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != 3L), WithIsNullAndWithoutCast()));
602+
await (ExpectAsync(db.NumericEntities.Where(o => 3 != o.NullableShort), WithIsNullAndWithoutCast()));
603+
604+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value != 3L), WithIsNullAndWithoutCast()));
605+
await (ExpectAsync(db.NumericEntities.Where(o => 3L != o.NullableShort.Value), WithIsNullAndWithoutCast()));
606+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != 3L), WithoutIsNullAndWithoutCast()));
607+
await (ExpectAsync(db.NumericEntities.Where(o => 3L != o.Short), WithoutIsNullAndWithoutCast()));
606608
}
607609

608610
private IResolveConstraint WithIsNullAndWithoutCast()

0 commit comments

Comments
 (0)