]> source.dussan.org Git - redmine.git/commitdiff
Use joins instead of sub-queries (#12713).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 3 Jan 2013 13:57:27 +0000 (13:57 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 3 Jan 2013 13:57:27 +0000 (13:57 +0000)
Sub-queries in group by are not supported by SQLServer.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11107 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/helpers/time_report.rb

index 5dd02f0aa749e4ce63269504580ebb5799a08847..181598af2c1a227bcd2852a17d387e03f71d56e2 100644 (file)
@@ -45,7 +45,10 @@ module Redmine
         unless @criteria.empty?
           time_columns = %w(tyear tmonth tweek spent_on)
           @hours = []
-          @scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours|
+          @scope.sum(:hours,
+              :include => [:issue, :activity],
+              :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns,
+              :joins => @criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).each do |hash, hours|
             h = {'hours' => hours}
             (@criteria + time_columns).each_with_index do |name, i|
               h[name] = hash[i]
@@ -127,21 +130,24 @@ module Redmine
         # Add list and boolean custom fields as available criteria
         custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
         custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
-          @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id ORDER BY c.value LIMIT 1)",
+          @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
+                                                 :joins => cf.join_for_order_statement,
                                                  :format => cf.field_format,
                                                  :label => cf.name}
         end if @project
 
         # Add list and boolean time entry custom fields
         TimeEntryCustomField.all.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
-          @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id ORDER BY c.value LIMIT 1)",
+          @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
+                                                 :joins => cf.join_for_order_statement,
                                                  :format => cf.field_format,
                                                  :label => cf.name}
         end
 
         # Add list and boolean time entry activity custom fields
         TimeEntryActivityCustomField.all.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
-          @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id ORDER BY c.value LIMIT 1)",
+          @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
+                                                 :joins => cf.join_for_order_statement,
                                                  :format => cf.field_format,
                                                  :label => cf.name}
         end