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
486 def to_default_fields_sql(builder, definition, &block)
487   raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
488 
489   # Search keywords found without context, just search on all the default fields
490   fragments = definition.default_fields_for(rhs.value, operator).map { |field|
491                   builder.sql_test(field, operator, rhs.value,'', &block) }.compact
492 
493   case fragments.length
494     when 0 then nil
495     when 1 then fragments.first
496     else "#{fragments.join(' OR ')}"
497   end
498 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
472 def to_null_sql(builder, definition, &block)
473   field = definition.field_by_name(rhs.value)
474   raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field
475 
476   if field.key_field
477     yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
478   end
479   case operator
480     when :null    then "#{field.to_sql(builder, &block)} IS NULL"
481     when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
482   end
483 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
501 def to_single_field_sql(builder, definition, &block)
502   raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
503   raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
504 
505   # Search only on the given field.
506   field = definition.field_by_name(lhs.value)
507   raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field
508 
509   # see if the value passes user defined validation
510   if operator == :in
511     rhs.value.split(',').each { |v| validate_value(field, v) }
512   else
513     validate_value(field, rhs.value)
514   end
515 
516   builder.sql_test(field, operator, rhs.value,lhs.value, &block)
517 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
520 def to_sql(builder, definition, &block)
521   if operator == :not && children.length == 1
522     builder.to_not_sql(rhs, definition, &block)
523   elsif [:null, :notnull].include?(operator)
524     to_null_sql(builder, definition, &block)
525   elsif children.length == 1
526     to_default_fields_sql(builder, definition, &block)
527   elsif children.length == 2
528     to_single_field_sql(builder, definition, &block)
529   else
530     raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
531   end
532 end

Private Instance Methods

validate_value(field, value) click to toggle source
    # File lib/scoped_search/query_builder.rb
536 def validate_value(field, value)
537   validator = field.validator
538   if validator
539     valid = field.special_values.include?(value) || validator.call(value)
540     raise ScopedSearch::QueryNotSupported, "Value '#{value}' is not valid for field '#{field.field}'" unless valid
541   end
542 end