Class: Rack::Bug::SQLPanel::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bug/panels/sql_panel/query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql, time, backtrace = []) ⇒ Query

Returns a new instance of Query.



10
11
12
13
14
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 10

def initialize(sql, time, backtrace = [])
  @sql = sql
  @time = time
  @backtrace = backtrace
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



8
9
10
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 8

def backtrace
  @backtrace
end

#sqlObject (readonly)

Returns the value of attribute sql.



6
7
8
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 6

def sql
  @sql
end

#timeObject (readonly)

Returns the value of attribute time.



7
8
9
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 7

def time
  @time
end

Class Method Details

.execute(sql) ⇒ Object



55
56
57
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 55

def self.execute(sql)
  ActiveRecord::Base.connection.execute(sql)
end

Instance Method Details

#executeObject



46
47
48
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 46

def execute
  self.class.execute(@sql)
end

#explainObject



31
32
33
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 31

def explain
  self.class.execute "EXPLAIN #{@sql}"
end

#filtered_backtraceObject



63
64
65
66
67
68
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 63

def filtered_backtrace
  @filtered_backtrace ||= @backtrace.map { |l| l.to_s.strip }.select do |line|
    line.starts_with?(Rails.root) &&
    !line.starts_with?(Rails.root.join("vendor"))
  end
end

#has_backtrace?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 59

def has_backtrace?
  filtered_backtrace.any?
end

#human_timeObject



16
17
18
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 16

def human_time
  "%.2fms" % (@time * 1_000)
end

#inspectable?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 20

def inspectable?
  sql.strip =~ /^SELECT /i
end

#profileObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 35

def profile
  with_profiling do
    execute
    self.class.execute <<-SQL
      SELECT *
        FROM information_schema.profiling
       WHERE query_id = (SELECT query_id FROM information_schema.profiling ORDER BY query_id DESC LIMIT 1)
    SQL
  end
end

#valid_hash?(secret_key, possible_hash) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 50

def valid_hash?(secret_key, possible_hash)
  hash = Digest::SHA1.hexdigest [secret_key, @sql].join(":")
  possible_hash == hash
end

#with_profilingObject



24
25
26
27
28
29
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 24

def with_profiling
  self.class.execute("SET PROFILING=1")
  result = yield
  self.class.execute("SET PROFILING=0")
  return result
end