Skip to content
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

Add ConnectionStringHelper.GetConnectionStringProperties #657

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 6 additions & 41 deletions src/Infrastructure/Helpers/ConnectionStringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
using Sqlbi.Bravo.Models;
using System;
using System.Data.OleDb;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Threading;

internal static class ConnectionStringHelper
{
Expand Down Expand Up @@ -143,47 +140,15 @@ static string Build(string serverName, string databaseName, string accessToken)
}
}

public static string? FindServerName(string? connectionString)
public static (string ServerName, string? DatabaseName) GetConnectionStringProperties(string? connectionString)
{
if (TryGetValue(connectionString, DataSourceKey, out var serverName))
{
return serverName;
}

return null;
}

public static string? FindDatabaseName(string? connectionString)
{
if (TryGetValue(connectionString, InitialCatalogKey, out var databaseName))
{
return databaseName;
}

return null;
}
var builder = new OleDbConnectionStringBuilder(connectionString);

private static bool TryGetValue(string? connectionString, string keyword, [NotNullWhen(true)] out string? value)
{
value = null;

OleDbConnectionStringBuilder builder;
try
{
builder = new OleDbConnectionStringBuilder(connectionString);
}
catch (ArgumentException)
{
return false;
}

if (builder.TryGetValue(keyword, out var objectValue) && objectValue is string stringValue)
{
value = stringValue;
return true;
}
var serverName = builder.DataSource;
_ = builder.TryGetValue(InitialCatalogKey, out var initialCatalog);
var databaseName = (string?)initialCatalog;

return false;
return (serverName, databaseName);
}
}
}
5 changes: 3 additions & 2 deletions src/Models/PBICloudDataset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ internal static PBICloudDataset CreateFrom(IPBICloudEnvironment environment, Clo
}
else if (dataset.IsOnPremModel == true)
{
dataset.ExternalServerName = dataset.ServerName = ConnectionStringHelper.FindServerName(dataset.OnPremModelConnectionString);
dataset.ExternalDatabaseName = dataset.DatabaseName = ConnectionStringHelper.FindDatabaseName(dataset.OnPremModelConnectionString);
var properties = ConnectionStringHelper.GetConnectionStringProperties(dataset.OnPremModelConnectionString);
dataset.ExternalServerName = dataset.ServerName = properties.ServerName;
dataset.ExternalDatabaseName = dataset.DatabaseName = properties.DatabaseName;
}
else
{
Expand Down