debian-mirror-gitlab/lib/gitlab/graphql/connections/keyset/query_builder.rb

68 lines
2.4 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
module Gitlab
module Graphql
module Connections
module Keyset
class QueryBuilder
def initialize(arel_table, order_list, decoded_cursor, before_or_after)
@arel_table, @order_list, @decoded_cursor, @before_or_after = arel_table, order_list, decoded_cursor, before_or_after
if order_list.empty?
raise ArgumentError.new('No ordering scopes have been supplied')
end
end
# Based on whether the main field we're ordering on is NULL in the
# cursor, we can more easily target our query condition.
# We assume that the last ordering field is unique, meaning
# it will not contain NULLs.
# We currently only support two ordering fields.
#
# Example of the conditions for
# relation: Issue.order(relative_position: :asc).order(id: :asc)
# after cursor: relative_position: 1500, id: 500
#
# when cursor[relative_position] is not NULL
#
# ("issues"."relative_position" > 1500)
# OR (
# "issues"."relative_position" = 1500
# AND
# "issues"."id" > 500
# )
# OR ("issues"."relative_position" IS NULL)
#
# when cursor[relative_position] is NULL
#
# "issues"."relative_position" IS NULL
# AND
# "issues"."id" > 500
#
def conditions
2020-03-13 15:44:24 +05:30
attr_values = order_list.map { |field| decoded_cursor[field.attribute_name] }
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
if order_list.count == 1 && attr_values.first.nil?
2019-12-26 22:10:19 +05:30
raise Gitlab::Graphql::Errors::ArgumentError.new('Before/after cursor invalid: `nil` was provided as only sortable value')
end
2020-03-13 15:44:24 +05:30
if order_list.count == 1 || attr_values.first.present?
Keyset::Conditions::NotNullCondition.new(arel_table, order_list, attr_values, operators, before_or_after).build
2019-12-26 22:10:19 +05:30
else
2020-03-13 15:44:24 +05:30
Keyset::Conditions::NullCondition.new(arel_table, order_list, attr_values, operators, before_or_after).build
2019-12-26 22:10:19 +05:30
end
end
private
attr_reader :arel_table, :order_list, :decoded_cursor, :before_or_after
def operators
order_list.map { |field| field.operator_for(before_or_after) }
end
end
end
end
end
end