Skip to content

Commit

Permalink
fix: allow Limit() without Order() with MSSQL (#1009)
Browse files Browse the repository at this point in the history
* fix: allow Limit() without Order() with MSSQL

* code review

* fix dup order

---------

Co-authored-by: Vladimir Mihailenco <vladimir.webdev@gmail.com>
  • Loading branch information
CL-Jeremy and vmihailenco authored Oct 26, 2024
1 parent 87c77b8 commit 1a46ddc
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 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 {
// MSSQL: allows Limit() without Order() as per https://stackoverflow.com/a/36156953
if q.limit > 0 && len(q.order) == 0 && fmter.Dialect().Name() == dialect.MSSQL {
b = append(b, "0 AS _temp_sort, "...)
}

b, err = q.appendColumns(fmter, b)
if err != nil {
return nil, err
Expand Down Expand Up @@ -793,6 +798,12 @@ func (q *SelectQuery) appendOrder(fmter schema.Formatter, b []byte) (_ []byte, e

return b, nil
}

// MSSQL: allows Limit() without Order() as per https://stackoverflow.com/a/36156953
if q.limit > 0 && fmter.Dialect().Name() == dialect.MSSQL {
return append(b, " ORDER BY _temp_sort"...), nil
}

return b, nil
}

Expand Down

0 comments on commit 1a46ddc

Please sign in to comment.