Skip to content

Commit fe25918

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

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/patches/db/pg.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ 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+
parameter = parameters[counter-1].is_a?(String) ? "'#{parameters[counter-1].gsub("'","''")}'" : parameters[counter-1]
50+
query = query.sub("$#{counter}",parameter.to_s)
51+
end
52+
end
53+
query
54+
end
55+
4256
def prepare(*args, &blk)
4357
# we have no choice but to do this here,
4458
# if we do the check for profiling first, our cache may miss critical stuff
@@ -58,7 +72,8 @@ def exec(*args, &blk)
5872
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
5973
result = exec_without_profiling(*args, &blk)
6074
elapsed_time = SqlPatches.elapsed_time(start)
61-
record = ::Rack::MiniProfiler.record_sql(args[0], elapsed_time)
75+
query_with_params = prepare_parameterized_statement(args)
76+
record = ::Rack::MiniProfiler.record_sql(query_with_params, elapsed_time)
6277
result.instance_variable_set("@miniprofiler_sql_id", record) if result
6378

6479
result

0 commit comments

Comments
 (0)