2016-08-31 11:20:36 -04:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-12-27 11:16:18 -05:00
|
|
|
module Visitors
|
|
|
|
class MSSQL < Arel::Visitors::ToSql
|
2014-04-08 23:56:49 -04:00
|
|
|
RowNumber = Struct.new :children
|
2014-04-08 23:30:40 -04:00
|
|
|
|
2014-11-26 17:00:05 -05:00
|
|
|
def initialize(*)
|
|
|
|
@primary_keys = {}
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-12-27 11:16:18 -05:00
|
|
|
private
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Visitors_MSSQL_RowNumber(o, collector)
|
|
|
|
collector << "ROW_NUMBER() OVER (ORDER BY "
|
|
|
|
inject_join(o.children, collector, ", ") << ") as _row_num"
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_SelectStatement(o, collector)
|
|
|
|
if !o.limit && !o.offset
|
|
|
|
return super
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
is_select_count = false
|
|
|
|
o.cores.each { |x|
|
|
|
|
core_order_by = row_num_literal determine_order_by(o.orders, x)
|
|
|
|
if select_count? x
|
|
|
|
x.projections = [core_order_by]
|
|
|
|
is_select_count = true
|
|
|
|
else
|
|
|
|
x.projections << core_order_by
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_select_count
|
|
|
|
# fixme count distinct wouldn't work with limit or offset
|
|
|
|
collector << "SELECT COUNT(1) as count_id FROM ("
|
|
|
|
end
|
2011-06-09 05:24:54 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
collector << "SELECT _t.* FROM ("
|
|
|
|
collector = o.cores.inject(collector) { |c, x|
|
|
|
|
visit_Arel_Nodes_SelectCore x, c
|
|
|
|
}
|
|
|
|
collector << ") as _t WHERE #{get_offset_limit_clause(o)}"
|
2014-04-08 23:56:49 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
if is_select_count
|
|
|
|
collector << ") AS subquery"
|
|
|
|
else
|
|
|
|
collector
|
|
|
|
end
|
2014-04-08 23:56:49 -04:00
|
|
|
end
|
2010-12-27 11:16:18 -05:00
|
|
|
|
2018-02-24 01:45:50 -05: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
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_DeleteStatement(o, collector)
|
|
|
|
collector << "DELETE "
|
|
|
|
if o.limit
|
|
|
|
collector << "TOP ("
|
|
|
|
visit o.limit.expr, collector
|
|
|
|
collector << ") "
|
|
|
|
end
|
|
|
|
collector << "FROM "
|
|
|
|
collector = visit o.relation, collector
|
|
|
|
if o.wheres.any?
|
|
|
|
collector << " WHERE "
|
|
|
|
inject_join o.wheres, collector, AND
|
|
|
|
else
|
|
|
|
collector
|
|
|
|
end
|
2015-02-23 12:58:22 -05:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def determine_order_by(orders, x)
|
|
|
|
if orders.any?
|
|
|
|
orders
|
|
|
|
elsif x.groups.any?
|
|
|
|
x.groups
|
|
|
|
else
|
|
|
|
pk = find_left_table_pk(x.froms)
|
|
|
|
pk ? [pk] : []
|
|
|
|
end
|
2011-06-09 05:24:54 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def row_num_literal(order_by)
|
|
|
|
RowNumber.new order_by
|
|
|
|
end
|
2011-06-09 05:24:54 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def select_count?(x)
|
|
|
|
x.projections.length == 1 && Arel::Nodes::Count === x.projections.first
|
|
|
|
end
|
2011-06-09 05:24:54 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
# FIXME raise exception of there is no pk?
|
|
|
|
def find_left_table_pk(o)
|
|
|
|
if o.kind_of?(Arel::Nodes::Join)
|
|
|
|
find_left_table_pk(o.left)
|
|
|
|
elsif o.instance_of?(Arel::Table)
|
|
|
|
find_primary_key(o)
|
|
|
|
end
|
2014-11-26 17:00:05 -05:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def find_primary_key(o)
|
|
|
|
@primary_keys[o.name] ||= begin
|
|
|
|
primary_key_name = @connection.primary_key(o.name)
|
|
|
|
# some tables might be without primary key
|
|
|
|
primary_key_name && o[primary_key_name]
|
|
|
|
end
|
2014-11-26 17:00:05 -05:00
|
|
|
end
|
2010-12-27 11:16:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|