Class: Rack::Bug::SQLPanel::Query
- Inherits:
-
Object
- Object
- Rack::Bug::SQLPanel::Query
- Defined in:
- lib/rack/bug/panels/sql_panel/query.rb
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
readonly
Returns the value of attribute backtrace.
-
#sql ⇒ Object
readonly
Returns the value of attribute sql.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
- #explain ⇒ Object
- #filtered_backtrace ⇒ Object
- #has_backtrace? ⇒ Boolean
- #human_time ⇒ Object
-
#initialize(sql, time, backtrace = []) ⇒ Query
constructor
A new instance of Query.
- #inspectable? ⇒ Boolean
- #profile ⇒ Object
- #valid_hash?(secret_key, possible_hash) ⇒ Boolean
- #with_profiling ⇒ Object
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
#backtrace ⇒ Object (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 |
#sql ⇒ Object (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 |
#time ⇒ Object (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
#execute ⇒ Object
46 47 48 |
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 46 def execute self.class.execute(@sql) end |
#explain ⇒ Object
31 32 33 |
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 31 def explain self.class.execute "EXPLAIN #{@sql}" end |
#filtered_backtrace ⇒ Object
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
59 60 61 |
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 59 def has_backtrace? filtered_backtrace.any? end |
#human_time ⇒ Object
16 17 18 |
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 16 def human_time "%.2fms" % (@time * 1_000) end |
#inspectable? ⇒ Boolean
20 21 22 |
# File 'lib/rack/bug/panels/sql_panel/query.rb', line 20 def inspectable? sql.strip =~ /^SELECT /i end |
#profile ⇒ Object
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
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_profiling ⇒ Object
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 |