-
Notifications
You must be signed in to change notification settings - Fork 314
[WIP] Porting SqlDataSourceEnumerator from System.Data.Sql #1428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Kaur-Parminder
wants to merge
3
commits into
dotnet:main
from
Kaur-Parminder:SqlDataSourceEnumerator
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
using Microsoft.Data.Common; | ||
using Microsoft.Data.SqlClient; | ||
using System; | ||
using System.Data; | ||
using System.Data.Common; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Microsoft.Data.Sql | ||
{ | ||
/// <summary> | ||
/// Provides a mechanism for enumerating all available instances of SQL Server within the local network | ||
/// </summary> | ||
public sealed class SqlDataSourceEnumerator : DbDataSourceEnumerator | ||
{ | ||
|
||
private static readonly SqlDataSourceEnumerator SingletonInstance = new SqlDataSourceEnumerator(); | ||
internal const string ServerName = "ServerName"; | ||
internal const string InstanceName = "InstanceName"; | ||
internal const string IsClustered = "IsClustered"; | ||
internal const string Version = "Version"; | ||
private static string _Version = "Version:"; | ||
private static string _Cluster = "Clustered:"; | ||
private static int _clusterLength = _Cluster.Length; | ||
private static int _versionLength = _Version.Length; | ||
private const int timeoutSeconds = ADP.DefaultCommandTimeout; | ||
private long timeoutTime; // variable used for timeout computations, holds the value of the hi-res performance counter at which this request should expire | ||
|
||
private SqlDataSourceEnumerator() : base() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets an instance of the SqlDataSourceEnumerator, which can be used to retrieve information about available SQL Server instances. | ||
/// </summary> | ||
public static SqlDataSourceEnumerator Instance => SqlDataSourceEnumerator.SingletonInstance; | ||
|
||
/// <summary> | ||
/// Retrieves a DataTable containing information about all visible SQL Server instances | ||
/// </summary> | ||
/// <returns></returns> | ||
override public DataTable GetDataSources() | ||
{ | ||
(new NamedPermissionSet("FullTrust")).Demand(); // SQLBUDT 244304 | ||
char[] buffer = null; | ||
StringBuilder strbldr = new StringBuilder(); | ||
|
||
Int32 bufferSize = 1024; | ||
Int32 readLength = 0; | ||
buffer = new char[bufferSize]; | ||
bool more = true; | ||
bool failure = false; | ||
IntPtr handle = ADP.s_ptrZero; | ||
|
||
RuntimeHelpers.PrepareConstrainedRegions(); | ||
try | ||
{ | ||
timeoutTime = TdsParserStaticMethods.GetTimeoutSeconds(timeoutSeconds); | ||
RuntimeHelpers.PrepareConstrainedRegions(); | ||
try | ||
{ } | ||
finally | ||
{ | ||
handle = SNINativeMethodWrapper.SNIServerEnumOpen(); | ||
} | ||
|
||
if (handle != ADP.s_ptrZero) | ||
{ | ||
while (more && !TdsParserStaticMethods.TimeoutHasExpired(timeoutTime)) | ||
{ | ||
readLength = SNINativeMethodWrapper.SNIServerEnumRead(handle, buffer, bufferSize, ref more); | ||
|
||
if (readLength > bufferSize) | ||
{ | ||
failure = true; | ||
more = false; | ||
} | ||
else if (0 < readLength) | ||
{ | ||
strbldr.Append(buffer, 0, readLength); | ||
} | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
if (handle != ADP.s_ptrZero) | ||
{ | ||
SNINativeMethodWrapper.SNIServerEnumClose(handle); | ||
} | ||
} | ||
|
||
if (failure) | ||
{ | ||
Debug.Assert(false, "GetDataSources:SNIServerEnumRead returned bad length"); | ||
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlDataSourceEnumerator.GetDataSources|ERR> GetDataSources:SNIServerEnumRead returned bad length, requested %d, received %d", bufferSize, readLength); | ||
throw ADP.ArgumentOutOfRange("readLength"); | ||
} | ||
|
||
return ParseServerEnumString(strbldr.ToString()); | ||
} | ||
|
||
static private System.Data.DataTable ParseServerEnumString(string serverInstances) | ||
{ | ||
DataTable dataTable = new DataTable("SqlDataSources"); | ||
dataTable.Locale = CultureInfo.InvariantCulture; | ||
dataTable.Columns.Add(ServerName, typeof(string)); | ||
dataTable.Columns.Add(InstanceName, typeof(string)); | ||
dataTable.Columns.Add(IsClustered, typeof(string)); | ||
dataTable.Columns.Add(Version, typeof(string)); | ||
DataRow dataRow = null; | ||
string serverName = null; | ||
string instanceName = null; | ||
string isClustered = null; | ||
string version = null; | ||
|
||
// Every row comes in the format "serverName\instanceName;Clustered:[Yes|No];Version:.." | ||
// Every row is terminated by a null character. | ||
// Process one row at a time | ||
foreach (string instance in serverInstances.Split(new string[] { "\0\0\0" }, StringSplitOptions.None)) | ||
{ | ||
// string value = instance.Trim('\0'); // MDAC 91934 | ||
string value = instance.Replace("\0", ""); | ||
if (0 == value.Length) | ||
{ | ||
continue; | ||
} | ||
foreach (string instance2 in value.Split(';')) | ||
{ | ||
if (serverName == null) | ||
{ | ||
foreach (string instance3 in instance2.Split('\\')) | ||
{ | ||
if (serverName == null) | ||
{ | ||
serverName = instance3; | ||
continue; | ||
} | ||
Debug.Assert(instanceName == null); | ||
instanceName = instance3; | ||
} | ||
continue; | ||
} | ||
if (isClustered == null) | ||
{ | ||
Debug.Assert(String.Compare(_Cluster, 0, instance2, 0, _clusterLength, StringComparison.OrdinalIgnoreCase) == 0); | ||
isClustered = instance2.Substring(_clusterLength); | ||
continue; | ||
} | ||
Debug.Assert(version == null); | ||
Debug.Assert(String.Compare(_Version, 0, instance2, 0, _versionLength, StringComparison.OrdinalIgnoreCase) == 0); | ||
version = instance2.Substring(_versionLength); | ||
} | ||
|
||
string query = "ServerName='" + serverName + "'"; | ||
|
||
if (!ADP.IsEmpty(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) | ||
{ | ||
dataRow = dataTable.NewRow(); | ||
dataRow[0] = serverName; | ||
dataRow[1] = instanceName; | ||
dataRow[2] = isClustered; | ||
dataRow[3] = version; | ||
dataTable.Rows.Add(dataRow); | ||
} | ||
serverName = null; | ||
instanceName = null; | ||
isClustered = null; | ||
version = null; | ||
} | ||
foreach (DataColumn column in dataTable.Columns) | ||
{ | ||
column.ReadOnly = true; | ||
} | ||
return dataTable; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlDataSourceEnumeratorTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Data; | ||
using System.ServiceProcess; | ||
using Microsoft.Data.Sql; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.Tests | ||
{ | ||
internal class SqlDataSourceEnumeratorTest | ||
{ | ||
[PlatformSpecific(TestPlatforms.Windows)] | ||
public void SqlDataSourceEnumerator_VerfifyDataTableSize() | ||
{ | ||
ServiceController sc = new ServiceController("SQLBrowser"); | ||
Assert.Equal(ServiceControllerStatus.Running, sc.Status); | ||
|
||
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance; | ||
DataTable table = instance.GetDataSources(); | ||
Assert.NotEmpty(table.Rows); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.