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, line 449
def to_default_fields_sql(builder, definition, &block)
  raise ScopedSearch::QueryNotSupported, "Value not a leaf node" unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)

  # Search keywords found without context, just search on all the default fields
  fragments = definition.default_fields_for(rhs.value, operator).map { |field|
                  builder.sql_test(field, operator, rhs.value,'', &block) }.compact

  case fragments.length
    when 0 then nil
    when 1 then fragments.first
    else "#{fragments.join(' OR ')}"
  end
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, line 435
def to_null_sql(builder, definition, &block)
  field = definition.field_by_name(rhs.value)
  raise ScopedSearch::QueryNotSupported, "Field '#{rhs.value}' not recognized for searching!" unless field

  if field.key_field
    yield(:parameter, rhs.value.to_s.sub(/^.*\./,''))
  end
  case operator
    when :null    then "#{field.to_sql(builder, &block)} IS NULL"
    when :notnull then "#{field.to_sql(builder, &block)} IS NOT NULL"
  end
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, line 464
def to_single_field_sql(builder, definition, &block)
  raise ScopedSearch::QueryNotSupported, "Field name not a leaf node" unless lhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)
  raise ScopedSearch::QueryNotSupported, "Value not a leaf node"      unless rhs.kind_of?(ScopedSearch::QueryLanguage::AST::LeafNode)

  # Search only on the given field.
  field = definition.field_by_name(lhs.value)
  raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field

  # see if the value passes user defined validation
  validate_value(field, rhs.value)

  builder.sql_test(field, operator, rhs.value,lhs.value, &block)
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, line 479
def to_sql(builder, definition, &block)
  if operator == :not && children.length == 1
    builder.to_not_sql(rhs, definition, &block)
  elsif [:null, :notnull].include?(operator)
    to_null_sql(builder, definition, &block)
  elsif children.length == 1
    to_default_fields_sql(builder, definition, &block)
  elsif children.length == 2
    to_single_field_sql(builder, definition, &block)
  else
    raise ScopedSearch::QueryNotSupported, "Don't know how to handle this operator node: #{operator.inspect} with #{children.inspect}!"
  end
end

Private Instance Methods

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