Skip to content

Commit

Permalink
Add ConnectionStringHelper.GetConnectionStringProperties (#657)
Browse files Browse the repository at this point in the history
Use OleDbConnectionStringBuilder.DataSource property to get the server name instead of using the hardcoded "Data Source" key.
  • Loading branch information
albertospelta authored May 14, 2023
1 parent eba3a05 commit e220c43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 43 deletions.
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

0 comments on commit e220c43

Please sign in to comment.