Module: Tripod::CriteriaExecution
- Extended by:
- ActiveSupport::Concern
- Included in:
- Criteria
- Defined in:
- lib/tripod/criteria/execution.rb
Overview
this module provides execution methods to a criteria object
Instance Method Summary collapse
-
#as_query(opts = {}) ⇒ Object
turn this criteria into a query.
-
#count(opts = {}) ⇒ Object
Return how many records the current criteria would return.
-
#first(opts = {}) ⇒ Object
Execute the query and return the first result as a hydrated resource.
-
#resources(opts = {}) ⇒ Object
Execute the query and return a
ResourceCollection
of all hydrated resourcesResourceCollection
is anEnumerable
, Array-like object. -
#serialize(opts = {}) ⇒ Object
run a query to get the raw serialisation of the results of this criteria object.
Instance Method Details
#as_query(opts = {}) ⇒ Object
turn this criteria into a query
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tripod/criteria/execution.rb', line 56 def as_query(opts={}) Tripod.logger.debug("TRIPOD: building select query for criteria...") return_graph = opts.has_key?(:return_graph) ? opts[:return_graph] : true Tripod.logger.debug("TRIPOD: with return_graph: #{return_graph.inspect}") select_query = "SELECT DISTINCT ?uri " if graph_lambdas.empty? if return_graph # if we are returning the graph, select it as a variable, and include either the <graph> or ?graph in the where clause if graph_uri select_query += "(<#{graph_uri}> as ?graph) WHERE { GRAPH <#{graph_uri}> { " else select_query += "?graph WHERE { GRAPH ?graph { " end else select_query += "WHERE { " # if we're not returning the graph, only restrict by the <graph> if there's one set at class level select_query += "GRAPH <#{graph_uri}> { " if graph_uri end select_query += self.query_where_clauses.join(" . ") select_query += " } " select_query += "} " if return_graph || graph_uri # close the graph clause else # whip through the graph lambdas and add into the query # we have multiple graphs so the above does not apply select_query += "WHERE { " graph_lambdas.each do |lambda_item| select_query += "GRAPH ?g { " select_query += lambda_item.call select_query += " } " end select_query += self.query_where_clauses.join(" . ") select_query += " } " end select_query += self.extra_clauses.join(" ") select_query += [order_clause, limit_clause, offset_clause].join(" ") select_query.strip end |
#count(opts = {}) ⇒ Object
Return how many records the current criteria would return
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tripod/criteria/execution.rb', line 43 def count(opts={}) sq = Tripod::SparqlQuery.new(self.as_query(opts)) count_sparql = sq.as_count_query_str result = Tripod::SparqlClient::Query.select(count_sparql) if result.length > 0 result[0]["tripod_count_var"]["value"].to_i else return 0 end end |
#first(opts = {}) ⇒ Object
Execute the query and return the first result as a hydrated resource
34 35 36 37 38 |
# File 'lib/tripod/criteria/execution.rb', line 34 def first(opts={}) sq = Tripod::SparqlQuery.new(self.as_query(opts)) first_sparql = sq.as_first_query_str self.resource_class._resources_from_sparql(first_sparql).first end |
#resources(opts = {}) ⇒ Object
Execute the query and return a ResourceCollection
of all hydrated resources ResourceCollection
is an Enumerable
, Array-like object.
13 14 15 16 17 18 19 20 |
# File 'lib/tripod/criteria/execution.rb', line 13 def resources(opts={}) Tripod::ResourceCollection.new( self.resource_class._resources_from_sparql(self.as_query(opts)), # pass in the criteria that was used to generate this collection, as well as whether the user specified return graph :return_graph => (opts.has_key?(:return_graph) ? opts[:return_graph] : true), :criteria => self ) end |
#serialize(opts = {}) ⇒ Object
run a query to get the raw serialisation of the results of this criteria object.
26 27 28 29 |
# File 'lib/tripod/criteria/execution.rb', line 26 def serialize(opts={}) select_sparql = self.as_query(:return_graph => opts[:return_graph]) self.resource_class._raw_describe_select_results(select_sparql, :accept_header => opts[:accept_header]) # note that this method defaults to using application/n-triples. end |