Class: Blazer::RunStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer/run_statement.rb

Instance Method Summary collapse

Instance Method Details

#perform(statement, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blazer/run_statement.rb', line 3

def perform(statement, options = {})
  query = options[:query]

  data_source = statement.data_source
  statement.bind

  # audit
  if Blazer.audit
    audit_statement = statement.bind_statement
    audit_statement += "\n\n#{statement.bind_values.to_json}" if statement.bind_values.any?
    audit = Blazer::Audit.new(statement: audit_statement)
    audit.query = query
    audit.data_source = data_source.id
    # only set user if present to avoid error with Rails 7.1 when no user model
    audit.user = options[:user] unless options[:user].nil?
    audit.save!
  end

  start_time = Blazer.monotonic_time
  result = data_source.run_statement(statement, options)
  duration = Blazer.monotonic_time - start_time

  if Blazer.audit
    audit.duration = duration if audit.respond_to?(:duration=)
    audit.error = result.error if audit.respond_to?(:error=)
    audit.timed_out = result.timed_out? if audit.respond_to?(:timed_out=)
    audit.cached = result.cached? if audit.respond_to?(:cached=)
    if !result.cached? && duration >= 10
      audit.cost = data_source.cost(statement) if audit.respond_to?(:cost=)
    end
    audit.save! if audit.changed?
  end

  if query && !result.timed_out? && !result.cached? && !query.variables.any?
    query.checks.each do |check|
      check.update_state(result)
    end
  end

  result
end