diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb index ef0b78058e..cc077d863b 100644 --- a/lib/arel/visitors/mssql.rb +++ b/lib/arel/visitors/mssql.rb @@ -1,6 +1,14 @@ module Arel module Visitors class MSSQL < Arel::Visitors::ToSql + class RowNumber + attr_reader :expr + + def initialize node + @expr = node + end + end + private # `top` wouldn't really work here. I.e. User.select("distinct first_name").limit(10) would generate @@ -10,6 +18,10 @@ module Arel "" end + def visit_Arel_Visitors_MSSQL_RowNumber o + "ROW_NUMBER() OVER (#{o.expr}) as _row_num" + end + def visit_Arel_Nodes_SelectStatement o if !o.limit && !o.offset return super o @@ -55,7 +67,7 @@ module Arel end def row_num_literal order_by - Nodes::SqlLiteral.new("ROW_NUMBER() OVER (#{order_by}) as _row_num") + RowNumber.new order_by end def select_count? x