mssql.rb 3.2 KB
Newer Older
O
ojab 已提交
1
# frozen_string_literal: true
M
Matthew Draper 已提交
2

M
Matthew Draper 已提交
3
module Arel # :nodoc: all
4 5
  module Visitors
    class MSSQL < Arel::Visitors::ToSql
A
Aaron Patterson 已提交
6
      RowNumber = Struct.new :children
7

8 9 10 11 12
      def initialize(*)
        @primary_keys = {}
        super
      end

13 14
      private

M
Matthew Draper 已提交
15 16 17
        def visit_Arel_Visitors_MSSQL_RowNumber(o, collector)
          collector << "ROW_NUMBER() OVER (ORDER BY "
          inject_join(o.children, collector, ", ") << ") as _row_num"
18 19
        end

M
Matthew Draper 已提交
20 21 22
        def visit_Arel_Nodes_SelectStatement(o, collector)
          if !o.limit && !o.offset
            return super
23 24
          end

M
Matthew Draper 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
          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
40

M
Matthew Draper 已提交
41 42 43 44 45
          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)}"
A
Aaron Patterson 已提交
46

M
Matthew Draper 已提交
47 48 49 50 51
          if is_select_count
            collector << ") AS subquery"
          else
            collector
          end
A
Aaron Patterson 已提交
52
        end
53

M
Matthew Draper 已提交
54 55 56 57 58 59 60 61
        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
62 63
        end

M
Matthew Draper 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        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
79 80
        end

M
Matthew Draper 已提交
81 82 83 84 85 86 87 88 89
        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
90 91
        end

M
Matthew Draper 已提交
92 93 94
        def row_num_literal(order_by)
          RowNumber.new order_by
        end
95

M
Matthew Draper 已提交
96 97 98
        def select_count?(x)
          x.projections.length == 1 && Arel::Nodes::Count === x.projections.first
        end
99

M
Matthew Draper 已提交
100 101 102 103 104 105 106
        # 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
107 108
        end

M
Matthew Draper 已提交
109 110 111 112 113 114
        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
115
        end
116 117 118
    end
  end
end