Class: Profiler::Models::SqlQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/models/sql_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql:, duration:, binds: [], name: nil, connection: nil, backtrace: []) ⇒ SqlQuery

Returns a new instance of SqlQuery.



8
9
10
11
12
13
14
15
# File 'lib/profiler/models/sql_query.rb', line 8

def initialize(sql:, duration:, binds: [], name: nil, connection: nil, backtrace: [])
  @sql = sql
  @duration = duration
  @binds = binds
  @name = name
  @connection = connection
  @backtrace = backtrace
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



6
7
8
# File 'lib/profiler/models/sql_query.rb', line 6

def backtrace
  @backtrace
end

#bindsObject (readonly)

Returns the value of attribute binds.



6
7
8
# File 'lib/profiler/models/sql_query.rb', line 6

def binds
  @binds
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/profiler/models/sql_query.rb', line 6

def connection
  @connection
end

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/profiler/models/sql_query.rb', line 6

def duration
  @duration
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/profiler/models/sql_query.rb', line 6

def name
  @name
end

#sqlObject (readonly)

Returns the value of attribute sql.



6
7
8
# File 'lib/profiler/models/sql_query.rb', line 6

def sql
  @sql
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/profiler/models/sql_query.rb', line 21

def cached?
  @name == "CACHE"
end

#slow?(threshold = 100) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/profiler/models/sql_query.rb', line 17

def slow?(threshold = 100)
  @duration > threshold
end

#to_hObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/profiler/models/sql_query.rb', line 29

def to_h
  {
    sql: @sql,
    duration: @duration,
    binds: @binds,
    name: @name,
    connection: @connection&.class&.name,
    backtrace: @backtrace.map(&:to_s),
    slow: slow?,
    cached: cached?,
    transaction: transaction?
  }
end

#to_json(*args) ⇒ Object



43
44
45
# File 'lib/profiler/models/sql_query.rb', line 43

def to_json(*args)
  to_h.to_json(*args)
end

#transaction?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/profiler/models/sql_query.rb', line 25

def transaction?
  @sql =~ /^(BEGIN|COMMIT|ROLLBACK)/i
end