Skip to content

Commit 006120a

Browse files
committed
Prepare parametrized PG query for showing query parameters in web panel
1 parent 071027d commit 006120a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/patches/db/pg.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ class PG::Connection
3939
alias_method :exec_params_without_profiling, :exec_params
4040
end
4141

42+
def prepare_parameterized_statement(args)
43+
if Array === args[1] && args[1].length > 0
44+
query = args[0]
45+
parameters = args[1]
46+
counter = 0
47+
loop do
48+
break if !query.include? "$#{counter+=1}"
49+
query = query.sub("$#{counter}",parameters[counter-1].to_s)
50+
end
51+
end
52+
query
53+
end
54+
4255
def prepare(*args, &blk)
4356
# we have no choice but to do this here,
4457
# if we do the check for profiling first, our cache may miss critical stuff
@@ -58,6 +71,7 @@ def exec(*args, &blk)
5871
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
5972
result = exec_without_profiling(*args, &blk)
6073
elapsed_time = SqlPatches.elapsed_time(start)
74+
args[0] = prepare_parameterized_statement(args)
6175
record = ::Rack::MiniProfiler.record_sql(args[0], elapsed_time)
6276
result.instance_variable_set("@miniprofiler_sql_id", record) if result
6377

0 commit comments

Comments
 (0)