Class: RDF::DataObjects::Repository
- Inherits:
-
Repository
- Object
- RDF::Dataset
- Repository
- RDF::DataObjects::Repository
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb
Overview
RDF::DataObjects::Repository is an RDF::Repository is backed by a DataObjects connection.
Constant Summary
Constants inherited from Repository
Constants inherited from RDF::Dataset
RDF::Dataset::DEFAULT_GRAPH, RDF::Dataset::ISOLATION_LEVELS
Instance Attribute Summary
Attributes inherited from Repository
Attributes included from Enumerable
Instance Method Summary collapse
-
#close
Close this connection.
-
#count ⇒ Integer
The number of statements in this repository.
-
#delete_statement(statement)
Delete a single statement from this repository.
-
#dispose
Close and dispose of this connection.
-
#each {|statement| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Statements in this repository.
-
#each_graph {|graph| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Resource graph names in this repository.
-
#each_object {|object| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Value objects in this repository.
-
#each_predicate {|predicate| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Resource predicates in this repository.
-
#each_subject {|subject| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Resource subjects in this repository.
-
#empty? ⇒ Boolean
Returns true if this repository is empty.
-
#exec(sql, *args) ⇒ Object
Execute the given non-query SQL with the given arguments against this repository's DataObjects::Connection.
-
#initialize(options = {}, &block) ⇒ RDF::DataObjects::Repository
constructor
Initializes this repository instance.
-
#insert_statement(statement)
Insert a single statement into this repository.
-
#insert_statements(statements)
Insert multiple statements into this repository.
-
#query_pattern(pattern, options = {}, &block) ⇒ Object
protected
Implementation of RDF::Queryable#query_pattern This implementation will do well for statements and hashes, and not so well for RDF::Query objects.
-
#result(sql, *args) ⇒ Object
Execute the given query SQL with the given arguments against this repository's DataObjects::Connection.
-
#serialize(value) ⇒ String
Serialize an RDF::Value into N-triples format.
- #supports?(feature) ⇒ Boolean
-
#unserialize(value) ⇒ RDF::Value
Unserialize an RDF::Value from N-triples format.
Methods inherited from Repository
#delete_insert, #isolation_level, load, #snapshot
Methods included from Transactable
#begin_transaction, #commit_transaction, #rollback_transaction, #transaction
Methods included from Mutable
#<<, add_entailment, #apply_changeset, #clear, #delete, #delete_insert, #delete_statements, #entail, #entail!, #immutable?, #insert, #load, #method_missing, #mutable?, #respond_to_missing?, #snapshot, #update
Methods included from Util::Aliasing::LateBound
Methods included from Util::Coercions
Methods included from Writable
#<<, #insert, #insert_graph, #insert_reader, #writable?
Methods included from Readable
Methods inherited from RDF::Dataset
#inspect, #inspect!, #isolation_level
Methods included from Queryable
#concise_bounded_description, #first, #first_literal, #first_object, #first_predicate, #first_subject, #first_value, #lint, #query, #query_execute, #query_without_sparql, #to_sparql
Methods included from RDF::Durable
Methods included from Enumerable
add_entailment, #canonicalize, #canonicalize!, #dump, #each_quad, #each_statement, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?, #validate!
Methods included from Isomorphic
#bijection_to, #isomorphic_with?
Constructor Details
#initialize(options = {}, &block) ⇒ RDF::DataObjects::Repository
Initializes this repository instance.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 42 def initialize( = {}, &block) warn "[DEPRECATION] RDF::DataObjects::Repository#initialize expects a uri argument. Called from #{Gem.location_of_caller.join(':')}" unless .is_a?(Hash) = {uri: .to_s} unless .is_a?(Hash) db = [:uri] || [:db] || 'sqlite3://:memory:' @db = ::DataObjects::Connection.new(db) adapter = [:adapter] || @db.instance_variable_get("@uri").scheme require 'rdf/do/adapters/' + adapter.to_s @adapter = RDF::DataObjects::Adapters::const_get(adapter.to_s.capitalize) @adapter.migrate? self super(, &block) rescue Exception => e raise LoadError, "Could not load a DataObjects adapter for #{}. You may need to add a 'require do_adapter', or you may be trying to use an unsupported adapter (Currently supporting postgres, sqlite3). The error message was: #{e.}" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RDF::Mutable
Instance Method Details
#close
This method returns an undefined value.
Close this connection.
80 81 82 83 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 80 def close @db.close @adapter = nil end |
#count ⇒ Integer
The number of statements in this repository
290 291 292 293 294 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 290 def count result = result(@adapter.count_sql) result.next! result.values.first end |
#delete_statement(statement)
This method returns an undefined value.
Delete a single statement from this repository.
111 112 113 114 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 111 def delete_statement(statement) query = @adapter.delete_sql exec(query, serialize(statement.subject), serialize(statement.predicate), serialize(statement.object), serialize(statement.graph_name)) end |
#dispose
This method returns an undefined value.
Close and dispose of this connection.
71 72 73 74 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 71 def dispose close @db.dispose end |
#each {|statement| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Statements in this repository.
211 212 213 214 215 216 217 218 219 220 221 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 211 def each(&block) return enum_for(:each) unless block_given? reader = result(@adapter.each_sql) while reader.next! block.call(RDF::Statement.new( subject: unserialize(reader.values[0]), predicate: unserialize(reader.values[1]), object: unserialize(reader.values[2]), graph_name: unserialize(reader.values[3]))) end end |
#each_graph {|graph| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Resource graph names in this repository.
275 276 277 278 279 280 281 282 283 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 275 def each_graph(&block) return enum_for(:each_graph) unless block_given? reader = result(@adapter.each_graph_sql) while reader.next! graph_name = unserialize(reader.values[0]) yield RDF::Graph.new(graph_name: graph_name, data: self) end end |
#each_object {|object| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Value objects in this repository.
260 261 262 263 264 265 266 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 260 def each_object(&block) return enum_for(:each_object) unless block_given? reader = result(@adapter.each_object_sql) while reader.next! block.call(unserialize(reader.values[0])) end end |
#each_predicate {|predicate| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Resource predicates in this repository.
245 246 247 248 249 250 251 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 245 def each_predicate(&block) return enum_for(:each_predicate) unless block_given? reader = result(@adapter.each_predicate_sql) while reader.next! block.call(unserialize(reader.values[0])) end end |
#each_subject {|subject| ... } ⇒ Enumerable::Enumerator, void
Iterate over all RDF::Resource subjects in this repository.
230 231 232 233 234 235 236 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 230 def each_subject(&block) return enum_for(:each_subject) unless block_given? reader = result(@adapter.each_subject_sql) while reader.next! block.call(unserialize(reader.values[0])) end end |
#empty? ⇒ Boolean
Returns true if this repository is empty.
90 91 92 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 90 def empty? count == 0 end |
#exec(sql, *args) ⇒ Object
Execute the given non-query SQL with the given arguments against this repository's DataObjects::Connection.
If the given sql is in a prepared statement format, it will be executed with the given *args.
186 187 188 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 186 def exec(sql, *args) @db.create_command(sql).execute_non_query(*args) end |
#insert_statement(statement)
This method returns an undefined value.
Insert a single statement into this repository.
100 101 102 103 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 100 def insert_statement(statement) raise ArgumentError, "Statement #{statement.inspect} is incomplete" if statement.incomplete? insert_statements [statement] end |
#insert_statements(statements)
This method returns an undefined value.
Insert multiple statements into this repository
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 122 def insert_statements(statements) raise ArgumentError, "Some statement is incomplete" if statements.any?(&:incomplete?) if @adapter.respond_to?(:multiple_insert_sql) each = statements.respond_to?(:each_statement) ? :each_statement : :each args = [] count = 0 statements.__send__(each) do |s| count += 1 args += [serialize(s.subject),serialize(s.predicate), serialize(s.object), serialize(s.graph_name)] end query = @adapter.multiple_insert_sql(count) exec(query,*(args.flatten)) else query = @adapter.insert_sql each = statements.respond_to?(:each_statement) ? :each_statement : :each statements.__send__(each) do |s| exec(query, serialize(s.subject),serialize(s.predicate), serialize(s.object), serialize(s.graph_name)) end end end |
#query_pattern(pattern, options = {}, &block) ⇒ Object (protected)
Implementation of RDF::Queryable#query_pattern
This implementation will do well for statements and hashes, and not so well for RDF::Query objects.
Accepts a query pattern argument as in RDF::Queryable. See Queryable for more information.
310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 310 def query_pattern(pattern, = {}, &block) return enum_for(:query_pattern, pattern, ) unless block_given? @nodes = {} # reset cache. FIXME this should probably be in Node.intern reader = @adapter.query(self, pattern.to_h) while reader.next! yield RDF::Statement.new( subject: unserialize(reader.values[0]), predicate: unserialize(reader.values[1]), object: unserialize(reader.values[2]), graph_name: unserialize(reader.values[3])) end end |
#result(sql, *args) ⇒ Object
Execute the given query SQL with the given arguments against this repository's DataObjects::Connection.
If the given sql is in a prepared statement format, it will be executed with the given *args.
199 200 201 202 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 199 def result(sql, *args) @nodes = {} # reset cache. FIXME this should probably be in Node.intern @db.create_command(sql).execute_reader(*args) end |
#serialize(value) ⇒ String
Serialize an RDF::Value into N-triples format. Nil values will be encoded as 'nil' to avoid problems with SQL implementations considering null values as distinct from one another.
151 152 153 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 151 def serialize(value) value.nil? ? 'nil' : RDF::NTriples::Writer.serialize(value) end |
#supports?(feature) ⇒ Boolean
59 60 61 62 63 64 65 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 59 def supports?(feature) case feature.to_sym when :graph_name then true when :literal_equality then true else false end end |
#unserialize(value) ⇒ RDF::Value
Unserialize an RDF::Value from N-triples format. Expects nil values to be encoded as 'nil'.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do.rb', line 162 def unserialize(value) result = value == 'nil' ? nil : RDF::NTriples::Reader.unserialize(value) case result when RDF::URI RDF::URI.intern(result) when RDF::Node # This should probably be done in RDF::Node.intern id = result.id.to_s @nodes ||= {} @nodes[id] ||= RDF::Node.new(id) else result end end |