Hi,
I wrote this for Postgres and was wondering about thoughts about
extending the approach for other database adapters. The idea being
that rather than putting a lot of now_diffs and logging statements in
the code itself, and probably repeating that for each different DB
system, it gets grouped in one place where it can also be turned on
and off as needs be.
I suppose in some ways adding the code to the actual calls might be
more performant and simpler, so this might be best left as a hack for
development mode.
Thoughts?
-module(sqltracer).
-include_lib("stdlib/include/ms_transform.hrl").
%% API
-export([trace/0, tracer/2]).
trace() ->
dbg:start(),
dbg:tracer(process,
{fun sqltracer:tracer/2, []}),
dbg:tpl(pgsql, equery, dbg:fun2ms(fun(_) -> return_trace() end)),
dbg:tpl(pgsql, equery, dbg:fun2ms(fun(_) -> return_trace() end)),
dbg:p(all, c).
tracer({trace, Pid, call, {pgsql, equery, [Conn, Sql]}}, _Acc) ->
{erlang:iolist_to_binary(Sql), [], os:timestamp()};
tracer({trace, Pid, call, {pgsql, equery, [Conn, Sql, Parameters]}}, _Acc) ->
{erlang:iolist_to_binary(Sql), Parameters, os:timestamp()};
tracer({trace, Pid, return_from, {pgsql, equery, Arity}, ReturnValue},
{Sql, Parameters, StartTime}) ->
error_logger:info_msg("pgsql:equery (~p microseconds) SQL: ~s ;
PARAMETERS ~p~n",
[timer:now_diff(os:timestamp(), StartTime),
Sql, Parameters]),
ok.
--
David N. Welton
http://www.welton.it/davidw/
http://www.dedasys.com/