Skip to content

Reduce cast usage for aggregate functions #2036

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 30 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7cdeeb5
Reduce cast usage for SUM aggregate function
maca88 Mar 2, 2019
a3699a1
Fix SQLite tests
maca88 Mar 2, 2019
5050805
Adjust tests and regen async
fredericDelaporte Mar 10, 2019
8fb1956
Extend the logic to be used for other aggregate functions
maca88 Mar 11, 2019
36772cf
Check the dialect cast type instead
maca88 Mar 12, 2019
046483e
Fix oracle and mysql issues
maca88 Mar 14, 2019
2a18f37
Add missed override for mysql
maca88 Mar 15, 2019
e966220
async regen
maca88 Mar 15, 2019
21e7dd4
Fix casting for custom registered types
maca88 Apr 10, 2019
8a04fa4
Update TryGetEntityName method
maca88 Oct 1, 2019
bad5b0e
Simplify SupportsType method
maca88 Oct 1, 2019
4fe3d7f
Fixed CodeFactor issue
maca88 Oct 1, 2019
6f32861
Fix TryGetEntityName for custom entity names
maca88 Oct 5, 2019
c35aff7
Remove GetComponentPropertyIndex method
hazzik Oct 5, 2019
f5fda9f
Remove unused parameter from TryGetAllMemberMetadata method
hazzik Oct 5, 2019
27af9bc
Replace TryGetAllMemberMetadata with a visitor class
hazzik Oct 5, 2019
b4027c6
Replace TryGetEntityName with TryGetMappedType
maca88 Oct 8, 2019
7ee50d7
Merge two conditions to reduce complexity
maca88 Oct 8, 2019
ae7a80c
Split TryGetMappedType into two methods
maca88 Oct 10, 2019
256f537
Avoid while(true)
hazzik Oct 11, 2019
418a588
Move getting next member into swith-case blocks
hazzik Oct 11, 2019
d277689
Inline ProcessComponentType
hazzik Oct 11, 2019
a49ddef
Fix typos
hazzik Oct 11, 2019
68f5407
Small corrections
maca88 Oct 11, 2019
315b573
Remove unneeded GetType parameter
maca88 Oct 13, 2019
b691d1d
Fix typo
maca88 Oct 13, 2019
9ea2d63
Add support for polymorphic queries, coalesce and conditional express…
maca88 Oct 28, 2019
17422d7
Revert cast function change
maca88 Nov 12, 2019
660f15d
Adjust test namings
fredericDelaporte Feb 16, 2020
392fb1e
Merge branch 'master' into ReduceSumCast
fredericDelaporte Feb 16, 2020
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
2 changes: 2 additions & 0 deletions src/NHibernate.DomainModel/FooComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public Int32 Count
set { _count = value; }
}

public int NotMapped { get; set; }

public DateTime[] ImportantDates
{
get { return _importantDates; }
Expand Down
91 changes: 90 additions & 1 deletion src/NHibernate.DomainModel/NHSpecific/NullableInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NHibernate.DomainModel.NHSpecific
/// A nullable type that wraps an <see cref="Int32"/> value.
/// </summary>
[TypeConverter(typeof(NullableInt32Converter)), Serializable()]
public struct NullableInt32 : IFormattable, IComparable
public struct NullableInt32 : IFormattable, IComparable, IConvertible
{
public static readonly NullableInt32 Default = new NullableInt32();

Expand Down Expand Up @@ -234,5 +234,94 @@ public static NullableInt32 Parse(string s)
// TODO: implement the rest of the Parse overloads found in Int32

#endregion

#region IConvertible

public TypeCode GetTypeCode()
{
return _value.GetTypeCode();
}

public bool ToBoolean(IFormatProvider provider)
{
return ((IConvertible) _value).ToBoolean(provider);
}

public char ToChar(IFormatProvider provider)
{
return ((IConvertible) _value).ToChar(provider);
}

public sbyte ToSByte(IFormatProvider provider)
{
return ((IConvertible) _value).ToSByte(provider);
}

public byte ToByte(IFormatProvider provider)
{
return ((IConvertible) _value).ToByte(provider);
}

public short ToInt16(IFormatProvider provider)
{
return ((IConvertible) _value).ToInt16(provider);
}

public ushort ToUInt16(IFormatProvider provider)
{
return ((IConvertible) _value).ToUInt16(provider);
}

public int ToInt32(IFormatProvider provider)
{
return ((IConvertible) _value).ToInt32(provider);
}

public uint ToUInt32(IFormatProvider provider)
{
return ((IConvertible) _value).ToUInt32(provider);
}

public long ToInt64(IFormatProvider provider)
{
return ((IConvertible) _value).ToInt64(provider);
}

public ulong ToUInt64(IFormatProvider provider)
{
return ((IConvertible) _value).ToUInt64(provider);
}

public float ToSingle(IFormatProvider provider)
{
return ((IConvertible) _value).ToSingle(provider);
}

public double ToDouble(IFormatProvider provider)
{
return ((IConvertible) _value).ToDouble(provider);
}

public decimal ToDecimal(IFormatProvider provider)
{
return ((IConvertible) _value).ToDecimal(provider);
}

public DateTime ToDateTime(IFormatProvider provider)
{
return ((IConvertible) _value).ToDateTime(provider);
}

public string ToString(IFormatProvider provider)
{
return _value.ToString(provider);
}

public object ToType(System.Type conversionType, IFormatProvider provider)
{
return ((IConvertible) _value).ToType(conversionType, provider);
}

#endregion
}
}
4 changes: 3 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Entities/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public string Fax
get { return _fax; }
}

