Module: ScopedSearch::QueryBuilder::AST::OperatorNode

Defined in:
lib/scoped_search/query_builder.rb

Overview

Defines the to_sql method for AST operator nodes

Instance Method Summary collapse

Instance Method Details

#to_default_fields_sql(builder, definition, &block) ⇒ Object

No explicit field name given, run the operator on all default fields



463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/scoped_search/query_builder.rb', line 463

def to_default_fields_sql(builder, definition, &block)
  raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)

  # Search keywords found without context, just search on all the default fields
  fragments = definition.default_fields_for(rhs.value, operator).map { |field|
                  builder.sql_test(field, operator, rhs.value,'', &block) }.compact

  case fragments.length
    when 0 then nil
    when 1 then fragments.first
    else "#{fragments.join(' OR ')}"
  end
end

#to_null_sql(builder, definition, &block) ⇒ Object

Returns an IS (NOT) NULL SQL fragment



449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/scoped_search/query_builder.rb', line 449

def to_null_sql(builder, definition, &block)
  field = definition.field_by_name(rhs.value)
  raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field

  if field.key_field
    yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
  end
  case operator
    when :null    then "#{field.to_sql(builder, &block)} IS NULL"
    when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
  end
end

#to_single_field_sql(builder, definition, &block) ⇒ Object

Explicit field name given, run the operator on the specified field only



478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/scoped_search/query_builder.rb', line 478

def to_single_field_sql(builder, definition, &block)
  raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
  raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)

  # Search only on the given field.
  field = definition.field_by_name(lhs.value)
  raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field

  # see if the value passes user defined validation
  validate_value(field, rhs.value)

  builder.sql_test(field, operator, rhs.value,lhs.value, &block)
end

#to_sql(builder, definition, &block) ⇒ Object

Convert this AST node to an SQL fragment.



493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/scoped_search/query_builder.rb', line 493

def to_sql(builder, definition, &block)
  if operator == :not && children.length == 1
    builder.to_not_sql(rhs, definition, &block)
  elsif [:null, :notnull].include?(operator)
    to_null_sql(builder, definition, &block)
  elsif children.length == 1
    to_default_fields_sql(builder, definition, &block)
  elsif children.length == 2
    to_single_field_sql(builder, definition, &block)
  else
    raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
  end
end