2020-09-15 09:11:36 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Core.Configuration
|
2019-11-15 11:07:37 +01:00
|
|
|
{
|
|
|
|
|
public class ConfigConnectionString
|
|
|
|
|
{
|
2020-09-15 09:11:36 +02:00
|
|
|
public ConfigConnectionString(string name, string connectionString, string providerName = null)
|
2019-11-15 11:07:37 +01:00
|
|
|
{
|
2020-09-15 09:11:36 +02:00
|
|
|
Name = name ?? throw new ArgumentNullException(nameof(name));
|
2019-11-15 11:07:37 +01:00
|
|
|
ConnectionString = connectionString;
|
2020-09-15 09:11:36 +02:00
|
|
|
|
|
|
|
|
ProviderName = string.IsNullOrEmpty(providerName) ? ParseProvider(connectionString) : providerName;
|
2019-11-15 11:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ConnectionString { get; }
|
|
|
|
|
public string ProviderName { get; }
|
|
|
|
|
public string Name { get; }
|
2020-09-15 09:11:36 +02:00
|
|
|
|
2021-08-03 09:04:49 +02:00
|
|
|
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)
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
2021-08-03 09:04:49 +02:00
|
|
|
if(builder.TryGetValue("Server", out var s) && s is string server)
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
2021-08-03 09:04:49 +02:00
|
|
|
return server;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 09:04:49 +02:00
|
|
|
if ((builder.TryGetValue("Data Source", out var ds)
|
2020-09-23 07:17:05 +02:00
|
|
|
|| builder.TryGetValue("DataSource", out ds)) && ds is string dataSource)
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
2021-08-03 09:04:49 +02:00
|
|
|
return dataSource;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 09:04:49 +02:00
|
|
|
return "";
|
|
|
|
|
}
|
2020-09-23 07:17:05 +02:00
|
|
|
|
2021-08-03 09:04:49 +02:00
|
|
|
private static string ParseProvider(string connectionString)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
2021-08-03 09:04:49 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = new DbConnectionStringBuilder {ConnectionString = connectionString};
|
|
|
|
|
if (IsSqlCe(builder))
|
|
|
|
|
{
|
|
|
|
|
return Constants.DbProviderNames.SqlCe;
|
|
|
|
|
}
|
2020-09-15 09:11:36 +02:00
|
|
|
|
|
|
|
|
|
2021-08-03 09:04:49 +02:00
|
|
|
if (IsSqlServer(builder))
|
|
|
|
|
{
|
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 09:04:49 +02:00
|
|
|
throw new ArgumentException("Cannot determine provider name from connection string",
|
|
|
|
|
nameof(connectionString));
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
2019-11-15 11:07:37 +01:00
|
|
|
}
|
|
|
|
|
}
|