Class: Valkyrie::Sequel::QueryService

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/sequel/query_service.rb

Constant Summary collapse

ACCEPTABLE_UUID =
%r{\A(\{)?([a-fA-F0-9]{4}-?){8}(?(1)\}|)\z}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ QueryService

Returns a new instance of QueryService.



7
8
9
# File 'lib/valkyrie/sequel/query_service.rb', line 7

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/valkyrie/sequel/query_service.rb', line 5

def adapter
  @adapter
end

Instance Method Details

#custom_queriesValkyrie::Persistence::CustomQueryContainer

Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service

Returns:

  • (Valkyrie::Persistence::CustomQueryContainer)


91
92
93
# File 'lib/valkyrie/sequel/query_service.rb', line 91

def custom_queries
  @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self)
end

#find_allObject



11
12
13
14
15
# File 'lib/valkyrie/sequel/query_service.rb', line 11

def find_all
  resources.use_cursor.lazy.map do |attributes|
    resource_factory.to_resource(object: attributes)
  end
end

#find_all_of_model(model:) ⇒ Object



26
27
28
29
30
# File 'lib/valkyrie/sequel/query_service.rb', line 26

def find_all_of_model(model:)
  resources.where(internal_resource: model.to_s).map do |attributes|
    resource_factory.to_resource(object: attributes)
  end
end

#find_by(id:) ⇒ Object

Raises:

  • (Valkyrie::Persistence::ObjectNotFoundError)


17
18
19
20
21
22
23
24
# File 'lib/valkyrie/sequel/query_service.rb', line 17

def find_by(id:)
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
  validate_id(id)
  raise Valkyrie::Persistence::ObjectNotFoundError unless ACCEPTABLE_UUID.match?(id.to_s)
  attributes = resources.first(id: id.to_s)
  raise Valkyrie::Persistence::ObjectNotFoundError unless attributes
  resource_factory.to_resource(object: attributes)
end

#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource

Find and a record using a Valkyrie ID for an alternate ID, and construct

a Valkyrie Resource

Parameters:

  • alternate_identifier (Valkyrie::ID)

Returns:

  • (Valkyrie::Resource)


69
70
71
72
73
74
# File 'lib/valkyrie/sequel/query_service.rb', line 69

def find_by_alternate_identifier(alternate_identifier:)
  alternate_identifier = Valkyrie::ID.new(alternate_identifier.to_s) if alternate_identifier.is_a?(String)
  validate_id(alternate_identifier)
  internal_array = { alternate_ids: [{ id: alternate_identifier.to_s }] }
  run_query(find_inverse_references_query, internal_array.to_json).first || raise(Valkyrie::Persistence::ObjectNotFoundError)
end

#find_inverse_references_by(resource: nil, id: nil, property:) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
# File 'lib/valkyrie/sequel/query_service.rb', line 57

def find_inverse_references_by(resource: nil, id: nil, property:)
  raise ArgumentError, "Provide resource or id" unless resource || id
  ensure_persisted(resource) if resource
  id ||= resource.id
  internal_array = { property => [id: id.to_s] }
  run_query(find_inverse_references_query, internal_array.to_json)
end

#find_many_by_ids(ids:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/valkyrie/sequel/query_service.rb', line 32

def find_many_by_ids(ids:)
  ids = ids.map do |id|
    id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
    validate_id(id)
    id.to_s
  end
  ids = ids.select do |id|
    ACCEPTABLE_UUID.match?(id)
  end

  resources.where(id: ids).map do |attributes|
    resource_factory.to_resource(object: attributes)
  end
end

#find_members(resource:, model: nil) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/valkyrie/sequel/query_service.rb', line 76

def find_members(resource:, model: nil)
  return [] if resource.id.blank?
  if model
    run_query(find_members_with_type_query, resource.id.to_s, model.to_s)
  else
    run_query(find_members_query, resource.id.to_s)
  end
end

#find_parents(resource:) ⇒ Object



85
86
87
# File 'lib/valkyrie/sequel/query_service.rb', line 85

def find_parents(resource:)
  find_inverse_references_by(resource: resource, property: :member_ids)
end

#find_references_by(resource:, property:) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/valkyrie/sequel/query_service.rb', line 47

def find_references_by(resource:, property:)
  return [] if resource.id.blank? || resource[property].blank?
  # only return ordered if needed to avoid performance penalties
  if ordered_property?(resource: resource, property: property)
    run_query(find_ordered_references_query, property.to_s, resource.id.to_s)
  else
    run_query(find_references_query, property.to_s, resource.id.to_s)
  end
end

#run_query(query, *args) ⇒ Object



95
96
97
98
99
# File 'lib/valkyrie/sequel/query_service.rb', line 95

def run_query(query, *args)
  connection[query, *args].map do |result|
    resource_factory.to_resource(object: result)
  end
end