Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Replace AMC with chainable idiom for extending #property_to_column_name
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubb committed May 25, 2010
1 parent 6ac1667 commit b64f32b
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions lib/dm-aggregates/adapters/dm-do-adapter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module DataMapper
module Aggregates
module DataObjectsAdapter
def self.included(base)
base.send(:include, SQL)
end
extend Chainable

def aggregate(query)
fields = query.fields
Expand Down Expand Up @@ -63,47 +61,40 @@ def sum(property, value)
property.load(value)
end

module SQL
def self.included(base)
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
# FIXME: figure out a cleaner approach than AMC
alias property_to_column_name_without_operator property_to_column_name
alias property_to_column_name property_to_column_name_with_operator
RUBY
end

def property_to_column_name_with_operator(property, qualify)
chainable do
def property_to_column_name(property, qualify)
case property
when DataMapper::Query::Operator
aggregate_field_statement(property.operator, property.target, qualify)

when Property, DataMapper::Query::Path
property_to_column_name_without_operator(property, qualify)
super

else
raise ArgumentError, "+property+ must be a DataMapper::Query::Operator, a DataMapper::Property or a Query::Path, but was a #{property.class} (#{property.inspect})"
end
end
end

def aggregate_field_statement(aggregate_function, property, qualify)
column_name = if aggregate_function == :count && property == :all
'*'
else
property_to_column_name(property, qualify)
end

function_name = case aggregate_function
when :count then 'COUNT'
when :min then 'MIN'
when :max then 'MAX'
when :avg then 'AVG'
when :sum then 'SUM'
else raise "Invalid aggregate function: #{aggregate_function.inspect}"
end
def aggregate_field_statement(aggregate_function, property, qualify)
column_name = if aggregate_function == :count && property == :all
'*'
else
property_to_column_name(property, qualify)
end

"#{function_name}(#{column_name})"
function_name = case aggregate_function
when :count then 'COUNT'
when :min then 'MIN'
when :max then 'MAX'
when :avg then 'AVG'
when :sum then 'SUM'
else raise "Invalid aggregate function: #{aggregate_function.inspect}"
end
end # module SQL

"#{function_name}(#{column_name})"
end

end # class DataObjectsAdapter
end # module Aggregates
end # module DataMapper

0 comments on commit b64f32b

Please sign in to comment.