2010-12-27 11:16:18 -05:00
|
|
|
module Arel
|
|
|
|
module Visitors
|
|
|
|
class MSSQL < Arel::Visitors::ToSql
|
2014-04-08 23:30:40 -04:00
|
|
|
class RowNumber
|
2014-04-08 23:44:42 -04:00
|
|
|
attr_reader :children
|
2014-04-08 23:30:40 -04:00
|
|
|
|
|
|
|
def initialize node
|
2014-04-08 23:44:42 -04:00
|
|
|
@children = node
|
2014-04-08 23:30:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-27 11:16:18 -05:00
|
|
|
private
|
|
|
|
|
2011-06-09 05:24:54 -04:00
|
|
|
# `top` wouldn't really work here. I.e. User.select("distinct first_name").limit(10) would generate
|
|
|
|
# "select top 10 distinct first_name from users", which is invalid query! it should be
|
|
|
|
# "select distinct top 10 first_name from users"
|
2014-03-24 23:42:27 -04:00
|
|
|
def visit_Arel_Nodes_Top o
|
2011-06-09 05:24:54 -04:00
|
|
|
""
|
2011-02-02 18:09:54 -05:00
|
|
|
end
|
|
|
|
|
2014-04-08 23:30:40 -04:00
|
|
|
def visit_Arel_Visitors_MSSQL_RowNumber o
|
2014-04-08 23:44:42 -04:00
|
|
|
"ROW_NUMBER() OVER (ORDER BY #{o.children.map { |x| visit x }.join ', '}) as _row_num"
|
2014-04-08 23:30:40 -04:00
|
|
|
end
|
|
|
|
|
2014-03-24 23:42:27 -04:00
|
|
|
def visit_Arel_Nodes_SelectStatement o
|
2011-06-09 05:24:54 -04:00
|
|
|
if !o.limit && !o.offset
|
2014-03-24 23:42:27 -04:00
|
|
|
return super o
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
is_select_count = false
|
2014-04-08 23:49:22 -04:00
|
|
|
o.cores.each { |x|
|
2014-04-08 23:44:42 -04:00
|
|
|
core_order_by = row_num_literal determine_order_by(o.orders, x)
|
2011-06-09 05:24:54 -04:00
|
|
|
if select_count? x
|
2014-04-08 23:44:42 -04:00
|
|
|
x.projections = [core_order_by]
|
2011-06-09 05:24:54 -04:00
|
|
|
is_select_count = true
|
|
|
|
else
|
2014-04-08 23:44:42 -04:00
|
|
|
x.projections << core_order_by
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
2014-04-08 23:49:22 -04:00
|
|
|
}
|
2011-06-09 05:24:54 -04:00
|
|
|
|
2014-04-08 23:49:22 -04:00
|
|
|
sql = o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join
|
2011-06-09 05:24:54 -04:00
|
|
|
|
|
|
|
sql = "SELECT _t.* FROM (#{sql}) as _t WHERE #{get_offset_limit_clause(o)}"
|
|
|
|
# fixme count distinct wouldn't work with limit or offset
|
|
|
|
sql = "SELECT COUNT(1) as count_id FROM (#{sql}) AS subquery" if is_select_count
|
|
|
|
sql
|
2010-12-27 11:16:18 -05:00
|
|
|
end
|
|
|
|
|
2011-06-09 05:24:54 -04:00
|
|
|
def get_offset_limit_clause o
|
|
|
|
first_row = o.offset ? o.offset.expr.to_i + 1 : 1
|
|
|
|
last_row = o.limit ? o.limit.expr.to_i - 1 + first_row : nil
|
|
|
|
if last_row
|
|
|
|
" _row_num BETWEEN #{first_row} AND #{last_row}"
|
|
|
|
else
|
|
|
|
" _row_num >= #{first_row}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-08 23:35:28 -04:00
|
|
|
def determine_order_by orders, x
|
|
|
|
if orders.any?
|
2014-04-08 23:44:42 -04:00
|
|
|
orders
|
|
|
|
elsif x.groups.any?
|
|
|
|
x.groups
|
2011-06-09 05:24:54 -04:00
|
|
|
else
|
2014-04-08 23:54:33 -04:00
|
|
|
pk = find_left_table_pk(x.froms)
|
|
|
|
pk ? [pk] : []
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def row_num_literal order_by
|
2014-04-08 23:30:40 -04:00
|
|
|
RowNumber.new order_by
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def select_count? x
|
|
|
|
x.projections.length == 1 && Arel::Nodes::Count === x.projections.first
|
|
|
|
end
|
|
|
|
|
2013-12-04 21:18:22 -05:00
|
|
|
# FIXME raise exception of there is no pk?
|
|
|
|
# FIXME!! Table.primary_key will be deprecated. What is the replacement??
|
2014-03-24 23:42:27 -04:00
|
|
|
def find_left_table_pk o
|
2014-04-08 23:54:33 -04:00
|
|
|
return o.primary_key if o.instance_of? Arel::Table
|
2014-03-24 23:42:27 -04:00
|
|
|
find_left_table_pk o.left if o.kind_of? Arel::Nodes::Join
|
2010-12-27 11:16:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|