Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow Limit() without Order() with MSSQL #1009

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions query_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ func (q *SelectQuery) appendQuery(
if count && !cteCount {
b = append(b, "count(*)"...)
} else {
// Allows Limit() without Order() with MSSQL as per https://stackoverflow.com/a/36156953
if q.limit > 0 && fmter.Dialect().Features().Has(feature.OffsetFetch) && len(q.order) == 0 {
b = append(b, "0 AS _temp_sort, "...)
q.order = []schema.QueryWithArgs{schema.UnsafeIdent("_temp_sort")}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is query generation and it should not change the original SelectQuery, because someone may be reusing them.

You probably need to add another block of code to generate order without changing the SelectQuery.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that if I have a SelectQuery in a variable a and then do b := a.Limit(5), the value of a would change, and that would be a problem? Isn't that also the case without the proposed change? I cannot seem to find an analog for this case to compare to. Do you mean something like ORDER BY after LIMIT, or is there another case where there could be something appended to a select query after Limit()? An example would be much appreciated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CL-Jeremy I've fixed this myself. See the commits in this branch.

}
b, err = q.appendColumns(fmter, b)
if err != nil {
return nil, err
Expand Down
Loading