debian-mirror-gitlab/lib/gitlab/sql/union.rb

44 lines
1.3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2015-11-26 14:37:03 +05:30
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-03-02 22:35:43 +05:30
# union = Gitlab::SQL::Union.new([user.personal_projects, user.projects])
2015-11-26 14:37:03 +05:30
# sql = union.to_sql
#
# Project.where("id IN (#{sql})")
class Union
2018-03-17 18:26:18 +05:30
def initialize(relations, remove_duplicates: true)
2015-11-26 14:37:03 +05:30
@relations = relations
2018-03-17 18:26:18 +05:30
@remove_duplicates = remove_duplicates
2015-11-26 14:37:03 +05:30
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
2017-01-15 13:20:01 +05:30
@relations.map { |rel| rel.reorder(nil).to_sql }.reject(&:blank?)
2015-11-26 14:37:03 +05:30
end
2018-03-17 18:26:18 +05:30
if fragments.any?
2019-12-26 22:10:19 +05:30
"(" + fragments.join(")\n#{union_keyword}\n(") + ")"
2018-03-17 18:26:18 +05:30
else
'NULL'
end
end
def union_keyword
@remove_duplicates ? 'UNION' : 'UNION ALL'
2015-11-26 14:37:03 +05:30
end
end
end
end