module ScopedSearch::QueryBuilder::AST::OperatorNode

Defines the to_sql method for AST operator nodes

Public Instance Methods

to_default_fields_sql(builder, definition, &block) click to toggle source

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

    # File lib/scoped_search/query_builder.rb
449 def to_default_fields_sql(builder, definition, &block)
450   raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
451 
452   # Search keywords found without context, just search on all the default fields
453   fragments = definition.default_fields_for(rhs.value, operator).map { |field|
454                   builder.sql_test(field, operator, rhs.value,'', &block) }.compact
455 
456   case fragments.length
457     when 0 then nil
458     when 1 then fragments.first
459     else "#{fragments.join(' OR ')}"
460   end
461 end
to_null_sql(builder, definition) { |:parameter, value.sub(/^.*\./,'')| ... } click to toggle source

Returns an IS (NOT) NULL SQL fragment

    # File lib/scoped_search/query_builder.rb
435 def to_null_sql(builder, definition, &block)
436   field = definition.field_by_name(rhs.value)
437   raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field
438 
439   if field.key_field
440     yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
441   end
442   case operator
443     when :null    then "#{field.to_sql(builder, &block)} IS NULL"
444     when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
445   end
446 end
to_single_field_sql(builder, definition, &block) click to toggle source

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

    # File lib/scoped_search/query_builder.rb
464 def to_single_field_sql(builder, definition, &block)
465   raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
466   raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
467 
468   # Search only on the given field.
469   field = definition.field_by_name(lhs.value)
470   raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field
471 
472   # see if the value passes user defined validation
473   validate_value(field, rhs.value)
474 
475   builder.sql_test(field, operator, rhs.value,lhs.value, &block)
476 end
to_sql(builder, definition, &block) click to toggle source

Convert this AST node to an SQL fragment.

    # File lib/scoped_search/query_builder.rb
479 def to_sql(builder, definition, &block)
480   if operator == :not && children.length == 1
481     builder.to_not_sql(rhs, definition, &block)
482   elsif [:null, :notnull].include?(operator)
483     to_null_sql(builder, definition, &block)
484   elsif children.length == 1
485     to_default_fields_sql(builder, definition, &block)
486   elsif children.length == 2
487     to_single_field_sql(builder, definition, &block)
488   else
489     raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
490   end
491 end

Private Instance Methods

validate_value(field, value) click to toggle source
    # File lib/scoped_search/query_builder.rb
495 def validate_value(field, value)
496   validator = field.validator
497   if validator
498     valid = validator.call(value)
499     raise ScopedSearch::QueryNotSupported, "Value '#{value}' is not valid for field '#{field.field}'" unless valid
500   end
501 end