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

Support SQL Server UTF8 strings #27634

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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 SqlServerStringTypeMapping(
: 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 SqlServerStringTypeMapping(
{
}

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,
System.Data.DbType.String,
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