Sha256: 3a088dfbfa8d5c033a711db12f784f3d393778273ab98e9cf7dd88b0f583603e

Contents?: true

Size: 1.97 KB

Versions: 34

Compression:

Stored size: 1.97 KB

Contents

module Rack::Insight
  class SQLPanel

    class QueryResult
      include Rack::Insight::FilteredBacktrace

      attr_reader :sql
      attr_reader :time

      def initialize(sql, time, backtrace = [], result=nil)
        @sql = sql
        @time = time
        @backtrace = backtrace
        @result = result
        @results = nil
      end

      def result
        @results ||= execute
        return @results
      end

      def column_names
        if result.respond_to?(:fields)
          return result.fields
        else
          return result.fetch_fields.map{|col| col.name}
        end
      end

      def rows
        if result.respond_to?(:values)
          result.values
        else
          result.map do |row|
            row
          end
        end
      end

      def human_time
        "%.2fms" % (@time)
      end

      def inspectable?
        sql.strip =~ /^SELECT /i
      end

      #Downside is: we re-execute the SQL...
      def self.execute(sql)
        ActiveRecord::Base.connection.execute(sql)
      end

      def execute
        self.class.execute(@sql)
      end

      def valid_hash?(secret_key, possible_hash)
        hash = Digest::SHA1.hexdigest [secret_key, @sql].join(":")
        possible_hash == hash
      end
    end

    class ExplainResult < QueryResult
      def execute
        self.class.execute "EXPLAIN #{@sql}"
      end
    end

    class ProfileResult < QueryResult
      def with_profiling
        result = nil
        begin
          self.class.execute("SET PROFILING=1")
          result = yield
        ensure
          self.class.execute("SET PROFILING=0")
        end
        return result
      end

      def execute
        with_profiling do
          super
          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
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
rack-insight-0.6.4 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.6.3 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.6.2 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.30 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.29 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.28 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.27 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.26 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.25 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.24 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.23 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.22 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.21 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.20 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.19 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.18 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.17 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.16 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.15 lib/rack/insight/panels/sql_panel/query.rb
rack-insight-0.5.14 lib/rack/insight/panels/sql_panel/query.rb