Skip to content

Commit 25675f5

Browse files
committed
no message
1 parent 1fd686d commit 25675f5

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

SubSonic.Tests/DAL/OData/ODataCompatibilityTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,47 @@ public void ShouldBeAbleToApplyOrderByThenByUsingOData(string direction)
210210
query.Sql.Should().Contain("DESC");
211211
}
212212
}
213+
214+
/// <summary>
215+
/// simulate a $top
216+
/// </summary>
217+
/// <param name="top"></param>
218+
[Test]
219+
[TestCase(1)]
220+
[TestCase(10)]
221+
public void ShouldBeAbleToApplyTopUsingOData(int top)
222+
{
223+
personQueryOptions.ApplyTo(Arg.Any<IQueryable>())
224+
.Returns(call =>
225+
{
226+
if (call.Arg<IQueryable>() is IQueryable<Models.Person> people)
227+
{
228+
return people.Take(top);
229+
}
230+
231+
throw new NotImplementedException();
232+
});
233+
234+
IQueryable select = personQueryOptions.ApplyTo(Context.People);
235+
236+
IDbQuery query = null;
237+
238+
var logging = Context.Instance.GetService<ISubSonicLogger<DbSelectExpression>>();
239+
240+
using (var perf = logging.Start("SQL Query Writer"))
241+
{
242+
FluentActions.Invoking(() =>
243+
{
244+
ISubSonicQueryProvider<Models.Person> builder = Context.Instance.GetService<ISubSonicQueryProvider<Models.Person>>();
245+
246+
query = builder.ToQuery(select.Expression);
247+
}).Should().NotThrow();
248+
}
249+
250+
logging.LogInformation("\n" + query.Sql + "\n");
251+
252+
query.Sql.Should().NotBeNullOrEmpty();
253+
query.Sql.Should().Contain($"SELECT TOP ({top})");
254+
}
213255
}
214256
}

SubSonic/Error.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ public static Exception Argument(string message, string parameter)
1818

1919
public static Exception NotImplemented()
2020
{
21-
throw new NotImplementedException();
21+
return new NotImplementedException();
22+
}
23+
24+
internal static Exception NotSupported(string message)
25+
{
26+
return new NotSupportedException(message);
2227
}
2328
}
2429
}

SubSonic/Extensions/Internal/MethodInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,15 @@ public static bool IsWhere(this MethodInfo info)
2828
{
2929
return info.Name.Equals(nameof(Legacy.Where), StringComparison.CurrentCulture);
3030
}
31+
32+
public static bool IsTake(this MethodInfo info)
33+
{
34+
return info.Name.Equals(nameof(Legacy.Take), StringComparison.CurrentCulture);
35+
}
36+
37+
public static bool IsSkip(this MethodInfo info)
38+
{
39+
return info.Name.Equals(nameof(Legacy.Skip), StringComparison.CurrentCulture);
40+
}
3141
}
3242
}

SubSonic/Infrastructure/Builders/DbSqlQueryBuilder/DbSqlQueryBuilderBuildMethods.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,17 @@ protected virtual Expression BuildQuery(Expression expression)
495495
{
496496
return BuildSelectWithWhere(call);
497497
}
498+
else if (call.Method.IsTake())
499+
{
500+
return BuildSelectWithTake(call);
501+
}
502+
else if (call.Method.IsSkip())
503+
{
504+
throw Error.NotImplemented();
505+
}
498506
else
499507
{
500-
throw new NotSupportedException(SubSonicErrorMessages.UnSupportedMethodException.Format(call.Method.Name));
508+
throw Error.NotSupported(SubSonicErrorMessages.UnSupportedMethodException.Format(call.Method.Name));
501509
}
502510
}
503511
}
@@ -526,6 +534,34 @@ private static Type[] GetTypeArguments(LambdaExpression expression)
526534
return Array.Empty<Type>();
527535
}
528536

537+
private Expression BuildSelectWithTake(MethodCallExpression expression)
538+
{
539+
if (!(expression is null))
540+
{
541+
DbSelectExpression select = null;
542+
Expression take = null;
543+
544+
foreach (var argument in expression.Arguments)
545+
{
546+
if (argument is DbSelectExpression _select)
547+
{
548+
select = _select;
549+
}
550+
else if (argument is ConstantExpression constant)
551+
{
552+
take = constant;
553+
}
554+
}
555+
556+
return DbExpression.DbSelect(
557+
select,
558+
take
559+
);
560+
}
561+
562+
return expression;
563+
}
564+
529565
private Expression BuildSelectWithWhere(MethodCallExpression expression)
530566
{
531567
if (!(expression is null))

SubSonic/Linq/Expressions/DbSelectExpression.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ public static DbExpression DbSelect(DbSelectExpression select, DbExpression wher
143143
return new DbSelectExpression(select.QueryObject, select.Type, select.From, select.Columns, where ?? select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Take);
144144
}
145145

146+
public static DbExpression DbSelect(DbSelectExpression select, Expression take)
147+
{
148+
if (select is null)
149+
{
150+
throw Error.ArgumentNull(nameof(select));
151+
}
152+
153+
return new DbSelectExpression(select.QueryObject, select.Type, select.From, select.Columns, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, take);
154+
}
155+
146156
public static DbExpression DbSelect(DbSelectExpression select, DbJoinExpression join)
147157
{
148158
if (select is null)

0 commit comments

Comments
 (0)