Module: DatabaseRecorder::PG::Recorder

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

Class Method Summary collapse

Class Method Details

.format_result(result) ⇒ Object



14
15
16
# File 'lib/database_recorder/pg/recorder.rb', line 14

def format_result(result)
  { count: result.count, fields: result.fields, values: result.values } if result
end

.ignore_query?(sql) ⇒ Boolean

Returns:

  • (Boolean)


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

def ignore_query?(sql)
  !Recording.started? ||
    sql.downcase.match?(/\A(begin|commit|release|rollback|savepoint)/i) ||
    sql.match?(/ pg_attribute |SHOW max_identifier_length|SHOW search_path/)
end

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



18
19
20
21
# File 'lib/database_recorder/pg/recorder.rb', line 18

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

.setupObject



23
24
25
26
27
# File 'lib/database_recorder/pg/recorder.rb', line 23

def setup
  ::PG::Connection.class_eval do
    prepend ConnectionExt
  end
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/database_recorder/pg/recorder.rb', line 29

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

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/database_recorder/pg/recorder.rb', line 47

def store_query(name: nil, sql: nil, binds: nil, source: nil)
  return yield if ignore_query?(sql)

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

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