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

Private Instance Methods

validate_value(field, value) click to toggle source
    # File lib/scoped_search/query_builder.rb
529 def validate_value(field, value)
530   validator = field.validator
531   if validator
532     valid = field.special_values.include?(value) || validator.call(value)
533     raise ScopedSearch::QueryNotSupported, "Value '#{value}' is not valid for field '#{field.field}'" unless valid
534   end
535 end