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
-
#to_default_fields_sql(builder, definition, &block) ⇒ Object
No explicit field name given, run the operator on all default fields.
-
#to_null_sql(builder, definition, &block) ⇒ Object
Returns an IS (NOT) NULL SQL fragment.
-
#to_single_field_sql(builder, definition, &block) ⇒ Object
Explicit field name given, run the operator on the specified field only.
-
#to_sql(builder, definition, &block) ⇒ Object
Convert this AST node to an SQL fragment.
Instance Method Details
#to_default_fields_sql(builder, definition, &block) ⇒ Object
No explicit field name given, run the operator on all default fields
511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/scoped_search/query_builder.rb', line 511 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, &block) ⇒ Object
Returns an IS (NOT) NULL SQL fragment
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/scoped_search/query_builder.rb', line 492 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.virtual? sql_operator = operator == :notnull ? 'IS NOT NULL' : 'IS NULL' return field.to_ext_method_sql(rhs.value, sql_operator, nil, &block) end 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) ⇒ Object
Explicit field name given, run the operator on the specified field only
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/scoped_search/query_builder.rb', line 526 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 if [:in, :notin].include?(operator) rhs.value.split(',').each { |v| validate_value(field, v) } else validate_value(field, rhs.value) end builder.sql_test(field, operator, rhs.value,lhs.value, &block) end |
#to_sql(builder, definition, &block) ⇒ Object
Convert this AST node to an SQL fragment.
545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/scoped_search/query_builder.rb', line 545 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 |