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
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
2020-09-15 09:11:36 +02:00
|
|
|
|
2021-09-15 15:41:20 +02:00
|
|
|
public string ConnectionString { get; }
|
2021-08-03 09:04:49 +02:00
|
|
|
|
2021-09-15 15:41:20 +02:00
|
|
|
public string ProviderName { get; }
|
2021-08-03 09:04:49 +02:00
|
|
|
|
2021-09-15 15:41:20 +02:00
|
|
|
public ConfigConnectionString(string name, string connectionString, string providerName = null)
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
2021-09-15 15:41:20 +02:00
|
|
|
Name = name ?? throw new ArgumentNullException(nameof(name));
|
2021-09-17 20:20:36 +02:00
|
|
|
ConnectionString = ParseConnectionString(connectionString, ref providerName);
|
|
|
|
|
ProviderName = providerName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ParseConnectionString(string connectionString, ref string providerName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
|
|
|
{
|
2021-09-20 11:38:54 +02:00
|
|
|
return connectionString;
|
2021-09-17 20:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = new DbConnectionStringBuilder
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = connectionString
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Replace data directory placeholder
|
|
|
|
|
const string attachDbFileNameKey = "AttachDbFileName";
|
|
|
|
|
const string dataDirectoryPlaceholder = "|DataDirectory|";
|
|
|
|
|
if (builder.TryGetValue(attachDbFileNameKey, out var attachDbFileNameValue) &&
|
|
|
|
|
attachDbFileNameValue is string attachDbFileName &&
|
|
|
|
|
attachDbFileName.Contains(dataDirectoryPlaceholder))
|
|
|
|
|
{
|
|
|
|
|
var dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString();
|
|
|
|
|
if (!string.IsNullOrEmpty(dataDirectory))
|
|
|
|
|
{
|
|
|
|
|
builder[attachDbFileNameKey] = attachDbFileName.Replace(dataDirectoryPlaceholder, dataDirectory);
|
2021-09-20 11:38:54 +02:00
|
|
|
|
|
|
|
|
// Mutate the existing connection string (note: the builder also lowercases the properties)
|
|
|
|
|
connectionString = builder.ToString();
|
2021-09-17 20:20:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also parse provider name now we already have a builder
|
|
|
|
|
if (string.IsNullOrEmpty(providerName))
|
|
|
|
|
{
|
|
|
|
|
providerName = ParseProviderName(builder);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 11:38:54 +02:00
|
|
|
return connectionString;
|
2021-08-03 09:04:49 +02:00
|
|
|
}
|
2020-09-23 07:17:05 +02:00
|
|
|
|
2021-09-15 15:41:20 +02:00
|
|
|
/// <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)
|
2021-08-03 09:04:49 +02:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
2021-08-03 09:04:49 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:41:20 +02:00
|
|
|
var builder = new DbConnectionStringBuilder
|
2021-08-03 09:04:49 +02:00
|
|
|
{
|
2021-09-15 15:41:20 +02:00
|
|
|
ConnectionString = connectionString
|
|
|
|
|
};
|
2020-09-15 09:11:36 +02:00
|
|
|
|
2021-09-17 20:20:36 +02:00
|
|
|
return ParseProviderName(builder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ParseProviderName(DbConnectionStringBuilder builder)
|
|
|
|
|
{
|
2021-09-15 15:41:20 +02:00
|
|
|
if ((builder.TryGetValue("Data Source", out var dataSource) || builder.TryGetValue("DataSource", out dataSource)) &&
|
|
|
|
|
dataSource?.ToString().EndsWith(".sdf", StringComparison.OrdinalIgnoreCase) == true)
|
2021-08-03 09:04:49 +02:00
|
|
|
{
|
2021-09-17 20:20:36 +02:00
|
|
|
return Constants.DbProviderNames.SqlCe;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-17 20:20:36 +02:00
|
|
|
return Constants.DbProviderNames.SqlServer;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
2019-11-15 11:07:37 +01:00
|
|
|
}
|
|
|
|
|
}
|