Class: Valkyrie::Sequel::QueryService
- Inherits:
-
Object
- Object
- Valkyrie::Sequel::QueryService
- Defined in:
- lib/valkyrie/sequel/query_service.rb
Constant Summary collapse
- ACCEPTABLE_UUID =
ACCEPTABLE_UUID is deprecated and unused. which IDs are acceptable is now decided by the database.
%r{\A(\{)?([a-fA-F0-9]{4}-?){8}(?(1)\}|)\z}.freeze
- DEFAULT_ID_TYPE =
:uuid
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
rubocop:enable Metrics/MethodLength.
-
#find_by(id:) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#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.
11 12 13 |
# File 'lib/valkyrie/sequel/query_service.rb', line 11 def initialize(adapter:) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
9 10 11 |
# File 'lib/valkyrie/sequel/query_service.rb', line 9 def adapter @adapter end |
Instance Method Details
#count_all_of_model(model:) ⇒ Object
Count all records for a specific resource type
52 53 54 |
# File 'lib/valkyrie/sequel/query_service.rb', line 52 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
116 117 118 |
# File 'lib/valkyrie/sequel/query_service.rb', line 116 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Object
15 16 17 18 19 |
# File 'lib/valkyrie/sequel/query_service.rb', line 15 def find_all resources.use_cursor.lazy.map do |attributes| resource_factory.to_resource(object: attributes) end end |
#find_all_of_model(model:) ⇒ Object
rubocop:enable Metrics/MethodLength
43 44 45 46 47 |
# File 'lib/valkyrie/sequel/query_service.rb', line 43 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
rubocop:disable Metrics/MethodLength
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/valkyrie/sequel/query_service.rb', line 22 def find_by(id:) id = Valkyrie::ID.new(id.to_s) if id.is_a?(String) validate_id(id) attributes = nil # Create a nested transaction so it can rollback to a non-essential # savepoint if the query fails, not wrecking upstream transactions. connection.transaction(savepoint: true) do attributes = resources.first(id: id.to_s) end raise Valkyrie::Persistence::ObjectNotFoundError unless attributes resource_factory.to_resource(object: attributes) rescue Sequel::DatabaseError => err case err.cause when PG::InvalidTextRepresentation raise Valkyrie::Persistence::ObjectNotFoundError else raise err end 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
94 95 96 97 98 99 |
# File 'lib/valkyrie/sequel/query_service.rb', line 94 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
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/valkyrie/sequel/query_service.rb', line 78 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
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/valkyrie/sequel/query_service.rb', line 56 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 resources.where(Sequel.lit('(id::varchar) IN ?', ids)).map do |attributes| resource_factory.to_resource(object: attributes) end end |
#find_members(resource:, model: nil) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/valkyrie/sequel/query_service.rb', line 101 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
110 111 112 |
# File 'lib/valkyrie/sequel/query_service.rb', line 110 def find_parents(resource:) find_inverse_references_by(resource: resource, property: :member_ids) end |
#find_references_by(resource:, property:, model: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/valkyrie/sequel/query_service.rb', line 68 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
120 121 122 123 124 |
# File 'lib/valkyrie/sequel/query_service.rb', line 120 def run_query(query, *args) connection[query, *args].map do |result| resource_factory.to_resource(object: result) end end |