Skip to content

Commit

Permalink
test and fix for Issue #26742 (#26758)
Browse files Browse the repository at this point in the history
  • Loading branch information
EamonHetherton committed Dec 6, 2021
1 parent 87f933f commit 9db26bd
Show file tree
Hide file tree
Showing 11 changed files with 550 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ protected override void ConfigureParameter(DbParameter parameter)

if (Precision.HasValue)
{
parameter.Precision = unchecked((byte)Precision.Value);
// Workaround for inconsistant definition of precision/scale between EF and SQLClient for VarTime types
parameter.Scale = unchecked((byte)Precision.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SqlServerDateTimeTypeMapping : DateTimeTypeMapping
// so the order of the entries in this array is important
private readonly string[] _dateTime2Formats =
{
"'{0:yyyy-MM-ddTHH:mm:ss}'",
"'{0:yyyy-MM-ddTHH:mm:ssK}'",
"'{0:yyyy-MM-ddTHH:mm:ss.fK}'",
"'{0:yyyy-MM-ddTHH:mm:ss.ffK}'",
"'{0:yyyy-MM-ddTHH:mm:ss.fffK}'",
Expand Down Expand Up @@ -86,7 +86,8 @@ protected override void ConfigureParameter(DbParameter parameter)

if (Precision.HasValue)
{
parameter.Precision = unchecked((byte)Precision.Value);
// Workaround for inconsistant definition of precision/scale between EF and SQLClient for VarTime types
parameter.Scale = unchecked((byte)Precision.Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data;
using System.Globalization;
using Microsoft.Data.SqlClient;

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
Expand All @@ -14,14 +15,36 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
/// </summary>
public class SqlServerTimeSpanTypeMapping : TimeSpanTypeMapping
{
// Note: this array will be accessed using the precision as an index
// so the order of the entries in this array is important
private readonly string[] _timeFormats =
{
@"'{0:hh\:mm\:ss}'",
@"'{0:hh\:mm\:ss\.F}'",
@"'{0:hh\:mm\:ss\.FF}'",
@"'{0:hh\:mm\:ss\.FFF}'",
@"'{0:hh\:mm\:ss\.FFFF}'",
@"'{0:hh\:mm\:ss\.FFFFF}'",
@"'{0:hh\:mm\:ss\.FFFFFF}'",
@"'{0:hh\:mm\:ss\.FFFFFFF}'"
};

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqlServerTimeSpanTypeMapping(string storeType, DbType? dbType = System.Data.DbType.Time)
: base(storeType, dbType)
public SqlServerTimeSpanTypeMapping(
string storeType,
DbType? dbType = System.Data.DbType.Time,
StoreTypePostfix storeTypePostfix = StoreTypePostfix.Precision)
: base(
new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(typeof(TimeSpan)),
storeType,
storeTypePostfix,
dbType))
{
}

Expand Down Expand Up @@ -59,5 +82,47 @@ protected override void ConfigureParameter(DbParameter parameter)
{
((SqlParameter)parameter).SqlDbType = SqlDbType.Time;
}
if (Precision.HasValue)
{
parameter.Scale = unchecked((byte)Precision.Value);
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string SqlLiteralFormatString
{
get
{
if (Precision.HasValue)
{
var precision = Precision.Value;
if (precision <= 7
&& precision >= 0)
{
return _timeFormats[precision];
}
}

return _timeFormats[7];
}
}

/// <summary>
/// Generates the SQL representation of a literal value without conversion.
/// </summary>
/// <param name="value">The literal value.</param>
/// <returns>
/// The generated string.
/// </returns>
protected override string GenerateNonNullSqlLiteral(object value)
{
return value is TimeSpan timeSpan && timeSpan.Milliseconds == 0
? string.Format(CultureInfo.InvariantCulture, _timeFormats[0], value) //handle trailing decimal seperator when no fractional seconds
: string.Format(CultureInfo.InvariantCulture, SqlLiteralFormatString, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ public class SqlServerTypeMappingSource : RelationalTypeMappingSource
"datetime2",
"datetimeoffset",
"double precision",
"float"
"float",
"time"
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public virtual void String_literal_generated_correctly()

[ConditionalFact]
public virtual void Timespan_literal_generated_correctly()
=> Test_GenerateSqlLiteral_helper(new TimeSpanTypeMapping("time"), new TimeSpan(7, 14, 30), "'07:14:30'");
=> Test_GenerateSqlLiteral_helper(new TimeSpanTypeMapping("time"), new TimeSpan(0, 7, 14, 30, 123), "'07:14:30.1230000'");

[ConditionalFact]
public virtual void UInt_literal_generated_correctly()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,15 +1574,16 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_scale()
var parameters = DumpParameters();
Assert.Equal(
@"@p0='77'
@p1='2017-01-02T12:11:12.3210000' (Precision = 3)
@p2='2016-01-02T11:11:12.7650000+00:00' (Precision = 3)
@p1='2017-01-02T12:11:12.3210000' (Scale = 3)
@p2='2016-01-02T11:11:12.7650000+00:00' (Scale = 3)
@p3='102' (Precision = 3)
@p4='101' (Precision = 3)
@p5='103' (Precision = 3)
@p6='85.55000305175781' (Size = 25)
@p7='85.5' (Size = 3)
@p8='83.33000183105469' (Size = 25)
@p9='83.3' (Size = 3)",
@p9='83.3' (Size = 3)
@p10='12:34:56.7890123' (Scale = 3)",
parameters,
ignoreLineEndingDifferences: true);

Expand All @@ -1602,6 +1603,7 @@ private static void AssertMappedScaledDataTypes(MappedScaledDataTypes entity, in
Assert.Equal(
new DateTimeOffset(new DateTime(2016, 1, 2, 11, 11, 12, 765), TimeSpan.Zero), entity.DateTimeOffsetAsDatetimeoffset3);
Assert.Equal(new DateTime(2017, 1, 2, 12, 11, 12, 321), entity.DateTimeAsDatetime23);
Assert.Equal(TimeSpan.Parse("12:34:56.789", System.Globalization.CultureInfo.InvariantCulture), entity.TimeSpanAsTime3);
Assert.Equal(101m, entity.DecimalAsDecimal3);
Assert.Equal(102m, entity.DecimalAsDec3);
Assert.Equal(103m, entity.DecimalAsNumeric3);
Expand All @@ -1619,7 +1621,8 @@ private static MappedScaledDataTypes CreateMappedScaledDataTypes(int id)
DateTimeAsDatetime23 = new DateTime(2017, 1, 2, 12, 11, 12, 321),
DecimalAsDecimal3 = 101m,
DecimalAsDec3 = 102m,
DecimalAsNumeric3 = 103m
DecimalAsNumeric3 = 103m,
TimeSpanAsTime3 = TimeSpan.Parse("12:34:56.7890123", System.Globalization.CultureInfo.InvariantCulture)
};

[ConditionalFact]
Expand All @@ -1635,15 +1638,16 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_scale_se
var parameters = DumpParameters();
Assert.Equal(
@"@p0='77'
@p1='2017-01-02T12:11:12.3210000' (Precision = 3)
@p2='2016-01-02T11:11:12.7650000+00:00' (Precision = 3)
@p1='2017-01-02T12:11:12.3210000' (Scale = 3)
@p2='2016-01-02T11:11:12.7650000+00:00' (Scale = 3)
@p3='102' (Precision = 3)
@p4='101' (Precision = 3)
@p5='103' (Precision = 3)
@p6='85.55000305175781' (Size = 25)
@p7='85.5' (Size = 3)
@p8='83.33000183105469' (Size = 25)
@p9='83.3' (Size = 3)",
@p9='83.3' (Size = 3)
@p10='12:34:56.7890000' (Scale = 3)",
parameters,
ignoreLineEndingDifferences: true);

Expand All @@ -1666,6 +1670,7 @@ private static void AssertMappedScaledSeparatelyDataTypes(MappedScaledSeparately
Assert.Equal(101m, entity.DecimalAsDecimal3);
Assert.Equal(102m, entity.DecimalAsDec3);
Assert.Equal(103m, entity.DecimalAsNumeric3);
Assert.Equal(TimeSpan.Parse("12:34:56.789", System.Globalization.CultureInfo.InvariantCulture), entity.TimeSpanAsTime3);
}

private static MappedScaledSeparatelyDataTypes CreateMappedScaledSeparatelyDataTypes(int id)
Expand All @@ -1680,7 +1685,8 @@ private static MappedScaledSeparatelyDataTypes CreateMappedScaledSeparatelyDataT
DateTimeAsDatetime23 = new DateTime(2017, 1, 2, 12, 11, 12, 321),
DecimalAsDecimal3 = 101m,
DecimalAsDec3 = 102m,
DecimalAsNumeric3 = 103m
DecimalAsNumeric3 = 103m,
TimeSpanAsTime3 = TimeSpan.Parse("12:34:56.789", System.Globalization.CultureInfo.InvariantCulture)
};

[ConditionalFact]
Expand Down Expand Up @@ -2480,18 +2486,19 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_scale_wi
Assert.Equal(1, context.SaveChanges());
}

var parameters = DumpParameters();
var parameters = DumpParameters();
Assert.Equal(
@"@p0='2017-01-02T12:11:12.1230000' (Precision = 3)
@p1='2016-01-02T11:11:12.5670000+00:00' (Precision = 3)
@"@p0='2017-01-02T12:11:12.1230000' (Scale = 3)
@p1='2016-01-02T11:11:12.5670000+00:00' (Scale = 3)
@p2='102' (Precision = 3)
@p3='101' (Precision = 3)
@p4='103' (Precision = 3)
@p5='85.55000305175781' (Size = 25)
@p6='85.5' (Size = 3)
@p7='83.33000183105469' (Size = 25)
@p8='83.3' (Size = 3)
@p9='77'",
@p9='77'
@p10='12:34:56.7890123' (Scale = 3)",
parameters,
ignoreLineEndingDifferences: true);

Expand All @@ -2514,6 +2521,7 @@ private static void AssertMappedScaledDataTypesWithIdentity(MappedScaledDataType
Assert.Equal(101m, entity.DecimalAsDecimal3);
Assert.Equal(102m, entity.DecimalAsDec3);
Assert.Equal(103m, entity.DecimalAsNumeric3);
Assert.Equal(TimeSpan.Parse("12:34:56.789", System.Globalization.CultureInfo.InvariantCulture), entity.TimeSpanAsTime3);
}

private static MappedScaledDataTypesWithIdentity CreateMappedScaledDataTypesWithIdentity(int id)
Expand All @@ -2528,7 +2536,8 @@ private static MappedScaledDataTypesWithIdentity CreateMappedScaledDataTypesWith
DateTimeAsDatetime23 = new DateTime(2017, 1, 2, 12, 11, 12, 123),
DecimalAsDecimal3 = 101m,
DecimalAsDec3 = 102m,
DecimalAsNumeric3 = 103m
DecimalAsNumeric3 = 103m,
TimeSpanAsTime3 = TimeSpan.Parse("12:34:56.7890123", System.Globalization.CultureInfo.InvariantCulture)
};

[ConditionalFact]
Expand Down Expand Up @@ -3232,6 +3241,7 @@ public virtual void Columns_have_expected_data_types()
MappedScaledDataTypes.FloatAsFloat25 ---> [float] [Precision = 53]
MappedScaledDataTypes.FloatAsFloat3 ---> [real] [Precision = 24]
MappedScaledDataTypes.Id ---> [int] [Precision = 10 Scale = 0]
MappedScaledDataTypes.TimeSpanAsTime3 ---> [time] [Precision = 3]
MappedScaledDataTypesWithIdentity.DateTimeAsDatetime23 ---> [datetime2] [Precision = 3]
MappedScaledDataTypesWithIdentity.DateTimeOffsetAsDatetimeoffset3 ---> [datetimeoffset] [Precision = 3]
MappedScaledDataTypesWithIdentity.DecimalAsDec3 ---> [decimal] [Precision = 3 Scale = 0]
Expand All @@ -3243,6 +3253,7 @@ public virtual void Columns_have_expected_data_types()
MappedScaledDataTypesWithIdentity.FloatAsFloat3 ---> [real] [Precision = 24]
MappedScaledDataTypesWithIdentity.Id ---> [int] [Precision = 10 Scale = 0]
MappedScaledDataTypesWithIdentity.Int ---> [int] [Precision = 10 Scale = 0]
MappedScaledDataTypesWithIdentity.TimeSpanAsTime3 ---> [time] [Precision = 3]
MappedScaledSeparatelyDataTypes.DateTimeAsDatetime23 ---> [datetime2] [Precision = 3]
MappedScaledSeparatelyDataTypes.DateTimeOffsetAsDatetimeoffset3 ---> [datetimeoffset] [Precision = 3]
MappedScaledSeparatelyDataTypes.DecimalAsDec3 ---> [decimal] [Precision = 3 Scale = 0]
Expand All @@ -3253,6 +3264,7 @@ public virtual void Columns_have_expected_data_types()
MappedScaledSeparatelyDataTypes.FloatAsFloat25 ---> [float] [Precision = 53]
MappedScaledSeparatelyDataTypes.FloatAsFloat3 ---> [real] [Precision = 24]
MappedScaledSeparatelyDataTypes.Id ---> [int] [Precision = 10 Scale = 0]
MappedScaledSeparatelyDataTypes.TimeSpanAsTime3 ---> [time] [Precision = 3]
MappedSizedDataTypes.BytesAsBinary3 ---> [nullable binary] [MaxLength = 3]
MappedSizedDataTypes.BytesAsBinaryVarying3 ---> [nullable varbinary] [MaxLength = 3]
MappedSizedDataTypes.BytesAsVarbinary3 ---> [nullable varbinary] [MaxLength = 3]
Expand Down Expand Up @@ -4127,6 +4139,9 @@ protected class MappedScaledDataTypes

[Column(TypeName = "numeric(3)")]
public decimal DecimalAsNumeric3 { get; set; }

[Column(TypeName = "time(3)")]
public TimeSpan TimeSpanAsTime3 { get; set; }
}

protected class MappedScaledSeparatelyDataTypes
Expand Down Expand Up @@ -4159,6 +4174,9 @@ protected class MappedScaledSeparatelyDataTypes

[Column(TypeName = "numeric")]
public decimal DecimalAsNumeric3 { get; set; }

[Column(TypeName = "time(3)")]
public TimeSpan TimeSpanAsTime3 { get; set; }
}

protected class DoubleDataTypes
Expand Down Expand Up @@ -4611,6 +4629,9 @@ protected class MappedScaledDataTypesWithIdentity

[Column(TypeName = "numeric(3)")]
public decimal DecimalAsNumeric3 { get; set; }

[Column(TypeName = "time(3)")]
public TimeSpan TimeSpanAsTime3 { get; set; }
}

protected class MappedPrecisionAndScaledDataTypesWithIdentity
Expand Down

0 comments on commit 9db26bd

Please sign in to comment.