Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/ransack/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def type_for(attr)
attr.klass.type_for_attribute(name).type
end

# The collation of the column behind this attribute, as the schema
# reports it, or nil when there is none: a ransacker, a column the
# adapter reports no collation for, or a non-string column.
def collation_for(attr)
return nil unless attr && attr.valid? && !attr.ransacker
column = attr.klass.columns_hash[attr.arel_attribute.name.to_s]
column.collation if column.respond_to?(:collation)
end

# Evaluating a search leaves the context as it was, so the search can
# be evaluated again, or given another condition or sort, and still
# line up with the query Active Record builds (#1745, #1750).
Expand Down
17 changes: 17 additions & 0 deletions lib/ransack/active_record/dialect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ def case_insensitive_like?
postgresql?
end

# Whether LIKE against a column with this collation already ignores
# case, so a case-insensitive predicate need not lower the column.
#
# MySQL and MariaDB compare with the column's collation, and the
# default ones (utf8mb4_0900_ai_ci, utf8mb4_general_ci and the like)
# are case-insensitive: their names end in `_ci`. Lowering such a
# column changes no result, and it keeps a prefix LIKE from using the
# column's index, so a search on `i_start` scans the whole table.
# A `_bin` or `_cs` column, or one with no collation at all, is still
# lowered, since there LIKE is case-sensitive.
#
# Elsewhere the collation is not consulted: PostgreSQL has ILIKE, and
# SQLite's LIKE ignores the column's collation.
def case_insensitive_collation?(collation)
mysql? && collation.to_s.end_with?('_ci'.freeze)
end

# CHAR_LENGTH is the SQL standard spelling and what PostgreSQL and MySQL
# use; their LENGTH counts bytes on MySQL. SQLite has no CHAR_LENGTH,
# and neither does Oracle, while both count characters with LENGTH, so
Expand Down
22 changes: 16 additions & 6 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,29 @@ def arel_predicate_for_attribute(attr)
end

# The column (or ransacker expression) a predicate compares against.
# A case-insensitive predicate lowers it, unless the dialect has a
# case-insensitive LIKE of its own (ILIKE), in which case the lowering
# is left to the database. LOWER() is applied as a function rather than
# through Arel's Attribute#lower so that a ransacker returning any Arel
# node — a SQL literal, an infix operation — is lowered too (#1357).
# A case-insensitive predicate lowers it, unless the database already
# matches it case-insensitively: the dialect has a case-insensitive
# LIKE of its own (ILIKE), or the column's collation ignores case (a
# `_ci` collation on MySQL). Leaving such a column bare keeps a prefix
# LIKE able to use its index. LOWER() is applied as a function rather
# than through Arel's Attribute#lower so that a ransacker returning any
# Arel node — a SQL literal, an infix operation — is lowered too (#1357).
def attr_value_for_attribute(attr)
if predicate.case_insensitive && !context.dialect.case_insensitive_like?
if predicate.case_insensitive && !case_insensitive_like_for?(attr)
Arel::Nodes::NamedFunction.new('LOWER'.freeze, [attr.attr])
else
attr.attr
end
end

def case_insensitive_like_for?(attr)
dialect = context.dialect
return true if dialect.case_insensitive_like?

context.respond_to?(:collation_for) &&
dialect.case_insensitive_collation?(context.collation_for(attr))
end

def default_type
attribute = attributes.first
predicate.type || (attribute && value_type_for_attribute(attribute))
Expand Down
16 changes: 16 additions & 0 deletions spec/helpers/ransack_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ def dialect
RansackHelper.dialect
end

# The column and operator a case-insensitive predicate (i_cont, i_start,
# i_end) renders for people.name: ILIKE on PostgreSQL, a bare LIKE on
# MySQL, where the column's default `_ci` collation already ignores case,
# and LIKE on LOWER(column) elsewhere.
def self.i_like_name(negated: false)
conn = ::ActiveRecord::Base.lease_connection
field = "#{conn.quote_table_name('people')}.#{conn.quote_column_name('name')}"
column = dialect.postgresql? || dialect.mysql? ? field : "LOWER(#{field})"
operator = dialect.case_insensitive_like? ? 'ILIKE' : 'LIKE'
"#{column} #{'NOT ' if negated}#{operator}"
end

def i_like_name(negated: false)
Regexp.escape(RansackHelper.i_like_name(negated: negated))
end

def quote_table_name(table)
::ActiveRecord::Base.lease_connection.quote_table_name(table)
end
Expand Down
23 changes: 23 additions & 0 deletions spec/ransack/active_record/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ module ActiveRecord
end
end

describe '#collation_for' do
def attribute_for(params)
Search.new(Person, params, context: subject).base.conditions.first.attributes.first
end

it "reports a string column's collation as the schema has it" do
expected = Person.columns_hash['name'].collation
expect(subject.collation_for(attribute_for(name_i_cont: 'x'))).to eq expected
end

it 'reports the database default collation on MySQL', if: RansackHelper.dialect.mysql? do
expect(subject.collation_for(attribute_for(name_i_cont: 'x'))).to end_with '_ci'
end

it 'is nil for a ransacker' do
expect(subject.collation_for(attribute_for(doubled_name_i_cont: 'x'))).to be_nil
end

it 'is nil for a non-string column' do
expect(subject.collation_for(attribute_for(salary_eq: 1))).to be_nil
end
end

describe '#type_for with the Attributes API' do
# A column redeclared with `attribute` is cast by its declared type,
# not the schema column's (#1028).
Expand Down
22 changes: 22 additions & 0 deletions spec/ransack/active_record/dialect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ def stub_adapter(name, parent)
end
end

describe '#case_insensitive_collation?' do
it 'is true for a MySQL _ci collation' do
mysql = Dialect.new(:mysql)
expect(mysql.case_insensitive_collation?('utf8mb4_0900_ai_ci')).to be true
expect(mysql.case_insensitive_collation?('utf8mb3_general_ci')).to be true
expect(mysql.case_insensitive_collation?('uca1400_ai_ci')).to be true
end

it 'is false for a case-sensitive, binary or missing collation' do
mysql = Dialect.new(:mysql)
expect(mysql.case_insensitive_collation?('utf8mb4_bin')).to be false
expect(mysql.case_insensitive_collation?('utf8mb4_0900_as_cs')).to be false
expect(mysql.case_insensitive_collation?(nil)).to be false
end

it 'is false outside MySQL' do
%i[postgresql sqlite generic].each do |name|
expect(Dialect.new(name).case_insensitive_collation?('und-x-icu_ci')).to be false
end
end
end

describe '#length_function' do
it 'uses CHAR_LENGTH where it exists and LENGTH elsewhere' do
expect(Dialect.new(:postgresql).length_function).to eq 'CHAR_LENGTH'
Expand Down
Loading