diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 196f4b4..b594909 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -92,6 +92,8 @@ jobs: runs-on: ubuntu-latest permissions: contents: read + # NuGet Trusted Publishing: the job's OIDC token is exchanged for a short-lived API key. + id-token: write needs: [build, test] # This environment gates the publish behind whatever protection rules are configured for it in # the repo's Settings -> Environments (e.g. required reviewers). GitHub auto-creates an environment @@ -109,7 +111,15 @@ jobs: pattern: nupkg-* path: artifacts merge-multiple: true + # nuget.org's trusted publishing policy for this repository names this workflow (release.yml) and + # the nuget-release environment, so renaming either one stops the login here, before any push. + # The key it returns lives one hour: the push right after it is the only step that uses it. + - name: Log in to nuget.org + id: nuget + uses: NuGet/login@8d196754b4036150537f80ac539e15c2f1028841 # v1.2.0 + with: + user: ${{ secrets.NUGET_USER }} - name: Push to NuGet.org env: - NUGET_KEY: ${{ secrets.nuget_key }} + NUGET_KEY: ${{ steps.nuget.outputs.NUGET_API_KEY }} run: dotnet nuget push "artifacts/*.nupkg" --skip-duplicate -k "$NUGET_KEY" -s https://api.nuget.org/v3/index.json diff --git a/README.md b/README.md index e276cff..ad1888f 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,8 @@ native engine as well, reference it yourself; the two implement the same contrac This package is the **Entity Framework Core implementation** of those contracts. It translates a single `QueryOptions` into an EF `IQueryable` — applying, in order, custom *before* hooks, the -specification and predicate filter, eager `Include`s, sortings (server-side / EF-translatable), -`AsNoTracking`, `IgnoreQueryFilters`, query tags and custom *after* hooks — and returns `PagedResult` +specification and predicate filter, eager `Include`s (shaped ones through a filtered include), sortings +(server-side / EF-translatable, nulls placed first or last on request), `Skip`/`Take`, `AsNoTracking`, `IgnoreQueryFilters`, query tags and custom *after* hooks — and returns `PagedResult` from paged reads. The relational providers also carry a parameterized raw-SQL executor (functions and stored procedures, always via `DbParameter`). diff --git a/Repository.md b/Repository.md index 27eb16e..aeae728 100644 --- a/Repository.md +++ b/Repository.md @@ -14,6 +14,7 @@ An entity is a plain class that implements `IEntity`. The key is exposed t ```csharp using System; +using System.Collections.Generic; using eQuantic.Core.Data.Repository; public enum OrderStatus { Pending, Paid, Cancelled } @@ -34,13 +35,24 @@ public class OrderData : IEntity public decimal Total { get; set; } public OrderStatus Status { get; set; } public DateTime CreatedAt { get; set; } + public DateTime? ShippedAt { get; set; } public Guid CustomerId { get; set; } public CustomerData Customer { get; set; } = default!; + public List Lines { get; set; } = []; + public Guid GetKey() => Id; public void SetKey(Guid key) => Id = key; } + +public class OrderLineData +{ + public Guid Id { get; set; } + public Guid OrderId { get; set; } + public string Status { get; set; } = string.Empty; + public DateTime AddedAt { get; set; } +} ``` ## 2. The DbContext @@ -180,6 +192,46 @@ public record OrderSummary(Guid Id, decimal Total, string Customer); Every method has a synchronous twin on the queryable repository (`GetPaged`, `Get`, `Count`, `Sum`, …). +### Read shapes: a page without its total, nulls placed, related rows chosen + +`QueryOptions` (eQuantic.Core.Data.Abstractions 7.0) also says which rows a read returns, and these +repositories honour it as the eQuantic.Core.Data engines do: + +```csharp +// rows 11 to 20 of an order, with no count run (pair Skip/Take with an order): +IEnumerable second = await orders.GetAllAsync( + new QueryOptions().OrderByDescending(o => o.CreatedAt).Skip(10).Take(10)); + +// nulls first or last whatever the store's default (PostgreSQL puts them last ascending, the others first): +IEnumerable byShipping = await orders.GetAllAsync( + new QueryOptions().OrderBy(o => o.ShippedAt, NullsOrder.Last)); + +// each order with only its three newest lines that are not cancelled, newest first: +IEnumerable recent = await orders.GetAllAsync(new QueryOptions() + .Include(o => o.Lines, lines => lines + .Where(l => l.Status != "cancelled") + .OrderByDescending(l => l.AddedAt) + .Take(3))); +``` + +- `Skip`/`Take` shape the rows a read returns. `CountAsync`, `AnyAsync`, `AllAsync`, `SumAsync` and + `GetPagedAsync` do not return rows that way, so they refuse them with a `NotSupportedException` rather than + count a page or page twice. With `GetFirstAsync` and `GetSingleAsync` the window applies before the read's own + limit. +- A placement of nulls on a relational store becomes a leading "is null" key. MongoDB and Cosmos DB sort nulls as + the smallest value, so first ascending and last descending are a plain sort there. Cosmos DB orders by + document paths only, so it refuses the other two placements before sending anything. +- A shaped include is Entity Framework's filtered include (`Include(o => o.Lines.Where(…).OrderBy(…).Take(3))`). + As Entity Framework documents for filtered includes, a tracking read in a context that already tracks some + of those lines fixes them up into the collection too. Read with `NoTracking()`, or in a fresh context, when the + collection must hold exactly the shape. + +The other additions of 7.0 are capabilities a repository opts into (`IUpsertRepository`, +`IReturningUpdateRepository`, `IPartialModifyRepository`, `IDistinctReadRepository`, `ILockingUnitOfWork`) and +the native engine's modeling attributes (`[EnumType]`, `[StoreGenerated]`, JSON members). These repositories do +not implement them: test for the interface before you use one, and map columns through Entity Framework's own +model configuration. + ## 6. Specifications Encapsulate a reusable rule as an `ISpecification` (base class `Specification`). diff --git a/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net10.csproj b/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net10.csproj index 1dbca03..75cc0d9 100644 --- a/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net10.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net10.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and Azure Cosmos DB eQuantic.Core.Data.EntityFramework.CosmosDb - 10.5.0.0 + 10.6.0.0 net10.0 eQuantic.Core.Data.EntityFramework.CosmosDb eQuantic.Core.Data.EntityFramework.CosmosDb @@ -12,8 +12,8 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 10.5.0.0 - 10.5.0.0 + 10.6.0.0 + 10.6.0.0 enable enable @@ -21,7 +21,7 @@ - + diff --git a/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net8.csproj b/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net8.csproj index 2f80c42..c0f659a 100644 --- a/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net8.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net8.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and Azure Cosmos DB eQuantic.Core.Data.EntityFramework.CosmosDb - 8.6.0.0 + 8.7.0.0 net8.0 eQuantic.Core.Data.EntityFramework.CosmosDb eQuantic.Core.Data.EntityFramework.CosmosDb @@ -12,8 +12,8 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 8.6.0.0 - 8.6.0.0 + 8.7.0.0 + 8.7.0.0 enable enable @@ -21,7 +21,7 @@ - + diff --git a/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net10.csproj b/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net10.csproj index d6705e6..dcfd77a 100644 --- a/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net10.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net10.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and Mongo DB eQuantic.Core.Data.EntityFramework.MongoDb - 10.5.0.0 + 10.6.0.0 net10.0 eQuantic.Core.Data.EntityFramework.MongoDb eQuantic.Core.Data.EntityFramework.MongoDb @@ -12,8 +12,8 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 10.5.0.0 - 10.5.0.0 + 10.6.0.0 + 10.6.0.0 enable enable @@ -21,7 +21,7 @@ - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net8.csproj b/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net8.csproj index c5feb72..f9e17b4 100644 --- a/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net8.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net8.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and Mongo DB eQuantic.Core.Data.EntityFramework.MongoDb - 8.6.0.0 + 8.7.0.0 net8.0 eQuantic.Core.Data.EntityFramework.MongoDb eQuantic.Core.Data.EntityFramework.MongoDb @@ -12,8 +12,8 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 8.6.0.0 - 8.6.0.0 + 8.7.0.0 + 8.7.0.0 enable enable @@ -21,7 +21,7 @@ - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net10.csproj b/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net10.csproj index c332a4b..a04871e 100644 --- a/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net10.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net10.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and MySQL eQuantic.Core.Data.EntityFramework.MySql - 10.4.0.0 + 10.5.0.0 net10.0 eQuantic.Core.Data.EntityFramework.MySql eQuantic.Core.Data.EntityFramework.MySql @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 10.4.0.0 - 10.4.0.0 + 10.5.0.0 + 10.5.0.0 - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net8.csproj b/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net8.csproj index bd6f03f..c5be9b0 100644 --- a/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net8.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net8.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and MySQL eQuantic.Core.Data.EntityFramework.MySql - 8.5.0.0 + 8.6.0.0 net8.0 eQuantic.Core.Data.EntityFramework.MySql eQuantic.Core.Data.EntityFramework.MySql @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 8.5.0.0 - 8.5.0.0 + 8.6.0.0 + 8.6.0.0 - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net10.csproj b/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net10.csproj index e192d7f..fcea2c5 100644 --- a/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net10.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net10.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and PostgreSQL eQuantic.Core.Data.EntityFramework.PostgreSql - 10.4.0.0 + 10.5.0.0 net10.0 eQuantic.Core.Data.EntityFramework.PostgreSql eQuantic.Core.Data.EntityFramework.PostgreSql @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 10.4.0.0 - 10.4.0.0 + 10.5.0.0 + 10.5.0.0 - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net8.csproj b/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net8.csproj index 64e932d..4335df6 100644 --- a/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net8.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.PostgreSql/eQuantic.Core.Data.EntityFramework.PostgreSql.Net8.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and PostgreSQL eQuantic.Core.Data.EntityFramework.PostgreSql - 8.5.0.0 + 8.6.0.0 net8.0 eQuantic.Core.Data.EntityFramework.PostgreSql eQuantic.Core.Data.EntityFramework.PostgreSql @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 8.5.0.0 - 8.5.0.0 + 8.6.0.0 + 8.6.0.0 - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.Relational/eQuantic.Core.Data.EntityFramework.Relational.csproj b/src/eQuantic.Core.Data.EntityFramework.Relational/eQuantic.Core.Data.EntityFramework.Relational.csproj index d4ea7a1..7ff959f 100644 --- a/src/eQuantic.Core.Data.EntityFramework.Relational/eQuantic.Core.Data.EntityFramework.Relational.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.Relational/eQuantic.Core.Data.EntityFramework.Relational.csproj @@ -4,20 +4,20 @@ Shared relational implementation for eQuantic Core Data Entity Framework providers eQuantic.Core.Data.EntityFramework.Relational - 4.3.0.0 + 4.4.0.0 net8.0;net10.0 eQuantic.Core.Data.EntityFramework.Relational eQuantic.Core.Data.EntityFramework.Relational eQuantic;Core;Data;Library;Repository;Pattern;SQL;Relational Shared relational base used by the SqlServer, PostgreSql and MySql providers - 4.3.0.0 - 4.3.0.0 + 4.4.0.0 + 4.4.0.0 - + diff --git a/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net10.csproj b/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net10.csproj index 24024df..371b4a2 100644 --- a/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net10.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net10.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and SQL Server eQuantic.Core.Data.EntityFramework.SqlServer - 10.4.0.0 + 10.5.0.0 net10.0 eQuantic.Core.Data.EntityFramework.SqlServer eQuantic.Core.Data.EntityFramework.SqlServer @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 10.4.0.0 - 10.4.0.0 + 10.5.0.0 + 10.5.0.0 - + all diff --git a/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net8.csproj b/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net8.csproj index fdf5736..2b264c1 100644 --- a/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net8.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.SqlServer/eQuantic.Core.Data.EntityFramework.SqlServer.Net8.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework and SQL Server eQuantic.Core.Data.EntityFramework.SqlServer - 8.5.0.0 + 8.6.0.0 net8.0 eQuantic.Core.Data.EntityFramework.SqlServer eQuantic.Core.Data.EntityFramework.SqlServer @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 8.5.0.0 - 8.5.0.0 + 8.6.0.0 + 8.6.0.0 - + all diff --git a/src/eQuantic.Core.Data.EntityFramework/Repository/Extensions/QueryableExtensions.cs b/src/eQuantic.Core.Data.EntityFramework/Repository/Extensions/QueryableExtensions.cs index ec918c4..ba86079 100644 --- a/src/eQuantic.Core.Data.EntityFramework/Repository/Extensions/QueryableExtensions.cs +++ b/src/eQuantic.Core.Data.EntityFramework/Repository/Extensions/QueryableExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using eQuantic.Core.Data.Repository.Options; using eQuantic.Linq.Web; using Microsoft.EntityFrameworkCore; @@ -63,6 +64,18 @@ public static IQueryable IncludeMany(this IQueryable /// The ordered query. public static IQueryable ApplySorts(this IQueryable query, IReadOnlyList> sortings) + { + return query.ApplySorts(sortings, null, NullsPlacement.AnyExpression); + } + + /// + /// Applies the sortings, each placing its nulls where says (a leading + /// "is null" key, which every relational store translates), and refusing a placement the store cannot + /// order by. + /// + internal static IQueryable ApplySorts(this IQueryable query, + IReadOnlyList> sortings, Func, NullsOrder> nullsOf, + NullsPlacement placement) { if (sortings is not { Count: > 0 }) { @@ -70,6 +83,7 @@ public static IQueryable ApplySorts(this IQueryable q } var first = true; + var expression = query.Expression; foreach (var sort in sortings) { if (sort == null) @@ -77,22 +91,181 @@ public static IQueryable ApplySorts(this IQueryable q continue; } - var method = first - ? (sort.Direction == SortDirection.Ascending ? nameof(Queryable.OrderBy) : nameof(Queryable.OrderByDescending)) - : (sort.Direction == SortDirection.Ascending ? nameof(Queryable.ThenBy) : nameof(Queryable.ThenByDescending)); + expression = Order(expression, true, typeof(TEntity), sort.KeySelector, + sort.Direction == SortDirection.Descending, nullsOf?.Invoke(sort) ?? NullsOrder.Default, placement, + ref first); + } + + return query.Provider.CreateQuery(expression); + } + + /// + /// Refuses Skip/Take on a read that works over every matching row (a count, a sum, an + /// All) or that pages by itself: silently counting a page, or paging twice, would answer another + /// question. The same rule, in the same words, as the eQuantic.Core.Data engines. + /// + /// The type of the entity. + /// The read's options. + /// The read, as the message names it (CountAsync). + internal static void RefuseRowWindow(QueryOptions options, string read) + where TEntity : class + { + if (options is { SkipCount: not null } or { TakeCount: not null }) + { + throw new NotSupportedException( + $"Skip and Take shape the rows a read returns, and {read} does not return them that way; drop them " + + "from the options (GetPagedAsync reads a page with its total)."); + } + } + + /// + /// Eagerly loads a shaped collection include through EF Core's filtered include + /// (Include(x => x.Steps.Where(…).OrderBy(…).Skip(…).Take(…))): each parent gets exactly the + /// related entities the shape keeps, in its order. + /// + internal static IQueryable IncludeShaped(this IQueryable query, IncludeShape shape, + NullsPlacement placement) + where TEntity : class + { + var navigation = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .FirstOrDefault(property => property.Name == shape.Path) + ?? throw new InvalidOperationException( + $"Cannot include '{shape.Path}': '{typeof(TEntity).Name}' has no such navigation property."); + var element = shape.ElementType; + if (!typeof(IEnumerable<>).MakeGenericType(element).IsAssignableFrom(navigation.PropertyType)) + { + throw new InvalidOperationException( + $"Cannot shape the include '{shape.Path}': a filter, an order or a limit applies to a collection, and it is a reference."); + } + + var parameter = Expression.Parameter(typeof(TEntity), "x"); + Expression body = Expression.Property(parameter, navigation); + if (shape.Filter is not null) + { + body = Expression.Call(typeof(Enumerable), nameof(Enumerable.Where), new[] { element }, body, shape.Filter); + } + + var first = true; + foreach (var sort in shape.Sortings) + { + body = Order(body, false, element, sort.KeySelector, sort.Descending, sort.Nulls, placement, ref first); + } + + if (shape.SkipCount is > 0) + { + body = Expression.Call(typeof(Enumerable), nameof(Enumerable.Skip), new[] { element }, body, + Expression.Constant(shape.SkipCount.Value)); + } + + if (shape.TakeCount is { } take) + { + body = Expression.Call(typeof(Enumerable), nameof(Enumerable.Take), new[] { element }, body, + Expression.Constant(take)); + } + + var include = Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(TEntity), body.Type), body, parameter); + return (IQueryable)IncludeMethod.MakeGenericMethod(typeof(TEntity), body.Type) + .Invoke(null, new object[] { query, include }); + } + + private static readonly MethodInfo IncludeMethod = typeof(EntityFrameworkQueryableExtensions) + .GetMethods(BindingFlags.Public | BindingFlags.Static) + .Single(method => method.Name == nameof(EntityFrameworkQueryableExtensions.Include) + && method.GetGenericArguments().Length == 2); - var call = Expression.Call( - typeof(Queryable), - method, - new[] { typeof(TEntity), sort.KeySelector.ReturnType }, - query.Expression, - Expression.Quote(sort.KeySelector)); + /// + /// Appends one sort to a query () or to an include's collection + /// (), preceded, when nulls have a placement the store does not give by itself, + /// by a key that is 0 for the rows that go first and 1 for the others. + /// + private static Expression Order(Expression source, bool queryable, Type element, LambdaExpression key, + bool descending, NullsOrder nulls, NullsPlacement placement, ref bool first) + { + var methods = queryable ? typeof(Queryable) : typeof(Enumerable); + Expression Argument(LambdaExpression lambda) => queryable ? Expression.Quote(lambda) : lambda; - query = query.Provider.CreateQuery(call); + var nullsKey = NullsKey(key, descending, nulls, placement); + if (nullsKey is not null) + { + source = Expression.Call(methods, first ? nameof(Queryable.OrderBy) : nameof(Queryable.ThenBy), + new[] { element, typeof(int) }, source, Argument(nullsKey)); first = false; } - return query; + var method = first + ? (descending ? nameof(Queryable.OrderByDescending) : nameof(Queryable.OrderBy)) + : (descending ? nameof(Queryable.ThenByDescending) : nameof(Queryable.ThenBy)); + first = false; + return Expression.Call(methods, method, new[] { element, key.ReturnType }, source, Argument(key)); + } + + /// + /// The key that places nulls, or null when none is needed: no placement asked for, a key that cannot + /// hold null, or a placement the store gives by itself. + /// + private static LambdaExpression NullsKey(LambdaExpression key, bool descending, NullsOrder nulls, + NullsPlacement placement) + { + if (nulls == NullsOrder.Default) + { + return null; + } + + var body = key.Body; + while (body is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } convert) + { + body = convert.Operand; + } + + if (body.Type.IsValueType && Nullable.GetUnderlyingType(body.Type) is null) + { + // A value read through an optional navigation (x.Team.Rank) is null when the navigation is: the store + // joins it and reads NULL. Only a value reached without one cannot be null. + if (!ReadThroughNavigation(body)) + { + return null; + } + + body = Expression.Convert(body, typeof(Nullable<>).MakeGenericType(body.Type)); + } + + if (placement != NullsPlacement.AnyExpression && nulls == (descending ? NullsOrder.Last : NullsOrder.First)) + { + return null; + } + + if (placement == NullsPlacement.SmallestByPathOnly) + { + throw new NotSupportedException( + $"Cosmos DB orders by document paths only and places nulls first when ascending and last when " + + $"descending; NullsOrder.{nulls} on a {(descending ? "descending" : "ascending")} sort needs an " + + "expression it cannot order by. Sort the other way, or drop the placement."); + } + + var first = nulls == NullsOrder.First; + var isNull = Expression.Equal(body, Expression.Constant(null, body.Type)); + return Expression.Lambda( + Expression.Condition(isNull, Expression.Constant(first ? 0 : 1), Expression.Constant(first ? 1 : 0)), + key.Parameters); + } + + /// + /// Whether a member chain reads through a reference other than the entity itself (x.Team.Rank), which + /// the store reaches through a join and can find missing. + /// + private static bool ReadThroughNavigation(Expression body) + { + while (body is MemberExpression { Expression: { } owner }) + { + if (owner is not ParameterExpression && !owner.Type.IsValueType) + { + return true; + } + + body = owner; + } + + return false; } /// @@ -100,7 +273,9 @@ public static IQueryable ApplySorts(this IQueryable q /// applying, in order: → /// and /// → the per-call → eager - /// → + /// (shaped ones through a filtered include) → + /// (with their nulls placement) → + /// / /// → → /// → /// → . @@ -112,10 +287,12 @@ public static IQueryable ApplySorts(this IQueryable q /// An optional per-call transformation (e.g. an explicit filter) applied right after the options' /// own filter and before eager loading, sorting and the remaining shaping. /// + /// How the store can place nulls in an order. /// The shaped query. internal static IQueryable ApplyOptions(this IQueryable source, QueryOptions options, - Func, IQueryable> internalQueryAction = null) + Func, IQueryable> internalQueryAction = null, + NullsPlacement placement = NullsPlacement.AnyExpression) where TEntity : class { var query = source; @@ -147,12 +324,30 @@ internal static IQueryable ApplyOptions(this IQueryable 0) { - query = query.IncludeMany(options.IncludePaths.ToArray()); + // A shaped path is loaded by its filtered include; a longer path through it ("Steps.Logs") keeps its + // string include, and EF Core applies the shape to the shared navigation. + query = query.IncludeMany(options.IncludePaths + .Where(path => !options.IncludeShapes.ContainsKey(path)).ToArray()); + } + + foreach (var shape in options.IncludeShapes.Values) + { + query = query.IncludeShaped(shape, placement); } if (options.Sortings.Count > 0) { - query = query.ApplySorts(options.Sortings); + query = query.ApplySorts(options.Sortings, options.NullsOrderOf, placement); + } + + if (options.SkipCount is > 0) + { + query = query.Skip(options.SkipCount.Value); + } + + if (options.TakeCount is { } take) + { + query = query.Take(take); } if (options.AsNoTracking) @@ -178,3 +373,19 @@ internal static IQueryable ApplyOptions(this IQueryableHow a store lets a sort place its nulls. +internal enum NullsPlacement +{ + /// The store orders by any expression (every relational store): a leading "is null" key places them. + AnyExpression, + + /// + /// The store sorts nulls as the smallest value (first ascending, last descending) and orders by expressions + /// too (MongoDB): the other placements take the leading key. + /// + Smallest, + + /// The store sorts nulls as the smallest value and orders by document paths only (Cosmos DB). + SmallestByPathOnly, +} diff --git a/src/eQuantic.Core.Data.EntityFramework/Repository/Read/AsyncQueryableReadRepository.cs b/src/eQuantic.Core.Data.EntityFramework/Repository/Read/AsyncQueryableReadRepository.cs index ac965d3..ea35f38 100644 --- a/src/eQuantic.Core.Data.EntityFramework/Repository/Read/AsyncQueryableReadRepository.cs +++ b/src/eQuantic.Core.Data.EntityFramework/Repository/Read/AsyncQueryableReadRepository.cs @@ -117,7 +117,7 @@ public async Task> GetPagedAsync(PageRequest page, QueryOpt throw new ArgumentNullException(nameof(page)); } - var query = GetSet().GetQueryable(options); + var query = GetSet().GetQueryableOverEveryRow(options, "GetPagedAsync"); var totalCount = await query.LongCountAsync(cancellationToken).ConfigureAwait(false); var items = await query .OrderByPrimaryKeyIfUnordered(GetSet().DbContext) @@ -142,7 +142,7 @@ public async Task> GetPagedAsync(PageRequest page, throw new ArgumentNullException(nameof(map), MapCannotBeNull); } - var query = GetSet().GetQueryable(options); + var query = GetSet().GetQueryableOverEveryRow(options, "GetPagedAsync"); var totalCount = await query.LongCountAsync(cancellationToken).ConfigureAwait(false); var items = await query .OrderByPrimaryKeyIfUnordered(GetSet().DbContext) @@ -156,12 +156,12 @@ public async Task> GetPagedAsync(PageRequest page, public Task CountAsync(QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).LongCountAsync(cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "CountAsync").LongCountAsync(cancellationToken); } public Task AnyAsync(QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).AnyAsync(cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "AnyAsync").AnyAsync(cancellationToken); } public Task AllAsync(Expression> predicate, QueryOptions options = null, @@ -172,66 +172,66 @@ public Task AllAsync(Expression> predicate, QueryOptio throw new ArgumentNullException(nameof(predicate), FilterExpressionCannotBeNull); } - return GetSet().GetQueryable(options).AllAsync(predicate, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "AllAsync").AllAsync(predicate, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } public Task SumAsync(Expression> selector, QueryOptions options = null, CancellationToken cancellationToken = default) { - return GetSet().GetQueryable(options).SumAsync(selector, cancellationToken); + return GetSet().GetQueryableOverEveryRow(options, "SumAsync").SumAsync(selector, cancellationToken); } } diff --git a/src/eQuantic.Core.Data.EntityFramework/Repository/Read/QueryableReadRepository.cs b/src/eQuantic.Core.Data.EntityFramework/Repository/Read/QueryableReadRepository.cs index 884586b..6a47aff 100644 --- a/src/eQuantic.Core.Data.EntityFramework/Repository/Read/QueryableReadRepository.cs +++ b/src/eQuantic.Core.Data.EntityFramework/Repository/Read/QueryableReadRepository.cs @@ -114,7 +114,7 @@ public PagedResult GetPaged(PageRequest page, QueryOptions opt throw new ArgumentNullException(nameof(page)); } - var query = GetSet().GetQueryable(options); + var query = GetSet().GetQueryableOverEveryRow(options, "GetPaged"); var totalCount = query.LongCount(); var items = query .OrderByPrimaryKeyIfUnordered(GetSet().DbContext) @@ -138,7 +138,7 @@ public PagedResult GetPaged(PageRequest page, Expression GetPaged(PageRequest page, Expression options = null) { - return GetSet().GetQueryable(options).LongCount(); + return GetSet().GetQueryableOverEveryRow(options, "Count").LongCount(); } public bool Any(QueryOptions options = null) { - return GetSet().GetQueryable(options).Any(); + return GetSet().GetQueryableOverEveryRow(options, "Any").Any(); } public bool All(Expression> predicate, QueryOptions options = null) @@ -167,57 +167,57 @@ public bool All(Expression> predicate, QueryOptions throw new ArgumentNullException(nameof(predicate), FilterExpressionCannotBeNull); } - return GetSet().GetQueryable(options).All(predicate); + return GetSet().GetQueryableOverEveryRow(options, "All").All(predicate); } public int Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public int? Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public long Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public long? Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public double Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public double? Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public float Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public float? Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public decimal Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public decimal? Sum(Expression> selector, QueryOptions options = null) { - return GetSet().GetQueryable(options).Sum(selector); + return GetSet().GetQueryableOverEveryRow(options, "Sum").Sum(selector); } public void Dispose() diff --git a/src/eQuantic.Core.Data.EntityFramework/Repository/SetBase.cs b/src/eQuantic.Core.Data.EntityFramework/Repository/SetBase.cs index 17696ca..b34f4f1 100644 --- a/src/eQuantic.Core.Data.EntityFramework/Repository/SetBase.cs +++ b/src/eQuantic.Core.Data.EntityFramework/Repository/SetBase.cs @@ -209,6 +209,28 @@ internal Expression> GetExpression(TKey id) internal IQueryable GetQueryable(QueryOptions options, Func, IQueryable> internalQueryAction = null) { - return InternalDbSet.ApplyOptions(options, internalQueryAction); + return InternalDbSet.ApplyOptions(options, internalQueryAction, NullsPlacement); } + + /// + /// Shapes a query for a read that works over every matching row (a count, a sum, All) or that pages + /// by itself, refusing Skip/Take first. + /// + /// The query options, or null for no shaping. + /// The read, as a refusal names it. + /// The shaped query. + internal IQueryable GetQueryableOverEveryRow(QueryOptions options, string read) + { + QueryableExtensions.RefuseRowWindow(options, read); + return GetQueryable(options); + } + + // How this context's store lets a sort place nulls, told by its provider (the document providers live in + // their own packages, which this one does not reference). + private NullsPlacement NullsPlacement => DbContext.Database.ProviderName switch + { + "Microsoft.EntityFrameworkCore.Cosmos" => NullsPlacement.SmallestByPathOnly, + "MongoDB.EntityFrameworkCore" => NullsPlacement.Smallest, + _ => NullsPlacement.AnyExpression, + }; } diff --git a/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net10.csproj b/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net10.csproj index 1f521f2..878afce 100644 --- a/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net10.csproj +++ b/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net10.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework eQuantic.Core.Data.EntityFramework - 10.5.0.0 + 10.6.0.0 net10.0 eQuantic.Core.Data.EntityFramework eQuantic.Core.Data.EntityFramework @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 10.5.0.0 - 10.5.0.0 + 10.6.0.0 + 10.6.0.0 - + all @@ -56,6 +56,9 @@ <_Parameter1>$(AssemblyName).CosmosDb + + <_Parameter1>$(AssemblyName).CosmosDb.Tests + <_Parameter1>$(AssemblyName).Relational diff --git a/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net8.csproj b/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net8.csproj index eb38928..853a4a9 100644 --- a/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net8.csproj +++ b/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.Net8.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework eQuantic.Core.Data.EntityFramework - 8.6.0.0 + 8.7.0.0 net8.0 eQuantic.Core.Data.EntityFramework eQuantic.Core.Data.EntityFramework @@ -12,13 +12,13 @@ Entity ignorant persistance with Repository Pattern for Entity Framework - 8.6.0.0 - 8.6.0.0 + 8.7.0.0 + 8.7.0.0 - + all @@ -56,6 +56,9 @@ <_Parameter1>$(AssemblyName).CosmosDb + + <_Parameter1>$(AssemblyName).CosmosDb.Tests + <_Parameter1>$(AssemblyName).Relational diff --git a/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.csproj b/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.csproj index 665701e..1ed7df7 100644 --- a/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.csproj +++ b/src/eQuantic.Core.Data.EntityFramework/eQuantic.Core.Data.EntityFramework.csproj @@ -4,20 +4,20 @@ Core Data library for Entity Framework eQuantic.Core.Data.EntityFramework - 4.9.0.0 + 4.10.0.0 net8.0;net10.0 eQuantic.Core.Data.EntityFramework eQuantic.Core.Data.EntityFramework eQuantic;Core;Data;Library;Repository;Pattern Entity ignorant persistance with Repository Pattern for Entity Framework - 4.9.0.0 - 4.9.0.0 + 4.10.0.0 + 4.10.0.0 - + @@ -73,5 +73,8 @@ <_Parameter1>$(AssemblyName).CosmosDb + + <_Parameter1>$(AssemblyName).CosmosDb.Tests + diff --git a/tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests/CosmosNullsOrderTests.cs b/tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests/CosmosNullsOrderTests.cs new file mode 100644 index 0000000..8a706a4 --- /dev/null +++ b/tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests/CosmosNullsOrderTests.cs @@ -0,0 +1,68 @@ +using eQuantic.Core.Data.EntityFramework.CosmosDb.Repository; +using eQuantic.Core.Data.EntityFramework.Repository.Read; +using eQuantic.Core.Data.Repository.Options; +using Microsoft.EntityFrameworkCore; + +namespace eQuantic.Core.Data.EntityFramework.CosmosDb.Tests; + +/// +/// Cosmos DB orders by document paths only and sorts nulls as the smallest value, so a sort whose nulls +/// placement is the one it gives by itself is sent as a plain ORDER BY, and the other placement is +/// refused before any request, rather than sent as an expression the service rejects (or, worse, ignored). +/// The context points at an endpoint nothing listens on: neither case may reach it. +/// +public class CosmosNullsOrderTests +{ + private static TestContext NewContext() => + new(new DbContextOptionsBuilder() + .UseCosmos("https://localhost:1/", Convert.ToBase64String(new byte[64]), "nulls-order") + .Options); + + [TestCase(false, NullsOrder.First)] + [TestCase(true, NullsOrder.Last)] + public void The_placement_cosmos_gives_by_itself_is_a_plain_order_by(bool descending, NullsOrder nulls) + { + using var context = NewContext(); + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Status, nulls) + : new QueryOptions().OrderBy(x => x.Status, nulls); + + var sql = new Set(context).GetQueryable(options).ToQueryString(); + + Assert.That(sql, Does.Contain($"ORDER BY c[\"Status\"]{(descending ? " DESC" : "")}")); + Assert.That(sql, Does.Not.Contain("?"), "no conditional key: Cosmos DB orders by paths only"); + } + + [TestCase(false, NullsOrder.Last)] + [TestCase(true, NullsOrder.First)] + public void The_other_placement_is_refused_before_any_request(bool descending, NullsOrder nulls) + { + using var context = NewContext(); + var repository = new AsyncQueryableReadRepository( + new DefaultUnitOfWork(new NoServices(), context)); + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Status, nulls) + : new QueryOptions().OrderBy(x => x.Status, nulls); + + var refusal = Assert.ThrowsAsync(() => repository.GetAllAsync(options)); + + Assert.That(refusal!.Message, Does.Contain("Cosmos DB orders by document paths only")); + } + + [Test] + public void A_key_that_cannot_hold_null_needs_no_placement() + { + using var context = NewContext(); + + var sql = new Set(context) + .GetQueryable(new QueryOptions().OrderBy(x => x.Count, NullsOrder.Last)) + .ToQueryString(); + + Assert.That(sql, Does.Contain("ORDER BY c[\"Count\"]")); + } + + private sealed class NoServices : IServiceProvider + { + public object? GetService(Type serviceType) => null; + } +} diff --git a/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/MongoReadShapesTests.cs b/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/MongoReadShapesTests.cs new file mode 100644 index 0000000..c3c1255 --- /dev/null +++ b/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/MongoReadShapesTests.cs @@ -0,0 +1,116 @@ +using eQuantic.Core.Data.EntityFramework.MongoDb.Repository; +using eQuantic.Core.Data.EntityFramework.Repository.Read; +using eQuantic.Core.Data.Repository; +using eQuantic.Core.Data.Repository.Options; +using Microsoft.EntityFrameworkCore; +using MongoDB.EntityFrameworkCore.Extensions; +using Testcontainers.MongoDb; + +namespace eQuantic.Core.Data.EntityFramework.MongoDb.Tests; + +/// +/// The read shapes of eQuantic.Core.Data 7.0.0 against a real MongoDB, through the MongoDB Entity Framework +/// provider. MongoDB sorts nulls as the smallest value (first ascending, last descending): those placements are a +/// plain sort, and the two others need the leading "is null" key, which only a real server proves translates. +/// +[TestFixture] +public sealed class MongoReadShapesTests +{ + private MongoDbContainer _mongo = null!; + private bool _available; + + public sealed class Run : IEntity + { + public int Id { get; set; } + + public int? Position { get; set; } + + public int GetKey() => Id; + + public void SetKey(int key) => Id = key; + } + + private sealed class RunsContext(DbContextOptions options) : DbContext(options) + { + public DbSet Runs => Set(); + + protected override void OnModelCreating(ModelBuilder builder) => + builder.Entity().ToCollection("runs"); + } + + private sealed class NoServices : IServiceProvider + { + public object? GetService(Type serviceType) => null; + } + + [OneTimeSetUp] + public async Task StartAsync() + { + // A replica set: the MongoDB provider writes SaveChanges in a transaction, which a standalone server refuses. + _mongo = new MongoDbBuilder().WithImage("mongo:7").WithReplicaSet().Build(); + try + { + await _mongo.StartAsync(); + _available = true; + } + catch (Exception) + { + _available = false; + } + } + + [OneTimeTearDown] + public async Task StopAsync() + { + if (_available) + { + await _mongo.DisposeAsync(); + } + } + + private async Task<(RunsContext Context, AsyncQueryableReadRepository Repository)> NewAsync(params Run[] seed) + { + if (!_available) + { + Assert.Ignore("MongoDB test container is unavailable (Docker required)."); + } + + var context = new RunsContext(new DbContextOptionsBuilder() + .UseMongoDB(_mongo.GetConnectionString(), $"shapes_{Guid.NewGuid():N}").Options); + context.Runs.AddRange(seed); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + return (context, new AsyncQueryableReadRepository(new DefaultUnitOfWork(new NoServices(), context))); + } + + [TestCase(false, NullsOrder.First, new[] { 0, 1, 2 })] + [TestCase(false, NullsOrder.Last, new[] { 1, 2, 0 })] + [TestCase(true, NullsOrder.First, new[] { 0, 2, 1 })] + [TestCase(true, NullsOrder.Last, new[] { 2, 1, 0 })] + public async Task Nulls_go_where_the_sort_places_them(bool descending, NullsOrder nulls, int[] positions) + { + var (context, repository) = await NewAsync( + new Run { Id = 1, Position = 2 }, + new Run { Id = 2, Position = null }, + new Run { Id = 3, Position = 1 }); + await using var _ = context; + + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Position, nulls) + : new QueryOptions().OrderBy(x => x.Position, nulls); + var runs = await repository.GetAllAsync(options); + + Assert.That(runs.Select(x => x.Position ?? 0), Is.EqualTo(positions)); + } + + [Test] + public async Task Skip_and_take_return_rows_eleven_to_twenty() + { + var (context, repository) = await NewAsync(Enumerable.Range(1, 25).Select(id => new Run { Id = id }).ToArray()); + await using var _ = context; + + var page = await repository.GetAllAsync(new QueryOptions().OrderBy(x => x.Id).Skip(10).Take(10)); + + Assert.That(page.Select(x => x.Id), Is.EqualTo(Enumerable.Range(11, 10))); + } +} diff --git a/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests.csproj b/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests.csproj index a4edb3e..92708a0 100644 --- a/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests.csproj +++ b/tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests.csproj @@ -14,6 +14,7 @@ + diff --git a/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/ReadShapesTests.cs b/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/ReadShapesTests.cs new file mode 100644 index 0000000..547767c --- /dev/null +++ b/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/ReadShapesTests.cs @@ -0,0 +1,222 @@ +using eQuantic.Core.Data.EntityFramework.PostgreSql.Repository; +using eQuantic.Core.Data.EntityFramework.Repository.Read; +using eQuantic.Core.Data.Repository; +using eQuantic.Core.Data.Repository.Options; +using Microsoft.EntityFrameworkCore; +using Testcontainers.PostgreSql; + +namespace eQuantic.Core.Data.EntityFramework.PostgreSql.Tests; + +/// +/// The read shapes of eQuantic.Core.Data 7.0.0 against a real PostgreSQL: what proves that the nulls key, the +/// filtered include and the row window translate to SQL, rather than pass on a provider that evaluates in +/// memory. PostgreSQL puts nulls LAST when ascending, unlike SQL Server, MySQL and the document stores, so +/// ascending is a placement it would not give by itself. +/// +[TestFixture] +public sealed class ReadShapesTests +{ + private PostgreSqlContainer _postgres = null!; + private bool _available; + + public sealed class Run : IEntity + { + public int Id { get; set; } + + public int? Position { get; set; } + + public List Steps { get; set; } = []; + + public int? TeamId { get; set; } + + public Team? Team { get; set; } + + public int GetKey() => Id; + + public void SetKey(int key) => Id = key; + } + + public sealed class Team + { + public int Id { get; set; } + + public int Rank { get; set; } + } + + public sealed class Step + { + public int Id { get; set; } + + public int RunId { get; set; } + + public string Status { get; set; } = ""; + + public DateTime? StartedAt { get; set; } + } + + private sealed class RunsContext(DbContextOptions options) : DbContext(options) + { + public DbSet Runs => Set(); + + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity().Property(x => x.Id).ValueGeneratedNever(); + builder.Entity().Property(x => x.Id).ValueGeneratedNever(); + builder.Entity().HasMany(x => x.Steps).WithOne().HasForeignKey(x => x.RunId); + builder.Entity().HasOne(x => x.Team).WithMany().HasForeignKey(x => x.TeamId); + builder.Entity().Property(x => x.Id).ValueGeneratedNever(); + } + } + + private sealed class NoServices : IServiceProvider + { + public object? GetService(Type serviceType) => null; + } + + [OneTimeSetUp] + public async Task StartAsync() + { + _postgres = new PostgreSqlBuilder().WithImage("postgres:16-alpine").Build(); + try + { + await _postgres.StartAsync(); + _available = true; + } + catch (Exception) + { + _available = false; + } + } + + [OneTimeTearDown] + public async Task StopAsync() + { + if (_available) + { + await _postgres.DisposeAsync(); + } + } + + private async Task<(RunsContext Context, AsyncQueryableReadRepository Repository)> NewAsync() + { + if (!_available) + { + Assert.Ignore("PostgreSQL test container is unavailable (Docker required)."); + } + + var builder = new Npgsql.NpgsqlConnectionStringBuilder(_postgres.GetConnectionString()) + { + Database = $"shapes_{Guid.NewGuid():N}", + }; + var context = new RunsContext(new DbContextOptionsBuilder().UseNpgsql(builder.ConnectionString).Options); + await context.Database.EnsureCreatedAsync(); + return (context, new AsyncQueryableReadRepository(new DefaultUnitOfWork(new NoServices(), context))); + } + + [TestCase(false, NullsOrder.First, new[] { 0, 1, 2 })] + [TestCase(false, NullsOrder.Last, new[] { 1, 2, 0 })] + [TestCase(true, NullsOrder.First, new[] { 0, 2, 1 })] + [TestCase(true, NullsOrder.Last, new[] { 2, 1, 0 })] + public async Task Nulls_go_where_the_sort_places_them(bool descending, NullsOrder nulls, int[] positions) + { + var (context, repository) = await NewAsync(); + await using var _ = context; + context.Runs.AddRange( + new Run { Id = 1, Position = 2 }, + new Run { Id = 2, Position = null }, + new Run { Id = 3, Position = 1 }); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Position, nulls) + : new QueryOptions().OrderBy(x => x.Position, nulls); + var runs = await repository.GetAllAsync(options); + + Assert.That(runs.Select(x => x.Position ?? 0), Is.EqualTo(positions)); + } + + [Test] + public async Task Skip_and_take_return_rows_eleven_to_twenty() + { + var (context, repository) = await NewAsync(); + await using var _ = context; + context.Runs.AddRange(Enumerable.Range(1, 25).Select(id => new Run { Id = id })); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + var page = await repository.GetAllAsync(new QueryOptions().OrderBy(x => x.Id).Skip(10).Take(10)); + + Assert.That(page.Select(x => x.Id), Is.EqualTo(Enumerable.Range(11, 10))); + } + + [Test] + public async Task A_shaped_include_loads_the_last_three_newest_first_for_each_run() + { + var (context, repository) = await NewAsync(); + await using var _ = context; + var start = new DateTime(2026, 11, 14, 12, 0, 0, DateTimeKind.Utc); + context.Runs.AddRange( + new Run + { + Id = 1, + Steps = + [ + new Step { Id = 1, Status = "success", StartedAt = start }, + new Step { Id = 2, Status = "skipped", StartedAt = start.AddMinutes(5) }, + new Step { Id = 3, Status = "success", StartedAt = start.AddMinutes(1) }, + new Step { Id = 4, Status = "failure", StartedAt = start.AddMinutes(3) }, + new Step { Id = 5, Status = "success", StartedAt = start.AddMinutes(2) }, + ], + }, + new Run + { + Id = 2, + Steps = + [ + new Step { Id = 6, Status = "queued", StartedAt = null }, + new Step { Id = 7, Status = "success", StartedAt = start }, + ], + }); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + var runs = await repository.GetAllAsync(new QueryOptions() + .OrderBy(x => x.Id) + .Include(x => x.Steps, steps => steps + .Where(s => s.Status != "skipped") + .OrderByDescending(s => s.StartedAt, NullsOrder.Last) + .Take(3))); + + Assert.That(runs.Select(r => r.Steps.Select(s => s.Id).ToArray()), + Is.EqualTo(new[] { new[] { 4, 5, 3 }, new[] { 7, 6 } }), + "PostgreSQL puts nulls first when descending, so the queued step comes last only through the placement"); + } + + [TestCase(false, NullsOrder.First, new[] { 2, 3, 1 })] + [TestCase(false, NullsOrder.Last, new[] { 3, 1, 2 })] + [TestCase(true, NullsOrder.First, new[] { 2, 1, 3 })] + [TestCase(true, NullsOrder.Last, new[] { 1, 3, 2 })] + public async Task A_value_read_through_an_optional_navigation_places_its_nulls(bool descending, NullsOrder nulls, + int[] ids) + { + var (context, repository) = await NewAsync(); + await using var _ = context; + var first = new Team { Id = 1, Rank = 1 }; + var second = new Team { Id = 2, Rank = 2 }; + context.Runs.AddRange( + new Run { Id = 1, Team = second }, + new Run { Id = 2 }, + new Run { Id = 3, Team = first }); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + // Rank is an int, but the run without a team reads it as NULL through the join. + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Team!.Rank, nulls) + : new QueryOptions().OrderBy(x => x.Team!.Rank, nulls); + var runs = await repository.GetAllAsync(options); + + Assert.That(runs.Select(x => x.Id), Is.EqualTo(ids)); + } +} diff --git a/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests.csproj b/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests.csproj index 5e22fab..8cc9c7a 100644 --- a/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests.csproj +++ b/tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests.csproj @@ -20,6 +20,7 @@ + diff --git a/tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests/ReadShapesTests.cs b/tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests/ReadShapesTests.cs new file mode 100644 index 0000000..c2b8f47 --- /dev/null +++ b/tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests/ReadShapesTests.cs @@ -0,0 +1,247 @@ +using eQuantic.Core.Data.EntityFramework.Repository.Read; +using eQuantic.Core.Data.EntityFramework.SqlServer.Repository; +using eQuantic.Core.Data.Repository; +using eQuantic.Core.Data.Repository.Options; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace eQuantic.Core.Data.EntityFramework.SqlServer.Tests; + +/// +/// The read shapes eQuantic.Core.Data 7.0.0 added to , honoured by the +/// Entity Framework read repositories as the core-data engines honour them: Skip/Take without a +/// total (and refused where a read does not return rows that way), nulls placed first or last, and shaped +/// collection includes. The same scenarios run against a real PostgreSQL in the PostgreSql tests, which is what +/// proves the translation; these prove the rules on the in-memory provider. +/// +public class ReadShapesTests +{ + public sealed class Run : IEntity + { + public int Id { get; set; } + + public int? Position { get; set; } + + public string? Label { get; set; } + + public List Steps { get; set; } = []; + + public int? TeamId { get; set; } + + public Team? Team { get; set; } + + public int GetKey() => Id; + + public void SetKey(int key) => Id = key; + } + + public sealed class Team + { + public int Id { get; set; } + + public int Rank { get; set; } + } + + public sealed class Step + { + public int Id { get; set; } + + public int RunId { get; set; } + + public string Status { get; set; } = ""; + + public DateTime? StartedAt { get; set; } + } + + private sealed class RunsContext(DbContextOptions options) : DbContext(options) + { + public DbSet Runs => Set(); + + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity().HasMany(x => x.Steps).WithOne().HasForeignKey(x => x.RunId); + builder.Entity().HasOne(x => x.Team).WithMany().HasForeignKey(x => x.TeamId); + } + } + + private static AsyncQueryableReadRepository NewRepository(out RunsContext context) + { + context = new RunsContext(new DbContextOptionsBuilder() + .UseInMemoryDatabase($"read-shapes-{Guid.NewGuid():N}").Options); + return new AsyncQueryableReadRepository( + new DefaultUnitOfWork(new ServiceCollection().BuildServiceProvider(), context)); + } + + [Test] + public async Task Skip_and_take_return_the_page_without_a_total() + { + var repository = NewRepository(out var context); + context.Runs.AddRange(Enumerable.Range(1, 25).Select(id => new Run { Id = id })); + await context.SaveChangesAsync(); + + var page = await repository.GetAllAsync(new QueryOptions().OrderBy(x => x.Id).Skip(10).Take(10)); + + Assert.That(page.Select(x => x.Id), Is.EqualTo(Enumerable.Range(11, 10))); + } + + [Test] + public async Task Take_composes_with_a_first_and_a_single() + { + var repository = NewRepository(out var context); + context.Runs.AddRange(new Run { Id = 1 }, new Run { Id = 2 }, new Run { Id = 3 }); + await context.SaveChangesAsync(); + + var second = await repository.GetFirstAsync(new QueryOptions().OrderBy(x => x.Id).Skip(1)); + var only = await repository.GetSingleAsync(new QueryOptions().OrderBy(x => x.Id).Take(1)); + + Assert.That(second!.Id, Is.EqualTo(2)); + Assert.That(only!.Id, Is.EqualTo(1), "the window holds one row, so a single finds exactly one"); + } + + [Test] + public void A_read_over_every_row_refuses_skip_and_take() + { + var repository = NewRepository(out _); + var page = new QueryOptions().Take(10); + + Assert.Multiple(() => + { + Assert.ThrowsAsync(() => repository.CountAsync(page)); + Assert.ThrowsAsync(() => repository.AnyAsync(page)); + Assert.ThrowsAsync(() => repository.AllAsync(x => x.Id > 0, page)); + Assert.ThrowsAsync(() => repository.SumAsync(x => x.Id, page)); + Assert.ThrowsAsync(() => repository.GetPagedAsync(new PageRequest(1, 10), page)); + Assert.Throws(() => repository.Count(new QueryOptions().Skip(1))); + Assert.Throws(() => repository.Sum(x => x.Id, new QueryOptions().Skip(1))); + Assert.Throws(() => repository.GetPaged(new PageRequest(1, 10), page)); + }); + } + + [TestCase(false, NullsOrder.First, new[] { 0, 1, 2 })] + [TestCase(false, NullsOrder.Last, new[] { 1, 2, 0 })] + [TestCase(true, NullsOrder.First, new[] { 0, 2, 1 })] + [TestCase(true, NullsOrder.Last, new[] { 2, 1, 0 })] + public async Task Nulls_go_where_the_sort_places_them(bool descending, NullsOrder nulls, int[] positions) + { + var repository = NewRepository(out var context); + context.Runs.AddRange( + new Run { Id = 1, Position = 2 }, + new Run { Id = 2, Position = null }, + new Run { Id = 3, Position = 1 }); + await context.SaveChangesAsync(); + + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Position, nulls) + : new QueryOptions().OrderBy(x => x.Position, nulls); + var runs = await repository.GetAllAsync(options); + + Assert.That(runs.Select(x => x.Position ?? 0), Is.EqualTo(positions)); + } + + [Test] + public async Task A_secondary_sort_places_its_nulls_within_the_primary() + { + var repository = NewRepository(out var context); + context.Runs.AddRange( + new Run { Id = 1, Position = 1, Label = "b" }, + new Run { Id = 2, Position = 1, Label = null }, + new Run { Id = 3, Position = 0, Label = "a" }); + await context.SaveChangesAsync(); + + var runs = await repository.GetAllAsync(new QueryOptions() + .OrderBy(x => x.Position).ThenBy(x => x.Label, NullsOrder.Last)); + + Assert.That(runs.Select(x => x.Id), Is.EqualTo(new[] { 3, 1, 2 })); + } + + [Test] + public async Task A_shaped_include_loads_the_last_three_newest_first() + { + var repository = NewRepository(out var context); + var start = new DateTime(2026, 11, 14, 12, 0, 0, DateTimeKind.Utc); + context.Runs.Add(new Run + { + Id = 1, + Steps = + [ + new Step { Id = 1, Status = "success", StartedAt = start }, + new Step { Id = 2, Status = "skipped", StartedAt = start.AddMinutes(5) }, + new Step { Id = 3, Status = "success", StartedAt = start.AddMinutes(1) }, + new Step { Id = 4, Status = "failure", StartedAt = start.AddMinutes(3) }, + new Step { Id = 5, Status = "success", StartedAt = start.AddMinutes(2) }, + ], + }); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + var run = await repository.GetAsync(1, new QueryOptions() + .Include(x => x.Steps, steps => steps + .Where(s => s.Status != "skipped") + .OrderByDescending(s => s.StartedAt) + .Take(3))); + + Assert.That(run!.Steps.Select(s => s.Id), Is.EqualTo(new[] { 4, 5, 3 })); + } + + [Test] + public async Task A_shaped_include_with_a_skip_and_nulls_last_pages_each_parent() + { + var repository = NewRepository(out var context); + var start = new DateTime(2026, 11, 14, 12, 0, 0, DateTimeKind.Utc); + context.Runs.AddRange( + new Run + { + Id = 1, + Steps = + [ + new Step { Id = 1, Status = "queued", StartedAt = null }, + new Step { Id = 2, Status = "success", StartedAt = start }, + new Step { Id = 3, Status = "success", StartedAt = start.AddMinutes(1) }, + ], + }, + new Run + { + Id = 2, + Steps = + [ + new Step { Id = 4, Status = "success", StartedAt = start.AddMinutes(9) }, + new Step { Id = 5, Status = "queued", StartedAt = null }, + ], + }); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + var runs = await repository.GetAllAsync(new QueryOptions() + .OrderBy(x => x.Id) + .Include(x => x.Steps, steps => steps.OrderBy(s => s.StartedAt, NullsOrder.Last).Skip(1))); + + Assert.That(runs.Select(r => r.Steps.Select(s => s.Id).ToArray()), + Is.EqualTo(new[] { new[] { 3, 1 }, new[] { 5 } })); + } + + [TestCase(false, NullsOrder.First, new[] { 2, 3, 1 })] + [TestCase(false, NullsOrder.Last, new[] { 3, 1, 2 })] + [TestCase(true, NullsOrder.First, new[] { 2, 1, 3 })] + [TestCase(true, NullsOrder.Last, new[] { 1, 3, 2 })] + public async Task A_value_read_through_an_optional_navigation_places_its_nulls(bool descending, NullsOrder nulls, + int[] ids) + { + var repository = NewRepository(out var context); + var first = new Team { Id = 1, Rank = 1 }; + var second = new Team { Id = 2, Rank = 2 }; + context.Runs.AddRange( + new Run { Id = 1, Team = second }, + new Run { Id = 2 }, + new Run { Id = 3, Team = first }); + await context.SaveChangesAsync(); + context.ChangeTracker.Clear(); + + // Rank is an int, but the run without a team reads it as NULL through the join. + var options = descending + ? new QueryOptions().OrderByDescending(x => x.Team!.Rank, nulls) + : new QueryOptions().OrderBy(x => x.Team!.Rank, nulls); + var runs = await repository.GetAllAsync(options); + + Assert.That(runs.Select(x => x.Id), Is.EqualTo(ids)); + } +}