Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for COUNT(DISTINCT ?foo)

  • Loading branch information...
commit 2d35125e9f1577bceee9a43c69aa4a8ae0ceba74 1 parent 6e45949
@moustaki moustaki authored
Showing with 4 additions and 2 deletions.
  1. +3 −2 lib/sparql/client/query.rb
  2. +1 −0  spec/query_spec.rb
View
5 lib/sparql/client/query.rb
@@ -300,12 +300,13 @@ def to_s
buffer = [form.to_s.upcase]
case form
when :select, :describe
- buffer << 'DISTINCT' if options[:distinct]
+ only_count = values.empty? and options[:count]
+ buffer << 'DISTINCT' if options[:distinct] and not only_count
buffer << 'REDUCED' if options[:reduced]
buffer << ((values.empty? and not options[:count]) ? '*' : values.map { |v| serialize_value(v[1]) }.join(' '))
if options[:count]
options[:count].each do |var, count|
- buffer << '( COUNT(' + (var.is_a?(String) ? var : "?#{var}") + ') AS ' + (count.is_a?(String) ? count : "?#{count}") + ' )'
+ buffer << '( COUNT(' + (options[:distinct] ? 'DISTINCT ' : '') + (var.is_a?(String) ? var : "?#{var}") + ') AS ' + (count.is_a?(String) ? count : "?#{count}") + ' )'
end
end
when :construct
View
1  spec/query_spec.rb
@@ -59,6 +59,7 @@
it "should support COUNT" do
@query.select(:count => { :s => :c }).where([:s, :p, :o]).to_s.should == "SELECT ( COUNT(?s) AS ?c ) WHERE { ?s ?p ?o . }"
+ @query.select(:count => { :s => :c }, :distinct => true).where([:s, :p, :o]).to_s.should == "SELECT ( COUNT(DISTINCT ?s) AS ?c ) WHERE { ?s ?p ?o . }"
@query.select(:count => { :s => '?c' }).where([:s, :p, :o]).to_s.should == "SELECT ( COUNT(?s) AS ?c ) WHERE { ?s ?p ?o . }"
@query.select(:count => { '?s' => '?c' }).where([:s, :p, :o]).to_s.should == "SELECT ( COUNT(?s) AS ?c ) WHERE { ?s ?p ?o . }"
@query.select(:o, :count => { :s => :c }).where([:s, :p, :o]).to_s.should == "SELECT ?o ( COUNT(?s) AS ?c ) WHERE { ?s ?p ?o . }"
Please sign in to comment.
Something went wrong with that request. Please try again.