From aa15b0d244612fc44bf7e81cb1c146a8b4a5cebd Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Wed, 15 Sep 2021 15:41:20 +0200 Subject: [PATCH] Simplify parsing provider name from connection string --- .../Configuration/ConfigConnectionString.cs | 72 +++++++------------ .../Migrations/Install/DatabaseBuilder.cs | 2 +- .../Persistence/DbConnectionExtensions.cs | 39 ++++------ 3 files changed, 39 insertions(+), 74 deletions(-) diff --git a/src/Umbraco.Core/Configuration/ConfigConnectionString.cs b/src/Umbraco.Core/Configuration/ConfigConnectionString.cs index e88d1f4d01..d0dec2548c 100644 --- a/src/Umbraco.Core/Configuration/ConfigConnectionString.cs +++ b/src/Umbraco.Core/Configuration/ConfigConnectionString.cs @@ -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) + /// + /// Parses the connection string to get the provider name. + /// + /// The connection string. + /// + /// The provider name or null is the connection string is empty. + /// + 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; } } } diff --git a/src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs b/src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs index b7437f4c2d..e4b8353e02 100644 --- a/src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs +++ b/src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs @@ -92,7 +92,7 @@ namespace Umbraco.Cms.Infrastructure.Migrations.Install if (string.IsNullOrWhiteSpace(connectionString) == false) { - providerName = DbConnectionExtensions.DetectProviderNameFromConnectionString(connectionString); + providerName = ConfigConnectionString.ParseProviderName(connectionString); } else if (integratedAuth) { diff --git a/src/Umbraco.Infrastructure/Persistence/DbConnectionExtensions.cs b/src/Umbraco.Infrastructure/Persistence/DbConnectionExtensions.cs index c4876aef7c..fb5d00abfd 100644 --- a/src/Umbraco.Infrastructure/Persistence/DbConnectionExtensions.cs +++ b/src/Umbraco.Infrastructure/Persistence/DbConnectionExtensions.cs @@ -1,35 +1,17 @@ -using System; +using System; using System.Data; using System.Data.Common; -using System.Linq; using Microsoft.Extensions.Logging; using StackExchange.Profiling.Data; using Umbraco.Cms.Core; using Umbraco.Cms.Infrastructure.Persistence.FaultHandling; -using Umbraco.Extensions; namespace Umbraco.Extensions { public static class DbConnectionExtensions { - public static string DetectProviderNameFromConnectionString(string connectionString) + public static bool IsConnectionAvailable(string connectionString, DbProviderFactory factory) { - var builder = new DbConnectionStringBuilder { ConnectionString = connectionString }; - var allKeys = builder.Keys.Cast(); - - if (allKeys.InvariantContains("Data Source") - //this dictionary is case insensitive - && builder["Data source"].ToString().InvariantContains(".sdf")) - { - return Cms.Core.Constants.DbProviderNames.SqlCe; - } - - return Cms.Core.Constants.DbProviderNames.SqlServer; - } - - public static bool IsConnectionAvailable(string connectionString, DbProviderFactory factory) - { - var connection = factory?.CreateConnection(); if (connection == null) @@ -42,7 +24,6 @@ namespace Umbraco.Extensions } } - public static bool IsAvailable(this IDbConnection connection) { try @@ -68,17 +49,25 @@ namespace Umbraco.Extensions internal static IDbConnection UnwrapUmbraco(this IDbConnection connection) { var unwrapped = connection; + IDbConnection c; do { c = unwrapped; - if (unwrapped is ProfiledDbConnection profiled) unwrapped = profiled.WrappedConnection; - if (unwrapped is RetryDbConnection retrying) unwrapped = retrying.Inner; - } while (c != unwrapped); + if (unwrapped is ProfiledDbConnection profiled) + { + unwrapped = profiled.WrappedConnection; + } + + if (unwrapped is RetryDbConnection retrying) + { + unwrapped = retrying.Inner; + } + } + while (c != unwrapped); return unwrapped; } - } }