Skip to content

Commit

Permalink
Prepare parametrized PG query for showing query parameters in web panel
Browse files Browse the repository at this point in the history
  • Loading branch information
unavailabl3 committed Oct 9, 2019
1 parent 071027d commit 584553e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/patches/db/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ class PG::Connection
alias_method :exec_params_without_profiling, :exec_params
end

def prepare_parameterized_statement(args)
if if Array === args[1] && args[1].length > 0
query = args[0]
parameters = args[1]
counter = 0
loop do
break if !query.include? "$#{counter+=1}"
query = query.sub("$#{counter}",parameters[counter-1].to_s)
end
end
query
end

def prepare(*args, &blk)
# we have no choice but to do this here,
# if we do the check for profiling first, our cache may miss critical stuff
Expand All @@ -58,6 +71,7 @@ def exec(*args, &blk)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = exec_without_profiling(*args, &blk)
elapsed_time = SqlPatches.elapsed_time(start)
args[0] = prepare_parameterized_statement(args)
record = ::Rack::MiniProfiler.record_sql(args[0], elapsed_time)
result.instance_variable_set("@miniprofiler_sql_id", record) if result

Expand Down

0 comments on commit 584553e

Please sign in to comment.