Skip to content

Commit

Permalink
Support SQL Server UTF8 strings (#27634)
Browse files Browse the repository at this point in the history
Closes #25798
  • Loading branch information
roji committed Mar 17, 2022
1 parent b906ca3 commit 587de30
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SqlServerStringTypeMapping : StringTypeMapping
private const int UnicodeMax = 4000;
private const int AnsiMax = 8000;

private readonly bool _isUtf16;
private readonly SqlDbType? _sqlDbType;
private readonly int _maxSpecificSize;
private readonly int _maxSize;
Expand All @@ -38,7 +39,7 @@ public class SqlServerStringTypeMapping : StringTypeMapping
: this(
new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(typeof(string)),
storeType ?? GetStoreName(unicode, fixedLength),
storeType ?? GetDefaultStoreName(unicode, fixedLength),
storeTypePostfix ?? StoreTypePostfix.Size,
GetDbType(unicode, fixedLength),
unicode,
Expand All @@ -48,7 +49,7 @@ public class SqlServerStringTypeMapping : StringTypeMapping
{
}

private static string GetStoreName(bool unicode, bool fixedLength)
private static string GetDefaultStoreName(bool unicode, bool fixedLength)
=> unicode
? fixedLength ? "nchar" : "nvarchar"
: fixedLength
Expand Down Expand Up @@ -84,6 +85,7 @@ protected SqlServerStringTypeMapping(RelationalTypeMappingParameters parameters,
_maxSize = AnsiMax;
}

_isUtf16 = parameters.Unicode && parameters.StoreType.StartsWith("n", StringComparison.OrdinalIgnoreCase);
_sqlDbType = sqlDbType;
}

Expand All @@ -93,7 +95,23 @@ protected SqlServerStringTypeMapping(RelationalTypeMappingParameters parameters,
/// <param name="parameters">The parameters for this mapping.</param>
/// <returns>The newly created mapping.</returns>
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new SqlServerStringTypeMapping(parameters, _sqlDbType);
{
if (parameters.Unicode)
{
parameters = new(
parameters.CoreParameters,
parameters.StoreType,
parameters.StoreTypePostfix,
GetDbType(parameters.Unicode, parameters.FixedLength),
parameters.Unicode,
parameters.Size,
parameters.FixedLength,
parameters.Precision,
parameters.Scale);
}

return new SqlServerStringTypeMapping(parameters, _sqlDbType);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -184,7 +202,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
AddConcatOperatorIfNeeded();

if (IsUnicode)
if (_isUtf16)
{
builder.Append('N');
}
Expand All @@ -206,7 +224,7 @@ protected override string GenerateNonNullSqlLiteral(object value)

AddConcatOperatorIfNeeded();

if (IsUnicode)
if (_isUtf16)
{
builder.Append('n');
}
Expand All @@ -222,7 +240,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
AddConcatOperatorIfNeeded();

if (IsUnicode)
if (_isUtf16)
{
builder.Append('N');
}
Expand All @@ -245,7 +263,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
{
AddConcatOperatorIfNeeded();

if (IsUnicode)
if (_isUtf16)
{
builder.Append('N');
}
Expand Down Expand Up @@ -275,7 +293,7 @@ protected override string GenerateNonNullSqlLiteral(object value)

if (builder.Length == 0)
{
if (IsUnicode)
if (_isUtf16)
{
builder.Append('N');
}
Expand All @@ -292,7 +310,7 @@ void AddConcatOperatorIfNeeded()
if (!castApplied)
{
builder.Append(" AS ");
if (IsUnicode)
if (_isUtf16)
{
builder.Append('n');
}
Expand Down
Loading

0 comments on commit 587de30

Please sign in to comment.