Module ScopedSearch::QueryBuilder::AST::OperatorNode
In: lib/scoped_search/query_builder.rb

Defines the to_sql method for AST operator nodes

Methods

Public Instance methods

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

[Source]

     # File lib/scoped_search/query_builder.rb, line 378
378:         def to_default_fields_sql(builder, definition, &block)
379:           raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
380: 
381:           # Search keywords found without context, just search on all the default fields
382:           fragments = definition.default_fields_for(rhs.value, operator).map { |field|
383:                           builder.sql_test(field, operator, rhs.value,'', &block) }.compact
384: 
385:           case fragments.length
386:             when 0 then nil
387:             when 1 then fragments.first
388:             else "#{fragments.join(' OR ')}"
389:           end
390:         end

Returns an IS (NOT) NULL SQL fragment

[Source]

     # File lib/scoped_search/query_builder.rb, line 364
364:         def to_null_sql(builder, definition, &block)
365:           field = definition.field_by_name(rhs.value)
366:           raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field
367: 
368:           if field.key_field
369:             yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
370:           end
371:           case operator
372:             when :null    then "#{field.to_sql(builder, &block)} IS NULL"
373:             when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
374:           end
375:         end

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

[Source]

     # File lib/scoped_search/query_builder.rb, line 393
393:         def to_single_field_sql(builder, definition, &block)
394:           raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
395:           raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
396: 
397:           # Search only on the given field.
398:           field = definition.field_by_name(lhs.value)
399:           raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field
400:           builder.sql_test(field, operator, rhs.value,lhs.value, &block)
401:         end

Convert this AST node to an SQL fragment.

[Source]

     # File lib/scoped_search/query_builder.rb, line 404
404:         def to_sql(builder, definition, &block)
405:           if operator == :not && children.length == 1
406:             builder.to_not_sql(rhs, definition, &block)
407:           elsif [:null, :notnull].include?(operator)
408:             to_null_sql(builder, definition, &block)
409:           elsif children.length == 1
410:             to_default_fields_sql(builder, definition, &block)
411:           elsif children.length == 2
412:             to_single_field_sql(builder, definition, &block)
413:           else
414:             raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
415:           end
416:         end

[Validate]