2020-03-16 18:51:35 +01:00
|
|
|
|
using System;
|
2020-03-23 14:31:21 +01:00
|
|
|
|
using System.Data.Common;
|
2020-03-16 18:51:35 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-03-23 14:31:21 +01:00
|
|
|
|
using Umbraco.Core;
|
2020-03-16 18:51:35 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Configuration.Models
|
|
|
|
|
|
{
|
2020-03-23 14:31:21 +01:00
|
|
|
|
public class ConnectionStrings : IConnectionStrings
|
2020-03-16 18:51:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
|
|
public ConnectionStrings(IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ConfigConnectionString this[string key]
|
|
|
|
|
|
{
|
2020-03-23 14:31:21 +01:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
var connectionString = _configuration.GetConnectionString(key);
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
|
|
|
|
|
|
}
|
2020-03-16 18:51:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|