From 7c2c33502cb7cb915084a5de4fff96e64696566c Mon Sep 17 00:00:00 2001 From: Alastair Pitts Date: Wed, 8 Jun 2022 12:35:24 +1000 Subject: [PATCH] Fix SQL generation when paging with specified columns (#143) --- ...eWithMultipleSpecifiedColumns.approved.txt | 12 +++++ ...tePaginateWithSpecifiedColumn.approved.txt | 10 +++++ ...eWithSpecifiedColumnAndNoSort.approved.txt | 10 +++++ .../QueryBuilderFixture.cs | 44 ++++++++++++++++++- source/Nevermore/Advanced/QueryBuilder.cs | 6 ++- .../SelectBuilders/SelectBuilderBase.cs | 2 + source/Nevermore/ISelectBuilder.cs | 1 + 7 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithMultipleSpecifiedColumns.approved.txt create mode 100644 source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumn.approved.txt create mode 100644 source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumnAndNoSort.approved.txt diff --git a/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithMultipleSpecifiedColumns.approved.txt b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithMultipleSpecifiedColumns.approved.txt new file mode 100644 index 00000000..ab935bcc --- /dev/null +++ b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithMultipleSpecifiedColumns.approved.txt @@ -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] \ No newline at end of file diff --git a/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumn.approved.txt b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumn.approved.txt new file mode 100644 index 00000000..e29ac451 --- /dev/null +++ b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumn.approved.txt @@ -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] \ No newline at end of file diff --git a/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumnAndNoSort.approved.txt b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumnAndNoSort.approved.txt new file mode 100644 index 00000000..1a2b2d99 --- /dev/null +++ b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.ShouldGeneratePaginateWithSpecifiedColumnAndNoSort.approved.txt @@ -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] \ No newline at end of file diff --git a/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.cs b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.cs index c6670a00..942fdd0c 100644 --- a/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.cs +++ b/source/Nevermore.Tests/QueryBuilderFixture/QueryBuilderFixture.cs @@ -342,7 +342,49 @@ public void ShouldGeneratePaginate() this.Assent(actual); } - + + [Test] + public void ShouldGeneratePaginateWithSpecifiedColumn() + { + string actual = null; + transaction.Stream(Arg.Do(s => actual = s), Arg.Any()); + CreateQueryBuilder("Orders") + .Column("Foo") + .Where("[Price] > 5") + .OrderBy("Foo") + .ToList(10, 20); + + this.Assent(actual); + } + + [Test] + public void ShouldGeneratePaginateWithMultipleSpecifiedColumns() + { + string actual = null; + transaction.Stream(Arg.Do(s => actual = s), Arg.Any()); + CreateQueryBuilder("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(Arg.Do(s => actual = s), Arg.Any()); + CreateQueryBuilder("Orders") + .Column("Foo") + .Where("[Price] > 5") + .ToList(10, 20); + + this.Assent(actual); + } [Test] public void ShouldGeneratePaginateForJoin() diff --git a/source/Nevermore/Advanced/QueryBuilder.cs b/source/Nevermore/Advanced/QueryBuilder.cs index ddb43267..3960d9e7 100644 --- a/source/Nevermore/Advanced/QueryBuilder.cs +++ b/source/Nevermore/Advanced/QueryBuilder.cs @@ -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()); var subqueryBuilder = CreateSubqueryBuilder(clonedSelectBuilder); diff --git a/source/Nevermore/Advanced/SelectBuilders/SelectBuilderBase.cs b/source/Nevermore/Advanced/SelectBuilders/SelectBuilderBase.cs index 3caa39cb..f27ab478 100644 --- a/source/Nevermore/Advanced/SelectBuilders/SelectBuilderBase.cs +++ b/source/Nevermore/Advanced/SelectBuilders/SelectBuilderBase.cs @@ -59,6 +59,8 @@ ISelect GenerateSelectInner(Func getDefaultOrderBy) } public abstract ISelectBuilder Clone(); + + public bool HasCustomColumnSelection => ColumnSelection != null; Where GetWhere() { diff --git a/source/Nevermore/ISelectBuilder.cs b/source/Nevermore/ISelectBuilder.cs index 66b646f2..e21d4cf1 100644 --- a/source/Nevermore/ISelectBuilder.cs +++ b/source/Nevermore/ISelectBuilder.cs @@ -33,5 +33,6 @@ public interface ISelectBuilder ISelect GenerateSelectWithoutDefaultOrderBy(); ISelectBuilder Clone(); + bool HasCustomColumnSelection { get; } } } \ No newline at end of file