|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Data; |
| 7 | +using Microsoft.Data.SqlClient.Server; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.Data.SqlClient.ManualTesting.Tests |
| 11 | +{ |
| 12 | + public class DateTimeOffsetList : SqlDataRecord |
| 13 | + { |
| 14 | + public DateTimeOffsetList(DateTimeOffset dateTimeOffset) |
| 15 | + : base(new SqlMetaData("dateTimeOffset", SqlDbType.DateTimeOffset, 0, 1)) // this is using scale 1 |
| 16 | + { |
| 17 | + this.SetValues(dateTimeOffset); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public class DateTimeOffsetVariableScale : SqlDataRecord |
| 22 | + { |
| 23 | + public DateTimeOffsetVariableScale(DateTimeOffset dateTimeOffset, int scale) |
| 24 | + : base(new SqlMetaData("dateTimeOffset", SqlDbType.DateTimeOffset, 0, (byte)scale)) // this is using variable scale |
| 25 | + { |
| 26 | + this.SetValues(dateTimeOffset); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public class UdtDateTimeOffsetTest |
| 31 | + { |
| 32 | + private readonly string _connectionString = null; |
| 33 | + private readonly string _udtTableType = DataTestUtility.GetUniqueNameForSqlServer("DataTimeOffsetTableType"); |
| 34 | + |
| 35 | + public UdtDateTimeOffsetTest() |
| 36 | + { |
| 37 | + _connectionString = DataTestUtility.TCPConnectionString; |
| 38 | + } |
| 39 | + |
| 40 | + // This unit test is for the reported issue #2423 using a specific scale of 1 |
| 41 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.IsNotAzureSynapse))] |
| 42 | + public void SelectFromSqlParameterShouldSucceed() |
| 43 | + { |
| 44 | + using SqlConnection connection = new(_connectionString); |
| 45 | + connection.Open(); |
| 46 | + SetupUserDefinedTableType(connection, _udtTableType); |
| 47 | + |
| 48 | + try |
| 49 | + { |
| 50 | + DateTimeOffset dateTimeOffset = new DateTimeOffset(2024, 1, 1, 23, 59, 59, 500, TimeSpan.Zero); |
| 51 | + var param = new SqlParameter |
| 52 | + { |
| 53 | + ParameterName = "@params", |
| 54 | + SqlDbType = SqlDbType.Structured, |
| 55 | + TypeName = $"dbo.{_udtTableType}", |
| 56 | + Value = new DateTimeOffsetList[] { new DateTimeOffsetList(dateTimeOffset) } |
| 57 | + }; |
| 58 | + |
| 59 | + using (var cmd = connection.CreateCommand()) |
| 60 | + { |
| 61 | + cmd.CommandText = "SELECT * FROM @params"; |
| 62 | + cmd.Parameters.Add(param); |
| 63 | + var result = cmd.ExecuteScalar(); |
| 64 | + Assert.Equal(dateTimeOffset, result); |
| 65 | + } |
| 66 | + } |
| 67 | + finally |
| 68 | + { |
| 69 | + DataTestUtility.DropUserDefinedType(connection, _udtTableType); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // This unit test is to ensure that time in DateTimeOffset with all scales are working as expected |
| 74 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.IsNotAzureSynapse))] |
| 75 | + public void DateTimeOffsetAllScalesTestShouldSucceed() |
| 76 | + { |
| 77 | + string tvpTypeName = DataTestUtility.GetUniqueNameForSqlServer("tvpType"); |
| 78 | + |
| 79 | + using SqlConnection connection = new(_connectionString); |
| 80 | + connection.Open(); |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + // Use different scale for each test: 0 to 7 |
| 85 | + int fromScale = 0; |
| 86 | + int toScale = 7; |
| 87 | + |
| 88 | + for (int scale = fromScale; scale <= toScale; scale++) |
| 89 | + { |
| 90 | + DateTimeOffset dateTimeOffset = new DateTimeOffset(2024, 1, 1, 23, 59, 59, TimeSpan.Zero); |
| 91 | + |
| 92 | + // Add sub-second offset corresponding to the scale being tested |
| 93 | + TimeSpan subSeconds = TimeSpan.FromTicks((long)(TimeSpan.TicksPerSecond / Math.Pow(10, scale))); |
| 94 | + dateTimeOffset = dateTimeOffset.Add(subSeconds); |
| 95 | + |
| 96 | + DataTestUtility.DropUserDefinedType(connection, tvpTypeName); |
| 97 | + SetupDateTimeOffsetTableType(connection, tvpTypeName, scale); |
| 98 | + |
| 99 | + var param = new SqlParameter |
| 100 | + { |
| 101 | + ParameterName = "@params", |
| 102 | + SqlDbType = SqlDbType.Structured, |
| 103 | + Scale = (byte)scale, |
| 104 | + TypeName = $"dbo.{tvpTypeName}", |
| 105 | + Value = new DateTimeOffsetVariableScale[] { new DateTimeOffsetVariableScale(dateTimeOffset, scale) } |
| 106 | + }; |
| 107 | + |
| 108 | + using (var cmd = connection.CreateCommand()) |
| 109 | + { |
| 110 | + cmd.CommandText = "SELECT * FROM @params"; |
| 111 | + cmd.Parameters.Add(param); |
| 112 | + var result = cmd.ExecuteScalar(); |
| 113 | + Assert.Equal(dateTimeOffset, result); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + finally |
| 118 | + { |
| 119 | + DataTestUtility.DropUserDefinedType(connection, tvpTypeName); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void SetupUserDefinedTableType(SqlConnection connection, string tableTypeName) |
| 124 | + { |
| 125 | + using (SqlCommand cmd = connection.CreateCommand()) |
| 126 | + { |
| 127 | + cmd.CommandType = CommandType.Text; |
| 128 | + cmd.CommandText = $"CREATE TYPE {tableTypeName} AS TABLE ([Value] DATETIMEOFFSET(1) NOT NULL) "; |
| 129 | + cmd.ExecuteNonQuery(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static void SetupDateTimeOffsetTableType(SqlConnection connection, string tableTypeName, int scale) |
| 134 | + { |
| 135 | + using (SqlCommand cmd = connection.CreateCommand()) |
| 136 | + { |
| 137 | + cmd.CommandType = CommandType.Text; |
| 138 | + cmd.CommandText = $"CREATE TYPE {tableTypeName} AS TABLE ([Value] DATETIMEOFFSET({scale}) NOT NULL) "; |
| 139 | + cmd.ExecuteNonQuery(); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments