Fixed: SQL error when filtering issues with an empty group or role (#7656).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4888 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-02-20 13:03:32 +00:00
parent 329b2aa263
commit f357912d21
2 changed files with 44 additions and 4 deletions

View File

@ -566,9 +566,19 @@ class Query < ActiveRecord::Base
sql = ''
case operator
when "="
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
if value.any?
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
else
# IN an empty set
sql = "1=0"
end
when "!"
sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
if value.any?
sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
else
# NOT IN an empty set
sql = "1=1"
end
when "!*"
sql = "#{db_table}.#{db_field} IS NULL"
sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter

View File

@ -484,7 +484,6 @@ class QueryTest < ActiveSupport::TestCase
# Users not in a group
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}')"
assert_find_issues_with_query_is_successful @query
end
should "search assigned to any group member (all)" do
@ -494,7 +493,22 @@ class QueryTest < ActiveSupport::TestCase
# Only users in a group
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}')"
assert_find_issues_with_query_is_successful @query
end
should "return an empty set with = empty group" do
@empty_group = Group.generate!
@query = Query.new(:name => '_')
@query.add_filter('member_of_group', '=', [@empty_group.id.to_s])
assert_equal [], find_issues_with_query(@query)
end
should "return issues with ! empty group" do
@empty_group = Group.generate!
@query = Query.new(:name => '_')
@query.add_filter('member_of_group', '!', [@empty_group.id.to_s])
assert_find_issues_with_query_is_successful @query
end
end
@ -540,6 +554,22 @@ class QueryTest < ActiveSupport::TestCase
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@manager.id}','#{@developer.id}','#{@boss.id}')"
assert_find_issues_with_query_is_successful @query
end
should "return an empty set with empty role" do
@empty_role = Role.generate!
@query = Query.new(:name => '_')
@query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s])
assert_equal [], find_issues_with_query(@query)
end
should "return issues with ! empty role" do
@empty_role = Role.generate!
@query = Query.new(:name => '_')
@query.add_filter('member_of_group', '!', [@empty_role.id.to_s])
assert_find_issues_with_query_is_successful @query
end
end
end