@@ -125,7 +125,7 class Query < ActiveRecord::Base | |||||
125 | filters.each_key do |field| |
|
125 | filters.each_key do |field| | |
126 | errors.add label_for(field), :activerecord_error_blank unless |
|
126 | errors.add label_for(field), :activerecord_error_blank unless | |
127 | # filter requires one or more values |
|
127 | # filter requires one or more values | |
128 |
(values_for(field) and !values_for(field).first. |
|
128 | (values_for(field) and !values_for(field).first.blank?) or | |
129 | # filter doesn't require any value |
|
129 | # filter doesn't require any value | |
130 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
130 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) | |
131 | end if filters |
|
131 | end if filters | |
@@ -296,11 +296,13 class Query < ActiveRecord::Base | |||||
296 | v = values_for(field).clone |
|
296 | v = values_for(field).clone | |
297 | next unless v and !v.empty? |
|
297 | next unless v and !v.empty? | |
298 |
|
298 | |||
299 |
sql = '' |
|
299 | sql = '' | |
|
300 | is_custom_filter = false | |||
300 | if field =~ /^cf_(\d+)$/ |
|
301 | if field =~ /^cf_(\d+)$/ | |
301 | # custom field |
|
302 | # custom field | |
302 | db_table = CustomValue.table_name |
|
303 | db_table = CustomValue.table_name | |
303 | db_field = 'value' |
|
304 | db_field = 'value' | |
|
305 | is_custom_filter = true | |||
304 | sql << "#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} WHERE " |
|
306 | sql << "#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} WHERE " | |
305 | else |
|
307 | else | |
306 | # regular field |
|
308 | # regular field | |
@@ -320,9 +322,11 class Query < ActiveRecord::Base | |||||
320 | when "!" |
|
322 | when "!" | |
321 | sql = sql + "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
323 | sql = sql + "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" | |
322 | when "!*" |
|
324 | when "!*" | |
323 |
sql = sql + "#{db_table}.#{db_field} IS NULL |
|
325 | sql = sql + "#{db_table}.#{db_field} IS NULL" | |
|
326 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter | |||
324 | when "*" |
|
327 | when "*" | |
325 |
sql = sql + "#{db_table}.#{db_field} IS NOT NULL |
|
328 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" | |
|
329 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter | |||
326 | when ">=" |
|
330 | when ">=" | |
327 | sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}" |
|
331 | sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}" | |
328 | when "<=" |
|
332 | when "<=" |
@@ -4,6 +4,7 custom_fields_001: | |||||
4 | min_length: 0 |
|
4 | min_length: 0 | |
5 | regexp: "" |
|
5 | regexp: "" | |
6 | is_for_all: true |
|
6 | is_for_all: true | |
|
7 | is_filter: true | |||
7 | type: IssueCustomField |
|
8 | type: IssueCustomField | |
8 | max_length: 0 |
|
9 | max_length: 0 | |
9 | possible_values: MySQL|PostgreSQL|Oracle |
|
10 | possible_values: MySQL|PostgreSQL|Oracle |
@@ -29,6 +29,80 class QueryTest < Test::Unit::TestCase | |||||
29 | assert_equal Issue.find(3), issues.first |
|
29 | assert_equal Issue.find(3), issues.first | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
|
32 | def test_operator_none | |||
|
33 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
34 | query.add_filter('fixed_version_id', '!*', ['']) | |||
|
35 | query.add_filter('cf_1', '!*', ['']) | |||
|
36 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") | |||
|
37 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") | |||
|
38 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
39 | end | |||
|
40 | ||||
|
41 | def test_operator_all | |||
|
42 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
43 | query.add_filter('fixed_version_id', '*', ['']) | |||
|
44 | query.add_filter('cf_1', '*', ['']) | |||
|
45 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") | |||
|
46 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") | |||
|
47 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
48 | end | |||
|
49 | ||||
|
50 | def test_operator_greater_than | |||
|
51 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
52 | query.add_filter('done_ratio', '>=', ['40']) | |||
|
53 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40") | |||
|
54 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
55 | end | |||
|
56 | ||||
|
57 | def test_operator_in_more_than | |||
|
58 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
59 | query.add_filter('due_date', '>t+', ['15']) | |||
|
60 | assert query.statement.include?("#{Issue.table_name}.due_date >=") | |||
|
61 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
62 | end | |||
|
63 | ||||
|
64 | def test_operator_in_less_than | |||
|
65 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
66 | query.add_filter('due_date', '<t+', ['15']) | |||
|
67 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |||
|
68 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
69 | end | |||
|
70 | ||||
|
71 | def test_operator_today | |||
|
72 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
73 | query.add_filter('due_date', 't', ['']) | |||
|
74 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |||
|
75 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
76 | end | |||
|
77 | ||||
|
78 | def test_operator_this_week_on_date | |||
|
79 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
80 | query.add_filter('due_date', 'w', ['']) | |||
|
81 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |||
|
82 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
83 | end | |||
|
84 | ||||
|
85 | def test_operator_this_week_on_datetime | |||
|
86 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
87 | query.add_filter('created_on', 'w', ['']) | |||
|
88 | assert query.statement.include?("#{Issue.table_name}.created_on BETWEEN") | |||
|
89 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
90 | end | |||
|
91 | ||||
|
92 | def test_operator_contains | |||
|
93 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
94 | query.add_filter('subject', '~', ['string']) | |||
|
95 | assert query.statement.include?("#{Issue.table_name}.subject LIKE '%string%'") | |||
|
96 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
97 | end | |||
|
98 | ||||
|
99 | def test_operator_does_not_contains | |||
|
100 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
101 | query.add_filter('subject', '!~', ['string']) | |||
|
102 | assert query.statement.include?("#{Issue.table_name}.subject NOT LIKE '%string%'") | |||
|
103 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement | |||
|
104 | end | |||
|
105 | ||||
32 | def test_default_columns |
|
106 | def test_default_columns | |
33 | q = Query.new |
|
107 | q = Query.new | |
34 | assert !q.columns.empty? |
|
108 | assert !q.columns.empty? |
General Comments 0
You need to be logged in to leave comments.
Login now