Class: Insight::SQLPanel::QueryResult

Inherits:
Object
  • Object
show all
Includes:
FilteredBacktrace
Defined in:
lib/insight/panels/sql_panel/query.rb

Direct Known Subclasses

ExplainResult, ProfileResult

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from FilteredBacktrace

#backtrace, backtrace_regexp, #filtered_backtrace, #has_backtrace?, root_for_backtrace_filtering

Constructor Details

- (QueryResult) initialize(sql, time, backtrace = [], result = nil)

A new instance of QueryResult



10
11
12
13
14
15
16
# File 'lib/insight/panels/sql_panel/query.rb', line 10

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

Instance Attribute Details

- (Object) sql (readonly)

Returns the value of attribute sql



7
8
9
# File 'lib/insight/panels/sql_panel/query.rb', line 7

def sql
  @sql
end

- (Object) time (readonly)

Returns the value of attribute time



8
9
10
# File 'lib/insight/panels/sql_panel/query.rb', line 8

def time
  @time
end

Class Method Details

+ (Object) execute(sql)

Downside is: we re-execute the SQL...



50
51
52
# File 'lib/insight/panels/sql_panel/query.rb', line 50

def self.execute(sql)
  ActiveRecord::Base.connection.execute(sql)
end

Instance Method Details

- (Object) column_names



23
24
25
26
27
28
29
# File 'lib/insight/panels/sql_panel/query.rb', line 23

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

- (Object) execute



54
55
56
# File 'lib/insight/panels/sql_panel/query.rb', line 54

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

- (Object) human_time



41
42
43
# File 'lib/insight/panels/sql_panel/query.rb', line 41

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

- (Boolean) inspectable?

Returns:

  • (Boolean)


45
46
47
# File 'lib/insight/panels/sql_panel/query.rb', line 45

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

- (Object) result



18
19
20
21
# File 'lib/insight/panels/sql_panel/query.rb', line 18

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

- (Object) rows



31
32
33
34
35
36
37
38
39
# File 'lib/insight/panels/sql_panel/query.rb', line 31

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

- (Boolean) valid_hash?(secret_key, possible_hash)

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/insight/panels/sql_panel/query.rb', line 58

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