Changed ConfigConnectionString to accept more valid connectionstrings (#10766)
* Added test cases for valid connectionstring configuration and updated code to pass tests * Removed unneeded reference added by reverted changes Co-authored-by: Dave de Moel <d.demoel@wearetriple.com> Co-authored-by: Elitsa Marinovska <elm@umbraco.dk>
This commit is contained in:
@@ -17,48 +17,57 @@ namespace Umbraco.Cms.Core.Configuration
|
||||
public string ProviderName { get; }
|
||||
public string Name { get; }
|
||||
|
||||
private string ParseProvider(string connectionString)
|
||||
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)
|
||||
{
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var builder = new DbConnectionStringBuilder
|
||||
var builder = new DbConnectionStringBuilder {ConnectionString = connectionString};
|
||||
if (IsSqlCe(builder))
|
||||
{
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
|
||||
if (
|
||||
(builder.TryGetValue("Data Source", out var ds)
|
||||
|| builder.TryGetValue("DataSource", out ds)) && ds is string dataSource)
|
||||
{
|
||||
if (dataSource.EndsWith(".sdf"))
|
||||
{
|
||||
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlCe;
|
||||
}
|
||||
return Constants.DbProviderNames.SqlCe;
|
||||
}
|
||||
|
||||
|
||||
if (builder.TryGetValue("Server", out var s) && s is string server && !string.IsNullOrEmpty(server))
|
||||
if (IsSqlServer(builder))
|
||||
{
|
||||
if (builder.TryGetValue("Database", out var db) && db is string database && !string.IsNullOrEmpty(database))
|
||||
{
|
||||
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName && !string.IsNullOrEmpty(attachDbFileName))
|
||||
{
|
||||
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog && !string.IsNullOrEmpty(initialCatalog))
|
||||
{
|
||||
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
|
||||
throw new ArgumentException("Cannot determine provider name from connection string",
|
||||
nameof(connectionString));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user