Replace raw SQL with a .count call.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8093 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-12-05 18:27:10 +00:00
parent 81afd9d0c7
commit 6a1830e9e3
1 changed files with 13 additions and 29 deletions

View File

@ -44,31 +44,22 @@ module Redmine
def run def run
unless @criteria.empty? unless @criteria.empty?
sql_select = @criteria.collect{|criteria| @available_criteria[criteria][:sql] + " AS " + criteria}.join(', ') scope = TimeEntry.visible.spent_between(@from, @to)
sql_group_by = @criteria.collect{|criteria| @available_criteria[criteria][:sql]}.join(', ')
sql_condition = ''
if @issue if @issue
sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}" scope = scope.on_issue(@issue)
else elsif @project
sql_condition = Project.allowed_to_condition(User.current, :view_time_entries, :project => @project, :with_subprojects => Setting.display_subprojects_issues?) scope = scope.on_project(@project, Setting.display_subprojects_issues?)
end end
time_columns = %w(tyear tmonth tweek spent_on)
sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours" @hours = []
sql << " FROM #{TimeEntry.table_name}" scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours|
sql << time_report_joins h = {'hours' => hours}
sql << " WHERE (%s)" % sql_condition (@criteria + time_columns).each_with_index do |name, i|
if @from && @to h[name] = hash[i]
sql << " AND (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)] end
elsif @from @hours << h
sql << " AND (spent_on BETWEEN >= '%s')" % ActiveRecord::Base.connection.quoted_date(@from)
elsif @to
sql << " AND (spent_on BETWEEN <= '%s')" % ActiveRecord::Base.connection.quoted_date(@to)
end end
sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
@hours = ActiveRecord::Base.connection.select_all(sql)
@hours.each do |row| @hours.each do |row|
case @columns case @columns
when 'year' when 'year'
@ -165,13 +156,6 @@ module Redmine
@available_criteria @available_criteria
end end
def time_report_joins
sql = ''
sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
sql
end
end end
end end
end end