Skip to content

Commit

Permalink
Merge in 'release/5.0' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnet-bot committed Dec 11, 2020
2 parents 2f4f88a + 0e91fa1 commit 8bb7518
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 41 deletions.
74 changes: 50 additions & 24 deletions src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
Expand Down Expand Up @@ -171,6 +172,10 @@ protected override string GenerateNonNullSqlLiteral(object value)
int length;
var concatenated = false;
var openApostrophe = false;
var lastConcatStartPoint = 0;
var concatCount = 1;
var concatStartList = new List<int>();
var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23518", out var enabled) && enabled;
for (i = 0; i < stringValue.Length; i++)
{
var lineFeed = stringValue[i] == '\n';
Expand All @@ -183,11 +188,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
if (!openApostrophe)
{
if (builder.Length != 0)
{
builder.Append(", ");
concatenated = true;
}
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
Expand All @@ -209,11 +210,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
openApostrophe = false;
}

if (builder.Length != 0)
{
builder.Append(", ");
concatenated = true;
}
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
Expand All @@ -229,11 +226,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
if (!openApostrophe)
{
if (builder.Length != 0)
{
builder.Append(", ");
concatenated = true;
}
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
Expand All @@ -253,11 +246,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
if (!openApostrophe)
{
if (builder.Length != 0)
{
builder.Append(", ");
concatenated = true;
}
AddConcatOperatorIfNeeded();

if (IsUnicode)
{
Expand All @@ -276,11 +265,21 @@ protected override string GenerateNonNullSqlLiteral(object value)
builder.Append('\'');
}

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

if (builder.Length == 0)
Expand All @@ -294,6 +293,33 @@ protected override string GenerateNonNullSqlLiteral(object value)
}

return builder.ToString();

void AddConcatOperatorIfNeeded()
{
if (builder.Length != 0)
{
builder.Append(", ");
if (useOldBehavior)
{
concatenated = true;
}
else
{
concatCount++;

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

if (concatCount == 254)
{
lastConcatStartPoint = builder.Length;
concatCount = 1;
}
}
}
}
}
}
}
108 changes: 93 additions & 15 deletions src/EFCore.Sqlite.Core/Storage/Internal/SqliteStringTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using JetBrains.Annotations;
Expand Down Expand Up @@ -61,11 +62,15 @@ protected override string GenerateNonNullSqlLiteral(object value)
var stringValue = (string)value;
var builder = new StringBuilder();

var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23459", out var enabled) && enabled;

var start = 0;
int i;
int length;
var concatenated = false;
var openApostrophe = false;
var lengths = new List<int>();
var startIndexes = new List<int> { 0 };
for (i = 0; i < stringValue.Length; i++)
{
var lineFeed = stringValue[i] == '\n';
Expand All @@ -80,8 +85,16 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
if (builder.Length != 0)
{
builder.Append(" || ");
concatenated = true;
if (useOldBehavior)
{
builder.Append(" || ");
concatenated = true;
}
else
{
lengths.Add(builder.Length - startIndexes[^1]);
startIndexes.Add(builder.Length);
}
}

builder.Append('\'');
Expand All @@ -101,8 +114,16 @@ protected override string GenerateNonNullSqlLiteral(object value)

if (builder.Length != 0)
{
builder.Append(" || ");
concatenated = true;
if (useOldBehavior)
{
builder.Append(" || ");
concatenated = true;
}
else
{
lengths.Add(builder.Length - startIndexes[^1]);
startIndexes.Add(builder.Length);
}
}

builder
Expand All @@ -117,8 +138,16 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
if (builder.Length != 0)
{
builder.Append(" || ");
concatenated = true;
if (useOldBehavior)
{
builder.Append(" || ");
concatenated = true;
}
else
{
lengths.Add(builder.Length - startIndexes[^1]);
startIndexes.Add(builder.Length);
}
}

builder.Append("'");
Expand All @@ -139,8 +168,16 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
if (builder.Length != 0)
{
builder.Append(" || ");
concatenated = true;
if (useOldBehavior)
{
builder.Append(" || ");
concatenated = true;
}
else
{
lengths.Add(builder.Length - startIndexes[^1]);
startIndexes.Add(builder.Length);
}
}

builder.Append('\'');
Expand All @@ -155,19 +192,60 @@ protected override string GenerateNonNullSqlLiteral(object value)
builder.Append('\'');
}

if (concatenated)
if (useOldBehavior)
{
builder
.Insert(0, '(')
.Append(')');
if (concatenated)
{
builder
.Insert(0, '(')
.Append(')');
}

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

return builder.ToString();
}

if (builder.Length == 0)
if (builder.Length != 0)
{
builder.Append("''");
lengths.Add(builder.Length - startIndexes[^1]);
}

return builder.ToString();
if (lengths.Count == 0
&& builder.Length == 0)
{
return "''";
}

var newBuilder = new StringBuilder();
GenerateBalancedTree(0, lengths.Count);

return newBuilder.ToString();

void GenerateBalancedTree(int start, int end)
{
var count = end - start;
if (count < 1)
{
return;
}

if (count == 1)
{
newBuilder.Append(builder, startIndexes[start], lengths[start]);
return;
}

var mid = start + count / 2;
newBuilder.Append("(");
GenerateBalancedTree(start, mid);
newBuilder.Append(" || ");
GenerateBalancedTree(mid, end);
newBuilder.Append(")");
}
}
}
}
26 changes: 26 additions & 0 deletions test/EFCore.Specification.Tests/BuiltInDataTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2084,13 +2084,24 @@ public virtual void Optional_datetime_reading_null_from_database()
}
}

