Class: Redlander::Query::Results Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redlander/query/results.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(q, options = {}) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

The returned value is determined by the type of the query:

  • Boolean

    for SPARQL ASK queries (ignores block, if given)

  • Redlander::Model

    for SPARQL CONSTRUCT queries

    if given a block, yields the constructed statements to it instead

  • Array<Hash>

    for SPARQL SELECT queries

    where hash values are Redlander::Node instances; if given a block, yields each binding hash to it

  • nil, if query fails

Query the model RDF graph using a query language

Parameters:

  • q (String)

    the text of the query

  • options (Hash<Symbol => [String, URI]>) (defaults to: {})

    options for the query

Options Hash (options):

  • :language (String)

    language of the query, one of:

    • “sparql10” SPARQL 1.0 W3C RDF Query Language (default)

    • “sparql” SPARQL 1.1 (DRAFT) Query and Update Languages

    • “sparql11-query” SPARQL 1.1 (DRAFT) Query Language

    • “sparql11-update” SPARQL 1.1 (DRAFT) Update Language

    • “laqrs” LAQRS adds to Querying RDF in SPARQL

    • “rdql” RDF Data Query Language (RDQL)

  • :language_uri (String)

    URI of the query language, if applicable

  • :base_uri (String)

    base URI of the query, if applicable

Raises:



17
18
19
20
21
22
23
24
25
26
# File 'lib/redlander/query/results.rb', line 17

def initialize(q, options = {})
  language = options[:language] || "sparql10"
  language_uri = options[:language_uri] && options[:language_uri].to_s
  base_uri = options[:base_uri] && options[:base_uri].to_s

  @rdf_query = Redland.librdf_new_query(Redlander.rdf_world, language, language_uri, q, base_uri)
  raise RedlandError, "Failed to create a #{language.upcase} query from '#{q}'" if @rdf_query.null?

  ObjectSpace.define_finalizer(self, self.class.send(:finalize_query, @rdf_query))
end

Instance Method Details

#bindings?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


72
73
74
# File 'lib/redlander/query/results.rb', line 72

def bindings?
  !Redland.librdf_query_results_is_bindings(@rdf_results).zero?
end

#boolean?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


76
77
78
# File 'lib/redlander/query/results.rb', line 76

def boolean?
  !Redland.librdf_query_results_is_boolean(@rdf_results).zero?
end

#eachObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
65
66
67
68
69
70
# File 'lib/redlander/query/results.rb', line 61

def each
  if block_given?
    while Redland.librdf_query_results_finished(@rdf_results).zero?
      yield self
      Redland.librdf_query_results_next(@rdf_results)
    end
  else
    enum_for(:each)
  end
end

#graph?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


80
81
82
# File 'lib/redlander/query/results.rb', line 80

def graph?
  !Redland.librdf_query_results_is_graph(@rdf_results).zero?
end

#process(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/redlander/query/results.rb', line 28

def process(model)
  @rdf_results = Redland.librdf_model_query_execute(model.rdf_model, @rdf_query)

  begin
    case
    when bindings?
      if block_given?
        return nil if @rdf_results.null?
        each { yield process_bindings }
      else
        return [] if @rdf_results.null?
        map { process_bindings }
      end
    when boolean?
      return nil if @rdf_results.null?
      process_boolean
    when graph?
      return nil if @rdf_results.null?
      if block_given?
        process_graph { |statement| yield statement }
      else
        process_graph
      end
    when syntax?
      process_syntax
    else
      raise RedlandError, "Cannot determine the type of query results"
    end
  ensure
    Redland.librdf_free_query_results(@rdf_results)
  end
end

#syntax?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


84
85
86
# File 'lib/redlander/query/results.rb', line 84

def syntax?
  !Redland.librdf_query_results_is_syntax(@rdf_results).zero?
end