Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IDbCommand to FormatSql method and Add VerboseSqlServerFormatter #59

Merged
merged 2 commits into from
May 12, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add IDbCommand parameter to ISqlFormatter.FormatSql method
  • Loading branch information
yellis committed May 11, 2014
commit 6cde7f9bcf81afebf5bb08b4d614cbce52e6c4ab
3 changes: 2 additions & 1 deletion StackExchange.Profiling/SqlFormatters/ISqlFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Data;

namespace StackExchange.Profiling.SqlFormatters
{
Expand All @@ -10,6 +11,6 @@ public interface ISqlFormatter
/// <summary>
/// Return SQL the way you want it to look on the in the trace. Usually used to format parameters.
/// </summary>
string FormatSql(string commandText, List<SqlTimingParameter> parameters);
string FormatSql(string commandText, List<SqlTimingParameter> parameters, IDbCommand command);
}
}
3 changes: 2 additions & 1 deletion StackExchange.Profiling/SqlFormatters/InlineFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;

namespace StackExchange.Profiling.SqlFormatters
Expand Down Expand Up @@ -27,7 +28,7 @@ public InlineFormatter(bool includeTypeInfo = false)
/// Formats the SQL in a generic friendly format, including the parameter type information
/// in a comment if it was specified in the InlineFormatter constructor
/// </summary>
public string FormatSql(string commandText, List<SqlTimingParameter> parameters)
public string FormatSql(string commandText, List<SqlTimingParameter> parameters, IDbCommand command)
{
if (parameters == null || parameters.Count == 0)
{
Expand Down
19 changes: 15 additions & 4 deletions StackExchange.Profiling/SqlFormatters/SqlServerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ namespace StackExchange.Profiling.SqlFormatters
/// </summary>
public class SqlServerFormatter : ISqlFormatter
{
private static readonly Dictionary<DbType, Func<SqlTimingParameter, string>> ParamTranslator;
private static readonly string[] DontQuote = new[] { "Int16", "Int32", "Int64", "Boolean", "Byte[]" };
/// <summary>
/// Lookup a function for translating a parameter by parameter type
/// </summary>
protected static readonly Dictionary<DbType, Func<SqlTimingParameter, string>> ParamTranslator;
/// <summary>
/// What data types should not be quoted when used in parameters
/// </summary>
protected static readonly string[] DontQuote = { "Int16", "Int32", "Int64", "Boolean", "Byte[]" };

private static Func<SqlTimingParameter, string> GetWithLenFormatter(string native)
{
Expand Down Expand Up @@ -53,7 +59,7 @@ static SqlServerFormatter()
/// <summary>
/// Formats the SQL in a SQL-Server friendly way, with DECLARE statements for the parameters up top.
/// </summary>
public string FormatSql(string commandText, List<SqlTimingParameter> parameters)
public string FormatSql(string commandText, List<SqlTimingParameter> parameters, IDbCommand command)
{
if (parameters == null || parameters.Count == 0)
{
Expand Down Expand Up @@ -108,7 +114,12 @@ public string FormatSql(string commandText, List<SqlTimingParameter> parameters)
.ToString();
}

private string PrepareValue(SqlTimingParameter parameter)
/// <summary>
/// Prepare the parameter value for use in SqlFormatter output
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
protected string PrepareValue(SqlTimingParameter parameter)
{
if (parameter.Value == null)
{
Expand Down
2 changes: 1 addition & 1 deletion StackExchange.Profiling/SqlTiming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SqlTiming(IDbCommand command, SqlExecuteType type, MiniProfiler profiler)

if (MiniProfiler.Settings.SqlFormatter != null)
{
commandText = MiniProfiler.Settings.SqlFormatter.FormatSql(commandText, parameters);
commandText = MiniProfiler.Settings.SqlFormatter.FormatSql(commandText, parameters, command);
}

_customTiming = profiler.CustomTiming("sql", commandText, type.ToString());
Expand Down