From f357912d211cc588921e2cc34a8f529d066656c6 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 20 Feb 2011 13:03:32 +0000 Subject: [PATCH] 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 --- app/models/query.rb | 14 ++++++++++++-- test/unit/query_test.rb | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index 1f033048a..057626885 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -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 diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 4d85a2b2b..ce4306441 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -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