Class: RDF::DataObjects::Repository

Inherits:
Repository show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/lib/rdf/do.rb

Overview

RDF::DataObjects::Repository is an RDF::Repository is backed by a DataObjects connection.

Constant Summary

Constants inherited from Repository

Repository::DEFAULT_TX_CLASS

Constants inherited from RDF::Dataset

RDF::Dataset::DEFAULT_GRAPH, RDF::Dataset::ISOLATION_LEVELS

Instance Attribute Summary

Attributes inherited from Repository

#options, #title, #uri

Instance Method Summary collapse

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

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #writable?

Methods included from Readable

#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

Methods included from RDF::Durable

#durable?, #nondurable?

Methods included from Enumerable

add_entailment, #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_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Constructor Details

#initialize(options = {}, &block) ⇒ RDF::DataObjects::Repository

Initializes this repository instance.

Examples:

RDF::DataObjects::Repository.new  # => new Repository based on sqlite3://:memory:
RDF::DataObjects::Repository.new uri: 'postgres://localhost/database'
  => New repository based on postgres adapter

Parameters:

Options Hash (options):

  • :uri (URI, #to_s) — default: nil
  • :title (String, #to_s) — default: nil
  • :adaptor (String, #to_s) — default: nil

    The default adapter will be loaded based on the URI scheme of the created connection.

  • :db (String)

    Synonym for :uri



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/lib/rdf/do.rb', line 42

def initialize(options = {}, &block)
  warn "[DEPRECATION] RDF::DataObjects::Repository#initialize expects a uri argument. Called from #{Gem.location_of_caller.join(':')}" unless options.is_a?(Hash)
  options = {uri: options.to_s} unless options.is_a?(Hash)
  db = options[:uri] || options[:db] || 'sqlite3://:memory:'
  @db = ::DataObjects::Connection.new(db)

  adapter = options[: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(options, &block)
rescue Exception => e
  raise LoadError, "Could not load a DataObjects adapter for #{options}.  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.message}"
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/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/lib/rdf/do.rb', line 80

def close
  @db.close
  @adapter = nil
end

#countInteger

The number of statements in this repository

Returns:

See Also:

  • Enumerable#count


290
291
292
293
294
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Parameters:

See Also:



111
112
113
114
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Yields:

  • statement

Yield Parameters:

Returns:

See Also:

  • Enumerable#each


211
212
213
214
215
216
217
218
219
220
221
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Yields:

  • graph

Yield Parameters:

Returns:

See Also:



275
276
277
278
279
280
281
282
283
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Yields:

  • object

Yield Parameters:

Returns:

See Also:



260
261
262
263
264
265
266
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Yields:

  • predicate

Yield Parameters:

Returns:

See Also:



245
246
247
248
249
250
251
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Yields:

  • subject

Yield Parameters:

Returns:

See Also:



230
231
232
233
234
235
236
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Returns:

  • (Boolean)

See Also:

  • Enumerable#empty


90
91
92
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Parameters:



186
187
188
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Parameters:

Raises:

  • (ArgumentError)

See Also:

  • Mutable#insert_statement


100
101
102
103
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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

Parameters:

Raises:

  • (ArgumentError)

See Also:

  • Mutable#insert_statements


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/lib/rdf/do.rb', line 310

def query_pattern(pattern, options = {}, &block)
  return enum_for(:query_pattern, pattern, options) 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.

Parameters:



199
200
201
202
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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.

Parameters:

Returns:

See Also:



151
152
153
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/lib/rdf/do.rb', line 151

def serialize(value)
  value.nil? ? 'nil' : RDF::NTriples::Writer.serialize(value)
end

#supports?(feature) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Mutable#insert_statement


59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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'.

Parameters:

Returns:

See Also:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-do-9c1e0ad28879/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