summaryrefslogtreecommitdiff
path: root/app/models/concerns/from_set_operator.rb
blob: 593fd251c5c38c562828c892eb1aa5436451237b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

module FromSetOperator
  # Define a high level method to more easily work with the SQL set operations
  # of UNION, INTERSECT, and EXCEPT as defined by Gitlab::SQL::Union,
  # Gitlab::SQL::Intersect, and Gitlab::SQL::Except respectively.
  def define_set_operator(operator)
    method_name = 'from_' + operator.name.demodulize.downcase
    method_name = method_name.to_sym

    raise "Trying to redefine method '#{method(method_name)}'" if methods.include?(method_name)

    define_method(method_name) do |members, remove_duplicates: true, alias_as: table_name|
      operator_sql = operator.new(members, remove_duplicates: remove_duplicates).to_sql

      from(Arel.sql("(#{operator_sql}) #{alias_as}"))
    end
  end
end