Skip to content

Commit

Permalink
Support SQL Server UTF8 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Mar 13, 2022
1 parent 96f7bb8 commit fe312b1
Show file tree
Hide file tree
Showing 3 changed files with 422 additions and 134 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 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

0 comments on commit fe312b1

Please sign in to comment.