From 5cb73fdc9fa408fcd033510b2e2182085c1f14c9 Mon Sep 17 00:00:00 2001 From: Edward Neal <55035479+edwardneal@users.noreply.github.com> Date: Tue, 14 May 2024 19:31:10 +0100 Subject: [PATCH] Merged SqlClientFactory between .NET Core and Framework (#2369) --- .../netcore/ref/Microsoft.Data.SqlClient.cs | 30 +++-- .../src/Microsoft.Data.SqlClient.csproj | 5 +- .../SqlClient/SqlClientFactory.NetCoreApp.cs | 24 ---- .../Data/SqlClient/SqlClientFactory.cs | 55 --------- .../netfx/src/Microsoft.Data.SqlClient.csproj | 6 +- .../Data/SqlClient/SqlClientFactory.cs | 98 ---------------- .../Data/SqlClient/SqlClientFactory.cs | 108 ++++++++++++++++++ .../FunctionalTests/SqlClientFactoryTest.cs | 36 +++--- 8 files changed, 149 insertions(+), 213 deletions(-) delete mode 100644 src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.NetCoreApp.cs delete mode 100644 src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.cs delete mode 100644 src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlClientFactory.cs create mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs diff --git a/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs b/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs index b228b0a137..72203bb433 100644 --- a/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs +++ b/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs @@ -189,23 +189,23 @@ public class SqlAuthenticationParameters /// protected SqlAuthenticationParameters(Microsoft.Data.SqlClient.SqlAuthenticationMethod authenticationMethod, string serverName, string databaseName, string resource, string authority, string userId, string password, System.Guid connectionId, int connectionTimeout) { } /// - public Microsoft.Data.SqlClient.SqlAuthenticationMethod AuthenticationMethod { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public Microsoft.Data.SqlClient.SqlAuthenticationMethod AuthenticationMethod { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public string Authority { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string Authority { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public System.Guid ConnectionId { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public System.Guid ConnectionId { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public string DatabaseName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string DatabaseName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public string Password { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string Password { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public string Resource { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string Resource { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public string ServerName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string ServerName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public string UserId { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string UserId { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public int ConnectionTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public int ConnectionTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } } /// public abstract partial class SqlAuthenticationProvider @@ -231,9 +231,9 @@ public partial class SqlAuthenticationToken /// public SqlAuthenticationToken(string accessToken, System.DateTimeOffset expiresOn) { } /// - public string AccessToken { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public string AccessToken { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } /// - public System.DateTimeOffset ExpiresOn { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public System.DateTimeOffset ExpiresOn { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } } /// public sealed partial class SqlBulkCopy : System.IDisposable @@ -427,6 +427,14 @@ internal SqlClientFactory() { } public override System.Data.Common.DbDataAdapter CreateDataAdapter() { throw null; } /// public override System.Data.Common.DbParameter CreateParameter() { throw null; } + /// + public override System.Data.Common.DbDataSourceEnumerator CreateDataSourceEnumerator() { throw null; } + /// + public override bool CanCreateBatch { get { throw null; } } + /// + public override System.Data.Common.DbBatch CreateBatch() { throw null; } + /// + public override System.Data.Common.DbBatchCommand CreateBatchCommand() { throw null; } } /// public partial class SqlClientLogger diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 57497fd2b0..2cd3537ea7 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -383,6 +383,9 @@ Microsoft\Data\SqlClient\SqlClientEventSource.cs + + Microsoft\Data\SqlClient\SqlClientFactory.cs + Microsoft\Data\SqlClient\SqlClientLogger.cs @@ -646,8 +649,6 @@ - - diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.NetCoreApp.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.NetCoreApp.cs deleted file mode 100644 index a48179b621..0000000000 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.NetCoreApp.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#if NET6_0_OR_GREATER - -using System.Data.Common; - -namespace Microsoft.Data.SqlClient -{ - public sealed partial class SqlClientFactory : DbProviderFactory - { - /// - public override bool CanCreateBatch => true; - - /// - public override DbBatch CreateBatch() => new SqlBatch(); - - /// - public override DbBatchCommand CreateBatchCommand() => new SqlBatchCommand(); - } -} - -#endif diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.cs deleted file mode 100644 index 2dd3261fe6..0000000000 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientFactory.cs +++ /dev/null @@ -1,55 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Data.Common; - -namespace Microsoft.Data.SqlClient -{ - /// - public sealed partial class SqlClientFactory : DbProviderFactory - { - /// - public static readonly SqlClientFactory Instance = new SqlClientFactory(); - - private SqlClientFactory() - { - } - - /// - public override DbCommand CreateCommand() - { - return new SqlCommand(); - } - - /// - public override DbCommandBuilder CreateCommandBuilder() - { - return new SqlCommandBuilder(); - } - - /// - public override DbConnection CreateConnection() - { - return new SqlConnection(); - } - - /// - public override DbConnectionStringBuilder CreateConnectionStringBuilder() - { - return new SqlConnectionStringBuilder(); - } - - /// - public override DbDataAdapter CreateDataAdapter() - { - return new SqlDataAdapter(); - } - - /// - public override DbParameter CreateParameter() - { - return new SqlParameter(); - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index 2dd8d69b08..e291206a4a 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -423,6 +423,9 @@ Microsoft\Data\SqlClient\SqlClientEventSource.cs + + Microsoft\Data\SqlClient\SqlClientFactory.cs + Microsoft\Data\SqlClient\SqlClientLogger.cs @@ -668,7 +671,6 @@ - @@ -766,4 +768,4 @@ - \ No newline at end of file + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlClientFactory.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlClientFactory.cs deleted file mode 100644 index 3d44d202c0..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlClientFactory.cs +++ /dev/null @@ -1,98 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Data.Common; -using System.Data.Sql; -using System.Security; -using System.Security.Permissions; -using Microsoft.Data.Common; - -namespace Microsoft.Data.SqlClient -{ - /// - public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider - { - - /// - public static readonly SqlClientFactory Instance = new SqlClientFactory(); - - private SqlClientFactory() - { - } - - /// - public override bool CanCreateDataSourceEnumerator - { - get - { - return true; - } - } - - /// - public override DbCommand CreateCommand() - { - return new SqlCommand(); - } - - /// - public override DbCommandBuilder CreateCommandBuilder() - { - return new SqlCommandBuilder(); - } - - /// - public override DbConnection CreateConnection() - { - return new SqlConnection(); - } - - /// - public override DbConnectionStringBuilder CreateConnectionStringBuilder() - { - return new SqlConnectionStringBuilder(); - } - - /// - public override DbDataAdapter CreateDataAdapter() - { - return new SqlDataAdapter(); - } - - /// - public override DbParameter CreateParameter() - { - return new SqlParameter(); - } - - /// - public override CodeAccessPermission CreatePermission(PermissionState state) - { - return new SqlClientPermission(state); - } - - /// - public override DbDataSourceEnumerator CreateDataSourceEnumerator() - { - return SqlDataSourceEnumerator.Instance; - } - - /// - /// Extension mechanism for additional services; currently the only service - /// supported is the DbProviderServices - /// - /// requested service provider or null. - object IServiceProvider.GetService(Type serviceType) - { - object result = null; - if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type) - { - result = GreenMethods.MicrosoftDataSqlClientSqlProviderServices_Instance(); - } - return result; - } - } -} - diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs new file mode 100644 index 0000000000..eacf4d6e55 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs @@ -0,0 +1,108 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Data.Sql; +using System; +using System.Data.Common; +using System.Security.Permissions; +using System.Security; +using Microsoft.Data.Common; + +namespace Microsoft.Data.SqlClient +{ + /// + public sealed class SqlClientFactory : DbProviderFactory +#if NETFRAMEWORK + , IServiceProvider +#endif + { + + /// + public static readonly SqlClientFactory Instance = new SqlClientFactory(); + + private SqlClientFactory() + { + } + + /// + public override bool CanCreateDataSourceEnumerator => true; + + /// + public override DbCommand CreateCommand() + { + return new SqlCommand(); + } + + /// + public override DbCommandBuilder CreateCommandBuilder() + { + return new SqlCommandBuilder(); + } + + /// + public override DbConnection CreateConnection() + { + return new SqlConnection(); + } + + /// + public override DbConnectionStringBuilder CreateConnectionStringBuilder() + { + return new SqlConnectionStringBuilder(); + } + + /// + public override DbDataAdapter CreateDataAdapter() + { + return new SqlDataAdapter(); + } + + /// + public override DbParameter CreateParameter() + { + return new SqlParameter(); + } + +#if NETFRAMEWORK + /// + public override CodeAccessPermission CreatePermission(PermissionState state) + { + return new SqlClientPermission(state); + } + + /// + /// Extension mechanism for additional services; currently the only service + /// supported is the DbProviderServices + /// + /// requested service provider or null. + object IServiceProvider.GetService(Type serviceType) + { + object result = null; + if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type) + { + result = GreenMethods.MicrosoftDataSqlClientSqlProviderServices_Instance(); + } + return result; + } +#endif + + /// + public override DbDataSourceEnumerator CreateDataSourceEnumerator() + { + return SqlDataSourceEnumerator.Instance; + } + +#if NET6_0_OR_GREATER + /// + public override bool CanCreateBatch => true; + + /// + public override DbBatch CreateBatch() => new SqlBatch(); + + /// + public override DbBatchCommand CreateBatchCommand() => new SqlBatchCommand(); +#endif + + } +} diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlClientFactoryTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlClientFactoryTest.cs index f0e8d39173..b88ae0a260 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlClientFactoryTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlClientFactoryTest.cs @@ -21,40 +21,34 @@ public void InstanceTest() public static readonly object[][] FactoryMethodTestData = { - new object[] { new Func(SqlClientFactory.Instance.CreateCommand), typeof(SqlCommand) }, - new object[] { new Func(SqlClientFactory.Instance.CreateCommandBuilder), typeof(SqlCommandBuilder) }, - new object[] { new Func(SqlClientFactory.Instance.CreateConnection), typeof(SqlConnection) }, - new object[] { new Func(SqlClientFactory.Instance.CreateConnectionStringBuilder), typeof(SqlConnectionStringBuilder) }, - new object[] { new Func(SqlClientFactory.Instance.CreateDataAdapter), typeof(SqlDataAdapter) }, - new object[] { new Func(SqlClientFactory.Instance.CreateParameter), typeof(SqlParameter) }, + new object[] { new Func(SqlClientFactory.Instance.CreateCommand), typeof(SqlCommand), false }, + new object[] { new Func(SqlClientFactory.Instance.CreateCommandBuilder), typeof(SqlCommandBuilder), false }, + new object[] { new Func(SqlClientFactory.Instance.CreateConnection), typeof(SqlConnection), false }, + new object[] { new Func(SqlClientFactory.Instance.CreateConnectionStringBuilder), typeof(SqlConnectionStringBuilder), false }, + new object[] { new Func(SqlClientFactory.Instance.CreateDataAdapter), typeof(SqlDataAdapter), false }, + new object[] { new Func(SqlClientFactory.Instance.CreateParameter), typeof(SqlParameter), false }, + new object[] { new Func(SqlClientFactory.Instance.CreateDataSourceEnumerator), typeof(Microsoft.Data.Sql.SqlDataSourceEnumerator), true }, }; [Theory] [MemberData(nameof(FactoryMethodTestData))] - public void FactoryMethodTest(Func factory, Type expectedType) + public void FactoryMethodTest(Func factory, Type expectedType, bool singleton) { object value1 = factory(); Assert.NotNull(value1); Assert.IsType(expectedType, value1); - object value2 = factory(); - Assert.NotNull(value2); - Assert.IsType(expectedType, value2); + if (!singleton) + { + object value2 = factory(); + Assert.NotNull(value2); + Assert.IsType(expectedType, value2); - Assert.NotSame(value1, value2); + Assert.NotSame(value1, value2); + } } #if NETFRAMEWORK - [Fact] - public void FactoryCreateDataSourceEnumerator() - { - // Unable to cover the in the FactoryMethodTest because the SqlDataSourceEnumerator is a singleton so, it's always the same. - object instance = SqlClientFactory.Instance.CreateDataSourceEnumerator(); - // SqlDataSourceEnumerator is not available for .NET core 3.1 and above, so the type check is only for .NET Framework. - Assert.IsType(instance); - Assert.NotNull(instance); - } - [Fact] public void FactoryGetService() {