Skip to content

Commit

Permalink
Clear query order when adding new order statements (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmarcussen authored May 13, 2021
1 parent a7236e3 commit a47ceb4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/YesSql.Core/Sql/SqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,20 @@ public void ClearOrder()

public virtual void OrderBy(string orderBy)
{
OrderSegments.Clear();
OrderSegments.Add(orderBy);
}

public virtual void OrderByDescending(string orderBy)
{
OrderSegments.Clear();
OrderSegments.Add(orderBy);
OrderSegments.Add(" DESC");
}

public virtual void OrderByRandom()
{
OrderSegments.Clear();
OrderSegments.Add(_dialect.RandomOrderByClause);
}

Expand Down
34 changes: 34 additions & 0 deletions test/YesSql.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,40 @@ public async Task ShouldOrderJoinedMapIndexes()
}
}

[Fact]
public async Task ShouldClearOrders()
{
_store.RegisterIndexes<PersonIndexProvider>();

using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates",
};

var steve = new Person
{
Firstname = "Steve",
Lastname = "Balmer"
};

session.Save(bill);
session.Save(steve);

await session.SaveChangesAsync();
}

using (var session = _store.CreateSession())
{
var query = session.Query<Person, PersonByName>().OrderByDescending(x => x.SomeName);
query.OrderByDescending(x => x.SomeName);

Assert.Equal("Steve", (await query.FirstOrDefaultAsync()).Firstname);
}
}

[Fact]
public async Task ShouldJoinReduceIndex()
{
Expand Down

0 comments on commit a47ceb4

Please sign in to comment.