Class: Valkyrie::Sequel::QueryService
- Inherits:
-
Object
- Object
- Valkyrie::Sequel::QueryService
- 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
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
-
#count_all_of_model(model:) ⇒ Object
Count all records for a specific resource type.
-
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service.
- #find_all ⇒ Object
- #find_all_of_model(model:) ⇒ Object
- #find_by(id:) ⇒ Object
-
#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.
- #find_inverse_references_by(resource: nil, id: nil, model: nil, property:) ⇒ Object
- #find_many_by_ids(ids:) ⇒ Object
- #find_members(resource:, model: nil) ⇒ Object
- #find_parents(resource:) ⇒ Object
- #find_references_by(resource:, property:, model: nil) ⇒ Object
-
#initialize(adapter:) ⇒ QueryService
constructor
A new instance of QueryService.
- #run_query(query, *args) ⇒ Object
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
#adapter ⇒ Object (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
#count_all_of_model(model:) ⇒ Object
Count all records for a specific resource type
35 36 37 |
# File 'lib/valkyrie/sequel/query_service.rb', line 35 def count_all_of_model(model:) resources.where(internal_resource: model.to_s).count end |
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service
102 103 104 |
# File 'lib/valkyrie/sequel/query_service.rb', line 102 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Object
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
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
80 81 82 83 84 85 |
# File 'lib/valkyrie/sequel/query_service.rb', line 80 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, model: nil, property:) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/valkyrie/sequel/query_service.rb', line 64 def find_inverse_references_by(resource: nil, id: nil, model: 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] } if model run_query(find_inverse_references_with_model_query, internal_array.to_json, model.to_s) else run_query(find_inverse_references_query, internal_array.to_json) end end |
#find_many_by_ids(ids:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/valkyrie/sequel/query_service.rb', line 39 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
87 88 89 90 91 92 93 94 |
# File 'lib/valkyrie/sequel/query_service.rb', line 87 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
96 97 98 |
# File 'lib/valkyrie/sequel/query_service.rb', line 96 def find_parents(resource:) find_inverse_references_by(resource: resource, property: :member_ids) end |
#find_references_by(resource:, property:, model: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/valkyrie/sequel/query_service.rb', line 54 def find_references_by(resource:, property:, model: nil) return [] if resource.id.blank? || resource[property].blank? # only return ordered if needed to avoid performance penalties if ordered_property?(resource: resource, property: property) find_ordered_references_by(resource: resource, property: property, model: model) else find_unordered_references_by(resource: resource, property: property, model: model) end end |
#run_query(query, *args) ⇒ Object
106 107 108 109 110 |
# File 'lib/valkyrie/sequel/query_service.rb', line 106 def run_query(query, *args) connection[query, *args].map do |result| resource_factory.to_resource(object: result) end end |