Class: SPARQL::Client::Query
- Inherits:
-
RDF::Query
- Object
- RDF::Query
- SPARQL::Client::Query
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb
Overview
A SPARQL query builder.
Defined Under Namespace
Classes: Filter
Instance Attribute Summary collapse
-
#form ⇒ :select, ...
readonly
The form of the query.
- #options ⇒ Hash{Symbol => Object} readonly
Attributes inherited from RDF::Query
Attributes included from RDF::Enumerable
Class Method Summary collapse
-
.ask(**options) ⇒ Query
Creates a boolean
ASK
query. -
.self.construct(*variables, **options) ⇒ Query
Creates a graph
CONSTRUCT
query. -
.self.describe(*variables, **options) ⇒ Query
Creates a
DESCRIBE
query. -
.self.select(*variables, **options) ⇒ Query
Creates a tuple
SELECT
query.
Instance Method Summary collapse
- #asc(var) ⇒ Query
- #ask ⇒ Query
- #construct(*patterns) ⇒ Query
- #desc(var) ⇒ Query
- #describe(*variables) ⇒ Query
- #distinct(state = true) ⇒ Query
-
#each_solution {|solution| ... } ⇒ Enumerator
Enumerates over each matching query solution.
- #each_statement {|statement| ... } ⇒ Enumerator
- #execute ⇒ Object
-
#expects_statements? ⇒ Boolean
Expects_statements?.
- #false? ⇒ Boolean
- #filter(string) ⇒ Query
- #from(uri) ⇒ Query
- #graph(graph_uri_or_var) ⇒ Query
- #group(*variables) ⇒ Query (also: #group_by)
-
#self.construct(*variables, **options) ⇒ Query
constructor
A new instance of Query.
-
#inspect ⇒ String
Returns a developer-friendly representation of this query.
-
#inspect!
Outputs a developer-friendly representation of this query to
stderr
. - #limit(length) ⇒ Query
-
#minus(*patterns) {|query| ... } ⇒ Query
The block form can be used for more complicated queries, using the
select
form (note, use either block or argument forms, not both):. - #offset(start) ⇒ Query
-
#optional(*patterns) {|query| ... } ⇒ Query
The block form can be used for adding filters:.
- #order(*variables) ⇒ Query (also: #order_by)
- #prefix(val) ⇒ Object
- #reduced(state = true) ⇒ Query
- #result ⇒ Object
- #select(*variables) ⇒ Query
-
#service(endpoint, *patterns, silent: false) {|query| ... } ⇒ Query
Federated Queries via the SERVICE keyword.
- #slice(start, length) ⇒ Query
- #solutions ⇒ Enumerable<RDF::Query::Solution>
-
#to_s ⇒ String
Returns the string representation of this query.
- #true? ⇒ Boolean
-
#union(*patterns) {|query| ... } ⇒ Query
The block form can be used for more complicated queries, using the
select
form (note, use either block or argument forms, not both):. -
#values(*args) ⇒ Object
Specify inline data for a query.
-
#where(*patterns_queries) {|query| ... } ⇒ Query
(also: #whether)
Block form can be used for chaining calls in addition to creating sub-select queries.
Methods inherited from RDF::Query
#+, #<<, #==, Solutions, #apply_graph_name, #bind, #default?, #dup, #empty?, #executable?, execute, #failed?, #matched?, #mergable?, #merge, #named?, #ndvars, #node?, #optimize, #optimize!, #optimize_without_expression!, #pattern, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #rewrite, #to_sparql, #to_sxp_bin, #unnamed?, #valid?, #validate!, #variable?, #variable_count, #variables, #vars
Methods included from RDF::Enumerable
add_entailment, #canonicalize, #canonicalize!, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?, #validate!
Methods included from RDF::Util::Aliasing::LateBound
Methods included from RDF::Isomorphic
#bijection_to, #isomorphic_with?
Methods included from RDF::Countable
Constructor Details
#self.construct(*variables, **options) ⇒ Query
Returns a new instance of Query.
106 107 108 109 110 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 106 def initialize(form = :ask, **, &block) @subqueries = [] @form = form.respond_to?(:to_sym) ? form.to_sym : form.to_s.to_sym super([], **, &block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RDF::Enumerable
Instance Attribute Details
#form ⇒ :select, ... (readonly)
The form of the query.
16 17 18 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 16 def form @form end |
Class Method Details
.ask(**options) ⇒ Query
Creates a boolean ASK
query.
31 32 33 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 31 def self.ask(**) self.new(:ask, **) end |
.self.construct(*variables, **options) ⇒ Query
Creates a graph CONSTRUCT
query.
91 92 93 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 91 def self.construct(*patterns, **) self.new(:construct, **).construct(*patterns) # FIXME end |
.self.describe(*variables, **options) ⇒ Query
Creates a DESCRIBE
query.
73 74 75 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 73 def self.describe(*variables, **) self.new(:describe, **).describe(*variables) end |
.self.select(*variables, **options) ⇒ Query
Creates a tuple SELECT
query.
55 56 57 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 55 def self.select(*variables, **) self.new(:select, **).select(*variables) end |
Instance Method Details
#asc(var) ⇒ Query
266 267 268 269 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 266 def asc(var) ([:order_by] ||= []) << {var => :asc} self end |
#ask ⇒ Query
118 119 120 121 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 118 def ask @form = :ask self end |
#construct(*patterns) ⇒ Query
166 167 168 169 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 166 def construct(*patterns) [:template] = build_patterns(patterns) self end |
#desc(var) ⇒ Query
279 280 281 282 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 279 def desc(var) ([:order_by] ||= []) << {var => :desc} self end |
#describe(*variables) ⇒ Query
152 153 154 155 156 157 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 152 def describe(*variables) @values = variables.map { |var| [var, var.is_a?(RDF::URI) ? var : RDF::Query::Variable.new(var)] } self end |
#distinct(state = true) ⇒ Query
304 305 306 307 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 304 def distinct(state = true) [:distinct] = state self end |
#each_solution {|solution| ... } ⇒ Enumerator
Enumerates over each matching query solution.
740 741 742 743 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 740 def each_solution(&block) @solutions = result super end |
#each_statement {|statement| ... } ⇒ Enumerator
731 732 733 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 731 def each_statement(&block) result.each_statement(&block) end |
#execute ⇒ Object
753 754 755 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 753 def execute raise NotImplementedError end |
#expects_statements? ⇒ Boolean
Returns expects_statements?.
685 686 687 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 685 def expects_statements? [:construct, :describe].include?(form) end |
#false? ⇒ Boolean
717 718 719 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 717 def false? !true? end |
#filter(string) ⇒ Query
699 700 701 702 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 699 def filter(string) (([:filters] ||= []) << Filter.new(string)) if string and not string.empty? self end |
#from(uri) ⇒ Query
178 179 180 181 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 178 def from(uri) [:from] = uri self end |
#graph(graph_uri_or_var) ⇒ Query
327 328 329 330 331 332 333 334 335 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 327 def graph(graph_uri_or_var) [:graph] = case graph_uri_or_var when Symbol then RDF::Query::Variable.new(graph_uri_or_var) when String then RDF::URI(graph_uri_or_var) when RDF::Value then graph_uri_or_var else raise ArgumentError end self end |
#group(*variables) ⇒ Query Also known as: group_by
291 292 293 294 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 291 def group(*variables) [:group_by] = variables self end |
#inspect ⇒ String
Returns a developer-friendly representation of this query.
914 915 916 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 914 def inspect sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s) end |
#inspect!
This method returns an undefined value.
Outputs a developer-friendly representation of this query to stderr
.
905 906 907 908 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 905 def inspect! warn(inspect) self end |
#limit(length) ⇒ Query
355 356 357 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 355 def limit(length) slice(nil, length) end |
#minus(*patterns) {|query| ... } ⇒ Query
The block form can be used for more complicated queries, using the select
form (note, use either block or argument forms, not both):
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 593 def minus(*patterns, &block) [:minuses] ||= [] if block_given? raise ArgumentError, "#minus requires either arguments or a block, not both." unless patterns.empty? # Evaluate calls in a new query instance query = self.class.select case block.arity when 1 then block.call(query) else query.instance_eval(&block) end [:minuses] << query elsif patterns.all? {|p| p.is_a?(SPARQL::Client::Query)} # With argument form, all must be patterns or queries [:minuses] += patterns elsif patterns.all? {|p| p.is_a?(Array)} # With argument form, all must be patterns, or queries [:minuses] << self.class.select.where(*patterns) else raise ArgumentError, "#minus arguments are triple patterns or queries, not both." end self end |
#offset(start) ⇒ Query
344 345 346 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 344 def offset(start) slice(start, nil) end |
#optional(*patterns) {|query| ... } ⇒ Query
The block form can be used for adding filters:
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 427 def optional(*patterns, &block) ([:optionals] ||= []) << build_patterns(patterns) if block_given? # Steal options[:filters] query_filters = [:filters] [:filters] = [] case block.arity when 1 then block.call(self) else instance_eval(&block) end [:optionals].last.concat([:filters]) [:filters] = query_filters end self end |
#order(*variables) ⇒ Query Also known as: order_by
251 252 253 254 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 251 def order(*variables) [:order_by] = variables self end |
#prefix(prefix: uri) ⇒ Query #prefix(string) ⇒ Query
394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 394 def prefix(val) [:prefixes] ||= [] if val.kind_of? String [:prefixes] << val elsif val.kind_of? Hash val.each do |k, v| [:prefixes] << "#{k}: <#{v}>" end else raise ArgumentError, "prefix must be a kind of String or a Hash" end self end |
#reduced(state = true) ⇒ Query
315 316 317 318 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 315 def reduced(state = true) [:reduced] = state self end |
#result ⇒ Object
747 748 749 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 747 def result @result ||= execute end |
#select(*variables) ⇒ Query
136 137 138 139 140 141 142 143 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 136 def select(*variables) @values = if variables.length == 1 && variables.first.is_a?(Hash) variables.to_a else variables.map { |var| [var, RDF::Query::Variable.new(var)] } end self end |
#service(endpoint, *patterns, silent: false) {|query| ... } ⇒ Query
Federated Queries via the SERVICE keyword.
Supports limited use of the SERVICE keyword with an endpoint term, a sequence of patterns, a query, or a block.
The block form can be used for more complicated queries, using the select
form (note, use either block or argument forms, not both):
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 481 def service(endpoint, *patterns, silent: false, &block) service = { endpoint: (endpoint.is_a?(Symbol) ? RDF::Query::Variable.new(endpoint) : endpoint), silent: silent, query: nil } ([:services] ||= []) << service if block_given? raise ArgumentError, "#service requires either arguments or a block, not both." unless patterns.empty? # Evaluate calls in a new query instance query = self.class.select.where case block.arity when 1 then block.call(query) else query.instance_eval(&block) end service[:query] = query elsif patterns.all? {|p| p.is_a?(SPARQL::Client::Query)} # With argument form, all must be patterns or queries raise ArgumentError, "#service arguments are triple patterns or a query, not both." if patterns.length != 1 service[:query] = patterns.first elsif patterns.all? {|p| p.is_a?(Array)} # With argument form, all must be patterns, or queries service[:query] = self.class.select.where(*patterns) else raise ArgumentError, "#service arguments are triple patterns a query, not both." end self end |
#slice(start, length) ⇒ Query
366 367 368 369 370 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 366 def slice(start, length) [:offset] = start.to_i if start [:limit] = length.to_i if length self end |
#solutions ⇒ Enumerable<RDF::Query::Solution>
723 724 725 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 723 def solutions result end |
#to_s ⇒ String
Returns the string representation of this query.
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 761 def to_s buffer = [form.to_s.upcase] case form when :select, :describe only_count = values.empty? && [:count] buffer << 'DISTINCT' if [:distinct] and not only_count buffer << 'REDUCED' if [:reduced] buffer << ((values.empty? and not [:count]) ? '*' : values.map { |v| SPARQL::Client.serialize_value(v[1]) }.join(' ')) if [:count] [:count].each do |var, count| buffer << '( COUNT(' + ([:distinct] ? 'DISTINCT ' : '') + (var.is_a?(String) ? var : "?#{var}") + ') AS ' + (count.is_a?(String) ? count : "?#{count}") + ' )' end end when :construct buffer << '{' buffer += SPARQL::Client.serialize_patterns([:template]) buffer << '}' end buffer << "FROM #{SPARQL::Client.serialize_value([:from])}" if [:from] unless patterns.empty? && form == :describe buffer += self.to_s_ggp.unshift('WHERE') end .fetch(:unions, []).each do |query| buffer += query.to_s_ggp.unshift('UNION') end if [:group_by] buffer << 'GROUP BY' buffer += [:group_by].map { |var| var.is_a?(String) ? var : "?#{var}" } end if [:order_by] buffer << 'ORDER BY' [:order_by].map { |elem| case elem # .order_by({ var1: :asc, var2: :desc}) when Hash elem.each { |key, val| # check provided values if !key.is_a?(Symbol) raise ArgumentError, 'keys of hash argument must be a Symbol' elsif !val.is_a?(Symbol) || (val != :asc && val != :desc) raise ArgumentError, 'values of hash argument must either be `:asc` or `:desc`' end buffer << "#{val == :asc ? 'ASC' : 'DESC'}(?#{key})" } # .order_by([:var1, :asc], [:var2, :desc]) when Array # check provided values if elem.length != 2 raise ArgumentError, 'array argument must specify two elements' elsif !elem[0].is_a?(Symbol) raise ArgumentError, '1st element of array argument must contain a Symbol' elsif !elem[1].is_a?(Symbol) || (elem[1] != :asc && elem[1] != :desc) raise ArgumentError, '2nd element of array argument must either be `:asc` or `:desc`' end buffer << "#{elem[1] == :asc ? 'ASC' : 'DESC'}(?#{elem[0]})" # .order_by(:var1, :var2) when Symbol buffer << "?#{elem}" # .order_by('ASC(?var1) DESC(?var2)') when String buffer << elem else raise ArgumentError, 'argument provided to `order()` must either be an Array, Symbol or String' end } end buffer << "OFFSET #{[:offset]}" if [:offset] buffer << "LIMIT #{[:limit]}" if [:limit] [:prefixes].reverse.each { |e| buffer.unshift("PREFIX #{e}") } if [:prefixes] buffer.join(' ') end |
#true? ⇒ Boolean
706 707 708 709 710 711 712 713 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 706 def true? case result when TrueClass, FalseClass then result when RDF::Literal::Boolean then result.true? when Enumerable then !result.empty? else false end end |
#union(*patterns) {|query| ... } ⇒ Query
The block form can be used for more complicated queries, using the select
form (note, use either block or argument forms, not both):
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 540 def union(*patterns, &block) [:unions] ||= [] if block_given? raise ArgumentError, "#union requires either arguments or a block, not both." unless patterns.empty? # Evaluate calls in a new query instance query = self.class.select case block.arity when 1 then block.call(query) else query.instance_eval(&block) end [:unions] << query elsif patterns.all? {|p| p.is_a?(SPARQL::Client::Query)} # With argument form, all must be patterns or queries [:unions] += patterns elsif patterns.all? {|p| p.is_a?(Array)} # With argument form, all must be patterns, or queries [:unions] << self.class.select.where(*patterns) else raise ArgumentError, "#union arguments are triple patterns or queries, not both." end self end |
#values ⇒ Array<Array(key, RDF::Value)> #values(vars, *data) ⇒ Query
Specify inline data for a query
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 651 def values(*args) return @values if args.empty? vars, *data = *args vars = Array(vars).map {|var| RDF::Query::Variable.new(var)} if vars.length == 1 # data may be a in array form or simple form if data.any? {|d| d.is_a?(Array)} && !data.all? {|d| d.is_a?(Array)} raise ArgumentError, "values data must all be in array form or all simple" end data = data.map {|d| Array(d)} end # Each data value must be an array with the same number of entries as vars unless data.all? {|d| d.is_a?(Array) && d.all? {|dd| dd.is_a?(RDF::Value) || dd.is_a?(String) || dd.nil?}} raise ArgumentError, "values data must each be an array of terms, strings, or nil" end # Turn strings into Literals data = data.map do |d| d.map do |nil_literal_or_term| case nil_literal_or_term when nil then nil when String then RDF::Literal(nil_literal_or_term) when RDF::Value then nil_literal_or_term else raise ArgumentError end end end [:values] = [vars, *data] self end |
#where(*patterns_queries) {|query| ... } ⇒ Query Also known as: whether
Block form can be used for chaining calls in addition to creating sub-select queries.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-client-464d3f76cfd5/lib/sparql/client/query.rb', line 211 def where(*patterns_queries, &block) subqueries, patterns = patterns_queries.partition {|pq| pq.is_a? SPARQL::Client::Query} @patterns += build_patterns(patterns) @subqueries += subqueries if block_given? decorated_query = WhereDecorator.new(self) case block.arity when 1 then block.call(decorated_query) else decorated_query.instance_eval(&block) end end self end |