Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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