Class: Hotdog::Commands::Search::TagRegexpExpressionNode

Inherits:
TagExpressionNode show all
Defined in:
lib/hotdog/commands/search.rb

Instance Attribute Summary

Attributes inherited from TagExpressionNode

#attribute, #identifier

Instance Method Summary collapse

Methods inherited from TagExpressionNode

#==, #attribute?, #fallback, #identifier?, #reload

Methods inherited from ExpressionNode

#optimize

Constructor Details

#initialize(identifier, attribute) ⇒ TagRegexpExpressionNode

Returns a new instance of TagRegexpExpressionNode.



484
485
486
487
488
# File 'lib/hotdog/commands/search.rb', line 484

def initialize(identifier, attribute)
  identifier = identifier.sub(%r{\A/(.*)/\z}) { $1 } if identifier
  attribute = attribute.sub(%r{\A/(.*)/\z}) { $1 } if attribute
  super(identifier, attribute)
end

Instance Method Details

#evaluate(environment, options = {}) ⇒ Object



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/hotdog/commands/search.rb', line 490

def evaluate(environment, options={})
  if identifier?
    if attribute?
      case identifier
      when /\Ahost\z/i
        q = "SELECT hosts.id FROM hosts " \
              "WHERE hosts.name REGEXP ?;"
        values = environment.execute(q, [attribute]).map { |row| row.first }
      else
        q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
              "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
                "WHERE tags.name REGEXP ? AND tags.value REGEXP ?;"
        values = environment.execute(q, [identifier, attribute]).map { |row| row.first }
      end
    else
      q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
            "INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
            "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
              "WHERE hosts.name REGEXP ? OR tags.name REGEXP ? OR tags.value REGEXP ?;"
      values = environment.execute(q, [identifier, identifier, identifier]).map { |row| row.first }
    end
  else
    if attribute?
      q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
            "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
              "WHERE tags.value REGEXP ?;"
      values = environment.execute(q, [attribute]).map { |row| row.first }
    else
      return []
    end
  end
  if values.empty?
    reload(environment)
  else
    values
  end
end