Skip to content

Commit

Permalink
Fix 'select 1' behavior introduced by AR 3.0.7
Browse files Browse the repository at this point in the history
Fixes jruby#56.
  • Loading branch information
nicksieger committed Jun 18, 2011
1 parent 0a5ab83 commit fec1fff
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/arjdbc/mssql/limit_helpers.rb
Expand Up @@ -68,10 +68,15 @@ def replace_limit_offset!(sql, limit, offset, order)
end_row = offset + limit.to_i
find_select = /\b(SELECT(?:\s+DISTINCT)?)\b(.*)/im
whole, select, rest_of_query = find_select.match(sql).to_a
if rest_of_query.strip!.first == '*'
rest_of_query.strip!
if rest_of_query.first == "1"
rest_of_query[0] = "*"
end
if rest_of_query.first == "*"
from_table = LimitHelpers.get_table_name(rest_of_query)
rest_of_query = from_table + '.' + rest_of_query
end
new_sql = "#{select} t.* FROM (SELECT ROW_NUMBER() OVER(#{order}) AS _row_num, #{from_table + '.' if from_table}#{rest_of_query}"
new_sql = "#{select} t.* FROM (SELECT ROW_NUMBER() OVER(#{order}) AS _row_num, #{rest_of_query}"
new_sql << ") AS t WHERE t._row_num BETWEEN #{start_row.to_s} AND #{end_row.to_s}"
sql.replace(new_sql)
end
Expand Down

0 comments on commit fec1fff

Please sign in to comment.