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?, #identifier?

Constructor Details

#initialize(identifier, attribute) ⇒ TagRegexpExpressionNode

Returns a new instance of TagRegexpExpressionNode.



371
372
373
374
375
# File 'lib/hotdog/commands/search.rb', line 371

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) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/hotdog/commands/search.rb', line 376

def evaluate(environment)
  if identifier?
    if attribute?
      case identifier
      when /\Ahost\z/i
        values = environment.execute(<<-EOS, attribute).map { |row| row.first }
          SELECT hosts_tags.id FROM hosts
            WHERE LOWER(hosts.name) REGEXP LOWER(?);
        EOS
      else
        environment.execute(<<-EOS, identifier, attribute).map { |row| row.first }
          SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
            INNER JOIN tags ON hosts_tags.tag_id = tags.id
              WHERE LOWER(tags.name) REGEXP LOWER(?) AND LOWER(tags.value) REGEXP LOWER(?);
        EOS
      end
    else
      environment.execute(<<-EOS, identifier, identifier, identifier).map { |row| row.first }
        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 LOWER(hosts.name) REGEXP LOWER(?) OR LOWER(tags.name) REGEXP LOWER(?) OR LOWER(tags.value) REGEXP LOWER(?);
      EOS
    end
  else
    if attribute?
      values = environment.execute(<<-EOS, attribute).map { |row| row.first }
        SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
          INNER JOIN tags ON hosts_tags.tag_id = tags.id
            WHERE LOWER(tags.value) REGEXP LOWER(?);
      EOS
    else
      values = []
    end
  end
  values
end