Skip to content

Commit

Permalink
Fix SQL generation when paging with specified columns (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
APErebus authored Jun 8, 2022
1 parent 7fb4d7e commit 7c2c335
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT *
FROM (
SELECT [Foo],
[Bar],
[Elephant],
ROW_NUMBER() OVER (ORDER BY [Foo]) AS RowNum
FROM [dbo].[Orders]
WHERE ([Price] > 5)
) ALIAS_GENERATED_1
WHERE ([RowNum] >= @_minrow)
AND ([RowNum] <= @_maxrow)
ORDER BY [RowNum]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT *
FROM (
SELECT [Foo],
ROW_NUMBER() OVER (ORDER BY [Foo]) AS RowNum
FROM [dbo].[Orders]
WHERE ([Price] > 5)
) ALIAS_GENERATED_1
WHERE ([RowNum] >= @_minrow)
AND ([RowNum] <= @_maxrow)
ORDER BY [RowNum]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT *
FROM (
SELECT [Foo],
ROW_NUMBER() OVER (ORDER BY [Id]) AS RowNum
FROM [dbo].[Orders]
WHERE ([Price] > 5)
) ALIAS_GENERATED_1
WHERE ([RowNum] >= @_minrow)
AND ([RowNum] <= @_maxrow)
ORDER BY [RowNum]
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,49 @@ public void ShouldGeneratePaginate()

this.Assent(actual);
}


[Test]
public void ShouldGeneratePaginateWithSpecifiedColumn()
{
string actual = null;
transaction.Stream<object>(Arg.Do<string>(s => actual = s), Arg.Any<CommandParameterValues>());
CreateQueryBuilder<object>("Orders")
.Column("Foo")
.Where("[Price] > 5")
.OrderBy("Foo")
.ToList(10, 20);

this.Assent(actual);
}

[Test]
public void ShouldGeneratePaginateWithMultipleSpecifiedColumns()
{
string actual = null;
transaction.Stream<object>(Arg.Do<string>(s => actual = s), Arg.Any<CommandParameterValues>());
CreateQueryBuilder<object>("Orders")
.Column("Foo")
.Column("Bar")
.Column("Elephant")
.Where("[Price] > 5")
.OrderBy("Foo")
.ToList(10, 20);

this.Assent(actual);
}

[Test]
public void ShouldGeneratePaginateWithSpecifiedColumnAndNoSort()
{
string actual = null;
transaction.Stream<object>(Arg.Do<string>(s => actual = s), Arg.Any<CommandParameterValues>());
CreateQueryBuilder<object>("Orders")
.Column("Foo")
.Where("[Price] > 5")
.ToList(10, 20);

this.Assent(actual);
}

[Test]
public void ShouldGeneratePaginateForJoin()
Expand Down
6 changes: 4 additions & 2 deletions source/Nevermore/Advanced/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@ SubquerySelectBuilder BuildToList(int skip, int take, out CommandParameterValues
var maxRowParameter = new UniqueParameter(uniqueParameterNameGenerator, new Parameter("_maxrow"));

var clonedSelectBuilder = selectBuilder.Clone();

clonedSelectBuilder.AddDefaultColumnSelection();

if(!clonedSelectBuilder.HasCustomColumnSelection)
clonedSelectBuilder.AddDefaultColumnSelection();

clonedSelectBuilder.AddRowNumberColumn(rowNumberColumnName, new List<Column>());

var subqueryBuilder = CreateSubqueryBuilder(clonedSelectBuilder);
Expand Down
2 changes: 2 additions & 0 deletions source/Nevermore/Advanced/SelectBuilders/SelectBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ ISelect GenerateSelectInner(Func<OrderBy> getDefaultOrderBy)
}

public abstract ISelectBuilder Clone();

public bool HasCustomColumnSelection => ColumnSelection != null;

Where GetWhere()
{
Expand Down
1 change: 1 addition & 0 deletions source/Nevermore/ISelectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ public interface ISelectBuilder
ISelect GenerateSelectWithoutDefaultOrderBy();

ISelectBuilder Clone();
bool HasCustomColumnSelection { get; }
}
}

0 comments on commit 7c2c335

Please sign in to comment.