-
-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the combination of using an Include() + Select() where the sele…
…ct uses the Id of the original document. Closes GH-3096
- Loading branch information
1 parent
ba0515b
commit 5259eea
Showing
8 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Marten; | ||
using Marten.Testing.Documents; | ||
using Marten.Testing.Harness; | ||
using Shouldly; | ||
using Xunit.Abstractions; | ||
|
||
namespace LinqTests.Bugs; | ||
|
||
public class Bug_3096_include_where_select : IntegrationContext | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public Bug_3096_include_where_select(DefaultStoreFixture fixture, ITestOutputHelper output) : base(fixture) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void include_to_dictionary_with_where_and_projection() | ||
{ | ||
var user1 = new User(); | ||
var user2 = new User(); | ||
|
||
var issue1 = new Issue { AssigneeId = user1.Id, Title = "Garage Door is ok" }; | ||
var issue2 = new Issue { AssigneeId = user2.Id, Title = "Garage Door is busted" }; | ||
var issue3 = new Issue { AssigneeId = user2.Id, Title = "Garage Door is busted" }; | ||
|
||
using var session = theStore.IdentitySession(); | ||
session.Store(user1, user2); | ||
session.Store(issue1, issue2, issue3); | ||
session.SaveChanges(); | ||
|
||
using var query = theStore.QuerySession(); | ||
query.Logger = new TestOutputMartenLogger(_output); | ||
|
||
var dict = new Dictionary<Guid, User>(); | ||
|
||
var issues = query | ||
.Query<Issue>() | ||
.Where(i => i.Title.Contains("ok")) | ||
.Include(x => x.AssigneeId, dict) | ||
.Select(i => new { i.Id, i.Title, }) | ||
.ToArray(); | ||
|
||
issues.Length.ShouldBe(1); | ||
|
||
dict.Count.ShouldBe(1); | ||
dict.ContainsKey(user1.Id).ShouldBeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Linq; | ||
using JasperFx.Core; | ||
using Marten.Linq; | ||
using Marten.Linq.QueryHandlers; | ||
using Marten.Linq.Selectors; | ||
using Marten.Linq.SqlGeneration; | ||
using Weasel.Postgresql; | ||
using Weasel.Postgresql.SqlGeneration; | ||
|
||
namespace Marten.Internal.Storage; | ||
|
||
internal class DataAndIdSelectClause<T>: ISelectClause, IModifyableFromObject | ||
{ | ||
private readonly IDocumentStorage<T> _inner; | ||
|
||
public DataAndIdSelectClause(IDocumentStorage<T> inner) | ||
{ | ||
_inner = inner; | ||
FromObject = inner.FromObject; | ||
} | ||
|
||
public void Apply(ICommandBuilder builder) | ||
{ | ||
builder.Append($"select {_inner.SelectFields().Concat(["d.id"]).Join(", ")} from "); | ||
builder.Append(FromObject); | ||
builder.Append(" as d"); | ||
} | ||
|
||
public string FromObject { get; set; } | ||
public Type SelectedType => typeof(T); | ||
public string[] SelectFields() => ["d.data", "d.id"]; | ||
|
||
public ISelector BuildSelector(IMartenSession session) | ||
{ | ||
return _inner.BuildSelector(session); | ||
} | ||
|
||
public IQueryHandler<T1> BuildHandler<T1>(IMartenSession session, ISqlFragment topStatement, ISqlFragment currentStatement) | ||
{ | ||
return _inner.BuildHandler<T1>(session, topStatement, currentStatement); | ||
} | ||
|
||
public ISelectClause UseStatistics(QueryStatistics statistics) | ||
{ | ||
return _inner.UseStatistics(statistics); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters