Home

Outputting SQL from NHibernate

I've added an interceptor to my Program, because it was not displaying any of the SQL in the SQL pane. I got the idea from here:
https://gist.github.com/rk4n/1242635

I made a small modification to the gist to make the output a bit more readable:
String output = Regex.Replace(sql.ToString(), "(FROM|INNER JOIN|WHERE|GROUP BY)", System.Environment.NewLine + "$1", RegexOptions.IgnoreCase);
output = Regex.Replace(output, @( as \w+, ), "$1" + System.Environment.NewLine + " " , RegexOptions.IgnoreCase);

log.Write(output);

Is there a better way to do this, maybe something that is supported by LinqPad?

I was reading about Custom Visualizers (http://www.linqpad.net/customvisualizers.aspx), but that is much more than I want to do, since I can run NHibernate Profiler to see the output cleanly.

Wondering if there is a way I can get nice output within LinqPad easily with just a SQL string.

Great product - I upgraded to premium today.

Thanks.

Comments

  • You don't need to use reflection to access LINQPad's SQL log writer. Just do this:
    
    TextWriter writer = Util.SqlOutputWriter;
    I take it you also want to format the SQL to make it more readable? LINQPad doesn't have any formatters to help. It's possible you'll find some open-source lexing/parsing framework that might help.

    Cheers
    Joe
Sign In or Register to comment.