Skip to content

Commit 38d87f0

Browse files
committed
[Rails3] Defensive table/column name for temporary ROW_NUMBER table.
1 parent 8030879 commit 38d87f0

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lib/arel/engines/sql/compilers/sqlserver_compiler.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def select_sql_with_complex_count
7171
build_query \
7272
"SELECT COUNT([count]) AS [count_id]",
7373
"FROM (",
74-
"SELECT #{top_clause}ROW_NUMBER() OVER (ORDER BY #{unique_orders(rowtable_order_clauses).join(', ')}) AS [rn],",
74+
"SELECT #{top_clause}ROW_NUMBER() OVER (ORDER BY #{unique_orders(rowtable_order_clauses).join(', ')}) AS [__rn],",
7575
"1 AS [count]",
7676
"FROM #{relation.from_clauses}",
7777
(locked unless locked.blank?),
@@ -80,8 +80,8 @@ def select_sql_with_complex_count
8080
("GROUP BY #{groups.join(', ')}" unless groups.blank?),
8181
("HAVING #{havings.join(' AND ')}" unless havings.blank?),
8282
("ORDER BY #{unique_orders(orders).join(', ')}" unless orders.blank?),
83-
") AS [_rnt]",
84-
"WHERE [_rnt].[rn] > #{relation.skipped.to_i}"
83+
") AS [__rnt]",
84+
"WHERE [__rnt].[__rn] > #{relation.skipped.to_i}"
8585
end
8686

8787
def select_sql_without_skipped(windowed=false)
@@ -123,17 +123,17 @@ def select_sql_with_skipped
123123
build_query \
124124
"SELECT #{tc}#{rowtable_select_clauses.join(', ')}",
125125
"FROM (",
126-
"SELECT ROW_NUMBER() OVER (ORDER BY #{unique_orders(rowtable_order_clauses).join(', ')}) AS [rn],",
126+
"SELECT ROW_NUMBER() OVER (ORDER BY #{unique_orders(rowtable_order_clauses).join(', ')}) AS [__rn],",
127127
select_sql_without_skipped(true),
128-
") AS [_rnt]",
129-
"WHERE [_rnt].[rn] > #{relation.skipped.to_i}"
128+
") AS [__rnt]",
129+
"WHERE [__rnt].[__rn] > #{relation.skipped.to_i}"
130130
end
131131

132132
def rowtable_select_clauses
133133
if single_distinct_select?
134134
::Array.wrap(relation.select_clauses.first.dup.tap do |sc|
135135
sc.sub! 'DISTINCT', "DISTINCT #{taken_clause if relation.taken.present?}".strip
136-
sc.sub! table_name_from_select_clause(sc), '_rnt'
136+
sc.sub! table_name_from_select_clause(sc), '__rnt'
137137
sc.strip!
138138
end)
139139
elsif relation.join? && all_select_clauses_aliased?
@@ -142,7 +142,7 @@ def rowtable_select_clauses
142142
end
143143
else
144144
relation.select_clauses.map do |sc|
145-
sc.gsub /\[#{relation.table.name}\]\./, '[_rnt].'
145+
sc.gsub /\[#{relation.table.name}\]\./, '[__rnt].'
146146
end
147147
end
148148
end

test/cases/offset_and_limit_test_sqlserver.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,28 @@ class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
2525
context 'When selecting with offset' do
2626

2727
should 'have no limit (top) if only offset is passed' do
28-
assert_sql(/SELECT \[_rnt\]\.\* FROM.*WHERE \[_rnt\]\.\[rn\] > 1/) { Book.all(:offset=>1) }
28+
assert_sql(/SELECT \[__rnt\]\.\* FROM.*WHERE \[__rnt\]\.\[__rn\] > 1/) { Book.all(:offset=>1) }
2929
end
3030

3131
end
3232

3333
context 'When selecting with limit and offset' do
3434

3535
should 'only allow integers for offset' do
36-
assert_sql(/WHERE \[_rnt\]\.\[rn\] > 0/) { Book.limit(10).offset('five').all }
36+
assert_sql(/WHERE \[__rnt\]\.\[__rn\] > 0/) { Book.limit(10).offset('five').all }
3737
end
3838

3939
should 'convert strings which look like integers to integers' do
40-
assert_sql(/WHERE \[_rnt\]\.\[rn\] > 5/) { Book.limit(10).offset('5').all }
40+
assert_sql(/WHERE \[__rnt\]\.\[__rn\] > 5/) { Book.limit(10).offset('5').all }
4141
end
4242

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

0 commit comments

Comments
 (0)