Sha256: 8376bfba369efc44052675ded419ba36a9dab702974c9891849ba27e13cc0c42

Contents?: true

Size: 914 Bytes

Versions: 12

Compression:

Stored size: 914 Bytes

Contents

module ActiveRecord
  class QueryCounter
    cattr_accessor :query_count do
      0
    end

    IGNORED_SQL = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/]

    def call(name, start, finish, message_id, values)
      # FIXME: this seems bad. we should probably have a better way to indicate
      # the query was cached
      unless 'CACHE' == values[:name]
        self.class.query_count += 1 unless IGNORED_SQL.any? { |r| values[:sql] =~ r }
      end
    end
  end
end

ActiveSupport::Notifications.subscribe('sql.active_record', ActiveRecord::QueryCounter.new)

module ActiveRecord
  class Base
    def self.count_queries(&block)
      ActiveRecord::QueryCounter.query_count = 0
      yield
      ActiveRecord::QueryCounter.query_count
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
listings-0.1.15 spec/support/query_counter.rb
listings-0.1.14 spec/support/query_counter.rb
listings-0.1.13 spec/support/query_counter.rb
listings-0.1.12 spec/support/query_counter.rb
listings-0.1.11 spec/support/query_counter.rb
listings-0.1.10 spec/support/query_counter.rb
listings-0.1.8 spec/support/query_counter.rb
listings-0.1.7 spec/support/query_counter.rb
listings-0.1.6 spec/support/query_counter.rb
listings-0.1.5 spec/support/query_counter.rb
listings-0.1.4 spec/support/query_counter.rb
listings-0.1.0 spec/support/query_counter.rb