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

- (Object) to_default_fields_sql(builder, definition, &block)

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



429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/scoped_search/query_builder.rb', line 429

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

- (Object) to_null_sql(builder, definition, &block)

Returns an IS (NOT) NULL SQL fragment



415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/scoped_search/query_builder.rb', line 415

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

- (Object) to_single_field_sql(builder, definition, &block)

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



444
445
446
447
448
449
450
451
452
# File 'lib/scoped_search/query_builder.rb', line 444

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
  builder.sql_test(field, operator, rhs.value,lhs.value, &block)
end

- (Object) to_sql(builder, definition, &block)

Convert this AST node to an SQL fragment.



455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/scoped_search/query_builder.rb', line 455

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