Add Mssql pagination feature with cte #825

Closed
cantasci wants to merge 1 commits from cantasci/added_mssql_pagination_feature into master
cantasci commented 2018-01-11 14:49:38 +00:00 (Migrated from github.com)

Hi,

I want to make pagination with mssql. But offset feature is not working when page is greater than one.
So, I want to add this feature.

I use CTE(Common Table Expression) with related query for this problem.

WITH SQLPaging AS (SELECT TOP(%d) ROW_NUMBER() OVER (ORDER BY %v ASC) AS RN, %v FROM(%v) AS TEMP_TABLE_NAME)

I added getPrimaryColumnForOffset function
func (statement *Statement) getPrimaryColumnForOffset() string { var column string if len(statement.RefTable.PKColumns()) == 0 { for _, index := range statement.RefTable.Indexes { if len(index.Cols) == 1 { column = index.Cols[0] break } } if len(column) == 0 { column = statement.RefTable.ColumnsSeq()[0] } } else { column = statement.RefTable.PKColumns()[0].Name } return column }

Final query return filtered result by [start, start+limitN].

SELECT %v FROM SQLPaging WITH(NOLOCK) WHERE RN > %d AND RN < %d", columnStr, statement.Start, statement.Start+statement.LimitN+1) }

Thank you,

Hi, I want to make pagination with mssql. But offset feature is not working when page is greater than one. So, I want to add this feature. I use CTE(Common Table Expression) with related query for this problem. `WITH SQLPaging AS (SELECT TOP(%d) ROW_NUMBER() OVER (ORDER BY %v ASC) AS RN, %v FROM(%v) AS TEMP_TABLE_NAME)` I added getPrimaryColumnForOffset function ` func (statement *Statement) getPrimaryColumnForOffset() string { var column string if len(statement.RefTable.PKColumns()) == 0 { for _, index := range statement.RefTable.Indexes { if len(index.Cols) == 1 { column = index.Cols[0] break } } if len(column) == 0 { column = statement.RefTable.ColumnsSeq()[0] } } else { column = statement.RefTable.PKColumns()[0].Name } return column } ` Final query return filtered result by [start, start+limitN]. ` SELECT %v FROM SQLPaging WITH(NOLOCK) WHERE RN > %d AND RN < %d", columnStr, statement.Start, statement.Start+statement.LimitN+1) } ` Thank you,
codecov-io commented 2018-01-11 15:06:55 +00:00 (Migrated from github.com)

Codecov Report

Merging #825 into master will decrease coverage by 0.12%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #825      +/-   ##
=========================================
- Coverage   53.23%   53.1%   -0.13%     
=========================================
  Files          39      39              
  Lines        7324    7340      +16     
=========================================
- Hits         3899    3898       -1     
- Misses       2931    2948      +17     
  Partials      494     494
Impacted Files Coverage Δ
statement.go 63.88% <0%> (-1.41%) ⬇️
dialect_postgres.go 77.77% <0%> (-0.09%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5185fdf...9477d37. Read the comment docs.

# [Codecov](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=h1) Report > Merging [#825](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=desc) into [master](https://codecov.io/gh/go-xorm/xorm/commit/5185fdfa1b97501b385a97ed25bc5ebd03cef7d8?src=pr&el=desc) will **decrease** coverage by `0.12%`. > The diff coverage is `0%`. [![Impacted file tree graph](https://codecov.io/gh/go-xorm/xorm/pull/825/graphs/tree.svg?width=650&height=150&src=pr&token=yB5nO1krEe)](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=tree) ```diff @@ Coverage Diff @@ ## master #825 +/- ## ========================================= - Coverage 53.23% 53.1% -0.13% ========================================= Files 39 39 Lines 7324 7340 +16 ========================================= - Hits 3899 3898 -1 - Misses 2931 2948 +17 Partials 494 494 ``` | [Impacted Files](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=tree) | Coverage Δ | | |---|---|---| | [statement.go](https://codecov.io/gh/go-xorm/xorm/pull/825/diff?src=pr&el=tree#diff-c3RhdGVtZW50Lmdv) | `63.88% <0%> (-1.41%)` | :arrow_down: | | [dialect\_postgres.go](https://codecov.io/gh/go-xorm/xorm/pull/825/diff?src=pr&el=tree#diff-ZGlhbGVjdF9wb3N0Z3Jlcy5nbw==) | `77.77% <0%> (-0.09%)` | :arrow_down: | ------ [Continue to review full report at Codecov](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=continue). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta) > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=footer). Last update [5185fdf...9477d37](https://codecov.io/gh/go-xorm/xorm/pull/825?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

outdated.

outdated.
lunny closed this pull request 2023-07-22 15:13:15 +00:00

Pull request closed

Sign in to join this conversation.
No description provided.