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 367
367:         def to_default_fields_sql(builder, definition, &block)
368:           raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
369: 
370:           # Search keywords found without context, just search on all the default fields
371:           fragments = definition.default_fields_for(rhs.value, operator).map { |field|
372:                           builder.sql_test(field, operator, rhs.value,'', &block) }.compact
373: 
374:           case fragments.length
375:             when 0 then nil
376:             when 1 then fragments.first
377:             else "#{fragments.join(' OR ')}"
378:           end
379:         end

Returns an IS (NOT) NULL SQL fragment

[Source]

     # File lib/scoped_search/query_builder.rb, line 353
353:         def to_null_sql(builder, definition, &block)
354:           field = definition.field_by_name(rhs.value)
355:           raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field
356: 
357:           if field.key_field
358:             yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
359:           end
360:           case operator
361:             when :null    then "#{field.to_sql(builder, &block)} IS NULL"
362:             when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
363:           end
364:         end

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

[Source]

     # File lib/scoped_search/query_builder.rb, line 382
382:         def to_single_field_sql(builder, definition, &block)
383:           raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
384:           raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
385: 
386:           # Search only on the given field.
387:           field = definition.field_by_name(lhs.value)
388:           raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field
389:           builder.sql_test(field, operator, rhs.value,lhs.value, &block)
390:         end

Convert this AST node to an SQL fragment.

[Source]

     # File lib/scoped_search/query_builder.rb, line 393
393:         def to_sql(builder, definition, &block)
394:           if operator == :not && children.length == 1
395:             builder.to_not_sql(rhs, definition, &block)
396:           elsif [:null, :notnull].include?(operator)
397:             to_null_sql(builder, definition, &block)
398:           elsif children.length == 1
399:             to_default_fields_sql(builder, definition, &block)
400:           elsif children.length == 2
401:             to_single_field_sql(builder, definition, &block)
402:           else
403:             raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
404:           end
405:         end

[Validate]