Welcome to mirror list, hosted at ThFree Co, Russian Federation.

set_operator.rb « sql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 59a808eafa99fa6e8515959114a9e7f7cee10aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

module Gitlab
  module SQL
    # Class for building SQL set operator statements (UNION, INTERSECT, and
    # EXCEPT).
    #
    # ORDER BYs are dropped from the relations as the final sort order is not
    # guaranteed any way.
    #
    # remove_order: false option can be used in special cases where the
    # ORDER BY is necessary for the query.
    #
    # Example usage:
    #
    #     union = Gitlab::SQL::Union.new([user.personal_projects, user.projects])
    #     sql   = union.to_sql
    #
    #     Project.where("id IN (#{sql})")
    class SetOperator
      def initialize(relations, remove_duplicates: true, remove_order: true)
        @relations = relations
        @remove_duplicates = remove_duplicates
        @remove_order = remove_order
      end

      def self.operator_keyword
        raise NotImplementedError
      end

      def to_sql
        # Some relations may include placeholders for prepared statements, these
        # aren't incremented properly when joining relations together this way.
        # By using "unprepared_statements" we remove the usage of placeholders
        # (thus fixing this problem), at a slight performance cost.
        fragments = ActiveRecord::Base.connection.unprepared_statement do
          relations.map do |rel|
            remove_order ? rel.reorder(nil).to_sql : rel.to_sql
          end.reject(&:blank?)
        end

        if fragments.any?
          "(" + fragments.join(")\n#{operator_keyword_fragment}\n(") + ")"
        else
          'NULL'
        end
      end

      # UNION [ALL] | INTERSECT [ALL] | EXCEPT [ALL]
      def operator_keyword_fragment
        remove_duplicates ? self.class.operator_keyword : "#{self.class.operator_keyword} ALL"
      end

      private

      attr_reader :relations, :remove_duplicates, :remove_order
    end
  end
end