Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ILIKE for model.arel_table[:column]#matches #115

Merged
merged 1 commit into from
Jan 31, 2024
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
10 changes: 10 additions & 0 deletions lib/arel/visitors/clickhouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def visit_Arel_Nodes_Using o, collector
collector
end

def visit_Arel_Nodes_Matches(o, collector)
op = o.case_sensitive ? " LIKE " : " ILIKE "
infix_value o, collector, op
end

def visit_Arel_Nodes_DoesNotMatch(o, collector)
op = o.case_sensitive ? " NOT LIKE " : " NOT ILIKE "
infix_value o, collector, op
end

def sanitize_as_setting_value(value)
if value == :default
'DEFAULT'
Expand Down
14 changes: 14 additions & 0 deletions spec/cases/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ class Model < ActiveRecord::Base
end
end

describe 'arel predicates' do
describe '#matches' do
it 'uses ilike for case insensitive matches' do
sql = model.where(model.arel_table[:event_name].matches('some event')).to_sql
expect(sql).to eq("SELECT sample.* FROM sample WHERE sample.event_name ILIKE 'some event'")
end

it 'uses like for case sensitive matches' do
sql = model.where(model.arel_table[:event_name].matches('some event', nil, true)).to_sql
expect(sql).to eq("SELECT sample.* FROM sample WHERE sample.event_name LIKE 'some event'")
end
end
end

describe 'DateTime64 create' do
it 'create a new record' do
time = DateTime.parse('2023-07-21 08:00:00.123')
Expand Down