Skip to content

Commit

Permalink
Count distinct field when :unique => true
Browse files Browse the repository at this point in the history
  • Loading branch information
tohchye committed Jul 18, 2008
1 parent 0a8eab9 commit 66b7b5c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dm-aggregates/lib/dm-aggregates/adapters/data_objects_adapter.rb
Expand Up @@ -48,22 +48,28 @@ module SQL

alias original_property_to_column_name property_to_column_name

def property_to_column_name(repository, property, qualify)
def fields_statement(query)
qualify = query.links.any?
query.fields.map { |p| property_to_column_name(query.repository, p, qualify, query.unique?) } * ', '
end

def property_to_column_name(repository, property, qualify, unique=false)
case property
when Query::Operator
aggregate_field_statement(repository, property.operator, property.target, qualify)
aggregate_field_statement(repository, property.operator, property.target, qualify, unique)
when Property
original_property_to_column_name(repository, property, qualify)
else
raise ArgumentError, "+property+ must be a DataMapper::Query::Operator or a DataMapper::Property, but was a #{property.class} (#{property.inspect})"
end
end

def aggregate_field_statement(repository, aggregate_function, property, qualify)
def aggregate_field_statement(repository, aggregate_function, property, qualify, unique=false)
column_name = if aggregate_function == :count && property == :all
'*'
else
property_to_column_name(repository, property, qualify)
unique ? "distinct #{property_to_column_name(repository, property, qualify)}" :
property_to_column_name(repository, property, qualify)
end

function_name = case aggregate_function
Expand Down

0 comments on commit 66b7b5c

Please sign in to comment.