Skip to content

Commit

Permalink
Support for COUNT(DISTINCT ?foo)
Browse files Browse the repository at this point in the history
  • Loading branch information
moustaki committed Nov 14, 2012
1 parent 6e45949 commit 2d35125
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/sparql/client/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 . }"
Expand Down

0 comments on commit 2d35125

Please sign in to comment.