Skip to content

Commit

Permalink
Make TestSqlLoggerFactory recognize quoting/curlies (#24617)
Browse files Browse the repository at this point in the history
Follow-up to #24595
  • Loading branch information
roji authored Apr 8, 2021
1 parent 407ee01 commit 9b65c28
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down Expand Up @@ -121,6 +122,8 @@ public TestSqlLogger(bool shouldLogCommands)
public List<string> SqlStatements { get; } = new();
public List<string> Parameters { get; } = new();

private StringBuilder _stringBuilder = new();

protected override void UnsafeClear()
{
base.UnsafeClear();
Expand Down Expand Up @@ -157,7 +160,37 @@ protected override void UnsafeLog<TState>(
if (!string.IsNullOrWhiteSpace(parameters))
{
Parameters.Add(parameters);
parameters = parameters.Replace(", ", _eol) + _eol + _eol;

_stringBuilder.Clear();

var inQuotes = false;
var inCurlies = false;
for (var i = 0; i < parameters.Length; i++)
{
var c = parameters[i];
switch (c)
{
case '\'':
inQuotes = !inQuotes;
goto default;
case '{':
inCurlies = true;
goto default;
case '}':
inCurlies = false;
goto default;
case ',' when parameters[i + 1] == ' ' && !inQuotes && !inCurlies:
_stringBuilder.Append(_eol);
i++;
continue;
default:
_stringBuilder.Append(c);
continue;
}
}

_stringBuilder.Append(_eol).Append(_eol);
parameters = _stringBuilder.ToString();
}

SqlStatements.Add(parameters + commandText);
Expand Down

0 comments on commit 9b65c28

Please sign in to comment.