Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

get rid of query_type state, determine type from sql rather than from…

… method
  • Loading branch information...
commit f3003ad13e0d2be5a00965cbd14b57edab3d9c41 1 parent 529020d
@dan-manges authored
View
46 lib/query_stats/holder.rb
@@ -21,31 +21,23 @@ class Holder
# Gets or sets the current label to be applied to queries for custom tracking.
# Including QueryStats in ApplicationController will label queries :controller or :view
attr_accessor :label
- # Gets the current query type
- attr_reader :query_type
# Creates a new instance of QueryStats::Holder with an empty array of stats.
def initialize
- @ignore_types = [
- :begin_db_transaction,
- :columns,
- :commit_db_transaction,
- :rollback_db_transaction
- ]
@stats = []
end
# Add data to the array of stats - should only be called by the active record connection adapter.
- def add(seconds, query, name = nil, *args) #:nodoc:
+ def add(sql, seconds, name = nil, *args) #:nodoc:
@stats.shift if @stats.size >= LIMIT
- return if @ignore_types.include?(@query_type)
+ query_type = query_type_from_sql(sql)
@stats << {
- :sql => query,
+ :sql => sql,
:name => name,
:label => @label,
:seconds => seconds,
- :type => @query_type
- }
+ :type => query_type
+ } if query_type
end
# Remove the current label and clear the array of stats.
@@ -69,12 +61,6 @@ def count_with_type(type)
with_type(type).length
end
- # Set the query type - this should only be called automatically from the connection adapter.
- def query_type=(sym) #:nodoc:
- @query_type = sym
- @query_type = :select if @query_type.to_s =~ /select/
- end
-
# Return an array of query statistics collected.
def stats
@stats
@@ -86,17 +72,31 @@ def runtime
end
alias :total_time :runtime
- # Returns an array of statistics for queries with a given label.
- # Set ignore to false to include transaction and column queries.
- def with_label(label, ignore = true)
+ def with_label(label)
stats = @stats.select { |q| q[:label] == label }
- ignore ? stats.reject { |q| @ignore_types.include?(q[:type]) } : stats
end
# Returns an array of statistics for queries with a given type.
def with_type(type)
@stats.select { |q| q[:type] == type }
end
+
+ protected
+
+ def query_type_from_sql(sql)
+ case sql.upcase
+ when /^SELECT/
+ :select
+ when /^INSERT/
+ :insert
+ when /^DELETE/
+ :delete
+ when /^UPDATE/
+ :update
+ else
+ nil
+ end
+ end
end
end
View
24 lib/query_stats/recorder.rb
@@ -3,28 +3,8 @@
module QueryStats
# Captures query data from ActiveRecord::Base.
module Recorder
- QUERY_METHODS = [
- :begin_db_transaction,
- :columns,
- :commit_db_transaction,
- :delete,
- :insert,
- :rollback_db_transaction,
- :select_all,
- :select_one,
- :select_value,
- :select_values,
- :update
- ]
def self.included(base) #:nodoc:
base.class_eval do
- QUERY_METHODS.each do |method|
- define_method("#{method}_with_query_stats") do |*args|
- queries.query_type = method
- send "#{method}_without_query_stats", *args
- end
- alias_method_chain method, :query_stats
- end
alias_method_chain :execute, :query_stats
end
end
@@ -41,8 +21,8 @@ def execute_with_query_stats(*args)
seconds = Benchmark.realtime do
result = execute_without_query_stats(*args)
end
- queries.add(seconds, *args)
+ queries.add(args.first, seconds, *args[1..-1])
result
end
end
-end
+end
View
2  test/query_stats_test.rb
@@ -1,4 +1,4 @@
-require File.expand_path(File.dirname(__FILE__) + "/test_helper")
+require File.dirname(__FILE__) + "/test_helper"
class QueryStatsTest < Test::Unit::TestCase
include QueryStats::Helper
Please sign in to comment.
Something went wrong with that request. Please try again.