Skip to content

Commit

Permalink
add tests and fix #26742
Browse files Browse the repository at this point in the history
test and fix
  • Loading branch information
EamonHetherton committed Dec 6, 2021
1 parent 6da773f commit 5013f47
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,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 @@ -25,7 +25,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 @@ -89,7 +89,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 Expand Up @@ -133,6 +134,6 @@ protected override string SqlLiteralFormatString
return _dateTime2Formats[7];
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Storage;

Expand All @@ -16,14 +18,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 @@ -61,6 +85,48 @@ 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 @@ -352,7 +352,8 @@ public SqlServerTypeMappingSource(
"datetime2",
"datetimeoffset",
"double precision",
"float"
"float",
"time"
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1584,15 +1584,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 @@ -1612,6 +1613,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 @@ -1629,7 +1631,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 @@ -1645,15 +1648,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 @@ -1676,6 +1680,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 @@ -1690,7 +1695,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 @@ -2492,16 +2498,17 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_scale_wi

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 @@ -2524,6 +2531,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 @@ -2538,7 +2546,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 @@ -3242,6 +3251,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 @@ -3253,6 +3263,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 @@ -3263,6 +3274,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 @@ -4137,6 +4149,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 @@ -4169,6 +4184,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 @@ -4621,6 +4639,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
Loading

0 comments on commit 5013f47

Please sign in to comment.