From 206e32753565af72c6d1048e90f2352e6737a4b8 Mon Sep 17 00:00:00 2001 From: Ben Russell Date: Mon, 18 Nov 2024 17:15:46 -0600 Subject: [PATCH 1/2] Move the localdb config classes into common project --- .../netfx/src/Microsoft.Data.SqlClient.csproj | 4 +++- .../src/Microsoft.Data.SqlClient.csproj | 2 +- .../Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs} | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) rename src/Microsoft.Data.SqlClient/{netfx/src/Microsoft/Data/SqlClient/LocalDBConfig.cs => src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs} (98%) 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 0321ff4dfd..61076ddf82 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -391,6 +391,9 @@ Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs + + Microsoft\Data\SqlClient\LocalDb\LocalDBConfig.netfx.cs + Microsoft\Data\SqlClient\LocalAppContextSwitches.cs @@ -839,7 +842,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj index f42fe42b2c..70f6b3ff2c 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBConfig.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs similarity index 98% rename from src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBConfig.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs index 4c830520d9..e2e12f5ad5 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBConfig.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#if NETFRAMEWORK + namespace Microsoft.Data { using System; @@ -79,3 +81,5 @@ public LocalDBInstancesCollection LocalDbInstances } } } + +#endif From 6d7191ed249513326a0b48c21963853727642834 Mon Sep 17 00:00:00 2001 From: Ben Russell Date: Mon, 18 Nov 2024 17:30:13 -0600 Subject: [PATCH 2/2] Split the LocalDbConfig file into a separate files in the right namespaces (and cleaning them up) --- .../netfx/src/Microsoft.Data.SqlClient.csproj | 10 ++- .../Data/SqlClient/LocalDBAPI.Windows.cs | 13 +-- .../SqlClient/LocalDb/LocalDBConfig.netfx.cs | 85 ------------------- .../LocalDbConfigurationSection.netfx.cs | 19 +++++ .../LocalDb/LocalDbInstanceElement.netfx.cs | 21 +++++ .../LocalDbInstancesCollection.netfx.cs | 48 +++++++++++ 6 files changed, 103 insertions(+), 93 deletions(-) delete mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs create mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbConfigurationSection.netfx.cs create mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstanceElement.netfx.cs create mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstancesCollection.netfx.cs 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 61076ddf82..1ff1dc373a 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -391,8 +391,14 @@ Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs - - Microsoft\Data\SqlClient\LocalDb\LocalDBConfig.netfx.cs + + Microsoft\Data\SqlClient\LocalDb\LocalDbConfigurationSection.netfx.cs + + + Microsoft\Data\SqlClient\LocalDb\LocalDbInstanceElement.netfx.cs + + + Microsoft\Data\SqlClient\LocalDb\LocalDbInstancesCollection.netfx.cs Microsoft\Data\SqlClient\LocalAppContextSwitches.cs diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs index 77194052da..efdad34495 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs @@ -8,15 +8,16 @@ using System.Runtime.InteropServices; using System.Security; using System.Text; +using Interop.Windows.Kernel32; using Interop.Windows.Sni; using Microsoft.Data.SqlClient; -using Interop.Windows.Kernel32; #if NETFRAMEWORK using System.Collections.Generic; using System.Configuration; using System.Runtime.CompilerServices; using System.Threading; +using Microsoft.Data.SqlClient.LocalDb; #endif namespace Microsoft.Data @@ -182,13 +183,13 @@ internal static void CreateLocalDBInstance(string instance) if (section != null) // if no section just skip creation { // validate section type - LocalDBConfigurationSection configSection = section as LocalDBConfigurationSection; + LocalDbConfigurationSection configSection = section as LocalDbConfigurationSection; if (configSection == null) { throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_BadConfigSectionType")); } - - foreach (LocalDBInstanceElement confElement in configSection.LocalDbInstances) + + foreach (LocalDbInstanceElement confElement in configSection.LocalDbInstances) { Debug.Assert(confElement.Name != null && confElement.Version != null, "Both name and version should not be null"); tempConfigurableInstances.Add(confElement.Name.Trim(), new InstanceInfo(confElement.Version.Trim())); @@ -215,7 +216,7 @@ internal static void CreateLocalDBInstance(string instance) if (!s_configurableInstances.TryGetValue(instance, out instanceInfo)) { // instance name was not in the config - return; + return; } if (instanceInfo.created) @@ -364,7 +365,7 @@ private static SqlException CreateLocalDBException(string errorMessage, string i if (localDbError != 0) { - collection.Add(new SqlError(errorCode, 0, TdsEnums.FATAL_ERROR_CLASS, instance, GetLocalDBMessage(localDbError), null, 0)); + collection.Add(new SqlError(errorCode, 0, TdsEnums.FATAL_ERROR_CLASS, instance, GetLocalDBMessage(localDbError), null, 0)); } SqlException exc = SqlException.CreateException(collection, null); diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs deleted file mode 100644 index e2e12f5ad5..0000000000 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDBConfig.netfx.cs +++ /dev/null @@ -1,85 +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 NETFRAMEWORK - -namespace Microsoft.Data -{ - using System; - using System.Collections; - using System.Configuration; - - internal sealed class LocalDBInstanceElement : ConfigurationElement - { - [ConfigurationProperty("name", IsRequired = true)] - public string Name - { - get - { - return this["name"] as string; - } - } - - [ConfigurationProperty("version", IsRequired = true)] - public string Version - { - get - { - return this["version"] as string; - } - } - } - - internal sealed class LocalDBInstancesCollection : ConfigurationElementCollection - { - - private class TrimOrdinalIgnoreCaseStringComparer : IComparer - { - public int Compare(object x, object y) - { - string xStr = x as string; - if (xStr != null) - x = xStr.Trim(); - - string yStr = y as string; - if (yStr != null) - y = yStr.Trim(); - - return StringComparer.OrdinalIgnoreCase.Compare(x, y); - } - } - - static readonly TrimOrdinalIgnoreCaseStringComparer s_comparer = new TrimOrdinalIgnoreCaseStringComparer(); - - internal LocalDBInstancesCollection() - : base(s_comparer) - { - } - - protected override ConfigurationElement CreateNewElement() - { - return new LocalDBInstanceElement(); - } - - protected override object GetElementKey(ConfigurationElement element) - { - return ((LocalDBInstanceElement)element).Name; - } - - } - - internal sealed class LocalDBConfigurationSection : ConfigurationSection - { - [ConfigurationProperty("localdbinstances", IsRequired = true)] - public LocalDBInstancesCollection LocalDbInstances - { - get - { - return (LocalDBInstancesCollection)this["localdbinstances"] ?? new LocalDBInstancesCollection(); - } - } - } -} - -#endif diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbConfigurationSection.netfx.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbConfigurationSection.netfx.cs new file mode 100644 index 0000000000..4fdb66788b --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbConfigurationSection.netfx.cs @@ -0,0 +1,19 @@ +// 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 NETFRAMEWORK + +using System.Configuration; + +namespace Microsoft.Data.SqlClient.LocalDb +{ + internal sealed class LocalDbConfigurationSection : ConfigurationSection + { + [ConfigurationProperty("localdbinstances", IsRequired = true)] + public LocalDbInstancesCollection LocalDbInstances => + (LocalDbInstancesCollection)this["localdbinstances"] ?? new LocalDbInstancesCollection(); + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstanceElement.netfx.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstanceElement.netfx.cs new file mode 100644 index 0000000000..ef35b59116 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstanceElement.netfx.cs @@ -0,0 +1,21 @@ +// 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 NETFRAMEWORK + +using System.Configuration; + +namespace Microsoft.Data.SqlClient.LocalDb +{ + internal sealed class LocalDbInstanceElement : ConfigurationElement + { + [ConfigurationProperty("name", IsRequired = true)] + public string Name => this["name"] as string; + + [ConfigurationProperty("version", IsRequired = true)] + public string Version => this["version"] as string; + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstancesCollection.netfx.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstancesCollection.netfx.cs new file mode 100644 index 0000000000..64860a991a --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalDb/LocalDbInstancesCollection.netfx.cs @@ -0,0 +1,48 @@ +// 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 NETFRAMEWORK + +using System; +using System.Collections; +using System.Configuration; + +namespace Microsoft.Data.SqlClient.LocalDb +{ + internal sealed class LocalDbInstancesCollection : ConfigurationElementCollection + { + private static readonly TrimOrdinalIgnoreCaseStringComparer s_comparer = new TrimOrdinalIgnoreCaseStringComparer(); + + internal LocalDbInstancesCollection() + : base(s_comparer) + { + } + + protected override ConfigurationElement CreateNewElement() => + new LocalDbInstanceElement(); + + protected override object GetElementKey(ConfigurationElement element) => + ((LocalDbInstanceElement)element).Name; + + private class TrimOrdinalIgnoreCaseStringComparer : IComparer + { + public int Compare(object x, object y) + { + if (x is string xStr) + { + x = xStr.Trim(); + } + + if (y is string yStr) + { + y = yStr.Trim(); + } + + return StringComparer.OrdinalIgnoreCase.Compare(x, y); + } + } + } +} + +#endif