Skip to content

Commit b43eaa9

Browse files
Erik Brynmetaskills
authored andcommitted
Changed #gsub to #sub to avoid #add_limit_offset! rewriting subquery SELECTs to include TOPs.
http://rails-sqlserver.lighthouseapp.com/projects/20277/tickets/4-subquery-select-statements-are-modified-by-add_limit_offset
1 parent 2cd43cb commit b43eaa9

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def add_limit_offset!(sql, options)
353353
end
354354
# The business of adding limit/offset
355355
if options[:limit] and options[:offset]
356-
total_rows = select_value("SELECT count(*) as TotalRows from (#{sql.gsub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT#{$1} TOP 1000000000")}) tally").to_i
356+
total_rows = select_value("SELECT count(*) as TotalRows from (#{sql.sub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT#{$1} TOP 1000000000")}) tally").to_i
357357
if (options[:limit] + options[:offset]) >= total_rows
358358
options[:limit] = (total_rows - options[:offset] >= 0) ? (total_rows - options[:offset]) : 0
359359
end

0 commit comments

Comments
 (0)