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

Private Instance Methods

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