Skip to content

Commit 7360394

Browse files
maca88hazzikbahusoid
authored
Avoid unnecessary casting for Linq provider (#2491)
Fixes #2490 Co-authored-by: Alex Zaytsev <[email protected]> Co-authored-by: Roman Artiukhin <[email protected]>
1 parent 1581887 commit 7360394

23 files changed

+1293
-66
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+
}
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.Test/Async/Linq/NullComparisonTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections.Generic;
1313
using System.Linq;
1414
using System.Text;
15+
using NHibernate.Dialect;
1516
using NHibernate.Linq;
1617
using NHibernate.DomainModel.Northwind.Entities;
1718
using NUnit.Framework;
@@ -472,6 +473,33 @@ public async Task NullEqualityAsync()
472473

473474
await (ExpectAsync(session.Query<User>().Where(o => o.CreatedBy.ModifiedBy.Id == 5), Does.Not.Contain("is null").IgnoreCase));
474475
await (ExpectAsync(session.Query<User>().Where(o => 5 == o.CreatedBy.ModifiedBy.Id), Does.Not.Contain("is null").IgnoreCase));
476+
477+
if (Sfi.Dialect is FirebirdDialect)
478+
{
479+
return;
480+
}
481+
482+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == o.NullableShort), WithIsNullAndWithoutCast()));
483+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == o.Short), WithoutIsNullAndWithoutCast()));
484+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == o.Short), WithoutIsNullAndWithoutCast()));
485+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == o.NullableShort), WithoutIsNullAndWithoutCast()));
486+
487+
short value = 3;
488+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == value), WithoutIsNullAndWithoutCast()));
489+
await (ExpectAsync(db.NumericEntities.Where(o => value == o.NullableShort), WithoutIsNullAndWithoutCast()));
490+
491+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value == value), WithoutIsNullAndWithoutCast()));
492+
await (ExpectAsync(db.NumericEntities.Where(o => value == o.NullableShort.Value), WithoutIsNullAndWithoutCast()));
493+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == value), WithoutIsNullAndWithoutCast()));
494+
await (ExpectAsync(db.NumericEntities.Where(o => value == o.Short), WithoutIsNullAndWithoutCast()));
495+
496+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort == 3L), WithoutIsNullAndWithoutCast()));
497+
await (ExpectAsync(db.NumericEntities.Where(o => 3L == o.NullableShort), WithoutIsNullAndWithoutCast()));
498+
499+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value == 3L), WithoutIsNullAndWithoutCast()));
500+
await (ExpectAsync(db.NumericEntities.Where(o => 3L == o.NullableShort.Value), WithoutIsNullAndWithoutCast()));
501+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short == 3L), WithoutIsNullAndWithoutCast()));
502+
await (ExpectAsync(db.NumericEntities.Where(o => 3L == o.Short), WithoutIsNullAndWithoutCast()));
475503
}
476504

477505
[Test]
@@ -560,6 +588,43 @@ public async Task NullInequalityAsync()
560588

561589
await (ExpectAsync(session.Query<User>().Where(o => o.CreatedBy.ModifiedBy.Id != 5), Does.Contain("is null").IgnoreCase));
562590
await (ExpectAsync(session.Query<User>().Where(o => 5 != o.CreatedBy.ModifiedBy.Id), Does.Contain("is null").IgnoreCase));
591+
592+
if (Sfi.Dialect is FirebirdDialect)
593+
{
594+
return;
595+
}
596+
597+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != o.NullableShort), WithIsNullAndWithoutCast()));
598+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != o.Short), WithoutIsNullAndWithoutCast()));
599+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != o.Short), WithIsNullAndWithoutCast()));
600+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != o.NullableShort), WithIsNullAndWithoutCast()));
601+
602+
short value = 3;
603+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != value), WithIsNullAndWithoutCast()));
604+
await (ExpectAsync(db.NumericEntities.Where(o => value != o.NullableShort), WithIsNullAndWithoutCast()));
605+
606+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value != value), WithIsNullAndWithoutCast()));
607+
await (ExpectAsync(db.NumericEntities.Where(o => value != o.NullableShort.Value), WithIsNullAndWithoutCast()));
608+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != value), WithoutIsNullAndWithoutCast()));
609+
await (ExpectAsync(db.NumericEntities.Where(o => value != o.Short), WithoutIsNullAndWithoutCast()));
610+
611+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort != 3L), WithIsNullAndWithoutCast()));
612+
await (ExpectAsync(db.NumericEntities.Where(o => 3 != o.NullableShort), WithIsNullAndWithoutCast()));
613+
614+
await (ExpectAsync(db.NumericEntities.Where(o => o.NullableShort.Value != 3L), WithIsNullAndWithoutCast()));
615+
await (ExpectAsync(db.NumericEntities.Where(o => 3L != o.NullableShort.Value), WithIsNullAndWithoutCast()));
616+
await (ExpectAsync(db.NumericEntities.Where(o => o.Short != 3L), WithoutIsNullAndWithoutCast()));
617+
await (ExpectAsync(db.NumericEntities.Where(o => 3L != o.Short), WithoutIsNullAndWithoutCast()));
618+
}
619+
620+
private IResolveConstraint WithIsNullAndWithoutCast()
621+
{
622+
return Does.Contain("is null").IgnoreCase.And.Not.Contain("cast").IgnoreCase;
623+
}
624+
625+
private IResolveConstraint WithoutIsNullAndWithoutCast()
626+
{
627+
return Does.Not.Contain("is null").IgnoreCase.And.Not.Contain("cast").IgnoreCase;
563628
}
564629

565630
[Test]

0 commit comments

Comments
 (0)