Class: Shiba::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/shiba/analyzer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, stats, options) ⇒ Analyzer

Returns a new instance of Analyzer.



13
14
15
16
17
18
19
# File 'lib/shiba/analyzer.rb', line 13

def initialize(file, stats, options)
  @file = file
  @stats = stats
  @options = options
  @fingerprints = {}
  @queries = []
end

Class Method Details

.analyze(file, stats, options) ⇒ Object



9
10
11
# File 'lib/shiba/analyzer.rb', line 9

def self.analyze(file, stats, options)
  new(file, stats, options).analyze
end

Instance Method Details

#analyzeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shiba/analyzer.rb', line 21

def analyze
  idx = 0

  if @options['sql']
    analyze_sql(@options['sql'])
    return @queries
  end

  while line = @file.gets
    # strip out colors
    begin
      line.gsub!(/\e\[?.*?[\@-~]/, '')
    rescue ArgumentError => e
      next
    end

    if line =~ /(select.*from.*)/i
      sql = $1
    else
      next
    end

    sql.chomp!
    analyze_sql(sql)
  end
  @queries
end

#analyze_sql(sql) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shiba/analyzer.rb', line 49

def analyze_sql(sql)
  query = Shiba::Query.new(sql, @stats)

  if !@fingerprints[query.fingerprint]
    if sql.downcase.start_with?("select")
      explain = analyze_query(query)
      if explain
        @queries << explain
      end
    end
  end

  @fingerprints[query.fingerprint] = true
end