Skip to content

Commit

Permalink
Fix broken limit_helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
fpauser authored and kares committed Sep 11, 2015
1 parent c9f6008 commit 75829cf
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/arjdbc/mssql/limit_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ def replace_limit_offset!(sql, limit, offset, order)
# ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: Column 'users.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
# SELECT t.* FROM ( SELECT ROW_NUMBER() OVER(ORDER BY users.id) AS _row_num, [users].[lft], COUNT([users].[lft]) FROM [users] GROUP BY [users].[lft] HAVING COUNT([users].[lft]) > 1 ) AS t WHERE t._row_num BETWEEN 1 AND 1
if rest_of_query.downcase.include?('group by')
order_start = order.strip[0, 8]; order_start.upcase!
if order_start == 'ORDER BY' && order.match(FIND_AGGREGATE_FUNCTION)
# do nothing
elsif order.count(',') == 0
order.gsub!(/ORDER +BY +([^\s]+)(\s+ASC|\s+DESC)?/i, 'ORDER BY MIN(\1)\2')
else
raise('Only one order condition allowed.')
# Do not catch 'GROUP BY' statements from sub-selects, indicated
# by more closing than opening brackets after the last group by.
rest_after_last_group_by = rest_of_query.downcase.split('group by').last
opening_brackets_count = rest_after_last_group_by.count('(')
closing_brackets_count = rest_after_last_group_by.count(')')

if opening_brackets_count == closing_brackets_count
order_start = order.strip[0, 8]; order_start.upcase!
if order_start == 'ORDER BY' && order.match(FIND_AGGREGATE_FUNCTION)
# do nothing
elsif order.count(',') == 0
order.gsub!(/ORDER +BY +([^\s]+)(\s+ASC|\s+DESC)?/i, 'ORDER BY MIN(\1)\2')
else
raise('Only one order condition allowed.')
end
end
end

Expand Down

0 comments on commit 75829cf

Please sign in to comment.