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

Returns an IS (NOT) NULL SQL fragment

[Source]

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

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

[Source]

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

Convert this AST node to an SQL fragment.

[Source]

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

[Validate]