summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Davis <edavis@littlestreamsoftware.com>2008-12-05 22:56:03 +0000
committerEric Davis <edavis@littlestreamsoftware.com>2008-12-05 22:56:03 +0000
commitea77929d62a372f9c2d6a645efc2ec0202a11016 (patch)
tree32991693788adcaad13d18f7cce0c54e549710bf
parent996b2a5c1d783310ba5dc2f1757ee3856d793bd3 (diff)
downloadredmine-ea77929d62a372f9c2d6a645efc2ec0202a11016.tar.gz
redmine-ea77929d62a372f9c2d6a645efc2ec0202a11016.zip
Refactor: Extracted new method Query#sql_for_field from Query#statement in
order to clean up Query#statement. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2090 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--app/models/query.rb99
1 files changed, 54 insertions, 45 deletions
diff --git a/app/models/query.rb b/app/models/query.rb
index 5ad0dd804..8b036eb57 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -309,51 +309,8 @@ class Query < ActiveRecord::Base
v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me")
end
- case operator_for field
- when "="
- sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
- when "!"
- sql = sql + "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
- when "!*"
- sql = sql + "#{db_table}.#{db_field} IS NULL"
- sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
- when "*"
- sql = sql + "#{db_table}.#{db_field} IS NOT NULL"
- sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
- when ">="
- sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}"
- when "<="
- sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}"
- when "o"
- sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
- when "c"
- sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
- when ">t-"
- sql = sql + date_range_clause(db_table, db_field, - v.first.to_i, 0)
- when "<t-"
- sql = sql + date_range_clause(db_table, db_field, nil, - v.first.to_i)
- when "t-"
- sql = sql + date_range_clause(db_table, db_field, - v.first.to_i, - v.first.to_i)
- when ">t+"
- sql = sql + date_range_clause(db_table, db_field, v.first.to_i, nil)
- when "<t+"
- sql = sql + date_range_clause(db_table, db_field, 0, v.first.to_i)
- when "t+"
- sql = sql + date_range_clause(db_table, db_field, v.first.to_i, v.first.to_i)
- when "t"
- sql = sql + date_range_clause(db_table, db_field, 0, 0)
- when "w"
- from = l(:general_first_day_of_week) == '7' ?
- # week starts on sunday
- ((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) :
- # week starts on monday (Rails default)
- Time.now.at_beginning_of_week
- sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(from), connection.quoted_date(from + 7.days)]
- when "~"
- sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'"
- when "!~"
- sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'"
- end
+ sql = sql + sql_for_field(field, v, db_table, db_field, is_custom_filter)
+
sql << ')'
filters_clauses << sql
end if filters and valid?
@@ -363,6 +320,58 @@ class Query < ActiveRecord::Base
private
+ # Helper method to generate the WHERE sql for a +field+ with a value (+v+)
+ def sql_for_field(field, v, db_table, db_field, is_custom_filter)
+ sql = ''
+ case operator_for field
+ when "="
+ return "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
+ when "!"
+ return "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
+ when "!*"
+ sql = "#{db_table}.#{db_field} IS NULL"
+ sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
+ return sql
+ when "*"
+ sql = "#{db_table}.#{db_field} IS NOT NULL"
+ sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
+ return sql
+ when ">="
+ return "#{db_table}.#{db_field} >= #{v.first.to_i}"
+ when "<="
+ return "#{db_table}.#{db_field} <= #{v.first.to_i}"
+ when "o"
+ return "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
+ when "c"
+ return "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
+ when ">t-"
+ return date_range_clause(db_table, db_field, - v.first.to_i, 0)
+ when "<t-"
+ return date_range_clause(db_table, db_field, nil, - v.first.to_i)
+ when "t-"
+ return date_range_clause(db_table, db_field, - v.first.to_i, - v.first.to_i)
+ when ">t+"
+ return date_range_clause(db_table, db_field, v.first.to_i, nil)
+ when "<t+"
+ return date_range_clause(db_table, db_field, 0, v.first.to_i)
+ when "t+"
+ return date_range_clause(db_table, db_field, v.first.to_i, v.first.to_i)
+ when "t"
+ return date_range_clause(db_table, db_field, 0, 0)
+ when "w"
+ from = l(:general_first_day_of_week) == '7' ?
+ # week starts on sunday
+ ((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) :
+ # week starts on monday (Rails default)
+ Time.now.at_beginning_of_week
+ return "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(from), connection.quoted_date(from + 7.days)]
+ when "~"
+ return "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'"
+ when "!~"
+ return "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'"
+ end
+ end
+
def add_custom_fields_filters(custom_fields)
@available_filters ||= {}