Skip to content

Commit

Permalink
get rid of query_type state, determine type from sql rather than from…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
dan-manges committed Aug 24, 2008
1 parent 529020d commit f3003ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
46 changes: 23 additions & 23 deletions lib/query_stats/holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
24 changes: 2 additions & 22 deletions lib/query_stats/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion test/query_stats_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f3003ad

Please sign in to comment.