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
508 def to_default_fields_sql(builder, definition, &block)
509   raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
510 
511   # Search keywords found without context, just search on all the default fields
512   fragments = definition.default_fields_for(rhs.value, operator).map { |field|
513                   builder.sql_test(field, operator, rhs.value,'', &block) }.compact
514 
515   case fragments.length
516     when 0 then nil
517     when 1 then fragments.first
518     else "#{fragments.join(' OR ')}"
519   end
520 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
494 def to_null_sql(builder, definition, &block)
495   field = definition.field_by_name(rhs.value)
496   raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field
497 
498   if field.key_field
499     yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
500   end
501   case operator
502     when :null    then "#{field.to_sql(builder, &block)} IS NULL"
503     when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
504   end
505 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
523 def to_single_field_sql(builder, definition, &block)
524   raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
525   raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
526 
527   # Search only on the given field.
528   field = definition.field_by_name(lhs.value)
529   raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field
530 
531   # see if the value passes user defined validation
532   if [:in, :notin].include?(operator)
533     rhs.value.split(',').each { |v| validate_value(field, v) }
534   else
535     validate_value(field, rhs.value)
536   end
537 
538   builder.sql_test(field, operator, rhs.value,lhs.value, &block)
539 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
542 def to_sql(builder, definition, &block)
543   if operator == :not && children.length == 1
544     builder.to_not_sql(rhs, definition, &block)
545   elsif [:null, :notnull].include?(operator)
546     to_null_sql(builder, definition, &block)
547   elsif children.length == 1
548     to_default_fields_sql(builder, definition, &block)
549   elsif children.length == 2
550     to_single_field_sql(builder, definition, &block)
551   else
552     raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
553   end
554 end

Private Instance Methods

validate_value(field, value) click to toggle source
    # File lib/scoped_search/query_builder.rb
558 def validate_value(field, value)
559   validator = field.validator
560   if validator
561     valid = field.special_values.include?(value) || validator.call(value)
562     raise ScopedSearch::QueryNotSupported, "Value '#{value}' is not valid for field '#{field.field}'" unless valid
563   end
564 end