Class: ScopedSearch::QueryBuilder::PostgreSQLAdapter

Inherits:
ScopedSearch::QueryBuilder show all
Defined in:
lib/scoped_search/query_builder.rb

Overview

The PostgreSQLAdapter make sure that searches are case sensitive when using the like/unlike operators, by using the PostrgeSQL-specific ILIKE operator instead of LIKE.

Constant Summary

Constant Summary

Constants inherited from ScopedSearch::QueryBuilder

SQL_OPERATORS

Instance Attribute Summary

Attributes inherited from ScopedSearch::QueryBuilder

#ast, #definition

Instance Method Summary (collapse)

Methods inherited from ScopedSearch::QueryBuilder

#build_find_params, build_query, class_for, #datetime_test, #has_many_through_join, #initialize, #set_test, #translate_value

Constructor Details

This class inherits a constructor from ScopedSearch::QueryBuilder

Instance Method Details

- (Object) order_by(order, &block)



545
546
547
548
549
# File 'lib/scoped_search/query_builder.rb', line 545

def order_by(order, &block)
  sql = super(order, &block)
  sql += sql.include?('DESC') ? ' NULLS LAST ' : ' NULLS FIRST ' if sql
  sql
end

- (Object) sql_operator(operator, field)

Switches out the default LIKE operator in the default sql_operator method for ILIKE or @@ if full text searching is enabled.



530
531
532
533
534
535
536
537
538
# File 'lib/scoped_search/query_builder.rb', line 530

def sql_operator(operator, field)
  raise ScopedSearch::QueryNotSupported, "the operator '#{operator}' is not supported for field type '#{field.type}'" if [:like, :unlike].include?(operator) and !field.textual?
  return '@@' if [:like, :unlike].include?(operator) and field.full_text_search
  case operator
    when :like   then 'ILIKE'
    when :unlike then 'NOT ILIKE'
    else super(operator, field)
  end
end

- (Object) sql_test(field, operator, value, lhs, &block)

Switches out the default query generation of the sql_test method if full text searching is enabled and a text search is being performed.



517
518
519
520
521
522
523
524
525
526
# File 'lib/scoped_search/query_builder.rb', line 517

def sql_test(field, operator, value, lhs, &block)
  if [:like, :unlike].include?(operator) and field.full_text_search
    yield(:parameter, value)
    negation = (operator == :unlike) ? "NOT " : ""
    locale = (field.full_text_search == true) ? 'english' : field.full_text_search
    return "#{negation}to_tsvector('#{locale}', #{field.to_sql(operator, &block)}) #{self.sql_operator(operator, field)} to_tsquery('#{locale}', ?)"
  else
    super
  end
end

- (Object) to_not_sql(rhs, definition, &block)

Returns a NOT (…) SQL fragment that negates the current AST node's children



541
542
543
# File 'lib/scoped_search/query_builder.rb', line 541

def to_not_sql(rhs, definition, &block)
  "NOT COALESCE(#{rhs.to_sql(self, definition, &block)}, false)"
end