Class: Hotdog::Commands::Search::TagExpressionNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, attribute) ⇒ TagExpressionNode

Returns a new instance of TagExpressionNode.



261
262
263
264
# File 'lib/hotdog/commands/search.rb', line 261

def initialize(identifier, attribute)
  @identifier = identifier
  @attribute = attribute
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



266
267
268
# File 'lib/hotdog/commands/search.rb', line 266

def attribute
  @attribute
end

#identifierObject (readonly)

Returns the value of attribute identifier.



265
266
267
# File 'lib/hotdog/commands/search.rb', line 265

def identifier
  @identifier
end

Instance Method Details

#attribute?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/hotdog/commands/search.rb', line 270

def attribute?
  !attribute.nil?
end

#evaluate(environment) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/hotdog/commands/search.rb', line 273

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) = LOWER(?);
        EOS
      else
        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) = LOWER(?) AND LOWER(tags.value) = LOWER(?);
        EOS
      end
    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) = LOWER(?) OR LOWER(tags.name) = LOWER(?) OR LOWER(tags.value) = 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) = LOWER(?);
      EOS
    else
      values = []
    end
  end
  if not environment.fixed_string? and values.empty?
    # fallback to glob expression
    identifier_glob = identifier.gsub(/[-.\/_]/, "?") if identifier?
    attribute_glob = attribute.gsub(/[-.\/_]/, "?") if attribute?
    if (identifier? and identifier != identifier_glob) or (attribute? and attribute != attribute_glob)
      environment.logger.info("fallback to glob expression: %s:%s" % [identifier_glob, attribute_glob])
      values = TagGlobExpressionNode.new(identifier_glob, attribute_glob).evaluate(environment)
    end
  end
  values
end

#identifier?Boolean

Returns:

  • (Boolean)


267
268
269
# File 'lib/hotdog/commands/search.rb', line 267

def identifier?
  !identifier.nil?
end