2020-09-15 09:11:36 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
|
2019-11-15 11:07:37 +01:00
|
|
|
namespace Umbraco.Core.Configuration
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
private string ParseProvider(string connectionString)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = new DbConnectionStringBuilder
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = connectionString
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-23 07:17:05 +02:00
|
|
|
if (
|
|
|
|
|
(builder.TryGetValue("Data Source", out var ds)
|
|
|
|
|
|| builder.TryGetValue("DataSource", out ds)) && ds is string dataSource)
|
2020-09-15 09:11:36 +02:00
|
|
|
{
|
|
|
|
|
if (dataSource.EndsWith(".sdf"))
|
|
|
|
|
{
|
|
|
|
|
return Constants.DbProviderNames.SqlCe;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 07:17:05 +02:00
|
|
|
|
2020-09-15 09:11:36 +02:00
|
|
|
if (builder.TryGetValue("Server", out var s) && s is string server && !string.IsNullOrEmpty(server))
|
|
|
|
|
{
|
|
|
|
|
if (builder.TryGetValue("Database", out var db) && db is string database && !string.IsNullOrEmpty(database))
|
|
|
|
|
{
|
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName && !string.IsNullOrEmpty(attachDbFileName))
|
|
|
|
|
{
|
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog && !string.IsNullOrEmpty(initialCatalog))
|
|
|
|
|
{
|
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
|
|
|
|
|
}
|
2019-11-15 11:07:37 +01:00
|
|
|
}
|
|
|
|
|
}
|