1452 |
1452 |
# * :starts_with - use LIKE 'value%' if true
|
1453 |
1453 |
# * :ends_with - use LIKE '%value' if true
|
1454 |
1454 |
# * :all_words - use OR instead of AND if false
|
|
1455 |
# (ignored if :starts_with or :ends_with is true)
|
1455 |
1456 |
def sql_contains(db_field, value, options={})
|
1456 |
1457 |
options = {} unless options.is_a?(Hash)
|
1457 |
1458 |
options.symbolize_keys!
|
1458 |
|
prefix = suffix = nil
|
1459 |
|
prefix = '%' if options[:ends_with]
|
1460 |
|
suffix = '%' if options[:starts_with]
|
1461 |
|
if prefix || suffix
|
1462 |
|
value = queried_class.sanitize_sql_like value
|
1463 |
|
queried_class.sanitize_sql_for_conditions(
|
1464 |
|
[Redmine::Database.like(db_field, '?', :match => options[:match]), "#{prefix}#{value}#{suffix}"]
|
1465 |
|
)
|
1466 |
|
else
|
1467 |
|
queried_class.sanitize_sql_for_conditions(
|
1468 |
|
::Query.tokenized_like_conditions(db_field, value, **options)
|
1469 |
|
)
|
1470 |
|
end
|
|
1459 |
queried_class.sanitize_sql_for_conditions(
|
|
1460 |
::Query.tokenized_like_conditions(db_field, value, **options)
|
|
1461 |
)
|
1471 |
1462 |
end
|
1472 |
1463 |
|
1473 |
1464 |
# rubocop:disable Lint/IneffectiveAccessModifier
|
1474 |
1465 |
def self.tokenized_like_conditions(db_field, value, **options)
|
1475 |
1466 |
tokens = Redmine::Search::Tokenizer.new(value).tokens
|
1476 |
1467 |
tokens = [value] unless tokens.present?
|
1477 |
|
logical_opr = options.delete(:all_words) == false ? ' OR ' : ' AND '
|
|
1468 |
|
|
1469 |
if options[:starts_with]
|
|
1470 |
prefix, suffix = nil, '%'
|
|
1471 |
logical_opr = ' OR '
|
|
1472 |
elsif options[:ends_with]
|
|
1473 |
prefix, suffix = '%', nil
|
|
1474 |
logical_opr = ' OR '
|
|
1475 |
else
|
|
1476 |
prefix = suffix = '%'
|
|
1477 |
logical_opr = options[:all_words] == false ? ' OR ' : ' AND '
|
|
1478 |
end
|
|
1479 |
|
1478 |
1480 |
sql, values = tokens.map do |token|
|
1479 |
|
[Redmine::Database.like(db_field, '?', options), "%#{sanitize_sql_like token}%"]
|
|
1481 |
[Redmine::Database.like(db_field, '?', options), "#{prefix}#{sanitize_sql_like token}#{suffix}"]
|
1480 |
1482 |
end.transpose
|
1481 |
1483 |
[sql.join(logical_opr), *values]
|
1482 |
1484 |
end
|