提交 9991868d 编写于 作者: W Will Bryant 提交者: Michael Koziarski

support end-exclusive ... Ranges in SQL hash condition sanitization properly

Signed-off-by: NMichael Koziarski <michael@koziarski.com>
[#1865 state:committed]
上级 ba146a84
......@@ -1992,12 +1992,16 @@ def all_attributes_exists?(attribute_names)
attribute_names.all? { |name| column_methods_hash.include?(name.to_sym) }
end
def attribute_condition(argument)
def attribute_condition(quoted_column_name, argument)
case argument
when nil then "IS ?"
when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope then "IN (?)"
when Range then "BETWEEN ? AND ?"
else "= ?"
when nil then "#{quoted_column_name} IS ?"
when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope then "#{quoted_column_name} IN (?)"
when Range then if argument.exclude_end?
"#{quoted_column_name} >= ? AND #{quoted_column_name} < ?"
else
"#{quoted_column_name} BETWEEN ? AND ?"
end
else "#{quoted_column_name} = ?"
end
end
......@@ -2307,7 +2311,7 @@ def sanitize_sql_hash_for_conditions(attrs, table_name = quoted_table_name)
table_name = connection.quote_table_name(table_name)
end
"#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value)
else
sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s))
end
......
......@@ -744,7 +744,7 @@ def validates_uniqueness_of(*attr_names)
if scope = configuration[:scope]
Array(scope).map do |scope_item|
scope_value = record.send(scope_item)
condition_sql << " AND #{record.class.quoted_table_name}.#{scope_item} #{attribute_condition(scope_value)}"
condition_sql << " AND " << attribute_condition("#{record.class.quoted_table_name}.#{scope_item}", scope_value)
condition_params << scope_value
end
end
......
......@@ -307,6 +307,12 @@ def test_find_on_hash_conditions_with_range
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
end
def test_find_on_hash_conditions_with_end_exclusive_range
assert_equal [1,2,3], Topic.find(:all, :conditions => { :id => 1..3 }).map(&:id).sort
assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1...3 }).map(&:id).sort
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(3, :conditions => { :id => 2...3 }) }
end
def test_find_on_hash_conditions_with_multiple_ranges
assert_equal [1,2,3], Comment.find(:all, :conditions => { :id => 1..3, :post_id => 1..2 }).map(&:id).sort
assert_equal [1], Comment.find(:all, :conditions => { :id => 1..1, :post_id => 1..10 }).map(&:id).sort
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册