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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ExpressionNode

#intermediates, #leafs

Constructor Details

#initialize(identifier, attribute, separator = nil) ⇒ TagExpressionNode

Returns a new instance of TagExpressionNode.



803
804
805
806
807
808
# File 'lib/hotdog/commands/search.rb', line 803

def initialize(identifier, attribute, separator=nil)
  @identifier = identifier
  @attribute = attribute
  @separator = separator
  @fallback = nil
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



810
811
812
# File 'lib/hotdog/commands/search.rb', line 810

def attribute
  @attribute
end

#identifierObject (readonly)

Returns the value of attribute identifier.



809
810
811
# File 'lib/hotdog/commands/search.rb', line 809

def identifier
  @identifier
end

#separatorObject (readonly)

Returns the value of attribute separator.



811
812
813
# File 'lib/hotdog/commands/search.rb', line 811

def separator
  @separator
end

Instance Method Details

#==(other) ⇒ Object



908
909
910
# File 'lib/hotdog/commands/search.rb', line 908

def ==(other)
  self.class == other.class and @identifier == other.identifier and @attribute == other.attribute
end

#attribute?Boolean

Returns:

  • (Boolean)


817
818
819
# File 'lib/hotdog/commands/search.rb', line 817

def attribute?
  !(attribute.nil? or attribute.to_s.empty?)
end

#condition(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


852
853
854
# File 'lib/hotdog/commands/search.rb', line 852

def condition(options={})
  raise(NotImplementedError.new("must be overridden"))
end

#condition_tables(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


856
857
858
# File 'lib/hotdog/commands/search.rb', line 856

def condition_tables(options={})
  raise(NotImplementedError.new("must be overridden"))
end

#condition_values(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


860
861
862
# File 'lib/hotdog/commands/search.rb', line 860

def condition_values(options={})
  raise(NotImplementedError.new("must be overridden"))
end

#dump(options = {}) ⇒ Object



938
939
940
941
942
943
944
945
# File 'lib/hotdog/commands/search.rb', line 938

def dump(options={})
  data = {}
  data[:identifier] = identifier.to_s if identifier
  data[:separator] = separator.to_s if separator
  data[:attribute] = attribute.to_s if attribute
  data[:fallback ] = @fallback.dump(options) if @fallback
  data
end

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



864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
# File 'lib/hotdog/commands/search.rb', line 864

def evaluate(environment, options={})
  q = maybe_query(options)
  if q
    values = environment.execute(q, condition_values(options)).map { |row| row.first }
    if values.empty?
      if options[:did_fallback]
        []
      else
        if not environment.fixed_string? and @fallback
          # avoid optimizing @fallback to prevent infinite recursion
          values = @fallback.evaluate(environment, options.merge(did_fallback: true))
          if values.empty?
            if reload(environment, options)
              evaluate(environment, options).tap do |values|
                if values.empty?
                  environment.logger.info("no result: #{self.dump.inspect}")
                end
              end
            else
              []
            end
          else
            values
          end
        else
          if reload(environment, options)
            evaluate(environment, options).tap do |values|
              if values.empty?
                environment.logger.info("no result: #{self.dump.inspect}")
              end
            end
          else
            []
          end
        end
      end
    else
      values
    end
  else
    []
  end
end

#identifier?Boolean

Returns:

  • (Boolean)


813
814
815
# File 'lib/hotdog/commands/search.rb', line 813

def identifier?
  !(identifier.nil? or identifier.to_s.empty?)
end

#maybe_fallback(options = {}) ⇒ Object



947
948
949
# File 'lib/hotdog/commands/search.rb', line 947

def maybe_fallback(options={})
  nil
end

#maybe_glob(s) ⇒ Object



922
923
924
# File 'lib/hotdog/commands/search.rb', line 922

def maybe_glob(s)
  s ? to_glob(s.to_s) : nil
end

#maybe_query(options = {}) ⇒ Object



825
826
827
828
829
830
831
832
# File 'lib/hotdog/commands/search.rb', line 825

def maybe_query(options={})
  query_without_condition = maybe_query_without_condition(options)
  if query_without_condition
    query_without_condition.sub(/\s*;\s*\z/, "") + " WHERE " + condition(options) + ";"
  else
    nil
  end
end

#maybe_query_without_condition(options = {}) ⇒ Object



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/hotdog/commands/search.rb', line 834

def maybe_query_without_condition(options={})
  tables = condition_tables(options)
  if tables.empty?
    nil
  else
    case tables
    when [:hosts]
      "SELECT hosts.id AS host_id FROM hosts;"
    when [:hosts, :tags]
      "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;"
    when [:tags]
      "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags INNER JOIN tags ON hosts_tags.tag_id = tags.id;"
    else
      raise(NotImplementedError.new("unknown tables: #{tables.join(", ")}"))
    end
  end
end

#optimize(options = {}) ⇒ Object



912
913
914
915
916
# File 'lib/hotdog/commands/search.rb', line 912

def optimize(options={})
  # fallback to glob expression
  @fallback = maybe_fallback(options)
  self
end

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



926
927
928
929
930
931
932
933
934
935
936
# File 'lib/hotdog/commands/search.rb', line 926

def reload(environment, options={})
  $did_reload ||= false
  if $did_reload
    false
  else
    $did_reload = true
    environment.logger.info("force reloading all hosts and tags.")
    environment.reload(force: true)
    true
  end
end

#separator?Boolean

Returns:

  • (Boolean)


821
822
823
# File 'lib/hotdog/commands/search.rb', line 821

def separator?
  !(separator.nil? or separator.to_s.empty?)
end

#to_glob(s) ⇒ Object



918
919
920
# File 'lib/hotdog/commands/search.rb', line 918

def to_glob(s)
  (s.start_with?("*") ? "" : "*") + s.gsub(/[-.\/_]/, "?") + (s.end_with?("*") ? "" : "*")
end