Skip to content

Commit

Permalink
InlineFormatter: Optionally support spacing around commas (#549)
Browse files Browse the repository at this point in the history
Fixes #539 

Default is enabled, like the existing GetReadableCommand logic.
  • Loading branch information
Turnerj authored Jul 3, 2021
1 parent c521319 commit 4408692
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/Releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ layout: "default"
### Release Notes
This page tracks major changes included in any update starting with version 4.0.0.3

#### Unreleased
- **New**:
- Added an option to control `SpacesAfterCommas` to `InlineSqlFormatter` and `SqlServerFormatter` ([#549](https://github.com/MiniProfiler/dotnet/pull/549) - thanks [Turnerj](https://github.com/Turnerj))

#### Version 4.2.1
- **New**:
- Added RavenDB Storage provider ([#483](https://github.com/MiniProfiler/dotnet/pull/483) - thanks [@lillo42](https://github.com/lillo42)!)
Expand Down
11 changes: 11 additions & 0 deletions src/MiniProfiler.Shared/SqlFormatters/InlineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ namespace StackExchange.Profiling.SqlFormatters
/// </summary>
public class InlineFormatter : ISqlFormatter
{
private static readonly Regex CommandSpacing = new Regex(@",([^\s])", RegexOptions.Compiled);
private static bool includeTypeInfo;

/// <summary>
/// Whether to modify the output query by adding spaces after commas.
/// </summary>
public bool InsertSpacesAfterCommas { get; set; } = true;

/// <summary>
/// Creates a new <see cref="InlineFormatter"/>, optionally including the parameter type info
/// in comments beside the replaced value
Expand All @@ -34,6 +40,11 @@ public string FormatSql(string commandText, List<SqlTimingParameter> parameters)
return commandText;
}

if (InsertSpacesAfterCommas)
{
commandText = CommandSpacing.Replace(commandText, ", $1");
}

var paramValuesByName = new Dictionary<string, string>(parameters.Count);
foreach (var p in parameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static string GetFormattedSql(this ISqlFormatter sqlFormatter, string com
/// <param name="command">The <see cref="IDbCommand"/> being represented.</param>
public static string GetFormattedSql(this ISqlFormatter sqlFormatter, IDbCommand command)
{
var commandText = command.GetReadableCommand();
var commandText = command.CommandText;
var parameters = command.GetParameters();

return sqlFormatter.GetFormattedSql(commandText, parameters, command);
Expand Down
13 changes: 13 additions & 0 deletions src/MiniProfiler.Shared/SqlFormatters/SqlServerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace StackExchange.Profiling.SqlFormatters
{
Expand All @@ -12,11 +13,18 @@ namespace StackExchange.Profiling.SqlFormatters
/// </summary>
public class SqlServerFormatter : ISqlFormatter
{
private static readonly Regex CommandSpacing = new Regex(@",([^\s])", RegexOptions.Compiled);

/// <summary>
/// Whether to include parameter declarations in the formatted output.
/// </summary>
public bool IncludeParameterValues { get; set; } = true;

/// <summary>
/// Whether to modify the output query by adding spaces after commas.
/// </summary>
public bool InsertSpacesAfterCommas { get; set; } = true;

/// <summary>
/// Lookup a function for translating a parameter by parameter type
/// </summary>
Expand Down Expand Up @@ -112,6 +120,11 @@ public virtual string FormatSql(string commandText, List<SqlTimingParameter> par
.Append("\n\n");
}

if (InsertSpacesAfterCommas)
{
commandText = CommandSpacing.Replace(commandText, ", $1");
}

// only treat 'StoredProcedure' differently since 'Text' may contain 'TableDirect' or 'StoredProcedure'
if (command?.CommandType == CommandType.StoredProcedure)
{
Expand Down
73 changes: 72 additions & 1 deletion tests/MiniProfiler.Tests/SqlFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public void InlineParameterNamesInParameterValues()
var formatted = formatter.FormatSql(command, parameters);
Assert.Equal("SELECT * FROM urls WHERE url = 'http://www.example.com?myid=1' OR myid = '1'", formatted);
}

[Fact]
public void InlineParameterValuesDisplayNullForStrings()
{
Expand All @@ -116,6 +115,40 @@ public void InlineParameterValuesDisplayNullForStrings()
Assert.Equal("SELECT * FROM urls WHERE url = 'http://www.example.com?myid=1' OR null IS NULL", formatted);
}

[Fact]
public void InlineSpacesAfterCommasEnabled()
{
var formatter = new InlineFormatter()
{
InsertSpacesAfterCommas = true
};
var parameters = new List<SqlTimingParameter>
{
new SqlTimingParameter() { DbType = "string", Name = "url", Value = "http://www.example.com?myid=1" },
new SqlTimingParameter() { DbType = "string", Name = "myid", Value = "1" }
};
const string command = "SELECT myid,url FROM urls WHERE url = @url OR myid = @myid";
var formatted = formatter.FormatSql(command, parameters);
Assert.Equal("SELECT myid, url FROM urls WHERE url = 'http://www.example.com?myid=1' OR myid = '1'", formatted);
}

[Fact]
public void InlineSpacesAfterCommasDisabled()
{
var formatter = new InlineFormatter()
{
InsertSpacesAfterCommas = false
};
var parameters = new List<SqlTimingParameter>
{
new SqlTimingParameter() { DbType = "string", Name = "url", Value = "http://www.example.com?myid=1" },
new SqlTimingParameter() { DbType = "string", Name = "myid", Value = "1" }
};
const string command = "SELECT myid,url FROM urls WHERE url = @url OR myid = @myid";
var formatted = formatter.FormatSql(command, parameters);
Assert.Equal("SELECT myid,url FROM urls WHERE url = 'http://www.example.com?myid=1' OR myid = '1'", formatted);
}

[Fact]
public void EnsureVerboseSqlServerFormatterOnlyAddsInformation()
{
Expand Down Expand Up @@ -226,6 +259,44 @@ public void TableQueryWithTwoParametersDisabled(string at)
Assert.Equal(expectedOutput, actualOutput);
}

[Theory]
[MemberData(nameof(GetParamPrefixes))]
public void TableQueryWithSpacesAfterCommasEnabled(string at)
{
const string text = "select 1 from dbo.Table where x = @x,y = @y";
var cmd = CreateDbCommand(CommandType.Text, text);
AddDbParameter<int>(cmd, at + "x", 123);
AddDbParameter<long>(cmd, at + "y", 123);

var formatter = new SqlServerFormatter()
{
InsertSpacesAfterCommas = true
};
var actualOutput = GenerateOutput(formatter, cmd, text);

const string expectedOutput = "DECLARE @x int = 123,\n @y bigint = 123;\n\nselect 1 from dbo.Table where x = @x, y = @y;";
Assert.Equal(expectedOutput, actualOutput);
}

[Theory]
[MemberData(nameof(GetParamPrefixes))]
public void TableQueryWithSpacesAfterCommasDisabled(string at)
{
const string text = "select 1 from dbo.Table where x = @x,y = @y";
var cmd = CreateDbCommand(CommandType.Text, text);
AddDbParameter<int>(cmd, at + "x", 123);
AddDbParameter<long>(cmd, at + "y", 123);

var formatter = new SqlServerFormatter()
{
InsertSpacesAfterCommas = false
};
var actualOutput = GenerateOutput(formatter, cmd, text);

const string expectedOutput = "DECLARE @x int = 123,\n @y bigint = 123;\n\nselect 1 from dbo.Table where x = @x,y = @y;";
Assert.Equal(expectedOutput, actualOutput);
}

[Theory]
[MemberData(nameof(GetParamPrefixes))]
public void TableQueryWithBit(string at)
Expand Down

0 comments on commit 4408692

Please sign in to comment.