Class: Tripod::SparqlQuery
- Inherits:
-
Object
- Object
- Tripod::SparqlQuery
- Defined in:
- lib/tripod/sparql_query.rb
Constant Summary collapse
- @@PREFIX_KEYWORDS =
%w(BASE PREFIX)
- @@KEYWORDS =
%w(CONSTRUCT ASK DESCRIBE SELECT)
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
the body of the query.
-
#prefixes ⇒ Object
readonly
any prefixes the query may have.
-
#query ⇒ Object
readonly
the original query string.
-
#query_type ⇒ Object
readonly
symbol representing the type (:select, :ask etc).
Class Method Summary collapse
Instance Method Summary collapse
- #as_count_query_str ⇒ Object
- #as_first_query_str ⇒ Object
- #check_subqueryable! ⇒ Object
- #extract_prefixes ⇒ Object
- #has_prefixes? ⇒ Boolean
-
#initialize(query_string, interpolations = nil) ⇒ SparqlQuery
constructor
A new instance of SparqlQuery.
Constructor Details
#initialize(query_string, interpolations = nil) ⇒ SparqlQuery
Returns a new instance of SparqlQuery.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tripod/sparql_query.rb', line 18 def initialize(query_string, interpolations=nil) query_string.strip! @query = interpolate_query(query_string, interpolations) if interpolations @query ||= query_string if self.has_prefixes? @prefixes, @body = self.extract_prefixes else @body = self.query end @query_type = get_query_type end |
Instance Attribute Details
#body ⇒ Object (readonly)
the body of the query
10 11 12 |
# File 'lib/tripod/sparql_query.rb', line 10 def body @body end |
#prefixes ⇒ Object (readonly)
any prefixes the query may have
11 12 13 |
# File 'lib/tripod/sparql_query.rb', line 11 def prefixes @prefixes end |
#query ⇒ Object (readonly)
the original query string
8 9 10 |
# File 'lib/tripod/sparql_query.rb', line 8 def query @query end |
#query_type ⇒ Object (readonly)
symbol representing the type (:select, :ask etc)
9 10 11 |
# File 'lib/tripod/sparql_query.rb', line 9 def query_type @query_type end |
Class Method Details
.get_expected_variables(query_string) ⇒ Object
75 76 77 |
# File 'lib/tripod/sparql_query.rb', line 75 def self.get_expected_variables(query_string) query_string.scan(/[.]?\%\{(\w+)\}[.]?/).flatten.uniq.map(&:to_sym) end |
Instance Method Details
#as_count_query_str ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tripod/sparql_query.rb', line 53 def as_count_query_str check_subqueryable! count_query = "SELECT (COUNT(*) as ?tripod_count_var) { #{self.body} }" count_query = "#{self.prefixes} #{count_query}" if self.prefixes # just returns the string representing the count query for this query. count_query end |
#as_first_query_str ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/tripod/sparql_query.rb', line 65 def as_first_query_str check_subqueryable! first_query = "SELECT * { #{self.body} } LIMIT 1" first_query = "#{self.prefixes} #{first_query}" if self.prefixes # just returns the string representing the 'first' query for this query. first_query end |
#check_subqueryable! ⇒ Object
48 49 50 51 |
# File 'lib/tripod/sparql_query.rb', line 48 def check_subqueryable! # only allow for selects raise SparqlQueryError.new("Can't turn this into a subquery") unless self.query_type == :select end |
#extract_prefixes ⇒ Object
41 42 43 44 45 46 |
# File 'lib/tripod/sparql_query.rb', line 41 def extract_prefixes i = self.class.KEYWORDS.map {|k| self.query.index(/#{k}/i) || self.query.size+1 }.min p = query[0..i-1] b = query[i..-1] return p.strip, b.strip end |
#has_prefixes? ⇒ Boolean
34 35 36 37 38 39 |
# File 'lib/tripod/sparql_query.rb', line 34 def has_prefixes? self.class.PREFIX_KEYWORDS.each do |k| return true if /^#{k}/i.match(query) end return false end |