Simplify parsing provider name from connection string
This commit is contained in:
@@ -5,69 +5,45 @@ namespace Umbraco.Cms.Core.Configuration
|
||||
{
|
||||
public class ConfigConnectionString
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
public string ConnectionString { get; }
|
||||
|
||||
public string ProviderName { get; }
|
||||
|
||||
public ConfigConnectionString(string name, string connectionString, string providerName = null)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
ConnectionString = connectionString;
|
||||
|
||||
ProviderName = string.IsNullOrEmpty(providerName) ? ParseProvider(connectionString) : providerName;
|
||||
ProviderName = string.IsNullOrEmpty(providerName) ? ParseProviderName(connectionString) : providerName;
|
||||
}
|
||||
|
||||
public string ConnectionString { get; }
|
||||
public string ProviderName { get; }
|
||||
public string Name { get; }
|
||||
|
||||
private static bool IsSqlCe(DbConnectionStringBuilder builder) => (builder.TryGetValue("Data Source", out var ds)
|
||||
|| builder.TryGetValue("DataSource", out ds)) &&
|
||||
ds is string dataSource &&
|
||||
dataSource.EndsWith(".sdf");
|
||||
|
||||
private static bool IsSqlServer(DbConnectionStringBuilder builder) =>
|
||||
!string.IsNullOrEmpty(GetServer(builder)) &&
|
||||
((builder.TryGetValue("Database", out var db) && db is string database &&
|
||||
!string.IsNullOrEmpty(database)) ||
|
||||
(builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName &&
|
||||
!string.IsNullOrEmpty(attachDbFileName)) ||
|
||||
(builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog &&
|
||||
!string.IsNullOrEmpty(initialCatalog)));
|
||||
|
||||
private static string GetServer(DbConnectionStringBuilder builder)
|
||||
{
|
||||
if(builder.TryGetValue("Server", out var s) && s is string server)
|
||||
{
|
||||
return server;
|
||||
}
|
||||
|
||||
if ((builder.TryGetValue("Data Source", out var ds)
|
||||
|| builder.TryGetValue("DataSource", out ds)) && ds is string dataSource)
|
||||
{
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static string ParseProvider(string connectionString)
|
||||
/// <summary>
|
||||
/// Parses the connection string to get the provider name.
|
||||
/// </summary>
|
||||
/// <param name="connectionString">The connection string.</param>
|
||||
/// <returns>
|
||||
/// The provider name or <c>null</c> is the connection string is empty.
|
||||
/// </returns>
|
||||
public static string ParseProviderName(string connectionString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var builder = new DbConnectionStringBuilder {ConnectionString = connectionString};
|
||||
if (IsSqlCe(builder))
|
||||
var builder = new DbConnectionStringBuilder
|
||||
{
|
||||
return Constants.DbProviderNames.SqlCe;
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
|
||||
if ((builder.TryGetValue("Data Source", out var dataSource) || builder.TryGetValue("DataSource", out dataSource)) &&
|
||||
dataSource?.ToString().EndsWith(".sdf", StringComparison.OrdinalIgnoreCase) == true)
|
||||
{
|
||||
return Cms.Core.Constants.DbProviderNames.SqlCe;
|
||||
}
|
||||
|
||||
|
||||
if (IsSqlServer(builder))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Cannot determine provider name from connection string",
|
||||
nameof(connectionString));
|
||||
return Cms.Core.Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user