Skip to content

Commit

Permalink
Add more cast nodes in multi-line breaks during migrations (#28064)
Browse files Browse the repository at this point in the history
Resolves #27206
  • Loading branch information
smitpatel committed May 23, 2022
1 parent bcece16 commit 3ff83c4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
var lastConcatStartPoint = 0;
var concatCount = 1;
var concatStartList = new List<int>();
var castApplied = false;
var insideConcat = false;
for (i = 0; i < stringValue.Length; i++)
{
var lineFeed = stringValue[i] == '\n';
Expand Down Expand Up @@ -282,12 +282,7 @@ protected override string GenerateNonNullSqlLiteral(object value)

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

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

Expand All @@ -307,7 +302,7 @@ void AddConcatOperatorIfNeeded()
{
if (builder.Length != 0)
{
if (!castApplied)
if (!insideConcat)
{
builder.Append(" AS ");
if (_isUtf16)
Expand All @@ -316,7 +311,7 @@ void AddConcatOperatorIfNeeded()
}

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

builder.Append(", ");
Expand All @@ -331,6 +326,7 @@ void AddConcatOperatorIfNeeded()
{
lastConcatStartPoint = builder.Length;
concatCount = 1;
insideConcat = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,35 @@ public virtual void DefaultValue_with_line_breaks(bool isUnicode)
}
});

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual void DefaultValue_with_line_breaks_2(bool isUnicode)
{
var defaultValue = Enumerable.Range(0, 300).Select(e => e.ToString())
.Select(e => e + "\r\n")
.Join("");

Generate(
new CreateTableOperation
{
Name = "TestLineBreaks",
Schema = "dbo",
Columns =
{
new AddColumnOperation
{
Name = "TestDefaultValue",
Table = "TestLineBreaks",
Schema = "dbo",
ClrType = typeof(string),
DefaultValue = defaultValue,
IsUnicode = isUnicode
}
}
});
}

private static void CreateGotModel(ModelBuilder b)
=> b.HasDefaultSchema("dbo").Entity(
"Person", pb =>
Expand Down
Loading

0 comments on commit 3ff83c4

Please sign in to comment.