Class: Rack::DevInsight::SqlRecorder

Inherits:
BaseRecorder show all
Defined in:
lib/rack/dev_insight/recorder/sql_recorder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.record(dialect:, statement:, binds: [], duration: 0.0) ⇒ nil

Parameters:

  • dialect (String)

    ‘mysql’, ‘postgresql’ or ‘sqlite’ are supported

  • statement (String)

    SQL statement

  • binds (Array) (defaults to: [])

    SQL statement binds

  • duration (Float) (defaults to: 0.0)

    milliseconds of SQL execution time

Returns:

  • (nil)


14
15
16
# File 'lib/rack/dev_insight/recorder/sql_recorder.rb', line 14

def record(dialect:, statement:, binds: [], duration: 0.0)
  new.record(dialect: dialect, statement: statement, binds: binds, duration: duration)
end

Instance Method Details

#record(dialect:, statement:, binds: [], duration: 0.0) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/dev_insight/recorder/sql_recorder.rb', line 19

def record(dialect:, statement:, binds: [], duration: 0.0)
  SqlDialects.validate!(dialect, ArgumentError)
  return if Context.current.nil?

  Context.current.result.add_sql(
    dialect: dialect,
    statement: statement,
    binds: format_binds(binds),
    backtrace: get_backtrace,
    duration: format('%.2f', duration).to_f,
  )
  nil
end

#record_from_event(started:, finished:, statement:, binds:, cached:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rack/dev_insight/recorder/sql_recorder.rb', line 50

def record_from_event(started:, finished:, statement:, binds:, cached:)
  return if Context.current.nil?
  return if DevInsight.config.detected_dialect.nil?
  return if DevInsight.config.skip_cached_sql && cached

  Context.current.result.add_sql(
    dialect: DevInsight.config.detected_dialect,
    statement: statement,
    binds: format_binds(binds),
    backtrace: get_backtrace,
    duration: format('%.2f', (finished - started) * 1000).to_f,
  )
  nil
end

#record_sql(dialect:, statement:, binds: []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/dev_insight/recorder/sql_recorder.rb', line 33

def record_sql(dialect:, statement:, binds: [])
  return yield if Context.current.nil?

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  res = yield
  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start

  Context.current.result.add_sql(
    dialect: dialect,
    statement: statement,
    binds: format_binds(binds),
    backtrace: get_backtrace,
    duration: format('%.2f', duration * 1000).to_f,
  )
  res
end