Module: ScopedSearch::QueryBuilder::AST::OperatorNode
- Defined in:
- lib/scoped_search/query_builder.rb
Overview
Defines the to_sql method for AST operator nodes
Instance Method Summary (collapse)
-
- (Object) to_default_fields_sql(builder, definition, &block)
No explicit field name given, run the operator on all default fields.
-
- (Object) to_null_sql(builder, definition, &block)
Returns an IS (NOT) NULL SQL fragment.
-
- (Object) to_single_field_sql(builder, definition, &block)
Explicit field name given, run the operator on the specified field only.
-
- (Object) to_sql(builder, definition, &block)
Convert this AST node to an SQL fragment.
Instance Method Details
- (Object) to_default_fields_sql(builder, definition, &block)
No explicit field name given, run the operator on all default fields
385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/scoped_search/query_builder.rb', line 385 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 |
- (Object) to_null_sql(builder, definition, &block)
Returns an IS (NOT) NULL SQL fragment
371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/scoped_search/query_builder.rb', line 371 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 |
- (Object) to_single_field_sql(builder, definition, &block)
Explicit field name given, run the operator on the specified field only
400 401 402 403 404 405 406 407 408 |
# File 'lib/scoped_search/query_builder.rb', line 400 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 builder.sql_test(field, operator, rhs.value,lhs.value, &block) end |
- (Object) to_sql(builder, definition, &block)
Convert this AST node to an SQL fragment.
411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/scoped_search/query_builder.rb', line 411 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 |