Skip to content

Commit 39893cf

Browse files
committed
#41 implement support for min
1 parent 9b28976 commit 39893cf

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

SubSonic.Tests/DAL/ExtensionMethod/ExtensionMethodTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,24 @@
1010

1111
namespace SubSonic.Tests.DAL.ExtensionMethod
1212
{
13+
using Extensions.Test;
14+
1315
[TestFixture]
1416
public class ExtensionMethodTests
1517
: BaseTestFixture
1618
{
19+
protected override void SetSelectBehaviors()
20+
{
21+
base.SetSelectBehaviors();
22+
23+
string
24+
person_min = @"SELECT MIN([T1].[ID])
25+
FROM [dbo].[Person] AS [T1]";
26+
27+
Context.Database.Instance.AddCommandBehavior(person_min, cmd => People.Min(x => x.ID));
28+
}
29+
30+
1731
[Test]
1832
public void TheCountMethodIsSupported()
1933
{
@@ -25,5 +39,11 @@ public void TheLongCountMethodIsSupported()
2539
{
2640
Context.People.LongCount().Should().BeGreaterThan(0).And.IsOfType<long>();
2741
}
42+
43+
[Test]
44+
public void TheMinMethodIsSupported()
45+
{
46+
Context.People.Min(x => x.ID).Should().Be(1);
47+
}
2848
}
2949
}

SubSonic.Tests/DAL/SUT/BaseTestFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public virtual void SetupTestFixture()
7676

7777
Renters = new List<Renter>()
7878
{
79-
new Renter() { PersonID = 1, UnitID = 1, StartDate = new DateTime(1980, 01, 01), EndDate = new DateTime(1990, 02, 28) },
80-
new Renter() { PersonID = 2, UnitID = 1, StartDate = new DateTime(1990, 03, 01) },
81-
new Renter() { PersonID = 3, UnitID = 2, StartDate = new DateTime(1980, 03, 01), EndDate = new DateTime(2000, 01, 01) },
82-
new Renter() { PersonID = 1, UnitID = 3, StartDate = new DateTime(1990, 03, 01) },
83-
new Renter() { PersonID = 4, UnitID = 4, StartDate = new DateTime(2000, 01, 01) }
79+
new Renter() { PersonID = 1, UnitID = 1, Rent = 100M, StartDate = new DateTime(1980, 01, 01), EndDate = new DateTime(1990, 02, 28) },
80+
new Renter() { PersonID = 2, UnitID = 1, Rent = 150M, StartDate = new DateTime(1990, 03, 01) },
81+
new Renter() { PersonID = 3, UnitID = 2, Rent = 200M, StartDate = new DateTime(1980, 03, 01), EndDate = new DateTime(2000, 01, 01) },
82+
new Renter() { PersonID = 1, UnitID = 3, Rent = 250M, StartDate = new DateTime(1990, 03, 01) },
83+
new Renter() { PersonID = 4, UnitID = 4, Rent = 300M, StartDate = new DateTime(2000, 01, 01) }
8484
};
8585

8686
RealEstateProperties = new List<RealEstateProperty>()

SubSonic/Infrastructure/Builders/DbSqlQueryBuilder/DbSqlQueryBuilderQueryProvider.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
5353
{
5454
if (unary.Operand is LambdaExpression lambda)
5555
{
56-
where = BuildWhere(dbSelect, lambda);
56+
switch (lambda.Body.NodeType)
57+
{
58+
case ExpressionType.MemberAccess:
59+
dbSelect = (DbSelectExpression)BuildSelect(dbSelect, dbSelect.Columns.Where(x => x.PropertyName.Equals(lambda.GetProperty().Name, StringComparison.Ordinal)));
60+
break;
61+
default:
62+
where = BuildWhere(dbSelect, lambda);
63+
break;
64+
}
5765
}
5866
}
5967
}
@@ -83,7 +91,7 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
8391
throw Error.InvalidOperation($"Method {call.Method.Name} expects data.");
8492
}
8593
}
86-
else if (call.Method.Name.In(nameof(Queryable.Count), nameof(Queryable.LongCount)))
94+
else if (call.Method.Name.In(nameof(Queryable.Count), nameof(Queryable.LongCount), nameof(Queryable.Min)))
8795
{
8896
if (BuildSelect(dbSelect, where) is DbSelectExpression select)
8997
{
@@ -92,9 +100,20 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
92100
throw Error.NotSupported(SubSonicErrorMessages.MethodNotSupported.Format(call.Method.Name));
93101
}
94102

103+
Expression argument = null;
104+
105+
if (select.Columns.Count > 1)
106+
{
107+
argument = select.Columns.First(x => x.Property.IsPrimaryKey).Expression;
108+
}
109+
else
110+
{
111+
argument = select.Columns.Single().Expression;
112+
}
113+
95114
TResult result = Execute<TResult>(DbExpression.DbSelectAggregate(select, new[]
96115
{
97-
DbExpression.DbAggregate(typeof(TResult), aggregateType, select.Columns.First(x => x.Property.IsPrimaryKey).Expression)
116+
DbExpression.DbAggregate(typeof(TResult), aggregateType, argument)
98117
}));
99118

100119
if (select.Take is ConstantExpression take)

0 commit comments

Comments
 (0)