|
6 | 6 | using System.Data; |
7 | 7 | using System.Data.Common; |
8 | 8 | using System.Data.SqlTypes; |
| 9 | +using System.Reflection; |
9 | 10 | using Xunit; |
10 | 11 |
|
11 | 12 | namespace Microsoft.Data.SqlClient.Tests |
@@ -1851,5 +1852,112 @@ private enum Int64Enum : long |
1851 | 1852 | A = long.MinValue, |
1852 | 1853 | B = long.MaxValue |
1853 | 1854 | } |
| 1855 | + |
| 1856 | + |
| 1857 | + private static readonly object _parameterLegacyScaleLock = new(); |
| 1858 | + |
| 1859 | + [Theory] |
| 1860 | + [InlineData(null, 7, true)] |
| 1861 | + [InlineData(0, 7, true)] |
| 1862 | + [InlineData(1, 1, true)] |
| 1863 | + [InlineData(2, 2, true)] |
| 1864 | + [InlineData(3, 3, true)] |
| 1865 | + [InlineData(4, 4, true)] |
| 1866 | + [InlineData(5, 5, true)] |
| 1867 | + [InlineData(6, 6, true)] |
| 1868 | + [InlineData(7, 7, true)] |
| 1869 | + [InlineData(null, 7, false)] |
| 1870 | + [InlineData(0, 0, false)] |
| 1871 | + [InlineData(1, 1, false)] |
| 1872 | + [InlineData(2, 2, false)] |
| 1873 | + [InlineData(3, 3, false)] |
| 1874 | + [InlineData(4, 4, false)] |
| 1875 | + [InlineData(5, 5, false)] |
| 1876 | + [InlineData(6, 6, false)] |
| 1877 | + [InlineData(7, 7, false)] |
| 1878 | + [InlineData(null, 7, null)] |
| 1879 | + [InlineData(0, 7, null)] |
| 1880 | + [InlineData(1, 1, null)] |
| 1881 | + [InlineData(2, 2, null)] |
| 1882 | + [InlineData(3, 3, null)] |
| 1883 | + [InlineData(4, 4, null)] |
| 1884 | + [InlineData(5, 5, null)] |
| 1885 | + [InlineData(6, 6, null)] |
| 1886 | + [InlineData(7, 7, null)] |
| 1887 | + public void SqlDatetime2Scale_Legacy(int? setScale, byte outputScale, bool? legacyVarTimeZeroScaleSwitchValue) |
| 1888 | + { |
| 1889 | + lock (_parameterLegacyScaleLock) |
| 1890 | + { |
| 1891 | + var originalLegacyVarTimeZeroScaleSwitchValue = SetLegacyVarTimeZeroScaleBehaviour(legacyVarTimeZeroScaleSwitchValue); |
| 1892 | + try |
| 1893 | + { |
| 1894 | + var parameter = new SqlParameter |
| 1895 | + { |
| 1896 | + DbType = DbType.DateTime2 |
| 1897 | + }; |
| 1898 | + if (setScale.HasValue) |
| 1899 | + { |
| 1900 | + parameter.Scale = (byte)setScale.Value; |
| 1901 | + } |
| 1902 | + |
| 1903 | + var actualScale = (byte)typeof(SqlParameter).GetMethod("GetActualScale", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(parameter, null); |
| 1904 | + |
| 1905 | + Assert.Equal(outputScale, actualScale); |
| 1906 | + } |
| 1907 | + |
| 1908 | + finally |
| 1909 | + { |
| 1910 | + SetLegacyVarTimeZeroScaleBehaviour(originalLegacyVarTimeZeroScaleSwitchValue); |
| 1911 | + } |
| 1912 | + } |
| 1913 | + } |
| 1914 | + |
| 1915 | + [Fact] |
| 1916 | + public void SetLegacyVarTimeZeroScaleBehaviour_Defaults_to_True() |
| 1917 | + { |
| 1918 | + var legacyVarTimeZeroScaleBehaviour = (bool)LocalAppContextSwitchesType.GetProperty("LegacyVarTimeZeroScaleBehaviour", BindingFlags.Public | BindingFlags.Static).GetValue(null); |
| 1919 | + |
| 1920 | + Assert.True(legacyVarTimeZeroScaleBehaviour); |
| 1921 | + } |
| 1922 | + |
| 1923 | + private static Type LocalAppContextSwitchesType => typeof(SqlCommand).Assembly.GetType("Microsoft.Data.SqlClient.LocalAppContextSwitches"); |
| 1924 | + |
| 1925 | + private static bool? SetLegacyVarTimeZeroScaleBehaviour(bool? value) |
| 1926 | + { |
| 1927 | + const string LegacyVarTimeZeroScaleBehaviourSwitchname = @"Switch.Microsoft.Data.SqlClient.LegacyVarTimeZeroScaleBehaviour"; |
| 1928 | + |
| 1929 | + //reset internal state to "NotInitialized" so we pick up the value via AppContext |
| 1930 | + FieldInfo switchField = LocalAppContextSwitchesType.GetField("s_legacyVarTimeZeroScaleBehaviour", BindingFlags.NonPublic | BindingFlags.Static); |
| 1931 | + switchField.SetValue(null, (byte)0); |
| 1932 | + |
| 1933 | + bool? returnValue = null; |
| 1934 | + if (AppContext.TryGetSwitch(LegacyVarTimeZeroScaleBehaviourSwitchname, out var originalValue)) |
| 1935 | + { |
| 1936 | + returnValue = originalValue; |
| 1937 | + } |
| 1938 | + |
| 1939 | + if (value.HasValue) |
| 1940 | + { |
| 1941 | + AppContext.SetSwitch(LegacyVarTimeZeroScaleBehaviourSwitchname, value.Value); |
| 1942 | + } |
| 1943 | + else |
| 1944 | + { |
| 1945 | + //need to remove the switch value via reflection as AppContext does not expose a means to do that. |
| 1946 | +#if NET5_0_OR_GREATER |
| 1947 | + var switches = typeof(AppContext).GetField("s_switches", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); |
| 1948 | + if (switches is not null) //may be null if not initialised yet |
| 1949 | + { |
| 1950 | + MethodInfo removeMethod = switches.GetType().GetMethod("Remove", BindingFlags.Public | BindingFlags.Instance, new Type[] { typeof(string) }); |
| 1951 | + removeMethod.Invoke(switches, new[] { LegacyVarTimeZeroScaleBehaviourSwitchname }); |
| 1952 | + } |
| 1953 | +#else |
| 1954 | + var switches = typeof(AppContext).GetField("s_switchMap", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); |
| 1955 | + MethodInfo removeMethod = switches.GetType().GetMethod("Remove", BindingFlags.Public | BindingFlags.Instance); |
| 1956 | + removeMethod.Invoke(switches, new[] { LegacyVarTimeZeroScaleBehaviourSwitchname }); |
| 1957 | +#endif |
| 1958 | + } |
| 1959 | + |
| 1960 | + return returnValue; |
| 1961 | + } |
1854 | 1962 | } |
1855 | 1963 | } |
0 commit comments