2020-03-16 18:51:35 +01:00
|
|
|
|
using System;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
using System.Collections.Generic;
|
2020-03-23 14:31:21 +01:00
|
|
|
|
using System.Data.Common;
|
2020-03-16 18:51:35 +01:00
|
|
|
|
|
2020-08-21 15:27:06 +01:00
|
|
|
|
namespace Umbraco.Core.Configuration.Models
|
2020-03-16 18:51:35 +01:00
|
|
|
|
{
|
2020-08-21 14:52:47 +01:00
|
|
|
|
public class ConnectionStrings
|
2020-03-16 18:51:35 +01:00
|
|
|
|
{
|
2020-09-14 21:27:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
2020-08-24 09:29:40 +02:00
|
|
|
|
// Backing field for UmbracoConnectionString to load from configuration value with key umbracoDbDSN.
|
|
|
|
|
|
// Attributes cannot be applied to map from keys that don't match, and have chosen to retain the key name
|
|
|
|
|
|
// used in configuration for older Umbraco versions.
|
|
|
|
|
|
// See: https://stackoverflow.com/a/54607296/489433
|
|
|
|
|
|
private string umbracoDbDSN
|
|
|
|
|
|
{
|
|
|
|
|
|
get => string.Empty;
|
2020-09-14 21:27:31 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
UmbracoConnectionString = value;
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionStringDictionary[Constants.System.UmbracoConnectionName] = value;
|
|
|
|
|
|
}
|
2020-08-24 09:29:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-14 21:27:31 +02:00
|
|
|
|
public string UmbracoConnectionString
|
|
|
|
|
|
{
|
2020-09-14 21:53:56 +02:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
ConnectionStringDictionary.TryGetValue(Constants.System.UmbracoConnectionName, out var value);
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
2020-09-14 21:27:31 +02:00
|
|
|
|
set => ConnectionStringDictionary[Constants.System.UmbracoConnectionName] = value;
|
|
|
|
|
|
}
|
2020-03-16 18:51:35 +01:00
|
|
|
|
|
2020-09-14 21:27:31 +02:00
|
|
|
|
private Dictionary<string, string> ConnectionStringDictionary { get; } = new Dictionary<string, string>();
|
2020-03-16 18:51:35 +01:00
|
|
|
|
|
|
|
|
|
|
public ConfigConnectionString this[string key]
|
|
|
|
|
|
{
|
2020-03-23 14:31:21 +01:00
|
|
|
|
get
|
|
|
|
|
|
{
|
2020-09-14 21:53:56 +02:00
|
|
|
|
if (!ConnectionStringDictionary.TryGetValue(key, out var connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-23 14:31:21 +01:00
|
|
|
|
var provider = ParseProvider(connectionString);
|
|
|
|
|
|
return new ConfigConnectionString(connectionString, provider, key);
|
|
|
|
|
|
}
|
2020-03-16 18:51:35 +01:00
|
|
|
|
set => throw new NotImplementedException();
|
|
|
|
|
|
}
|
2020-03-23 14:31:21 +01:00
|
|
|
|
|
|
|
|
|
|
private string ParseProvider(string connectionString)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var builder = new DbConnectionStringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
builder.ConnectionString = connectionString;
|
|
|
|
|
|
|
|
|
|
|
|
if (builder.TryGetValue("Data Source", out var ds) && ds is string dataSource)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dataSource.EndsWith(".sdf"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Constants.DbProviderNames.SqlCe;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-17 16:39:28 +02:00
|
|
|
|
if (builder.TryGetValue("Server", out var s) && s is string server && !string.IsNullOrEmpty(server))
|
2020-03-23 14:31:21 +01:00
|
|
|
|
{
|
2020-06-17 16:39:28 +02:00
|
|
|
|
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))
|
2020-03-23 14:31:21 +01:00
|
|
|
|
{
|
|
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
|
|
|
|
|
}
|
2020-08-18 13:49:15 +02:00
|
|
|
|
|
|
|
|
|
|
if (builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog && !string.IsNullOrEmpty(initialCatalog))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
|
|
|
|
|
}
|
2020-03-23 14:31:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
|
|
|
|
|
|
}
|
2020-03-16 18:51:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|