summaryrefslogtreecommitdiff
path: root/lib/gitlab/graphql/connections/keyset/conditions/not_null_condition.rb
blob: 3b56ddb996d584c286fe3107f46755be1e503860 (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
# frozen_string_literal: true

module Gitlab
  module Graphql
    module Connections
      module Keyset
        module Conditions
          class NotNullCondition < BaseCondition
            def build
              conditions = [first_attribute_condition]

              # If there is only one order field, we can assume it
              # does not contain NULLs, and don't need additional
              # conditions
              unless names.count == 1
                conditions << [second_attribute_condition, final_condition]
              end

              conditions.join
            end

            private

            # ex: "(relative_position > 23)"
            def first_attribute_condition
              <<~SQL
                (#{table_condition(names.first, values.first, operator.first).to_sql})
              SQL
            end

            # ex: " OR (relative_position = 23 AND id > 500)"
            def second_attribute_condition
              condition = <<~SQL
                OR (
                  #{table_condition(names.first, values.first, '=').to_sql}
                  AND
                  #{table_condition(names[1], values[1], operator[1]).to_sql}
                )
              SQL

              condition
            end

            # ex: " OR (relative_position IS NULL)"
            def final_condition
              if before_or_after == :after
                <<~SQL
                  OR (#{table_condition(names.first, nil, 'is_null').to_sql})
                SQL
              end
            end
          end
        end
      end
    end
  end
end