Module: DatabaseRecorder::Mysql2::Recorder

Defined in:
lib/database_recorder/mysql2/recorder.rb

Class Method Summary collapse

Class Method Details

.format_result(result) ⇒ Object



15
16
17
18
19
20
# File 'lib/database_recorder/mysql2/recorder.rb', line 15

def format_result(result)
  { count: result.count, fields: result.fields, values: result.to_a } if result.is_a?(::Mysql2::Result)
  # else
  #   last_insert_id = adapter.query('SELECT LAST_INSERT_ID() AS _dbr_last_insert_id').to_a
  #   { 'count' => last_insert_id.count, 'fields' => ['id'], 'values' => last_insert_id }
end

.ignore_query?(sql) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'lib/database_recorder/mysql2/recorder.rb', line 8

def ignore_query?(sql)
  !Recording.started? ||
    sql == 'SELECT LAST_INSERT_ID() AS _dbr_last_insert_id' ||
    sql.downcase.match?(/\A(begin|commit|release|rollback|savepoint|show full fields from)/i) ||
    sql.match?(/information_schema.statistics/)
end

.prepare_statement(adapter, sql: nil, name: nil, binds: nil, source: nil) ⇒ Object



22
23
24
25
# File 'lib/database_recorder/mysql2/recorder.rb', line 22

def prepare_statement(adapter, sql: nil, name: nil, binds: nil, source: nil)
  @last_prepared = Recording.push_prepared(name: name, sql: sql, binds: binds, source: source)
  yield if !Config.replay_recordings || Recording.cache.nil?
end

.setupObject



27
28
29
30
31
32
33
34
35
# File 'lib/database_recorder/mysql2/recorder.rb', line 27

def setup
  ::Mysql2::Client.class_eval do
    prepend ClientExt
  end

  ::Mysql2::Statement.class_eval do
    prepend StatementExt
  end
end

.store_prepared_statement(adapter, source:, binds:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/database_recorder/mysql2/recorder.rb', line 37

def store_prepared_statement(adapter, source:, binds:)
  # sql = @last_prepared&.send(:[], 'sql')
  sql = @last_prepared[:sql]
  Core.log_query(sql, source)
  if Config.replay_recordings && !Recording.cache.nil?
    data = Recording.cache.find { |query| query[:sql] == sql }
    return yield unless data # cache miss

    Recording.push(sql: data[:sql], binds: data[:binds], source: source)
    RecordedResult.new(data[:result].slice(:count, :fields, :values))
  else
    yield.tap do |result|
      Recording.update_prepared(sql: sql, binds: binds, result: format_result(result), source: source)
    end
  end
end

.store_query(adapter, sql:, source:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/database_recorder/mysql2/recorder.rb', line 54

def store_query(adapter, sql:, source:)
  return yield if ignore_query?(sql)

  Core.log_query(sql, source)
  if Config.replay_recordings && !Recording.cache.nil?
    Recording.push(sql: sql, source: source)
    data = Recording.cached_query_for(sql)
    return yield unless data # cache miss

    RecordedResult.new.prepare(data[:result].slice(:count, :fields, :values)) if data[:result]
  else
    yield.tap do |result|
      Recording.push(sql: sql, result: format_result(result), source: source)
    end
  end
end