2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-12 10:16:49 -05:00
|
|
|
module Gitlab
|
|
|
|
module SQL
|
|
|
|
# Class for building SQL UNION statements.
|
|
|
|
#
|
|
|
|
# ORDER BYs are dropped from the relations as the final sort order is not
|
|
|
|
# guaranteed any way.
|
|
|
|
#
|
|
|
|
# Example usage:
|
|
|
|
#
|
2019-01-17 09:35:23 -05:00
|
|
|
# union = Gitlab::SQL::Union.new([user.personal_projects, user.projects])
|
2015-11-12 10:16:49 -05:00
|
|
|
# sql = union.to_sql
|
|
|
|
#
|
|
|
|
# Project.where("id IN (#{sql})")
|
|
|
|
class Union
|
2017-10-02 04:14:23 -04:00
|
|
|
def initialize(relations, remove_duplicates: true)
|
2015-11-12 10:16:49 -05:00
|
|
|
@relations = relations
|
2017-10-02 04:14:23 -04:00
|
|
|
@remove_duplicates = remove_duplicates
|
2015-11-12 10:16:49 -05:00
|
|
|
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
|
2016-12-08 20:56:31 -05:00
|
|
|
@relations.map { |rel| rel.reorder(nil).to_sql }.reject(&:blank?)
|
2015-11-12 10:16:49 -05:00
|
|
|
end
|
|
|
|
|
2017-09-07 05:46:58 -04:00
|
|
|
if fragments.any?
|
|
|
|
fragments.join("\n#{union_keyword}\n")
|
|
|
|
else
|
|
|
|
'NULL'
|
|
|
|
end
|
2017-10-02 04:14:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def union_keyword
|
|
|
|
@remove_duplicates ? 'UNION' : 'UNION ALL'
|
2015-11-12 10:16:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|