Fixed: error when filtering by numeric custom field with postgresql (#9719).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8098 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-12-05 20:45:45 +00:00
parent c562d79e4f
commit 5ca558f191
2 changed files with 48 additions and 9 deletions

View File

@ -674,9 +674,17 @@ class Query < ActiveRecord::Base
when :date, :date_past
sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil))
when :integer
sql = "#{db_table}.#{db_field} = #{value.first.to_i}"
if is_custom_filter
sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})"
else
sql = "#{db_table}.#{db_field} = #{value.first.to_i}"
end
when :float
sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}"
if is_custom_filter
sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})"
else
sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}"
end
else
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
end
@ -702,7 +710,7 @@ class Query < ActiveRecord::Base
sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
else
if is_custom_filter
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f}"
sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})"
else
sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
end
@ -712,7 +720,7 @@ class Query < ActiveRecord::Base
sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
else
if is_custom_filter
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f}"
sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})"
else
sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
end
@ -722,7 +730,7 @@ class Query < ActiveRecord::Base
sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
else
if is_custom_filter
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})"
else
sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
end

View File

@ -147,6 +147,32 @@ class QueryTest < ActiveSupport::TestCase
assert_equal 2, issues.first.id
end
def test_operator_is_on_integer_custom_field
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true)
CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
query = Query.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['12'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
assert_equal 2, issues.first.id
end
def test_operator_is_on_float_custom_field
f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true)
CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3')
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
query = Query.new(:name => '_')
query.add_filter("cf_#{f.id}", '=', ['12.7'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
assert_equal 2, issues.first.id
end
def test_operator_greater_than
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '>=', ['40'])
@ -161,12 +187,17 @@ class QueryTest < ActiveSupport::TestCase
find_issues_with_query(query)
end
def test_operator_greater_than_on_custom_field
def test_operator_greater_than_on_int_custom_field
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '>=', ['40'])
assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) >= 40.0")
find_issues_with_query(query)
query.add_filter("cf_#{f.id}", '>=', ['8'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
assert_equal 2, issues.first.id
end
def test_operator_lesser_than