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 c693395
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/patches/db/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class PG::Connection
alias_method :exec_params_without_profiling, :exec_params
end

def prepare_parameterized_statement(args)
if Array === args[1] && args[1].length > 0
query = args[0]
parameters = args[1]
counter = 0
loop do
break if !query.include? "$#{counter+=1}"
parameter = parameters[counter-1].is_a?(String) ? "'#{parameters[counter-1].gsub("''","''")}'" : parameters[counter-1]
query = query.sub("$#{counter}",parameter.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,7 +72,8 @@ def exec(*args, &blk)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = exec_without_profiling(*args, &blk)
elapsed_time = SqlPatches.elapsed_time(start)
record = ::Rack::MiniProfiler.record_sql(args[0], elapsed_time)
query_with_params = prepare_parameterized_statement(args)
record = ::Rack::MiniProfiler.record_sql(query_with_params, elapsed_time)
result.instance_variable_set("@miniprofiler_sql_id", record) if result

result
Expand Down

0 comments on commit c693395

Please sign in to comment.