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));
|
|
|
|
|
ConnectionString = connectionString;
|
|
|
|
|
ProviderName = string.IsNullOrEmpty(providerName) ? ParseProviderName(connectionString) : providerName;
|
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-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-15 15:41:20 +02:00
|
|
|
return Cms.Core.Constants.DbProviderNames.SqlCe;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:41:20 +02:00
|
|
|
return Cms.Core.Constants.DbProviderNames.SqlServer;
|
2020-09-15 09:11:36 +02:00
|
|
|
}
|
2019-11-15 11:07:37 +01:00
|
|
|
}
|
|
|
|
|
}
|