resync from rails_port 11795:12304
[rails.git] / vendor / gems / composite_primary_keys-1.1.0 / lib / composite_primary_keys / calculations.rb
1 module CompositePrimaryKeys
2   module ActiveRecord
3     module Calculations
4       def self.append_features(base)
5         super
6         base.send(:extend, ClassMethods)
7       end
8
9       module ClassMethods
10         def construct_calculation_sql(operation, column_name, options) #:nodoc:
11           operation = operation.to_s.downcase
12           options = options.symbolize_keys
13
14           scope           = scope(:find)
15           merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
16           aggregate_alias = column_alias_for(operation, column_name)
17           use_workaround  = !connection.supports_count_distinct? && options[:distinct] && operation.to_s.downcase == 'count'
18           join_dependency = nil
19
20           if merged_includes.any? && operation.to_s.downcase == 'count'
21             options[:distinct] = true
22             use_workaround  = !connection.supports_count_distinct?
23             column_name = options[:select] || primary_key.map{ |part| "#{quoted_table_name}.#{connection.quote_column_name(part)}"}.join(',')
24           end
25
26           sql  = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}"
27
28           # A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT.
29           sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround
30
31           sql << ", #{connection.quote_column_name(options[:group_field])} AS #{options[:group_alias]}" if options[:group]
32           sql << " FROM (SELECT DISTINCT #{column_name}" if use_workaround
33           sql << " FROM #{quoted_table_name} "
34           if merged_includes.any?
35             join_dependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins])
36             sql << join_dependency.join_associations.collect{|join| join.association_join }.join
37           end
38           add_joins!(sql, options, scope)
39           add_conditions!(sql, options[:conditions], scope)
40           add_limited_ids_condition!(sql, options, join_dependency) if \
41             join_dependency &&
42             !using_limitable_reflections?(join_dependency.reflections) &&
43             ((scope && scope[:limit]) || options[:limit])
44
45           if options[:group]
46             group_key = connection.adapter_name == 'FrontBase' ?  :group_alias : :group_field
47             sql << " GROUP BY #{connection.quote_column_name(options[group_key])} "
48           end
49
50           if options[:group] && options[:having]
51             # FrontBase requires identifiers in the HAVING clause and chokes on function calls
52             if connection.adapter_name == 'FrontBase'
53               options[:having].downcase!
54               options[:having].gsub!(/#{operation}\s*\(\s*#{column_name}\s*\)/, aggregate_alias)
55             end
56
57             sql << " HAVING #{options[:having]} "
58           end
59
60           sql << " ORDER BY #{options[:order]} " if options[:order]
61           add_limit!(sql, options, scope)
62           sql << ') w1' if use_workaround # assign a dummy table name as required for postgresql
63           sql
64         end
65       end
66     end
67   end
68 end