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-08-21 14:52:47 +01:00
|
|
|
|
using System.Text.Json.Serialization;
|
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-08-21 14:52:47 +01:00
|
|
|
|
[JsonPropertyName(Constants.System.UmbracoConnectionName)]
|
|
|
|
|
|
public string UmbracoConnectionString { get; set; }
|
2020-03-16 18:51:35 +01:00
|
|
|
|
|
2020-08-21 14:52:47 +01:00
|
|
|
|
private Dictionary<string, string> AsDictionary() => new Dictionary<string, string>
|
|
|
|
|
|
{
|
|
|
|
|
|
{ Constants.System.UmbracoConnectionName, UmbracoConnectionString }
|
|
|
|
|
|
};
|
2020-03-16 18:51:35 +01:00
|
|
|
|
|
|
|
|
|
|
public ConfigConnectionString this[string key]
|
|
|
|
|
|
{
|
2020-03-23 14:31:21 +01:00
|
|
|
|
get
|
|
|
|
|
|
{
|
2020-08-21 14:52:47 +01:00
|
|
|
|
var connectionString = this.AsDictionary()[key];
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|