Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ internal static class IpAddressPreferenceUtilities
/// </summary>
private static readonly Dictionary<string, SqlConnectionIPAddressPreference> s_preferenceNames = new(StringComparer.InvariantCultureIgnoreCase);

/// <summary>
/// All values of <see cref="SqlConnectionIPAddressPreference"/>, listed explicitly for trimming compatibility.
/// </summary>
internal static IReadOnlyList<SqlConnectionIPAddressPreference> AllPreferences { get; } =
[
SqlConnectionIPAddressPreference.IPv4First,
SqlConnectionIPAddressPreference.IPv6First,
SqlConnectionIPAddressPreference.UsePlatformDefault
];
Comment thread
charlesroddie marked this conversation as resolved.
Resolved

static IpAddressPreferenceUtilities()
{
foreach (SqlConnectionIPAddressPreference item in Enum.GetValues(typeof(SqlConnectionIPAddressPreference)))
foreach (SqlConnectionIPAddressPreference item in AllPreferences)
{
s_preferenceNames.Add(item.ToString(), item);
}
Comment thread
charlesroddie marked this conversation as resolved.
Resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
Expand Down Expand Up @@ -147,15 +148,8 @@ private static DataTable ParseServerEnumString(string serverInstances)
version = instance2.Substring(s_versionLength);
}

string query = "ServerName='" + serverName + "'";

if (!string.IsNullOrEmpty(instanceName))
{ // SQL BU DT 20006584: only append instanceName if present.
query += " AND InstanceName='" + instanceName + "'";
}

// SNI returns dupes - do not add them. SQL BU DT 290323
if (dataTable.Select(query).Length == 0)
if (!ContainsInstance(dataTable, serverName, instanceName))
Comment thread
charlesroddie marked this conversation as resolved.
Resolved
Comment thread
charlesroddie marked this conversation as resolved.
Resolved
{
DataRow dataRow = dataTable.NewRow();
dataRow[0] = serverName;
Expand All @@ -171,5 +165,28 @@ private static DataTable ParseServerEnumString(string serverInstances)
}
return dataTable.SetColumnsReadOnly();
}

private static bool ContainsInstance(DataTable dataTable, string serverName, string instanceName)
{
CompareInfo compareInfo = dataTable.Locale.CompareInfo;

foreach (DataRow row in dataTable.Rows)
{
if (EqualsInTable(compareInfo, row[ServerNameCol] as string, serverName) &&
// SQL BU DT 20006584: only match instanceName if present.
(string.IsNullOrEmpty(instanceName) || EqualsInTable(compareInfo, row[InstanceNameCol] as string, instanceName)))
{
return true;
}
}
return false;
}
Comment thread
charlesroddie marked this conversation as resolved.
Resolved

// Matches DataTable's string equality for a case-insensitive table.
private static bool EqualsInTable(CompareInfo compareInfo, string s1, string s2) =>
compareInfo.Compare(
s1?.TrimEnd(' ', '\u3000'),
s2?.TrimEnd(' ', '\u3000'),
CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth) == 0;
Comment thread
charlesroddie marked this conversation as resolved.
Resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private bool LoadUserInstanceDll()
}

// Set the delegate the invoke.
localDBStartInstanceFunc = (LocalDBStartInstance)Marshal.GetDelegateForFunctionPointer(_startInstanceHandle, typeof(LocalDBStartInstance));
localDBStartInstanceFunc = Marshal.GetDelegateForFunctionPointer<LocalDBStartInstance>(_startInstanceHandle);

if (localDBStartInstanceFunc == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -371,6 +372,10 @@ private string GetParameterName(string neededCollectionName, int neededRestricti
return result;
}

#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072",
Justification = "DataColumn only reflects over INullable column types, and no schema collection columns are INullable.")]
#endif
private async ValueTask<DataTable> ExecuteCommandAsync(DataRow requestedCollectionRow, string[] restrictions, DbConnection connection, bool isAsync, CancellationToken cancellationToken)
{
Debug.Assert(requestedCollectionRow is not null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -170,9 +171,21 @@ public static async Task GetDataTypesFromSchema()

Assert.All(testColumnNames, column => Assert.Contains(column, syncColumnNames));
Assert.All(testColumnNames, column => Assert.Contains(column, asyncColumnNames));
VerifyNoINullableColumns(syncTable);
VerifyNoINullableColumns(asyncTable);
return (syncTable, asyncTable);
}

// SqlMetaDataFactory.ExecuteCommandAsync suppresses IL2072 on the basis that no schema column type is INullable.
private static void VerifyNoINullableColumns(DataTable schemaTable)
{
foreach (DataColumn column in schemaTable.Columns)
{
Assert.False(typeof(INullable).IsAssignableFrom(column.DataType),
$"Column '{column.ColumnName}' of schema collection '{schemaTable.TableName}' has INullable type {column.DataType}.");
}
}

private static void VerifyDataTypesTable(DataTable dataTypesTable)
{
string[] expectedTypes = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.Linq;
using Microsoft.Data.Common.ConnectionString;
using Microsoft.Data.SqlClient;
using Xunit;

namespace Microsoft.Data.Common.UnitTests;

/// <summary>
/// Tests for <see cref="IpAddressPreferenceUtilities"/>.
/// </summary>
public class IpAddressPreferenceUtilitiesTest
{
/// <summary>
/// Verifies that the explicit list of preferences stays in sync with the enum definition.
/// </summary>
[Fact]
public void AllPreferences_MatchesEnumValues()
{
SqlConnectionIPAddressPreference[] expected = Enum.GetValues(typeof(SqlConnectionIPAddressPreference))
.Cast<SqlConnectionIPAddressPreference>()
.OrderBy(value => value)
.ToArray();
Comment thread
charlesroddie marked this conversation as resolved.
Resolved

Assert.Equal(expected, IpAddressPreferenceUtilities.AllPreferences.OrderBy(value => value));
}
}