Module: RDF::DataObjects::Adapters::Defaults

Included in:
Postgres, Sqlite3
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb

Overview

Default SQL statements and methods for RDF::DataObjects::Repository::Adapters.

Instance Method Summary collapse

Instance Method Details

#count_sqlObject



10
11
12
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 10

def count_sql
  'select count(*) from quads'
end

#delete_sqlObject



18
19
20
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 18

def delete_sql
  'DELETE FROM `quads` where (subject = ? AND predicate = ? AND object = ? AND context = ?)'
end

#each_graph_sqlObject



38
39
40
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 38

def each_graph_sql
  'select distinct context from quads'
end

#each_object_sqlObject



34
35
36
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 34

def each_object_sql
  'select distinct object from quads'
end

#each_predicate_sqlObject



30
31
32
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 30

def each_predicate_sql
  'select distinct predicate from quads'
end

#each_sqlObject



22
23
24
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 22

def each_sql
  'select * from quads'
end

#each_subject_sqlObject



26
27
28
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 26

def each_subject_sql
  'select distinct subject from quads'
end

#insert_sqlObject



14
15
16
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 14

def insert_sql
  'REPLACE INTO `quads` (subject, predicate, object, context) VALUES (?, ?, ?, ?)'
end

#query(repository, hash = {}) ⇒ DataObjects::Result

Perform a query on an RDF::DataObjects::Repository based on a hash of components.

This is meant to be called by RDF::DataObjects::Repository.

Supports symbols and RDF::Query::Variable values as a wild-card for a non-nil value.

Supports false for a specifically nil value representing the default context.

Examples:

adapter.query(repository, predicate: predicate)

Returns:

  • (DataObjects::Result)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-do-a92b866a7db7/lib/rdf/do/adapters/defaults.rb', line 54

def query(repository, hash = {})
  return repository.result(each_sql) if hash.empty?
  conditions = []
  params = []
  [:subject, :predicate, :object, :graph_name].each do |resource|
    do_resource = resource == :graph_name ? :context : resource
    unless hash[resource].nil?
      case hash[resource]
      when Symbol, RDF::Query::Variable
        conditions << "#{do_resource} != 'nil'"
        next
      when false
        conditions << "#{do_resource} = 'nil'"
        next
      else
        conditions << "#{do_resource} = ?"
      end
      params     << repository.serialize(hash[resource])
    end
  end
  where = conditions.empty? ? "" : "WHERE "
  where << conditions.join(' AND ')
  #puts "query: #{where.inspect}, #{params.inspect}"
  repository.result('select * from quads ' + where, *params)
end