Class: Hotdog::Commands::Search::TagGlobExpressionNode

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

Constructor Details

This class inherits a constructor from Hotdog::Commands::Search::TagExpressionNode

Instance Method Details

#evaluate(environment) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/hotdog/commands/search.rb', line 253

def evaluate(environment)
  if attribute?
    values = 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) GLOB LOWER(?) AND LOWER(tags.value) GLOB LOWER(?);
    EOS
  else
    values = 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) GLOB LOWER(?) OR LOWER(tags.name) GLOB LOWER(?) OR LOWER(tags.value) GLOB LOWER(?);

    EOS
  end
  if not environment.fixed_string? and values.empty?
    # fallback to glob expression
    identifier_glob = identifier.gsub(/[-.\/_]/, "?")
    if identifier != identifier_glob
      if attribute?
        attribute_glob = attribute.gsub(/[-.\/:_]/, "?")
        environment.logger.info("fallback to glob expression: %s:%s" % [identifier_glob, attribute_glob])
      else
        attribute_glob = nil
        environment.logger.info("fallback to glob expression: %s" % [identifier_glob])
      end
      values = TagGlobExpressionNode.new(identifier_glob, attribute_glob).evaluate(environment)
    end
  end
  values
end