Skip to content

Commit

Permalink
Add more cast nodes in multi-line breaks during migrations (#28576)
Browse files Browse the repository at this point in the history
Resolves #27206
  • Loading branch information
smitpatel authored Aug 4, 2022
1 parent 7ed0421 commit f70e9ae
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 91 deletions.
314 changes: 226 additions & 88 deletions src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,153 +173,291 @@ protected override string GenerateNonNullSqlLiteral(object value)
var lastConcatStartPoint = 0;
var concatCount = 1;
var concatStartList = new List<int>();
var castApplied = false;
for (i = 0; i < stringValue.Length; i++)
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27206", out var enabled)
&& enabled)
{
var lineFeed = stringValue[i] == '\n';
var carriageReturn = stringValue[i] == '\r';
var apostrophe = stringValue[i] == '\'';
if (lineFeed || carriageReturn || apostrophe)
var castApplied = false;
for (i = 0; i < stringValue.Length; i++)
{
length = i - start;
if (length != 0)
var lineFeed = stringValue[i] == '\n';
var carriageReturn = stringValue[i] == '\r';
var apostrophe = stringValue[i] == '\'';
if (lineFeed || carriageReturn || apostrophe)
{
if (!openApostrophe)
length = i - start;
if (length != 0)
{
if (!openApostrophe)
{
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
builder.Append('N');
}

builder.Append('\'');
openApostrophe = true;
}

builder.Append(stringValue.AsSpan().Slice(start, length));
}

if (lineFeed || carriageReturn)
{
if (openApostrophe)
{
builder.Append('\'');
openApostrophe = false;
}

AddConcatOperatorIfNeeded();

if (IsUnicode)
{
builder.Append('N');
builder.Append('n');
}

builder.Append('\'');
openApostrophe = true;
builder
.Append("char(")
.Append(lineFeed ? "10" : "13")
.Append(')');
}
else if (apostrophe)
{
if (!openApostrophe)
{
AddConcatOperatorIfNeeded();

builder.Append(stringValue.AsSpan().Slice(start, length));
}
if (IsUnicode)
{
builder.Append('N');
}

if (lineFeed || carriageReturn)
{
if (openApostrophe)
{
builder.Append('\'');
openApostrophe = false;
builder.Append('\'');
openApostrophe = true;
}

builder.Append("''");
}

start = i + 1;
}
}

length = i - start;
if (length != 0)
{
if (!openApostrophe)
{
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
builder.Append('n');
builder.Append('N');
}

builder
.Append("char(")
.Append(lineFeed ? "10" : "13")
.Append(')');
builder.Append('\'');
openApostrophe = true;
}
else if (apostrophe)

builder.Append(stringValue.AsSpan().Slice(start, length));
}

if (openApostrophe)
{
builder.Append('\'');
}

for (var j = concatStartList.Count - 1; j >= 0; j--)
{
if (castApplied && j == 0)
{
if (!openApostrophe)
{
AddConcatOperatorIfNeeded();
builder.Insert(concatStartList[j], "CAST(");
}

builder.Insert(concatStartList[j], "CONCAT(");
builder.Append(')');
}

void AddConcatOperatorIfNeeded()
{
if (builder.Length != 0)
{
if (!castApplied)
{
builder.Append(" AS ");
if (IsUnicode)
{
builder.Append('N');
builder.Append("n");
}

builder.Append('\'');
openApostrophe = true;
builder.Append("varchar(max))");
castApplied = true;
}

builder.Append("''");
}
builder.Append(", ");
concatCount++;

start = i + 1;
if (concatCount == 2)
{
concatStartList.Add(lastConcatStartPoint);
}

if (concatCount == 254)
{
lastConcatStartPoint = builder.Length;
concatCount = 1;
}
}
}
}

length = i - start;
if (length != 0)
else
{
if (!openApostrophe)
var insideConcat = false;
for (i = 0; i < stringValue.Length; i++)
{
AddConcatOperatorIfNeeded();

if (IsUnicode)
var lineFeed = stringValue[i] == '\n';
var carriageReturn = stringValue[i] == '\r';
var apostrophe = stringValue[i] == '\'';
if (lineFeed || carriageReturn || apostrophe)
{
builder.Append('N');
}
length = i - start;
if (length != 0)
{
if (!openApostrophe)
{
AddConcatOperatorIfNeeded();

builder.Append('\'');
openApostrophe = true;
}
if (IsUnicode)
{
builder.Append('N');
}

builder.Append(stringValue.AsSpan().Slice(start, length));
}
builder.Append('\'');
openApostrophe = true;
}

if (openApostrophe)
{
builder.Append('\'');
}
builder.Append(stringValue.AsSpan().Slice(start, length));
}

for (var j = concatStartList.Count - 1; j >= 0; j--)
{
if (castApplied && j == 0)
{
builder.Insert(concatStartList[j], "CAST(");
}
if (lineFeed || carriageReturn)
{
if (openApostrophe)
{
builder.Append('\'');
openApostrophe = false;
}

builder.Insert(concatStartList[j], "CONCAT(");
builder.Append(')');
}
AddConcatOperatorIfNeeded();

if (builder.Length == 0)
{
if (IsUnicode)
{
builder.Append('N');
}
if (IsUnicode)
{
builder.Append('n');
}

builder.Append("''");
}
builder
.Append("char(")
.Append(lineFeed ? "10" : "13")
.Append(')');
}
else if (apostrophe)
{
if (!openApostrophe)
{
AddConcatOperatorIfNeeded();

return builder.ToString();
if (IsUnicode)
{
builder.Append('N');
}

void AddConcatOperatorIfNeeded()
{
if (builder.Length != 0)
builder.Append('\'');
openApostrophe = true;
}

builder.Append("''");
}

start = i + 1;
}
}

length = i - start;
if (length != 0)
{
if (!castApplied)
if (!openApostrophe)
{
builder.Append(" AS ");
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
builder.Append("n");
builder.Append('N');
}

builder.Append("varchar(max))");
castApplied = true;
builder.Append('\'');
openApostrophe = true;
}

builder.Append(", ");
concatCount++;
builder.Append(stringValue.AsSpan().Slice(start, length));
}

if (concatCount == 2)
{
concatStartList.Add(lastConcatStartPoint);
}
if (openApostrophe)
{
builder.Append('\'');
}

for (var j = concatStartList.Count - 1; j >= 0; j--)
{
builder.Insert(concatStartList[j], "CONCAT(CAST(");
builder.Append(')');
}

if (concatCount == 254)
void AddConcatOperatorIfNeeded()
{
if (builder.Length != 0)
{
lastConcatStartPoint = builder.Length;
concatCount = 1;
if (!insideConcat)
{
builder.Append(" AS ");
if (IsUnicode)
{
builder.Append('n');
}

builder.Append("varchar(max))");
insideConcat = true;
}

builder.Append(", ");
concatCount++;

if (concatCount == 2)
{
concatStartList.Add(lastConcatStartPoint);
}

if (concatCount == 254)
{
lastConcatStartPoint = builder.Length;
concatCount = 1;
insideConcat = false;
}
}
}
}

if (builder.Length == 0)
{
if (IsUnicode)
{
builder.Append('N');
}

builder.Append("''");
}

return builder.ToString();
}
}
}
Loading

0 comments on commit f70e9ae

Please sign in to comment.