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

array_scope_columns.rb « in_operator_optimization « keyset « pagination « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95afd5a8595d996d38541e959af402033cc300c7 (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 Pagination
    module Keyset
      module InOperatorOptimization
        class ArrayScopeColumns
          ARRAY_SCOPE_CTE_NAME = 'array_cte'

          def initialize(columns)
            validate_columns!(columns)

            array_scope_table = Arel::Table.new(ARRAY_SCOPE_CTE_NAME)
            @columns = columns.map do |column|
              ColumnData.new(column, "array_scope_#{column}", array_scope_table)
            end
          end

          def array_scope_cte_name
            ARRAY_SCOPE_CTE_NAME
          end

          def array_aggregated_columns
            columns.map(&:array_aggregated_column)
          end

          def array_aggregated_column_names
            columns.map(&:array_aggregated_column_name)
          end

          def arel_columns
            columns.map(&:arel_column)
          end

          def array_lookup_expressions_by_position(table_name)
            columns.map do |column|
              Arel.sql("#{table_name}.#{column.array_aggregated_column_name}[position]")
            end
          end

          private

          attr_reader :columns

          def validate_columns!(columns)
            if columns.blank?
              msg = <<~MSG
              No array columns were given.
              Make sure you explicitly select the columns in the array_scope parameter.
              Example: Project.select(:id)
              MSG
              raise StandardError, msg
            end
          end
        end
      end
    end
  end
end