mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor case_{sensitive|insensitive}_comparison
Before: ``` SELECT 1 AS one FROM "topics" WHERE "topics"."title" = 'abc' LIMIT $1 [["LIMIT", 1]] ``` After: ``` SELECT 1 AS one FROM "topics" WHERE "topics"."title" = $1 LIMIT $2 [["title", "abc"], ["LIMIT", 1]] ```
This commit is contained in:
parent
8167fa4562
commit
b80d304f11
4 changed files with 17 additions and 20 deletions
|
@ -391,19 +391,17 @@ module ActiveRecord
|
||||||
def release_savepoint(name = nil)
|
def release_savepoint(name = nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def case_sensitive_modifier(node, table_attribute)
|
|
||||||
node
|
|
||||||
end
|
|
||||||
|
|
||||||
def case_sensitive_comparison(table, attribute, column, value)
|
def case_sensitive_comparison(table, attribute, column, value)
|
||||||
table_attr = table[attribute]
|
if value.nil?
|
||||||
value = case_sensitive_modifier(value, table_attr) unless value.nil?
|
table[attribute].eq(value)
|
||||||
table_attr.eq(value)
|
else
|
||||||
|
table[attribute].eq(Arel::Nodes::BindParam.new)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def case_insensitive_comparison(table, attribute, column, value)
|
def case_insensitive_comparison(table, attribute, column, value)
|
||||||
if can_perform_case_insensitive_comparison_for?(column)
|
if can_perform_case_insensitive_comparison_for?(column)
|
||||||
table[attribute].lower.eq(table.lower(value))
|
table[attribute].lower.eq(table.lower(Arel::Nodes::BindParam.new))
|
||||||
else
|
else
|
||||||
case_sensitive_comparison(table, attribute, column, value)
|
case_sensitive_comparison(table, attribute, column, value)
|
||||||
end
|
end
|
||||||
|
|
|
@ -752,16 +752,11 @@ module ActiveRecord
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def case_sensitive_modifier(node, table_attribute)
|
|
||||||
node = Arel::Nodes.build_quoted node, table_attribute
|
|
||||||
Arel::Nodes::Bin.new(node)
|
|
||||||
end
|
|
||||||
|
|
||||||
def case_sensitive_comparison(table, attribute, column, value)
|
def case_sensitive_comparison(table, attribute, column, value)
|
||||||
if column.case_sensitive?
|
if value.nil? || column.case_sensitive?
|
||||||
table[attribute].eq(value)
|
|
||||||
else
|
|
||||||
super
|
super
|
||||||
|
else
|
||||||
|
table[attribute].eq(Arel::Nodes::Bin.new(Arel::Nodes::BindParam.new))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -769,7 +764,7 @@ module ActiveRecord
|
||||||
if column.case_sensitive?
|
if column.case_sensitive?
|
||||||
super
|
super
|
||||||
else
|
else
|
||||||
table[attribute].eq(value)
|
table[attribute].eq(Arel::Nodes::BindParam.new)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ module ActiveRecord
|
||||||
parts = predicate_builder.build_from_hash(attributes)
|
parts = predicate_builder.build_from_hash(attributes)
|
||||||
when Arel::Nodes::Node
|
when Arel::Nodes::Node
|
||||||
parts = [opts]
|
parts = [opts]
|
||||||
|
binds = other
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Unsupported argument type: #{opts} (#{opts.class})"
|
raise ArgumentError, "Unsupported argument type: #{opts} (#{opts.class})"
|
||||||
end
|
end
|
||||||
|
|
|
@ -73,15 +73,18 @@ module ActiveRecord
|
||||||
value = value.to_s[0, column.limit]
|
value = value.to_s[0, column.limit]
|
||||||
end
|
end
|
||||||
|
|
||||||
value = Arel::Nodes::Quoted.new(value)
|
|
||||||
|
|
||||||
comparison = if !options[:case_sensitive] && !value.nil?
|
comparison = if !options[:case_sensitive] && !value.nil?
|
||||||
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
||||||
klass.connection.case_insensitive_comparison(table, attribute, column, value)
|
klass.connection.case_insensitive_comparison(table, attribute, column, value)
|
||||||
else
|
else
|
||||||
klass.connection.case_sensitive_comparison(table, attribute, column, value)
|
klass.connection.case_sensitive_comparison(table, attribute, column, value)
|
||||||
end
|
end
|
||||||
|
if value.nil?
|
||||||
klass.unscoped.where(comparison)
|
klass.unscoped.where(comparison)
|
||||||
|
else
|
||||||
|
bind = Relation::QueryAttribute.new(attribute.to_s, value, Type::Value.new)
|
||||||
|
klass.unscoped.where(comparison, bind)
|
||||||
|
end
|
||||||
rescue RangeError
|
rescue RangeError
|
||||||
klass.none
|
klass.none
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue