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

Make TestSqlLoggerFactory recognize quoting/curlies #24617

Merged
1 commit merged into from
Apr 8, 2021
Merged
Changes from all commits
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
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