Skip to content

Commit 67e28d4

Browse files
committed
Create a select statement helper that does the task of merging source/froms with a lock.
1 parent cabad3d commit 67e28d4

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/arel/visitors/sqlserver.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
143143
[ ("SELECT" if !windowed),
144144
(visit(o.limit) if o.limit && !windowed),
145145
(projections.map{ |x| visit(x) }.join(', ')),
146-
# TODO: [ARel 2.2] Use #from/#source vs. #froms
147-
# visit(core.source),
148-
("FROM #{visit core.froms}" if core.froms),
149-
(visit(o.lock) if o.lock),
146+
(source_with_lock_for_select_statement(o)),
150147
("WHERE #{core.wheres.map{ |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
151148
("GROUP BY #{groups.map { |x| visit x }.join ', ' }" unless groups.empty?),
152149
(visit(core.having) if core.having),
@@ -177,10 +174,7 @@ def visit_Arel_Nodes_SelectStatementForComplexCount(o)
177174
(visit(o.limit) if o.limit),
178175
"ROW_NUMBER() OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS [__rn],",
179176
"1 AS [count]",
180-
# TODO: [ARel 2.2] Use #from/#source vs. #froms
181-
# visit(core.source),
182-
("FROM #{visit core.froms}" if core.froms),
183-
(visit(o.lock) if o.lock),
177+
(source_with_lock_for_select_statement(o)),
184178
("WHERE #{core.wheres.map{ |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
185179
("GROUP BY #{core.groups.map { |x| visit x }.join ', ' }" unless core.groups.empty?),
186180
(visit(core.having) if core.having),
@@ -193,6 +187,19 @@ def visit_Arel_Nodes_SelectStatementForComplexCount(o)
193187

194188
# SQLServer Helpers
195189

190+
def source_with_lock_for_select_statement(o)
191+
# TODO: [ARel 2.2] Use #from/#source vs. #froms
192+
core = o.cores.first
193+
source = "FROM #{visit core.froms}" if core.froms
194+
if source && o.lock
195+
lock = visit o.lock
196+
index = source.match(/FROM [\w\[\]\.]+/)[0].length
197+
source.insert index, " #{lock}"
198+
else
199+
source
200+
end
201+
end
202+
196203
def table_from_select_statement(o)
197204
core = o.cores.first
198205
# TODO: [ARel 2.2] Use #from/#source vs. #froms

test/cases/offset_and_limit_test_sqlserver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
4141
end
4242

4343
should 'alter SQL to limit number of records returned offset by specified amount' do
44-
sql = %|SELECT TOP (3) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [books].[id] ASC) AS [__rn], [books].* FROM [books] ) AS [__rnt] WHERE [__rnt].[__rn] > 5|
44+
sql = %|SELECT TOP (3) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [books].[id] ASC) AS [__rn], [books].* FROM [books] ) AS [__rnt] WHERE [__rnt].[__rn] > 5|
4545
assert_sql(sql) { Book.limit(3).offset(5).all }
4646
end
4747

0 commit comments

Comments
 (0)