Class: Shiba::QueryWatcher
- Inherits:
-
Object
- Object
- Shiba::QueryWatcher
- Defined in:
- lib/shiba/query_watcher.rb
Overview
Logs ActiveRecord SELECT queries that originate from application code.
Instance Attribute Summary collapse
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
Instance Method Summary collapse
- #call(name, start, finish, id, payload) ⇒ Object
-
#initialize(file) ⇒ QueryWatcher
constructor
A new instance of QueryWatcher.
- #interpolate(sql, binds) ⇒ Object
Constructor Details
#initialize(file) ⇒ QueryWatcher
Returns a new instance of QueryWatcher.
10 11 12 13 14 |
# File 'lib/shiba/query_watcher.rb', line 10 def initialize(file) @file = file # fixme mem growth on this is kinda nasty @queries = {} end |
Instance Attribute Details
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
8 9 10 |
# File 'lib/shiba/query_watcher.rb', line 8 def queries @queries end |
Instance Method Details
#call(name, start, finish, id, payload) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/shiba/query_watcher.rb', line 16 def call(name, start, finish, id, payload) sql = payload[:sql] return if !sql.start_with?("SELECT") if sql.include?("$1") sql = interpolate(sql, payload[:type_casted_binds]) end sql = sql.gsub(/\n/, ' ') fingerprint = Query.get_fingerprint(sql) return if @queries[fingerprint] lines = Backtrace.from_app return if !lines @file.puts("#{sql} /*shiba#{lines}*/") @queries[fingerprint] = true end |
#interpolate(sql, binds) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/shiba/query_watcher.rb', line 36 def interpolate(sql, binds) binds.each_with_index do |val, i| sql = sql.sub("$#{i +1}", ActiveRecord::Base.connection.quote(val)) end sql end |