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

67 lines
2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module SQL
# Class for easily building recursive CTE statements.
#
# Example:
#
# cte = RecursiveCTE.new(:my_cte_name)
# ns = Arel::Table.new(:namespaces)
#
# cte << Namespace.
# where(ns[:parent_id].eq(some_namespace_id))
#
# cte << Namespace.
# from([ns, cte.table]).
# where(ns[:parent_id].eq(cte.table[:id]))
#
# Namespace.with.
# recursive(cte.to_arel).
# from(cte.alias_to(ns))
class RecursiveCTE
attr_reader :table
# name - The name of the CTE as a String or Symbol.
2021-04-29 21:17:54 +05:30
# union_args - The arguments supplied to Gitlab::SQL::Union class when building inner recursive query
def initialize(name, union_args: {})
2017-09-10 17:25:29 +05:30
@table = Arel::Table.new(name)
@queries = []
2021-04-29 21:17:54 +05:30
@union_args = union_args
2017-09-10 17:25:29 +05:30
end
# Adds a query to the body of the CTE.
#
# relation - The relation object to add to the body of the CTE.
def <<(relation)
@queries << relation
end
# Returns the Arel relation for this CTE.
def to_arel
2021-04-29 21:17:54 +05:30
sql = Arel::Nodes::SqlLiteral.new(Union.new(@queries, **@union_args).to_sql)
2017-09-10 17:25:29 +05:30
Arel::Nodes::As.new(table, Arel::Nodes::Grouping.new(sql))
end
# Returns an "AS" statement that aliases the CTE name as the given table
# name. This allows one to trick ActiveRecord into thinking it's selecting
# from an actual table, when in reality it's selecting from a CTE.
#
# alias_table - The Arel table to use as the alias.
def alias_to(alias_table)
2019-07-07 11:18:12 +05:30
Arel::Nodes::As.new(table, Arel::Table.new(alias_table.name.tr('.', '_')))
2017-09-10 17:25:29 +05:30
end
# Applies the CTE to the given relation, returning a new one that will
# query from it.
def apply_to(relation)
relation.except(:where)
.with
.recursive(to_arel)
.from(alias_to(relation.model.arel_table))
end
end
end
end