Skip to content

Commit

Permalink
support LIKE case-insensitive matching
Browse files Browse the repository at this point in the history
  • Loading branch information
akostadinov committed Jan 12, 2022
1 parent abe8ace commit 9dc7556
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/arel/visitors/oracle.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# frozen_string_literal: true

require_relative "oracle_common"

module Arel # :nodoc: all
module Visitors
class Oracle < Arel::Visitors::ToSql
include OracleCommon

private
def visit_Arel_Nodes_SelectStatement(o, collector)
o = order_hacks(o)
Expand Down
4 changes: 4 additions & 0 deletions lib/arel/visitors/oracle12.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# frozen_string_literal: true

require_relative "oracle_common"

module Arel # :nodoc: all
module Visitors
class Oracle12 < Arel::Visitors::ToSql
include OracleCommon

private
def visit_Arel_Nodes_SelectStatement(o, collector)
# Oracle does not allow LIMIT clause with select for update
Expand Down
17 changes: 17 additions & 0 deletions lib/arel/visitors/oracle_common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Visitors
module OracleCommon
private
def visit_Arel_Nodes_Matches(o, collector)
if !o.case_sensitive && o.left && o.right
o.left = Arel::Nodes::NamedFunction.new("UPPER", [o.left])
o.right = Arel::Nodes::NamedFunction.new("UPPER", [o.right])
end

super o, collector
end
end
end
end
24 changes: 24 additions & 0 deletions spec/active_record/oracle_enhanced/type/character_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ::TestItem < ActiveRecord::Base
end

after(:each) do
TestItem.delete_all
Object.send(:remove_const, "TestItem")
ActiveRecord::Base.clear_cache!
end
Expand All @@ -40,4 +41,27 @@ class ::TestItem < ActiveRecord::Base
item_reloaded = TestItem.first
expect(item_reloaded.padded).to eq(str)
end

it "should support case sensitive matching" do
TestItem.create!(
padded: "First",
)
TestItem.create!(
padded: "first",
)

expect(TestItem.where(TestItem.arel_table[:padded].matches("first%", "\\", true))).to have_attributes(count: 1)
end

it "should support case insensitive matching" do
TestItem.create!(
padded: "First",
)
TestItem.create!(
padded: "first",
)

expect(TestItem.where(TestItem.arel_table[:padded].matches("first%", "\\", false))).to have_attributes(count: 2)
expect(TestItem.where(TestItem.arel_table[:padded].matches("first%"))).to have_attributes(count: 2)
end
end

0 comments on commit 9dc7556

Please sign in to comment.