Class: DbTextSearch::CaseInsensitive::LowerAdapter Private

Inherits:
AbstractAdapter show all
Defined in:
lib/db_text_search/case_insensitive/lower_adapter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides case-insensitive string-in-set querying by applying the database LOWER function.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#initialize

Methods included from QueryBuilding

included, #sanitize_sql_like

Constructor Details

This class inherits a constructor from DbTextSearch::CaseInsensitive::AbstractAdapter

Class Method Details

.add_index(connection, table_name, column_name, options = {}) ⇒ Object

This method is abstract.

Add an index for case-insensitive string search.

Parameters:

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter)
  • table_name (String, Symbol)
  • column_name (String, Symbol)
  • options (Hash) (defaults to: {})

    passed down to ActiveRecord::ConnectionAdapters::SchemaStatements#add_index.

Options Hash (options):

  • name (String)

    index name

  • unique (Boolean)

    default: false



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/db_text_search/case_insensitive/lower_adapter.rb', line 26

def self.add_index(connection, table_name, column_name, options = {})
  unsupported = -> { DbTextSearch.unsupported_adapter! connection }
  DbTextSearch.match_adapter(
      connection,
      # TODO: Switch to native Rails support once it lands.
      # https://github.com/rails/rails/pull/18499
      postgres: -> {
        options              = options.dup
        options[:name]       ||= "#{table_name}_#{column_name}_lower"
        options[:expression] = "(LOWER(#{connection.quote_column_name(column_name)}) text_pattern_ops)"
        connection.exec_query(quoted_create_index(connection, table_name, **options))
      },
      mysql:    unsupported,
      sqlite:   unsupported
  )
end

Instance Method Details

#column_for_order(asc_or_desc) ⇒ Arel::Collectors::SQLString

This method is abstract.

Parameters:

  • asc_or_desc (Symbol)

Returns:

  • (Arel::Collectors::SQLString)


21
22
23
# File 'lib/db_text_search/case_insensitive/lower_adapter.rb', line 21

def column_for_order(asc_or_desc)
  Arel.sql("LOWER(#{quoted_scope_column}) #{asc_or_desc}")
end

#in(values) ⇒ ActiveRecord::Relation

This method is abstract.

Parameters:

  • values (Array<String>)

Returns:

  • (ActiveRecord::Relation)


10
11
12
13
# File 'lib/db_text_search/case_insensitive/lower_adapter.rb', line 10

def in(values)
  conn = @scope.connection
  @scope.where "LOWER(#{quoted_scope_column}) IN (#{values.map { |v| "LOWER(#{conn.quote(v)})" }.join(', ')})"
end

#prefix(query) ⇒ ActiveRecord::Relation

This method is abstract.

Parameters:

  • query (String)

Returns:

  • (ActiveRecord::Relation)


16
17
18
# File 'lib/db_text_search/case_insensitive/lower_adapter.rb', line 16

def prefix(query)
  @scope.where "LOWER(#{quoted_scope_column}) LIKE LOWER(?)", "#{sanitize_sql_like(query)}%"
end