[ConditionalFact]
public virtual void Can_insert_query_multiline_string()
{
using var context = CreateContext();

Assert.Equal(Fixture.ReallyLargeString, Assert.Single(context.Set<StringEnclosure>()).Value);
}

public abstract class BuiltInDataTypesFixtureBase : SharedStoreFixtureBase<PoolableDbContext>
{
protected override string StoreName { get; } = "BuiltInDataTypes";

public virtual int LongStringLength
=> 9000;

public virtual string ReallyLargeString
=> string.Join("", Enumerable.Repeat(Environment.NewLine, 1001));

protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
modelBuilder.Entity<BinaryKeyDataType>();
Expand Down Expand Up @@ -2346,6 +2357,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
.HasData(
new DateTimeEnclosure { Id = 1, DateTimeOffset = new DateTimeOffset(2020, 3, 12, 1, 1, 1, new TimeSpan(3, 0, 0)) },
new DateTimeEnclosure { Id = 2 });

modelBuilder.Entity<StringEnclosure>()
.HasData(
new StringEnclosure
{
Id = 1,
Value = ReallyLargeString
});
}

protected static void MakeRequired<TEntity>(ModelBuilder modelBuilder)
Expand Down Expand Up @@ -3152,5 +3171,12 @@ protected class DateTimeEnclosure
public int Id { get; set; }
public DateTimeOffset? DateTimeOffset { get; set; }
}

protected class StringEnclosure
{
public int Id { get; set; }

public string Value { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3329,6 +3329,8 @@ public virtual void Columns_have_expected_data_types()
MaxLengthDataTypes.Id ---> [int] [Precision = 10 Scale = 0]
MaxLengthDataTypes.String3 ---> [nullable nvarchar] [MaxLength = 3]
MaxLengthDataTypes.String9000 ---> [nullable nvarchar] [MaxLength = -1]
StringEnclosure.Id ---> [int] [Precision = 10 Scale = 0]
StringEnclosure.Value ---> [nullable nvarchar] [MaxLength = -1]
StringForeignKeyDataType.Id ---> [int] [Precision = 10 Scale = 0]
StringForeignKeyDataType.StringKeyDataTypeId ---> [nullable nvarchar] [MaxLength = 450]
StringKeyDataType.Id ---> [nvarchar] [MaxLength = 450]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public virtual void Columns_have_expected_data_types()
MaxLengthDataTypes.Id ---> [int] [Precision = 10 Scale = 0]
MaxLengthDataTypes.String3 ---> [nullable varbinary] [MaxLength = 3]
MaxLengthDataTypes.String9000 ---> [nullable varbinary] [MaxLength = -1]
StringEnclosure.Id ---> [int] [Precision = 10 Scale = 0]
StringEnclosure.Value ---> [nullable nvarchar] [MaxLength = -1]
StringForeignKeyDataType.Id ---> [int] [Precision = 10 Scale = 0]
StringForeignKeyDataType.StringKeyDataTypeId ---> [nullable varbinary] [MaxLength = 900]
StringKeyDataType.Id ---> [varbinary] [MaxLength = 900]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ public virtual void Columns_have_expected_data_types()
SimpleCounter.Discriminator ---> [nullable nvarchar] [MaxLength = -1]
SimpleCounter.IsTest ---> [bit]
SimpleCounter.StyleKey ---> [nullable nvarchar] [MaxLength = -1]
StringEnclosure.Id ---> [int] [Precision = 10 Scale = 0]
StringEnclosure.Value ---> [nullable nvarchar] [MaxLength = -1]
StringForeignKeyDataType.Id ---> [int] [Precision = 10 Scale = 0]
StringForeignKeyDataType.StringKeyDataTypeId ---> [nullable nvarchar] [MaxLength = 450]
StringKeyDataType.Id ---> [nvarchar] [MaxLength = 450]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public virtual void Columns_have_expected_data_types()
MaxLengthDataTypes.Id ---> [varbinary] [MaxLength = 4]
MaxLengthDataTypes.String3 ---> [nullable varbinary] [MaxLength = 3]
MaxLengthDataTypes.String9000 ---> [nullable varbinary] [MaxLength = -1]
StringEnclosure.Id ---> [varbinary] [MaxLength = 4]
StringEnclosure.Value ---> [nullable varbinary] [MaxLength = -1]
StringForeignKeyDataType.Id ---> [varbinary] [MaxLength = 4]
StringForeignKeyDataType.StringKeyDataTypeId ---> [nullable varbinary] [MaxLength = 900]
StringKeyDataType.Id ---> [varbinary] [MaxLength = 900]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public virtual void Columns_have_expected_data_types()
MaxLengthDataTypes.Id ---> [nvarchar] [MaxLength = 64]
MaxLengthDataTypes.String3 ---> [nullable nvarchar] [MaxLength = 3]
MaxLengthDataTypes.String9000 ---> [nullable nvarchar] [MaxLength = -1]
StringEnclosure.Id ---> [nvarchar] [MaxLength = 64]
StringEnclosure.Value ---> [nullable nvarchar] [MaxLength = -1]
StringForeignKeyDataType.Id ---> [nvarchar] [MaxLength = 64]
StringForeignKeyDataType.StringKeyDataTypeId ---> [nullable nvarchar] [MaxLength = 450]
StringKeyDataType.Id ---> [nvarchar] [MaxLength = 450]
Expand Down
Loading

0 comments on commit 8bb7518

Please sign in to comment.