Class: PublishMyData::SparqlQuery
- Inherits:
-
Tripod::SparqlQuery
- Object
- Tripod::SparqlQuery
- PublishMyData::SparqlQuery
- Defined in:
- lib/publish_my_data/sparql_query.rb
Constant Summary collapse
- @@reserved_variables =
[:controller, :action, :page, :per_page, :id, :commit ,:utf8, :query]
Instance Attribute Summary collapse
-
#expected_variables ⇒ Object
readonly
tokens that appear in the query.
-
#interpolations ⇒ Object
readonly
interpolations supplied at construct time.
-
#parent_query ⇒ Object
readonly
set if this query originated from another (e.g. pagination or count).
-
#request_format ⇒ Object
readonly
symbol representing the format of the original request.
Instance Method Summary collapse
- #allow_pagination? ⇒ Boolean
- #as_count_query(format = :json) ⇒ Object
-
#as_pagination_query(page, per_page, look_ahead = 0) ⇒ Object
for selects only, turn this into a paginated version.
-
#count ⇒ Object
return the number of results that this query returns (creates and executes a count query behind the scenes).
-
#execute ⇒ Object
executes the query, using the right format parameters (for fuseki) for the query type and request format.
-
#initialize(query_string, opts = {}) ⇒ SparqlQuery
constructor
options :request_format (symbol, e.g. :html, :json ) :parent_query :interpolations => { :a => ‘blah’, :b => ‘bleh’ }.
-
#paginate(page, per_page, look_ahead = 0) ⇒ Object
make a pagination version and execute that.
Constructor Details
#initialize(query_string, opts = {}) ⇒ SparqlQuery
options
:request_format (symbol, e.g. :html, :json )
:parent_query
:interpolations => { :a => ‘blah’, :b => ‘bleh’ }
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/publish_my_data/sparql_query.rb', line 33 def initialize(query_string, opts={}) @opts = opts # save off the original opts @interpolations = (opts[:interpolations] || {}).delete_if{ |k,v| self.class.reserved_variables.include?(k.to_sym) } @expected_variables = self.class.get_expected_variables(query_string) check_reserved_variables! super(query_string, @interpolations) @parent_query = opts[:parent_query] @request_format = opts[:request_format] || :html end |
Instance Attribute Details
#expected_variables ⇒ Object (readonly)
tokens that appear in the query
26 27 28 |
# File 'lib/publish_my_data/sparql_query.rb', line 26 def expected_variables @expected_variables end |
#interpolations ⇒ Object (readonly)
interpolations supplied at construct time
27 28 29 |
# File 'lib/publish_my_data/sparql_query.rb', line 27 def interpolations @interpolations end |
#parent_query ⇒ Object (readonly)
set if this query originated from another (e.g. pagination or count)
25 26 27 |
# File 'lib/publish_my_data/sparql_query.rb', line 25 def parent_query @parent_query end |
#request_format ⇒ Object (readonly)
symbol representing the format of the original request
24 25 26 |
# File 'lib/publish_my_data/sparql_query.rb', line 24 def request_format @request_format end |
Instance Method Details
#allow_pagination? ⇒ Boolean
90 91 92 |
# File 'lib/publish_my_data/sparql_query.rb', line 90 def allow_pagination? self.query_type == :select end |
#as_count_query(format = :json) ⇒ Object
94 95 96 |
# File 'lib/publish_my_data/sparql_query.rb', line 94 def as_count_query(format = :json) PublishMyData::SparqlQuery.new(as_count_query_str, {:request_format => format, :parent_query => self}) # pass in the original query end |
#as_pagination_query(page, per_page, look_ahead = 0) ⇒ Object
for selects only, turn this into a paginated version. Returns a whole new SparqlQuery object.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/publish_my_data/sparql_query.rb', line 99 def as_pagination_query(page, per_page, look_ahead=0) check_subqueryable! limit = per_page + look_ahead offset = per_page * (page-1) # wrap it in a subselect with limit and offset paginated_query = "SELECT * { #{self.body} } LIMIT #{limit} OFFSET #{offset}" # put the prefixes back on the start paginated_query = "#{self.prefixes} #{paginated_query}" if self.prefixes # return the paginated version PublishMyData::SparqlQuery.new(paginated_query, {:request_format => self.request_format, :parent_query => self}) # pass in the original query end |
#count ⇒ Object
return the number of results that this query returns (creates and executes a count query behind the scenes)
85 86 87 88 |
# File 'lib/publish_my_data/sparql_query.rb', line 85 def count result = JSON.parse(self.as_count_query.execute.to_s)["results"]["bindings"] result[0]["tripod_count_var"]["value"].to_i end |
#execute ⇒ Object
executes the query, using the right format parameters (for fuseki) for the query type and request format
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/publish_my_data/sparql_query.rb', line 47 def execute begin case query_type when :select result_str = Tripod::SparqlClient::Query.query(self.query, "*/*", {:output => select_format_str} ) when :ask result_str = Tripod::SparqlClient::Query.query(self.query, "*/*", {:output => ask_format_str} ) when :construct result_str = Tripod::SparqlClient::Query.query(self.query, construct_or_describe_header) when :describe result_str = Tripod::SparqlClient::Query.query(self.query, construct_or_describe_header) else raise SparqlQueryExecutionException.new("Unsupported Query Type. Please enter only SELECT, CONSTRUCT, DESCRIBE or ASK queries.") end rescue Tripod::Errors::BadSparqlRequest => bad_sparql Rails.logger.debug bad_sparql.inspect if self.parent_query # call execute on the parent(this will fail too), but it means that we get the right error for # the user-entered query parent_query.execute else raise SparqlQueryExecutionException.new((bad_sparql)) end end PublishMyData::SparqlQueryResult.new(result_str) end |
#paginate(page, per_page, look_ahead = 0) ⇒ Object
make a pagination version and execute that.
79 80 81 |
# File 'lib/publish_my_data/sparql_query.rb', line 79 def paginate(page, per_page, look_ahead=0) self.as_pagination_query(page, per_page, look_ahead).execute end |