summaryrefslogtreecommitdiff
path: root/lib/gitlab/pagination/keyset/in_operator_optimization/strategies/record_loader_strategy.rb
blob: b12c33d6e51d211be22597545b44c081b4d853e5 (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
# frozen_string_literal: true

module Gitlab
  module Pagination
    module Keyset
      module InOperatorOptimization
        module Strategies
          class RecordLoaderStrategy
            RECORDS_COLUMN = 'records'

            def initialize(finder_query, model, order_by_columns)
              @finder_query = finder_query
              @order_by_columns = order_by_columns
              @table_name = model.table_name
            end

            def initializer_columns
              ["NULL::#{table_name} AS #{RECORDS_COLUMN}"]
            end

            def columns
              query = finder_query
                .call(*order_by_columns.array_lookup_expressions_by_position(QueryBuilder::RECURSIVE_CTE_NAME))
                .select("#{table_name}")
                .limit(1)

              ["(#{query.to_sql})"]
            end

            def final_projections
              ["(#{RECORDS_COLUMN}).*"]
            end

            private

            attr_reader :finder_query, :order_by_columns, :table_name
          end
        end
      end
    end
  end
end