Class: Adhoq::Query

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
TimeBasedOrders
Defined in:
app/models/adhoq/query.rb

Constant Summary collapse

PARAMETER_PATTERN =
/\$(?<name>\w+)|\${(?<name>\w+)}/i.freeze

Instance Method Summary collapse

Instance Method Details

#execute!(report_format, query_parameters = {}) ⇒ Object



9
10
11
12
13
14
# File 'app/models/adhoq/query.rb', line 9

def execute!(report_format, query_parameters = {})
  executions.create! {|exe|
    exe.report_format = report_format
    exe.raw_sql       = substitute_query(query_parameters)
  }.tap(&:generate_report!)
end

#parametersObject



16
17
18
19
20
21
22
23
24
# File 'app/models/adhoq/query.rb', line 16

def parameters
  return @parameters if @parameters

  @parameters = query.scan(PARAMETER_PATTERN).each_with_object([]) do |(match1, match2), arr|
    name = match1 || match2

    arr << name.downcase
  end
end

#substitute_query(query_parameters) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/models/adhoq/query.rb', line 26

def substitute_query(query_parameters)
  return query if parameters.empty?

  query_parameters = query_parameters.with_indifferent_access
  query.gsub(PARAMETER_PATTERN) do |_, arr|
    name = Regexp.last_match["name"]
    query_parameters[name]
  end
end