public int NotMapped => 1;

public static bool operator ==(Address address1, Address address2)
{
if (!ReferenceEquals(address1, null) &&
Expand Down Expand Up @@ -114,4 +116,4 @@ public override int GetHashCode()
(_fax ?? string.Empty).GetHashCode();
}
}
}
}
15 changes: 15 additions & 0 deletions src/NHibernate.DomainModel/Northwind/Entities/IEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernate.DomainModel.Northwind.Entities
{
public interface IEntity<TId>
{
TId Id { get; set; }
}

public interface IEntity : IEntity<int>
{
}
}
4 changes: 3 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Entities/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ public virtual float ShippingWeight
set { _shippingWeight = value; }
}

public virtual int NotMapped => 1;

public virtual ReadOnlyCollection<OrderLine> OrderLines
{
get { return new ReadOnlyCollection<OrderLine>(_orderLines); }
}
}
}
}
6 changes: 5 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IUser
EnumStoredAsInt32 Enum2 { get; set; }
}

public class User : IUser
public class User : IUser, IEntity
{
public virtual int Id { get; set; }

Expand All @@ -50,6 +50,10 @@ public class User : IUser

public virtual EnumStoredAsInt32 Enum2 { get; set; }

public virtual int NotMapped { get; set; }

public virtual Role NotMappedRole { get; set; }

public User() { }

public User(string name, DateTime registeredAt)
Expand Down
23 changes: 23 additions & 0 deletions src/NHibernate.Test/Async/Linq/SelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.DomainModel.NHSpecific;
using NHibernate.DomainModel.Northwind.Entities;
using NHibernate.Type;
using NUnit.Framework;
using NHibernate.Linq;

Expand Down Expand Up @@ -307,6 +309,10 @@ public async Task CanProjectWithCastAsync()

var names5 = await (db.Users.Select(p => new { p1 = (p as IUser).Name }).ToListAsync());
Assert.AreEqual(3, names5.Count);

var names6 = await (db.Users.Select(p => new { p1 = (long) p.Id }).ToListAsync());
Assert.AreEqual(3, names6.Count);

// ReSharper restore RedundantCast
}

Expand Down Expand Up @@ -453,6 +459,23 @@ public async Task CanSelectConditionalObjectAsync()
Assert.That(fatherIsKnown, Has.Exactly(1).With.Property("FatherIsKnown").True);
}

[Test]
public async Task CanCastToDerivedTypeAsync()
{
var dogs = await (db.Animals
.Where(a => ((Dog) a).Pregnant)
.Select(a => new {a.SerialNumber})
.ToListAsync());
Assert.That(dogs, Has.Exactly(1).With.Property("SerialNumber").Not.Null);
}

[Test]
public async Task CanCastToCustomRegisteredTypeAsync()
{
TypeFactory.RegisterType(typeof(NullableInt32), new NullableInt32Type(), Enumerable.Empty<string>());
Assert.That(await (db.Users.Where(o => (NullableInt32) o.Id == 1).ToListAsync()), Has.Count.EqualTo(1));
}

public class Wrapper<T>
{
public T item;
Expand Down
Loading