Skip to content

Commit 3cf43ea

Browse files
committed
Cleanup
1 parent ba39216 commit 3cf43ea

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- [#1318](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1318) Reverse order of values when upserting
66
- [#1321](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1321) Fix SQL statement to calculate `updated_at` when upserting
7-
- []() When using `LIMIT`/`OFFSET` without ordering try to order using projection rather than with the primary key.
7+
- [1322](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1322) When using `FETCH` without ordering try to order using projection rather than the primary key.
88

99
## v8.0.5
1010

lib/arel/visitors/sqlserver.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,8 @@ def select_statement_lock?
302302
@select_statement && @select_statement.lock
303303
end
304304

305-
# LIMIT/OFFSET cannot be used without an ORDER. This method adds a deterministic ORDER using following rules:
306-
# 1. If the query has projections, use the first projection as the ORDER BY clause.
307-
# 2. If the query has SQL literal projection, use the first part of the SQL literal as the ORDER BY clause.
308-
# 3. If the query has a table with a primary key, use the primary key as the ORDER BY clause.
305+
# FETCH cannot be used without an order. If an order is not given then try to use the projections for the ordering.
306+
# If no suitable projection are present then fallback to using the primary key of the table.
309307
def make_Fetch_Possible_And_Deterministic(o)
310308
return if o.limit.nil? && o.offset.nil?
311309
return if o.orders.any?
@@ -318,6 +316,8 @@ def make_Fetch_Possible_And_Deterministic(o)
318316
end
319317
end
320318

319+
# Find the first projection or part of projection that can be used for ordering. Cannot use
320+
# projections with '*' or '1 AS one' in them.
321321
def projection_to_order_by_for_fetch(o)
322322
o.cores.first.projections.each do |projection|
323323
case projection
@@ -335,8 +335,7 @@ def projection_to_order_by_for_fetch(o)
335335
nil
336336
end
337337

338-
# Remove last AS from projection that could contain multiple AS clauses.
339-
# Examples:
338+
# Remove last AS from projection. Example projections:
340339
# - 'name'
341340
# - 'name AS first_name'
342341
# - 'AVG(accounts.credit_limit AS DECIMAL) AS avg_credit_limit)'

0 commit comments

Comments
 (0)