Skip to content

Commit

Permalink
Allow store type name shape to be customized
Browse files Browse the repository at this point in the history
Fixes #12405
  • Loading branch information
ajcvickers committed Jul 7, 2019
1 parent e454071 commit 321a236
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 55 deletions.
96 changes: 45 additions & 51 deletions src/EFCore.Relational/Storage/RelationalTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,34 +243,58 @@ protected RelationalTypeMapping(RelationalTypeMappingParameters parameters)
{
Parameters = parameters;

var size = parameters.Size;
var storeType = parameters.StoreType;

if (storeType != null)
{
StoreTypeNameBase = GetBaseName(storeType);
if (size != null && parameters.StoreTypePostfix == StoreTypePostfix.Size)
{
storeType = StoreTypeNameBase + "(" + size + ")";
}
else if (parameters.StoreTypePostfix == StoreTypePostfix.PrecisionAndScale
|| parameters.StoreTypePostfix == StoreTypePostfix.Precision)
var storeTypeNameBase = GetBaseName(storeType);
StoreTypeNameBase = storeTypeNameBase;

storeType = ProcessStoreType(parameters, storeType, storeTypeNameBase);
}

StoreType = storeType;
}

/// <summary>
/// Processes the store type name to add appropriate postfix/prefix text as needed.
/// </summary>
/// <param name="parameters"> The parameters for this mapping. </param>
/// <param name="storeType"> The specified store type name. </param>
/// <param name="storeTypeNameBase"> The calculated based name</param>
/// <returns> The store type name to use. </returns>
protected virtual string ProcessStoreType(
RelationalTypeMappingParameters parameters,
[NotNull] string storeType,
[NotNull] string storeTypeNameBase)
{
Check.NotNull(storeType, nameof(storeType));
Check.NotNull(storeTypeNameBase, nameof(storeTypeNameBase));

var size = parameters.Size;

if (size != null
&& parameters.StoreTypePostfix == StoreTypePostfix.Size)
{
storeType = storeTypeNameBase + "(" + size + ")";
}
else if (parameters.StoreTypePostfix == StoreTypePostfix.PrecisionAndScale
|| parameters.StoreTypePostfix == StoreTypePostfix.Precision)
{
var precision = parameters.Precision;
if (precision != null)
{
var precision = parameters.Precision;
if (precision != null)
{
var scale = parameters.Scale;
storeType = StoreTypeNameBase
+ "("
+ (scale == null || parameters.StoreTypePostfix == StoreTypePostfix.Precision
? precision.ToString()
: precision + "," + scale)
+ ")";
}
var scale = parameters.Scale;
storeType = storeTypeNameBase
+ "("
+ (scale == null || parameters.StoreTypePostfix == StoreTypePostfix.Precision
? precision.ToString()
: precision + "," + scale)
+ ")";
}
}

StoreType = storeType;
return storeType;
}

private static string GetBaseName(string storeType)
Expand Down Expand Up @@ -349,37 +373,7 @@ public override CoreTypeMapping Clone(ValueConverter converter)
/// <param name="mappingInfo"> The mapping info containing the facets to use. </param>
/// <returns> The cloned mapping, or the original mapping if no clone was needed. </returns>
public virtual RelationalTypeMapping Clone(in RelationalTypeMappingInfo mappingInfo)
{
if ((mappingInfo.Scale != null
&& mappingInfo.Scale != Parameters.Scale
&& StoreTypePostfix == StoreTypePostfix.PrecisionAndScale)
|| (mappingInfo.Precision != null
&& mappingInfo.Precision != Parameters.Precision
&& (StoreTypePostfix == StoreTypePostfix.PrecisionAndScale
|| StoreTypePostfix == StoreTypePostfix.Precision)))
{
var storeTypeChanged = mappingInfo.StoreTypeNameBase != null
&& !string.Equals(mappingInfo.StoreTypeNameBase, StoreTypeNameBase, StringComparison.OrdinalIgnoreCase);

return storeTypeChanged
? Clone(Parameters.WithTypeMappingInfo(mappingInfo))
: Clone(
mappingInfo.Precision ?? Parameters.Precision,
mappingInfo.Scale ?? Parameters.Scale);
}

var storeTypeOrSizeChanged = (mappingInfo.Size != null
&& mappingInfo.Size != Size
&& StoreTypePostfix == StoreTypePostfix.Size)
|| (mappingInfo.StoreTypeName != null
&& !string.Equals(mappingInfo.StoreTypeName, StoreType, StringComparison.OrdinalIgnoreCase));

return storeTypeOrSizeChanged
? Clone(
mappingInfo.StoreTypeName ?? StoreType,
mappingInfo.Size ?? Size)
: this;
}
=> Clone(Parameters.WithTypeMappingInfo(mappingInfo));

/// <summary>
/// Gets the name of the database type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void Does_type_mapping_from_named_string_with_MaxLength()
{
var mapping = GetNamedMapping(typeof(string), "some_string(666)");

Assert.Equal("some_string(666)", mapping.StoreType);
Assert.Equal("(666)some_string", mapping.StoreType);
Assert.Equal(666, mapping.Size);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ public TestStringTypeMapping(
fixedLength))
{
}

protected override string ProcessStoreType(
RelationalTypeMappingParameters parameters,
string storeType,
string storeTypeNameBase)
=> storeTypeNameBase == "some_string"
&& parameters.Size != null
? $"({parameters.Size})some_string"
: storeType;
}

protected override RelationalTypeMapping FindMapping(in RelationalTypeMappingInfo mappingInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_scale()
@p1='2017-01-02T12:11:12' (Size = 3)
@p2='2016-01-02T11:11:12.0000000+00:00' (Size = 3)
@p3='102.2' (Size = 3)
@p4='101.1'
@p4='101.1' (Size = 3)
@p5='103.3' (Size = 3)
@p6='85.55000305175781' (Size = 25)
@p7='85.5' (Size = 3)
Expand Down Expand Up @@ -2110,7 +2110,7 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_scale_wi
@"@p0='2017-01-02T12:11:12' (Size = 3)
@p1='2016-01-02T11:11:12.0000000+00:00' (Size = 3)
@p2='102.2' (Size = 3)
@p3='101.1'
@p3='101.1' (Size = 3)
@p4='103.3' (Size = 3)
@p5='85.55000305175781' (Size = 25)
@p6='85.5' (Size = 3)
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.SqlServer.Tests/SqlServerTypeMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public void Throws_for_unrecognized_property_types()
[InlineData("VarCHaR(333)", typeof(string), 333, false)] // case-insensitive
[InlineData("varchar(333)", typeof(string), 333, false)]
[InlineData("varchar(max)", typeof(string), null, false)]
[InlineData("VARCHAR(max)", typeof(string), null, false, "varchar(max)")]
[InlineData("VARCHAR(max)", typeof(string), null, false, "VARCHAR(max)")]
public void Can_map_by_type_name(string typeName, Type clrType, int? size, bool unicode, string expectedType = null)
{
var mapping = CreateTypeMapper().FindMapping(typeName);
Expand Down

0 comments on commit 321a236

Please sign in to comment.